Skip to content

Remove axios and change browser bundle name #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div id="indexes"></div>
</body>
</html>
<script src="../../dist/bundles/meilisearch.browser.js"></script>
<script src="../../dist/bundles/meilisearch.umd.js"></script>
<script>
const client = new window.MeiliSearch({
host: 'http://127.0.0.1:7700',
Expand Down
2 changes: 1 addition & 1 deletion examples/node/search_example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const MeiliSearch = require('../../dist/bundles/meilisearch.cjs.js')
const MeiliSearch = require('../../dist/bundles/meilisearch.umd.js')
const dataset = require('./small_movies.json')

const config = {
Expand Down
2 changes: 1 addition & 1 deletion examples/node/small_index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const MeiliSearch = require('../../dist/bundles/meilisearch.cjs.js')
const MeiliSearch = require('../../dist/bundles/meilisearch.umd.js')

const config = {
host: 'http://127.0.0.1:7700',
Expand Down
20 changes: 16 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const config = {
preset: 'ts-jest',
rootDir: '.',
testMatch: ['<rootDir>/tests/**/*.ts?(x)'],
testPathIgnorePatterns: ['meilisearch-test-utils'],
coverageThreshold: {
global: {
'ts-jest': {
Expand All @@ -19,7 +16,22 @@ const config = {
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
testEnvironment: 'node',
projects: [
{
preset: 'ts-jest',
displayName: 'dom',
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/tests/**/*.ts?(x)'],
testPathIgnorePatterns: ['meilisearch-test-utils'],
},
{
preset: 'ts-jest',
displayName: 'node',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/*.ts?(x)'],
testPathIgnorePatterns: ['meilisearch-test-utils'],
},
],
}

module.exports = config
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"qdequele <[email protected]>"
],
"license": "MIT",
"main": "./dist/bundles/meilisearch.cjs.js",
"main": "./dist/bundles/meilisearch.umd.js",
"module": "./dist/bundles/meilisearch.esm.js",
"browser": "./dist/bundles/meilisearch.browser.js",
"browser": "./dist/bundles/meilisearch.umd.js",
"typings": "./dist/types/types.d.ts",
"types": "./dist/types/types.d.ts",
"jsnext:main": "./dist/bundles/meilisearch.esm.js",
Expand All @@ -30,12 +30,13 @@
"scripts": {
"cleanup": "shx rm -rf dist/",
"build": "yarn cleanup && rollup -c && rollup -c --environment NODE_ENV:production",
"watch": "yarn cleanup && rollup -c --watch",
"postbuild": "yarn size && yarn typingsheader",
"test": "jest --runInBand",
"test:watch": "yarn test --watch",
"test:coverage": "yarn test --coverage",
"test:ci": "yarn test --ci",
"size": "node scripts/file-size ./dist/bundles/meilisearch.cjs.min.js ./dist/bundles/meilisearch.esm.min.js ./dist/bundles/meilisearch.browser.min.js",
"size": "node scripts/file-size ./dist/bundles/meilisearch.esm.min.js ./dist/bundles/meilisearch.umd.min.js",
"style": "yarn lint",
"style:fix": "yarn lint:fix",
"lint": "eslint --ext .js,.ts,.tsx .",
Expand All @@ -53,12 +54,15 @@
]
},
"dependencies": {
"axios": "^0.20.0"
"cross-fetch": "^3.0.5"
},
"devDependencies": {
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "15.0.0",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "9.0.0",
"@types/isomorphic-fetch": "^0.0.35",
"@types/jest": "26.0.10",
"@types/prettier": "^2.0.0",
"@typescript-eslint/eslint-plugin": "2.34.0",
Expand Down
37 changes: 21 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import json from '@rollup/plugin-json'
import typescript from 'rollup-plugin-typescript2'
import pkg from './package.json'
import { terser } from 'rollup-plugin-terser'
import { babel } from '@rollup/plugin-babel'

function getOutputFileName(fileName, isProd = false) {
return isProd ? fileName.replace(/\.js$/, '.min.js') : fileName
Expand All @@ -27,9 +28,10 @@ const PLUGINS = [
]

module.exports = [
// browser-friendly IIFE build
// browser-friendly UMD build
{
input: 'src/meilisearch.ts', // directory to transpilation of typescript
external: ['cross-fetch', 'cross-fetch/polyfill'],
output: {
name: LIB_NAME,
file: getOutputFileName(
Expand All @@ -39,20 +41,33 @@ module.exports = [
),
format: 'umd',
sourcemap: env === 'production', // create sourcemap for error reporting in production mode
globals: {
axios: 'axios',
},
},
plugins: [
...PLUGINS,
babel({
babelrc: false,
extensions: ['.ts'],
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: ['last 2 versions', 'ie >= 11'],
},
},
],
],
}),
nodeResolve({
mainFields: ['jsnext', 'main'],
preferBuiltins: true,
browser: true,
}),
commonjs({
include: 'node_modules/axios/**',
include: ['node_modules/**'],
}),
// nodePolyfills
json(),
env === 'production' ? terser() : {}, // will minify the file in production mode
],
Expand All @@ -66,18 +81,8 @@ module.exports = [
// `file` and `format` for each target)
{
input: 'src/meilisearch.ts',
external: ['axios'],
external: ['cross-fetch', 'cross-fetch/polyfill'],
output: [
{
file: getOutputFileName(
// will add .min. in filename if in production env
resolve(ROOT, pkg.main),
env === 'production'
),
exports: 'default',
format: 'cjs',
sourcemap: env === 'production', // create sourcemap for error reporting in production mode
},
{
file: getOutputFileName(
resolve(ROOT, pkg.module),
Expand Down
30 changes: 20 additions & 10 deletions src/errors/http-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { AxiosError } from 'axios'
import MeiliSearchApiError from './meilisearch-api-error'
import MeiliSearchCommunicationError from './meilisearch-communication-error'
import MeiliSearchApiError from './meilisearch-api-error'
import * as Types from '../types'

async function httpResponseErrorHandler(response: Response): Promise<Response> {
if (!response.ok) {
let err
try {
err = await response.json()
} catch (e) {
throw new MeiliSearchCommunicationError(response.statusText, response)
}
throw new MeiliSearchApiError(err, response.status)
}
return response
}

function httpErrorHandler(e: AxiosError, cachedStack?: string): void {
if (e.response !== undefined) {
throw new MeiliSearchApiError(e, cachedStack)
} else if (e.isAxiosError === true) {
throw new MeiliSearchCommunicationError(e.message)
} else {
throw e
function httpErrorHandler(response: Types.FetchError): Promise<void> {
if (response.type !== 'MeiliSearchApiError') {
throw new MeiliSearchCommunicationError(response.message, response)
}
throw response
}

export { httpErrorHandler }
export { httpResponseErrorHandler, httpErrorHandler }
43 changes: 10 additions & 33 deletions src/errors/meilisearch-api-error.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
import { AxiosError } from 'axios'
import * as Types from '../types'

const MeiliSearchApiError: Types.MeiliSearchApiErrorConstructor = class
extends Error
implements Types.MeiliSearchApiErrorInterface {
const MeiliSearchApiError: Types.MSApiErrorConstructor = class extends Error
implements Types.MSApiError {
httpStatus: number
response?: Types.MeiliSearchApiErrorResponse
errorCode?: string
errorType?: string
errorLink?: string
stack?: string
type: string

constructor(error: AxiosError, cachedStack?: string) {
constructor(error: Types.MSApiError, status: number) {
super(error.message)

this.type = 'MeiliSearchApiError'
this.name = 'MeiliSearchApiError'

// Fetch the native error message but add our application name in front of it.
// This means slicing the "Error" string at the start of the message.
if (error.response !== undefined) {
this.response = {
status: error.response.status,
statusText: error.response.statusText,
path: error.response.config.url,
method: error.response.config.method,
}

// If a custom message was sent back by our API
// We change the error message to be more explicit
if (error.response.data?.message !== undefined) {
this.errorCode = error.response.data.errorCode
this.errorType = error.response.data.errorType
this.errorLink = error.response.data.errorLink
this.message = error.response.data.message
}
}

// use cached Stack on error object to keep the call stack
if (cachedStack !== undefined && error.stack !== undefined) {
this.stack = `${this.name}: ${this.message}\n${cachedStack
.split('\n')
.slice(1)
.join('\n')}`
}
this.errorCode = error.errorCode
this.errorType = error.errorType
this.errorLink = error.errorLink
this.message = error.message
this.httpStatus = status
Error.captureStackTrace(this, MeiliSearchApiError)
}
}
export default MeiliSearchApiError
18 changes: 17 additions & 1 deletion src/errors/meilisearch-communication-error.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import 'cross-fetch/polyfill'
import * as Types from '../types'

class MeiliSearchCommunicationError extends Error {
type: string
constructor(message: string) {
statusCode?: number
errno?: string
code?: string

constructor(message: string, body: Response | Types.FetchError) {
super(message)
this.name = 'MeiliSearchCommunicationError'
this.type = 'MeiliSearchCommunicationError'
if (body instanceof Response) {
this.message = body.statusText
this.statusCode = body.status
}
if (body instanceof Error) {
this.errno = body.errno
this.code = body.code
}

Error.captureStackTrace(this, MeiliSearchCommunicationError)
}
}
Expand Down
Loading