rest-client-sdk
Advanced tools
Comparing version 3.2.0 to 3.2.1
128
CHANGELOG.md
# Changelog | ||
## 3.2.1 | ||
### Changed | ||
- Refactor error handling with error factory and handle 400's in refreshToken of PasswordGenerator | ||
## 3.2.0 | ||
@@ -7,3 +13,3 @@ | ||
* Pre-emptively refresh the access token if it is close to expiration before doing any request | ||
- Pre-emptively refresh the access token if it is close to expiration before doing any request | ||
@@ -14,3 +20,3 @@ ## 3.1.1 | ||
* Read json response in \_manageUnauthorized when www-authenticate header is not available | ||
- Read json response in \_manageUnauthorized when www-authenticate header is not available | ||
@@ -21,4 +27,4 @@ ## 3.1.0 | ||
* Store a new key `expires_at` in the access token (which is accessible with `TokenStorage.getAccessTokenObject`) to have the timestamp after which the access token will be invalid, based on the `expires_in` | ||
* Add `TokenStorage.getCurrentTokenExpiresIn` function to known how many seconds are remaining for the access token currently stored | ||
- Store a new key `expires_at` in the access token (which is accessible with `TokenStorage.getAccessTokenObject`) to have the timestamp after which the access token will be invalid, based on the `expires_in` | ||
- Add `TokenStorage.getCurrentTokenExpiresIn` function to known how many seconds are remaining for the access token currently stored | ||
@@ -29,5 +35,5 @@ ## 3.0.0 | ||
* [Breaking] `AccessDeniedError` is now `UnauthorizedError` | ||
* [Breaking] Default error is now `HttpError` | ||
* Add `ConflictError` to react to 409 | ||
- [Breaking] `AccessDeniedError` is now `UnauthorizedError` | ||
- [Breaking] Default error is now `HttpError` | ||
- Add `ConflictError` to react to 409 | ||
@@ -38,4 +44,4 @@ ## 2.3.0 | ||
* `TokenStorage` add possibility to pass access_token async storage key as constructor param | ||
* `AbstractClient._manageAccessDenied` checks headers instead of json to ensure refreshing the token is required | ||
- `TokenStorage` add possibility to pass access_token async storage key as constructor param | ||
- `AbstractClient._manageAccessDenied` checks headers instead of json to ensure refreshing the token is required | ||
@@ -46,4 +52,4 @@ ## 2.2.1 | ||
* `TokenStorage.getAccessTokenObject()` returns an object and null if the stored token is not an object | ||
* [Might Break] `TokenStorage.getAccessTokenObject()` does not return `undefined` anymore but `null` if no token object is found | ||
- `TokenStorage.getAccessTokenObject()` returns an object and null if the stored token is not an object | ||
- [Might Break] `TokenStorage.getAccessTokenObject()` does not return `undefined` anymore but `null` if no token object is found | ||
@@ -54,4 +60,4 @@ ## 2.2.0 | ||
* Add possibility to pass null as tokenStorage argument of the sdk | ||
* switch method names \_doFetch and \_fetchWithToken in AbstractClient for coherence | ||
- Add possibility to pass null as tokenStorage argument of the sdk | ||
- switch method names \_doFetch and \_fetchWithToken in AbstractClient for coherence | ||
@@ -62,3 +68,3 @@ ## 2.1.0 | ||
* Add method getAccessTokenObject to TokenStorage | ||
- Add method getAccessTokenObject to TokenStorage | ||
@@ -69,3 +75,3 @@ ## 2.0.1 | ||
* Better packaging system [#48](https://github.com/mapado/rest-client-js-sdk/pull/48) | ||
- Better packaging system [#48](https://github.com/mapado/rest-client-js-sdk/pull/48) | ||
@@ -76,6 +82,6 @@ ## 2.0.0 | ||
* [Might break] Custom serializer can: | ||
* `serializeItem` has been splitted into `normalizeItem` + `encodeItem` | ||
* `deserializeItem` has been splitted into `denormalizeItem` + `decodeItem` | ||
* `deserializeList` has been splitted into `denormalizeList` + `decodeList` | ||
- [Might break] Custom serializer can: | ||
- `serializeItem` has been splitted into `normalizeItem` + `encodeItem` | ||
- `deserializeItem` has been splitted into `denormalizeItem` + `decodeItem` | ||
- `deserializeList` has been splitted into `denormalizeList` + `decodeList` | ||
@@ -86,5 +92,5 @@ You must implement them if you have a custom serialization that do not use plain javascript objects. | ||
* [Breaking] Calling `restClientSdk.foo.xxx` is deprecated, you must now call `restClientSdk.getRepository('foo').xxx` | ||
* [Breaking] RestClientSdk now takes a Mapping instance instead of a clientList. This instance is required | ||
* [Breaking] `getName` has been removed and replaced by the classmetadata key. Its return was previously sent to the serializer, it's now the mapping that is sent now. | ||
- [Breaking] Calling `restClientSdk.foo.xxx` is deprecated, you must now call `restClientSdk.getRepository('foo').xxx` | ||
- [Breaking] RestClientSdk now takes a Mapping instance instead of a clientList. This instance is required | ||
- [Breaking] `getName` has been removed and replaced by the classmetadata key. Its return was previously sent to the serializer, it's now the mapping that is sent now. | ||
Beware that now, the `key` is now used both to call the repository name, and passed to the serializer | ||
@@ -119,11 +125,11 @@ Before: | ||
* [Breaking] Custom Serializer: If you had a Collection entity containing the result of you entities, you will need to implement the [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) on this object, this way, we can register all entities in the unit of work. | ||
- [Breaking] Custom Serializer: If you had a Collection entity containing the result of you entities, you will need to implement the [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) on this object, this way, we can register all entities in the unit of work. | ||
### Removed | ||
* Dropped support for node 6 | ||
* AbstractClient does not need to specify `getPathBase` anymore: this is generated via the classmetata. You can still override it. | ||
* `getEntityURI` is not mandatory anymore. it will be autogenerate by the AbstractClient. You can still override it | ||
* There is no need to create an empty client extending `AbstractClient` now, the default will be `AbstractClient` | ||
* config `prefix` is not used anymore, you should specify your prefix in the mapping: `new Mapping('/v1')` | ||
- Dropped support for node 6 | ||
- AbstractClient does not need to specify `getPathBase` anymore: this is generated via the classmetata. You can still override it. | ||
- `getEntityURI` is not mandatory anymore. it will be autogenerate by the AbstractClient. You can still override it | ||
- There is no need to create an empty client extending `AbstractClient` now, the default will be `AbstractClient` | ||
- config `prefix` is not used anymore, you should specify your prefix in the mapping: `new Mapping('/v1')` | ||
@@ -178,3 +184,3 @@ ## 2.0.0-rc.14 | ||
* `ONE_TO_MANY` and `MANY_TO_ONE` constant are now exported via the `Relation.ONE_TO_MANY` and `Relation.MANY_TO_ONE`, not in the main package | ||
- `ONE_TO_MANY` and `MANY_TO_ONE` constant are now exported via the `Relation.ONE_TO_MANY` and `Relation.MANY_TO_ONE`, not in the main package | ||
@@ -189,3 +195,3 @@ ## 2.0.0-rc.1 | ||
* fix simultaneous refresh token throwing error when trying to access to | ||
- fix simultaneous refresh token throwing error when trying to access to | ||
response body twice | ||
@@ -197,3 +203,3 @@ | ||
* add error message to AccessDeniedError in PasswordGenerator's refreshToken | ||
- add error message to AccessDeniedError in PasswordGenerator's refreshToken | ||
@@ -204,3 +210,3 @@ ## [1.3.0] - 2017-11-29 | ||
* throw AccessDeniedError only on BadRequestError in PasswordGenerator | ||
- throw AccessDeniedError only on BadRequestError in PasswordGenerator | ||
refreshToken | ||
@@ -212,5 +218,5 @@ | ||
* fix response passed as error message in Errors | ||
* throw AccessDeniedError on any Error in PasswordGenerator refreshToken | ||
* allow to pass scope param in tokenGeneratorConfig | ||
- fix response passed as error message in Errors | ||
- throw AccessDeniedError on any Error in PasswordGenerator refreshToken | ||
- allow to pass scope param in tokenGeneratorConfig | ||
@@ -221,3 +227,3 @@ ## [1.1.1] - 2017-10-08 | ||
* upgrade rollup to 0.50.0 | ||
- upgrade rollup to 0.50.0 | ||
@@ -228,5 +234,5 @@ ## [1.1.0] - 2017-08-30 | ||
* Pass on response to serializer | ||
* Upgrade dependencies (rollup 0.49, eslint 4, etc.) | ||
* Add prettier to project | ||
- Pass on response to serializer | ||
- Upgrade dependencies (rollup 0.49, eslint 4, etc.) | ||
- Add prettier to project | ||
@@ -237,3 +243,3 @@ ## [1.0.5] - 2017-08-30 | ||
* Fix token loop when token is not valid anymore | ||
- Fix token loop when token is not valid anymore | ||
@@ -244,3 +250,3 @@ ## [1.0.4] - 2017-07-28 | ||
* Remove undefined headers | ||
- Remove undefined headers | ||
@@ -251,3 +257,3 @@ ## [1.0.3] - 2017-07-27 | ||
* Allow overriding base headers | ||
- Allow overriding base headers | ||
[#24](https://github.com/mapado/rest-client-js-sdk/pull/24) | ||
@@ -261,3 +267,3 @@ | ||
* Rolled back to urijs because domurl use the default node implementation which | ||
- Rolled back to urijs because domurl use the default node implementation which | ||
is not compatible with the stack we use. The real future solution will be to | ||
@@ -270,4 +276,4 @@ use Javascript `URL` object but too early and unstable for now | ||
* Make urijs implementation work again but might be breaking | ||
* Url constructor passed with `noTransform = true` for better perf and avoid | ||
- Make urijs implementation work again but might be breaking | ||
- Url constructor passed with `noTransform = true` for better perf and avoid | ||
potential bugs | ||
@@ -279,3 +285,3 @@ | ||
* Replace [urijs](https://medialize.github.io/URI.js/) dependency by smaller | ||
- Replace [urijs](https://medialize.github.io/URI.js/) dependency by smaller | ||
[domurl](https://github.com/Mikhus/domurl) | ||
@@ -287,3 +293,3 @@ | ||
* Added the current URI as referer for http calls | ||
- Added the current URI as referer for http calls | ||
@@ -304,16 +310,16 @@ ## [0.14.x] | ||
* Do not depend on `@id` anymore: The `AbstractClient` need to implements a | ||
- Do not depend on `@id` anymore: The `AbstractClient` need to implements a | ||
`getEntityURI(entity)` and return an query string from it. This is not a great | ||
pattern, but it will do for now. | ||
(https://github.com/mapado/rest-client-js-sdk/pull/19) | ||
* The `entityFactory` does not exists anymore. It has been replaced by a | ||
- The `entityFactory` does not exists anymore. It has been replaced by a | ||
`Serializer`: (https://github.com/mapado/rest-client-js-sdk/pull/21) | ||
* If you used the default entityFactory, you will now receive plain Javascript | ||
- If you used the default entityFactory, you will now receive plain Javascript | ||
objects instead of immutable's Map or List | ||
* If you previously overrided the `entityFactory`, you will need to switch to | ||
- If you previously overrided the `entityFactory`, you will need to switch to | ||
the new `Serializer` object: It is much more extensible but is a bit more | ||
complex to extend. See README to know how to do it. | ||
* the `createEntityFromJsonResponse` method has been renamed to | ||
- the `createEntityFromJsonResponse` method has been renamed to | ||
`deserializeResponse` | ||
* The library moved from ES5 to bundling with rollupjs, that should not break | ||
- The library moved from ES5 to bundling with rollupjs, that should not break | ||
anything and should improve size of bundle but I am not so sure of this, so | ||
@@ -326,3 +332,3 @@ patch may follow (https://github.com/mapado/rest-client-js-sdk/pull/20) | ||
* a `delete` does not call `createEntityFromJsonResponse` anymore as it should | ||
- a `delete` does not call `createEntityFromJsonResponse` anymore as it should | ||
return a "204 No Content" or a "404 Not Found" | ||
@@ -334,3 +340,3 @@ | ||
* `ProviderTokenGenerator` does not accept a param object anymore on its second | ||
- `ProviderTokenGenerator` does not accept a param object anymore on its second | ||
argument. | ||
@@ -340,3 +346,3 @@ | ||
* `ProviderTokenGenerator` now accept a function on its second argument which | ||
- `ProviderTokenGenerator` now accept a function on its second argument which | ||
will be called on `refreshToken`, so you can fully customize the behavior. The | ||
@@ -349,3 +355,3 @@ function must return a Promise. | ||
* if the response status is 4xx or 5xx: an error is thrown | ||
- if the response status is 4xx or 5xx: an error is thrown | ||
[#13](https://github.com/mapado/rest-client-js-sdk/pull/13) | ||
@@ -355,3 +361,3 @@ | ||
* memoize token generation and refresh | ||
- memoize token generation and refresh | ||
[#12](https://github.com/mapado/rest-client-js-sdk/pull/12) | ||
@@ -365,4 +371,4 @@ | ||
* `findAll(queryParam = {}, pathParameters = {})` | ||
* `create(entity, queryParam = {}, pathParameters = {})` | ||
- `findAll(queryParam = {}, pathParameters = {})` | ||
- `create(entity, queryParam = {}, pathParameters = {})` | ||
@@ -373,2 +379,2 @@ ### new features | ||
* `update(entity, queryParam = {})` | ||
- `update(entity, queryParam = {})` |
@@ -786,21 +786,21 @@ import diff from 'deep-diff'; | ||
function handleBadResponse(response) { | ||
var getHttpErrorFromResponse = function getHttpErrorFromResponse(response) { | ||
switch (true) { | ||
case response.status === 401: | ||
throw new UnauthorizedError(null, response); | ||
return new UnauthorizedError(null, response); | ||
case response.status === 403: | ||
throw new ForbiddenError(null, response); | ||
return new ForbiddenError(null, response); | ||
case response.status === 404: | ||
throw new ResourceNotFoundError(null, response); | ||
return new ResourceNotFoundError(null, response); | ||
case response.status === 409: | ||
throw new ConflictError(null, response); | ||
return new ConflictError(null, response); | ||
case response.status >= 400 && response.status < 500: | ||
throw new BadRequestError(null, response); | ||
return new BadRequestError(null, response); | ||
case response.status >= 500 && response.status < 600: | ||
throw new InternalServerError(null, response); | ||
return new InternalServerError(null, response); | ||
@@ -810,3 +810,3 @@ default: | ||
} | ||
} | ||
}; | ||
@@ -1102,4 +1102,2 @@ var EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new UnauthorizedError('Unable to renew access_token', response); | ||
}); | ||
@@ -1175,3 +1173,4 @@ } | ||
if (response.status !== 401) { | ||
return handleBadResponse(response); | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -1455,3 +1454,4 @@ }); | ||
if (response.status >= 400) { | ||
return handleBadResponse(response); | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -1502,2 +1502,3 @@ | ||
_this._doFetch = memoizePromise(_this._doFetch); | ||
_this._manageBadRequest = _this._manageBadRequest.bind(_this); | ||
return _this; | ||
@@ -1525,2 +1526,22 @@ } | ||
}, { | ||
key: '_manageBadRequest', | ||
value: function _manageBadRequest(response) { | ||
return response.json().then(function (body) { | ||
if (body.error === 'invalid_grant') { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
throw new UnauthorizedError(body.error, response); | ||
} | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}).catch(function (err) { | ||
if (err instanceof UnauthorizedError) { | ||
throw err; | ||
} | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}); | ||
} | ||
}, { | ||
key: 'refreshToken', | ||
@@ -1547,10 +1568,2 @@ value: function refreshToken(accessToken) { | ||
return response.clone().json(); | ||
}).catch(function (err) { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
if (err instanceof BadRequestError) { | ||
throw new UnauthorizedError(err.message, err.baseResponse); | ||
} | ||
throw err; | ||
}); | ||
@@ -1576,2 +1589,4 @@ } | ||
value: function _doFetch(parameters) { | ||
var _this2 = this; | ||
var uri = new URI(this.tokenGeneratorConfig.path); | ||
@@ -1590,7 +1605,14 @@ uri.scheme(this.tokenGeneratorConfig.scheme); | ||
}).then(function (response) { | ||
if (response.status >= 400) { | ||
handleBadResponse(response); | ||
if (response.status < 400) { | ||
return response; | ||
} | ||
return response; | ||
if (response.status === 400) { | ||
return _this2._manageBadRequest(response); | ||
} | ||
if (response.status !== 400) { | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
}); | ||
@@ -1597,0 +1619,0 @@ } |
@@ -792,21 +792,21 @@ (function (global, factory) { | ||
function handleBadResponse(response) { | ||
var getHttpErrorFromResponse = function getHttpErrorFromResponse(response) { | ||
switch (true) { | ||
case response.status === 401: | ||
throw new UnauthorizedError(null, response); | ||
return new UnauthorizedError(null, response); | ||
case response.status === 403: | ||
throw new ForbiddenError(null, response); | ||
return new ForbiddenError(null, response); | ||
case response.status === 404: | ||
throw new ResourceNotFoundError(null, response); | ||
return new ResourceNotFoundError(null, response); | ||
case response.status === 409: | ||
throw new ConflictError(null, response); | ||
return new ConflictError(null, response); | ||
case response.status >= 400 && response.status < 500: | ||
throw new BadRequestError(null, response); | ||
return new BadRequestError(null, response); | ||
case response.status >= 500 && response.status < 600: | ||
throw new InternalServerError(null, response); | ||
return new InternalServerError(null, response); | ||
@@ -816,3 +816,3 @@ default: | ||
} | ||
} | ||
}; | ||
@@ -1108,4 +1108,2 @@ var EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new UnauthorizedError('Unable to renew access_token', response); | ||
}); | ||
@@ -1181,3 +1179,4 @@ } | ||
if (response.status !== 401) { | ||
return handleBadResponse(response); | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -1461,3 +1460,4 @@ }); | ||
if (response.status >= 400) { | ||
return handleBadResponse(response); | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -1508,2 +1508,3 @@ | ||
_this._doFetch = memoizePromise(_this._doFetch); | ||
_this._manageBadRequest = _this._manageBadRequest.bind(_this); | ||
return _this; | ||
@@ -1531,2 +1532,22 @@ } | ||
}, { | ||
key: '_manageBadRequest', | ||
value: function _manageBadRequest(response) { | ||
return response.json().then(function (body) { | ||
if (body.error === 'invalid_grant') { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
throw new UnauthorizedError(body.error, response); | ||
} | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}).catch(function (err) { | ||
if (err instanceof UnauthorizedError) { | ||
throw err; | ||
} | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}); | ||
} | ||
}, { | ||
key: 'refreshToken', | ||
@@ -1553,10 +1574,2 @@ value: function refreshToken(accessToken) { | ||
return response.clone().json(); | ||
}).catch(function (err) { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
if (err instanceof BadRequestError) { | ||
throw new UnauthorizedError(err.message, err.baseResponse); | ||
} | ||
throw err; | ||
}); | ||
@@ -1582,2 +1595,4 @@ } | ||
value: function _doFetch(parameters) { | ||
var _this2 = this; | ||
var uri = new URI(this.tokenGeneratorConfig.path); | ||
@@ -1596,7 +1611,14 @@ uri.scheme(this.tokenGeneratorConfig.scheme); | ||
}).then(function (response) { | ||
if (response.status >= 400) { | ||
handleBadResponse(response); | ||
if (response.status < 400) { | ||
return response; | ||
} | ||
return response; | ||
if (response.status === 400) { | ||
return _this2._manageBadRequest(response); | ||
} | ||
if (response.status !== 400) { | ||
var httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
}); | ||
@@ -1603,0 +1625,0 @@ } |
{ | ||
"name": "rest-client-sdk", | ||
"version": "v3.2.0", | ||
"version": "v3.2.1", | ||
"description": "Rest Client SDK for API", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"files": ["dist", "src"], | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
@@ -17,3 +20,3 @@ "clean": "rimraf dist", | ||
"dependencies": { | ||
"deep-diff": "^0.3.8", | ||
"deep-diff": "^1.0.2", | ||
"urijs": "^1.19.0" | ||
@@ -26,24 +29,25 @@ }, | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-env": "^1.7.0", | ||
"bundlesize": "^0.17.0", | ||
"eslint": "^4.15.0", | ||
"eslint-config-airbnb": "^16.1.0", | ||
"eslint-config-prettier": "^2.7.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.2", | ||
"eslint-plugin-prettier": "^2.5.0", | ||
"eslint-plugin-react": "^7.4.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-config-prettier": "^3.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.2", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"eslint-plugin-react": "^7.11.1", | ||
"fetch-mock": "^5.13.1", | ||
"form-data": "^2.1.2", | ||
"husky": "^0.14.3", | ||
"immutable": "^4.0.0-rc.9", | ||
"jest": "^23.2.0", | ||
"lint-staged": "^7.0.3", | ||
"form-data": "^2.3.3", | ||
"husky": "^1.1.4", | ||
"immutable": "^4.0.0-rc.12", | ||
"jest": "^23.2.1", | ||
"jest-fetch-mock": "^1.7.4", | ||
"lint-staged": "^8.0.4", | ||
"pluralize": "^7.0.0", | ||
"prettier": "^1.10.2", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.57.1", | ||
"rollup": "^0.67.1", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-commonjs": "^9.1.0", | ||
"rollup-plugin-node-resolve": "^3.2.0" | ||
"rollup-plugin-node-resolve": "^3.2.1" | ||
}, | ||
@@ -57,3 +61,7 @@ "optionalDependencies": { | ||
}, | ||
"keywords": ["Rest", "SDK", "API"], | ||
"keywords": [ | ||
"Rest", | ||
"SDK", | ||
"API" | ||
], | ||
"author": "Mapado", | ||
@@ -75,7 +83,11 @@ "license": "MIT", | ||
"lint-staged": { | ||
"*.{js,jsx,json,md}": ["yarn run prettier --write", "git add"] | ||
"*.{js,jsx,json,md}": [ | ||
"yarn run prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"jest": { | ||
"automock": false, | ||
"setupTestFrameworkScriptFile": "./setupJest.js" | ||
} | ||
} |
import URI from 'urijs'; | ||
import { UnauthorizedError, handleBadResponse } from '../Error'; | ||
import { UnauthorizedError, getHttpErrorFromResponse } from '../ErrorFactory'; | ||
@@ -233,15 +233,17 @@ const EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes | ||
this._tokenStorage.getCurrentTokenExpiresIn(), | ||
this._tokenStorage.getAccessToken() | ||
]).then(([accessTokenExpiresIn, accessToken]) => { | ||
if (accessTokenExpiresIn !== null && accessTokenExpiresIn <= EXPIRE_LIMIT_SECONDS) { | ||
return this._tokenStorage | ||
.refreshToken() | ||
.then(refreshedTokenObject => refreshedTokenObject.access_token) | ||
; | ||
} | ||
this._tokenStorage.getAccessToken(), | ||
]) | ||
.then(([accessTokenExpiresIn, accessToken]) => { | ||
if ( | ||
accessTokenExpiresIn !== null && | ||
accessTokenExpiresIn <= EXPIRE_LIMIT_SECONDS | ||
) { | ||
return this._tokenStorage | ||
.refreshToken() | ||
.then(refreshedTokenObject => refreshedTokenObject.access_token); | ||
} | ||
return accessToken; | ||
}) | ||
.then(token => this._doFetch(token, input, init)) | ||
; | ||
return accessToken; | ||
}) | ||
.then(token => this._doFetch(token, input, init)); | ||
} | ||
@@ -253,15 +255,10 @@ | ||
_refreshTokenAndRefetch(response, input, init) { | ||
return this._tokenStorage | ||
.refreshToken() | ||
.then(() => { | ||
const params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers), | ||
}); | ||
delete params.headers.Authorization; | ||
return this._tokenStorage.refreshToken().then(() => { | ||
const params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers), | ||
}); | ||
delete params.headers.Authorization; | ||
return this._fetchWithToken(input, params); | ||
}) | ||
.catch(() => { | ||
throw new UnauthorizedError('Unable to renew access_token', response); | ||
}); | ||
return this._fetchWithToken(input, params); | ||
}); | ||
} | ||
@@ -347,3 +344,4 @@ | ||
if (response.status !== 401) { | ||
return handleBadResponse(response); | ||
const httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -350,0 +348,0 @@ }); |
@@ -10,3 +10,3 @@ import RestClientSdk from './RestClientSdk'; | ||
UnauthorizedError, | ||
} from './Error'; | ||
} from './ErrorFactory'; | ||
import AbstractClient from './client/AbstractClient'; | ||
@@ -13,0 +13,0 @@ import TokenStorage from './TokenStorage'; |
import URI from 'urijs'; | ||
import { handleBadResponse } from '../Error'; | ||
import { getHttpErrorFromResponse } from '../ErrorFactory'; | ||
import AbstractTokenGenerator from './AbstractTokenGenerator'; | ||
@@ -43,3 +43,4 @@ import { memoizePromise } from '../decorator'; | ||
if (response.status >= 400) { | ||
return handleBadResponse(response); | ||
const httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
@@ -46,0 +47,0 @@ |
import URI from 'urijs'; | ||
import AbstractTokenGenerator from './AbstractTokenGenerator'; | ||
import { memoizePromise } from '../decorator'; | ||
import { | ||
UnauthorizedError, | ||
handleBadResponse, | ||
BadRequestError, | ||
} from '../Error'; | ||
import { UnauthorizedError, getHttpErrorFromResponse } from '../ErrorFactory'; | ||
@@ -24,2 +20,3 @@ const ERROR_CONFIG_EMPTY = 'TokenGenerator config must be set'; | ||
this._doFetch = memoizePromise(this._doFetch); | ||
this._manageBadRequest = this._manageBadRequest.bind(this); | ||
} | ||
@@ -42,2 +39,24 @@ | ||
_manageBadRequest(response) { | ||
return response | ||
.json() | ||
.then(body => { | ||
if (body.error === 'invalid_grant') { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
throw new UnauthorizedError(body.error, response); | ||
} | ||
const httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}) | ||
.catch(err => { | ||
if (err instanceof UnauthorizedError) { | ||
throw err; | ||
} | ||
const httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
}); | ||
} | ||
refreshToken(accessToken, baseParameters = {}) { | ||
@@ -61,13 +80,3 @@ if (!(accessToken && accessToken.refresh_token)) { | ||
return this._doFetch(parameters) | ||
.then(response => response.clone().json()) | ||
.catch(err => { | ||
// bad params like wrong scopes sent to oauth server | ||
// will generate a 400, we want final clients to consider it | ||
// like 401 in order to take proper action | ||
if (err instanceof BadRequestError) { | ||
throw new UnauthorizedError(err.message, err.baseResponse); | ||
} | ||
throw err; | ||
}); | ||
return this._doFetch(parameters).then(response => response.clone().json()); | ||
} | ||
@@ -103,7 +112,14 @@ | ||
}).then(response => { | ||
if (response.status >= 400) { | ||
handleBadResponse(response); | ||
if (response.status < 400) { | ||
return response; | ||
} | ||
return response; | ||
if (response.status === 400) { | ||
return this._manageBadRequest(response); | ||
} | ||
if (response.status !== 400) { | ||
const httpError = getHttpErrorFromResponse(response); | ||
throw httpError; | ||
} | ||
}); | ||
@@ -110,0 +126,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
351586
4452
27
+ Addeddeep-diff@1.0.2(transitive)
- Removeddeep-diff@0.3.8(transitive)
Updateddeep-diff@^1.0.2