-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove axios and change browser bundle name (#573)
* Update Error handler * Remove axios * Add cross-fetch * Update tests to test on both node and dom * Remove node polyfill * Types cleanup * Umd works in node and browser, removes cjs from repo
- Loading branch information
Showing
18 changed files
with
1,274 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 .", | ||
|
@@ -53,9 +54,11 @@ | |
] | ||
}, | ||
"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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.