rest-client-sdk 7.0.2 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/index.es.js +12 -2
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/types/RestClientSdkInterface.d.ts +1 -0
- package/package.json +1 -1
- package/src/RestClientSdkInterface.ts +1 -0
- package/src/TokenStorage.ts +1 -1
- package/src/client/AbstractClient.ts +27 -11
|
@@ -13,6 +13,7 @@ export declare type Config = {
|
|
|
13
13
|
useDefaultParameters?: boolean;
|
|
14
14
|
unitOfWorkEnabled?: boolean;
|
|
15
15
|
loggerEnabled?: boolean;
|
|
16
|
+
onRefreshTokenFailure?: (error: Error) => void;
|
|
16
17
|
};
|
|
17
18
|
export default interface RestClientSdkInterface<M extends SdkMetadata> {
|
|
18
19
|
tokenStorage: TokenStorageInterface<Token>;
|
package/package.json
CHANGED
package/src/TokenStorage.ts
CHANGED
|
@@ -193,7 +193,7 @@ class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
|
|
|
193
193
|
.then((body) => {
|
|
194
194
|
if (isOauthError(body)) {
|
|
195
195
|
// throw error if response body is an oauth error
|
|
196
|
-
manageOauthError(body,
|
|
196
|
+
manageOauthError(body, originalResponse);
|
|
197
197
|
} else if (response.status >= 400) {
|
|
198
198
|
// throw an error if response status code is an "error" status code
|
|
199
199
|
throw getHttpErrorFromResponse(originalResponse);
|
|
@@ -401,17 +401,30 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
401
401
|
input: string,
|
|
402
402
|
requestParams: RequestInit = {}
|
|
403
403
|
): Promise<Response> {
|
|
404
|
-
return this.#tokenStorage
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
404
|
+
return this.#tokenStorage
|
|
405
|
+
.refreshToken()
|
|
406
|
+
.then(() => {
|
|
407
|
+
// eslint-disable-next-line prefer-const
|
|
408
|
+
let { headers, ...rest } = requestParams;
|
|
409
|
+
|
|
410
|
+
const updatedRequestParams: RequestInit = {
|
|
411
|
+
...rest,
|
|
412
|
+
headers: removeAuthorization(headers),
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
return this._fetchWithToken(input, updatedRequestParams);
|
|
416
|
+
})
|
|
417
|
+
.catch((e) => {
|
|
418
|
+
if (e instanceof OauthError) {
|
|
419
|
+
this.#tokenStorage.logout().then(() => {
|
|
420
|
+
if (this.sdk.config.onRefreshTokenFailure) {
|
|
421
|
+
this.sdk.config.onRefreshTokenFailure(e);
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
412
425
|
|
|
413
|
-
|
|
414
|
-
|
|
426
|
+
throw e;
|
|
427
|
+
});
|
|
415
428
|
}
|
|
416
429
|
|
|
417
430
|
private _manageUnauthorized(
|
|
@@ -437,7 +450,10 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
437
450
|
return response
|
|
438
451
|
.json()
|
|
439
452
|
.then((body) => {
|
|
440
|
-
if (
|
|
453
|
+
if (
|
|
454
|
+
this.#tokenStorage &&
|
|
455
|
+
(body.error === 'invalid_scope' || body.error === 'access_denied')
|
|
456
|
+
) {
|
|
441
457
|
return this._refreshTokenAndRefetch(input, requestParams);
|
|
442
458
|
}
|
|
443
459
|
throw error;
|