rest-client-sdk 7.2.0 → 7.2.1
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/dist/{index.js → index.cjs} +1 -1
- package/dist/index.cjs.map +1 -0
- package/dist/{index.es.js → index.mjs} +1 -1
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -14
- package/dist/index.es.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/src/AsyncStorageInterface.ts +0 -7
- package/src/ErrorFactory.ts +0 -258
- package/src/Mapping/Attribute.ts +0 -33
- package/src/Mapping/ClassMetadata.ts +0 -125
- package/src/Mapping/Relation.ts +0 -68
- package/src/Mapping.ts +0 -120
- package/src/RestClientSdk.ts +0 -127
- package/src/RestClientSdkInterface.ts +0 -27
- package/src/TokenGenerator/AbstractTokenGenerator.ts +0 -86
- package/src/TokenGenerator/AuthorizationCodeFlowTokenGenerator.ts +0 -146
- package/src/TokenGenerator/ClientCredentialsGenerator.ts +0 -97
- package/src/TokenGenerator/PasswordGenerator.ts +0 -140
- package/src/TokenGenerator/ProvidedTokenGenerator.ts +0 -44
- package/src/TokenGenerator/TokenGeneratorInterface.ts +0 -27
- package/src/TokenGenerator/types.ts +0 -33
- package/src/TokenStorage.ts +0 -256
- package/src/TokenStorageInterface.ts +0 -14
- package/src/UnitOfWork.ts +0 -354
- package/src/client/AbstractClient.ts +0 -545
- package/src/client/headerUtils.ts +0 -53
- package/src/decorator.ts +0 -53
- package/src/index.ts +0 -86
- package/src/isImmutable.ts +0 -52
- package/src/serializer/JsSerializer.ts +0 -30
- package/src/serializer/Serializer.ts +0 -138
- package/src/serializer/SerializerInterface.ts +0 -89
- package/src/utils/levenshtein.ts +0 -53
- package/src/utils/logging.ts +0 -96
- package/src/utils/repositoryGenerator.ts +0 -16
|
@@ -1,545 +0,0 @@
|
|
|
1
|
-
import URI from 'urijs';
|
|
2
|
-
import { OauthError, getHttpErrorFromResponse } from '../ErrorFactory';
|
|
3
|
-
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
4
|
-
// eslint-disable-next-line import/no-duplicates
|
|
5
|
-
import type RestClientSdk from '../RestClientSdk';
|
|
6
|
-
// eslint-disable-next-line import/no-duplicates
|
|
7
|
-
import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
|
|
8
|
-
import { Token } from '../TokenGenerator/types';
|
|
9
|
-
import TokenStorageInterface from '../TokenStorageInterface';
|
|
10
|
-
import type SerializerInterface from '../serializer/SerializerInterface';
|
|
11
|
-
import { generateRepository } from '../utils/repositoryGenerator';
|
|
12
|
-
import { removeAuthorization, removeUndefinedHeaders } from './headerUtils';
|
|
13
|
-
|
|
14
|
-
const EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes
|
|
15
|
-
|
|
16
|
-
class AbstractClient<D extends MetadataDefinition> {
|
|
17
|
-
sdk: RestClientSdk<SdkMetadata>;
|
|
18
|
-
|
|
19
|
-
#tokenStorage: TokenStorageInterface<Token>;
|
|
20
|
-
|
|
21
|
-
serializer: SerializerInterface;
|
|
22
|
-
|
|
23
|
-
metadata: ClassMetadata;
|
|
24
|
-
|
|
25
|
-
#isUnitOfWorkEnabled: boolean;
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
sdk: RestClientSdk<SdkMetadata>,
|
|
29
|
-
metadata: ClassMetadata,
|
|
30
|
-
isUnitOfWorkEnabled = true
|
|
31
|
-
) {
|
|
32
|
-
this.sdk = sdk;
|
|
33
|
-
this.#tokenStorage = sdk.tokenStorage;
|
|
34
|
-
this.serializer = sdk.serializer;
|
|
35
|
-
this.metadata = metadata;
|
|
36
|
-
this.#isUnitOfWorkEnabled = isUnitOfWorkEnabled;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
withUnitOfWork(enabled: boolean): AbstractClient<D> {
|
|
40
|
-
// eslint-disable-next-line new-cap
|
|
41
|
-
return generateRepository<D>(this.sdk, this.metadata, enabled);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
getDefaultParameters(): Record<string, unknown> {
|
|
45
|
-
return {};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
|
-
getPathBase(pathParameters: Record<string, unknown>): string {
|
|
50
|
-
return `/${this.metadata.pathRoot}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
getEntityURI(entity: D['entity']): string {
|
|
54
|
-
let idValue = this._getEntityIdentifier(entity);
|
|
55
|
-
|
|
56
|
-
if (idValue === null) {
|
|
57
|
-
throw new Error('Cannot call `getEntityURI for entity without id.`');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (typeof idValue === 'number') {
|
|
61
|
-
if (Number.isFinite(idValue)) {
|
|
62
|
-
idValue = idValue.toString();
|
|
63
|
-
} else {
|
|
64
|
-
throw new Error('Unable to handle non-finite number');
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const pathBase = this.getPathBase({});
|
|
69
|
-
if (idValue.indexOf(pathBase) > -1) {
|
|
70
|
-
return idValue;
|
|
71
|
-
}
|
|
72
|
-
return `${pathBase}/${idValue}`;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* get an entity by its id
|
|
77
|
-
*
|
|
78
|
-
* @param {string|number} id the entity identifier
|
|
79
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
80
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
81
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
82
|
-
*/
|
|
83
|
-
find(
|
|
84
|
-
id: string | number,
|
|
85
|
-
queryParam: Record<string, unknown> = {},
|
|
86
|
-
pathParameters: Record<string, unknown> = {},
|
|
87
|
-
requestParams: Record<string, unknown> = {}
|
|
88
|
-
): Promise<D['entity']> {
|
|
89
|
-
const url = this._generateUrlFromParams(queryParam, pathParameters, id);
|
|
90
|
-
|
|
91
|
-
return this.deserializeResponse(
|
|
92
|
-
this.authorizedFetch(url, requestParams),
|
|
93
|
-
'item'
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* get a list of entities by some parameters
|
|
99
|
-
*
|
|
100
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
101
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
102
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
103
|
-
*/
|
|
104
|
-
findBy(
|
|
105
|
-
queryParam: Record<string, unknown>,
|
|
106
|
-
pathParameters: Record<string, unknown> = {},
|
|
107
|
-
requestParams: Record<string, unknown> = {}
|
|
108
|
-
): Promise<D['list']> {
|
|
109
|
-
const url = this._generateUrlFromParams(queryParam, pathParameters);
|
|
110
|
-
|
|
111
|
-
return this.deserializeResponse(
|
|
112
|
-
this.authorizedFetch(url, requestParams),
|
|
113
|
-
'list'
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* get a list of all entities
|
|
119
|
-
*
|
|
120
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
121
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
122
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
123
|
-
*/
|
|
124
|
-
findAll(
|
|
125
|
-
queryParam: Record<string, unknown> = {},
|
|
126
|
-
pathParameters: Record<string, unknown> = {},
|
|
127
|
-
requestParams: Record<string, unknown> = {}
|
|
128
|
-
): Promise<D['list']> {
|
|
129
|
-
return this.findBy(queryParam, pathParameters, requestParams);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* create an entity
|
|
134
|
-
*
|
|
135
|
-
* @param {Record<string, unknown>} entity the entity to persist
|
|
136
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
137
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
138
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
139
|
-
*/
|
|
140
|
-
create(
|
|
141
|
-
entity: D['entity'],
|
|
142
|
-
queryParam: Record<string, unknown> = {},
|
|
143
|
-
pathParameters: Record<string, unknown> = {},
|
|
144
|
-
requestParams: Record<string, unknown> = {}
|
|
145
|
-
): Promise<D['entity']> {
|
|
146
|
-
const url = new URI(this.getPathBase(pathParameters));
|
|
147
|
-
url.addSearch(queryParam);
|
|
148
|
-
|
|
149
|
-
const oldSerializedModel = this.metadata.getDefaultSerializedModel();
|
|
150
|
-
const newSerializedModel = this.serializer.normalizeItem(
|
|
151
|
-
entity,
|
|
152
|
-
this.metadata
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const diff = this.sdk.unitOfWork.getDirtyData(
|
|
156
|
-
newSerializedModel,
|
|
157
|
-
oldSerializedModel,
|
|
158
|
-
this.metadata
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
return this.deserializeResponse(
|
|
162
|
-
this.authorizedFetch(url, {
|
|
163
|
-
method: 'POST',
|
|
164
|
-
body: this.serializer.encodeItem(diff, this.metadata),
|
|
165
|
-
...requestParams,
|
|
166
|
-
}),
|
|
167
|
-
'item'
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* update an entity
|
|
173
|
-
*
|
|
174
|
-
* @param {Record<string, unknown>} entity the entity to update
|
|
175
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
176
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
177
|
-
*/
|
|
178
|
-
update(
|
|
179
|
-
entity: D['entity'],
|
|
180
|
-
queryParam: Record<string, unknown> = {},
|
|
181
|
-
requestParams: Record<string, unknown> = {}
|
|
182
|
-
): Promise<D['entity']> {
|
|
183
|
-
const url = new URI(this.getEntityURI(entity));
|
|
184
|
-
url.addSearch(queryParam);
|
|
185
|
-
|
|
186
|
-
let newSerializedModel = this.serializer.normalizeItem(
|
|
187
|
-
entity,
|
|
188
|
-
this.metadata
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
const identifier = this._getEntityIdentifier(newSerializedModel);
|
|
192
|
-
let oldModel;
|
|
193
|
-
if (identifier !== null) {
|
|
194
|
-
oldModel = this.sdk.unitOfWork.getDirtyEntity(identifier);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (oldModel) {
|
|
198
|
-
newSerializedModel = this.sdk.unitOfWork.getDirtyData(
|
|
199
|
-
newSerializedModel,
|
|
200
|
-
oldModel,
|
|
201
|
-
this.metadata
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return this.deserializeResponse(
|
|
206
|
-
this.authorizedFetch(url, {
|
|
207
|
-
method: 'PUT',
|
|
208
|
-
body: this.serializer.encodeItem(newSerializedModel, this.metadata),
|
|
209
|
-
...requestParams,
|
|
210
|
-
}),
|
|
211
|
-
'item'
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* delete an entity
|
|
217
|
-
*
|
|
218
|
-
* @param {Record<string, unknown>} the entity to delete
|
|
219
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
220
|
-
*/
|
|
221
|
-
delete(entity: D['entity'], requestParams = {}): Promise<Response> {
|
|
222
|
-
const url = this.getEntityURI(entity);
|
|
223
|
-
const identifier = this._getEntityIdentifier(entity);
|
|
224
|
-
|
|
225
|
-
return this.authorizedFetch(url, {
|
|
226
|
-
method: 'DELETE',
|
|
227
|
-
...requestParams,
|
|
228
|
-
}).then((response) => {
|
|
229
|
-
if (identifier !== null) {
|
|
230
|
-
this.sdk.unitOfWork.clear(identifier);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return response;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
deserializeResponse(
|
|
238
|
-
requestPromise: Promise<Response>,
|
|
239
|
-
listOrItem: 'list'
|
|
240
|
-
): Promise<D['list']>;
|
|
241
|
-
|
|
242
|
-
deserializeResponse(
|
|
243
|
-
requestPromise: Promise<Response>,
|
|
244
|
-
listOrItem: 'item'
|
|
245
|
-
): Promise<D['entity']>;
|
|
246
|
-
|
|
247
|
-
deserializeResponse<LOR extends 'list' | 'item'>(
|
|
248
|
-
requestPromise: Promise<Response>,
|
|
249
|
-
listOrItem: LOR
|
|
250
|
-
): Promise<D['entity'] | D['list']> {
|
|
251
|
-
return requestPromise
|
|
252
|
-
.then((response) => response.text().then((text) => ({ response, text })))
|
|
253
|
-
.then(({ response, text }) => {
|
|
254
|
-
if (listOrItem === 'list') {
|
|
255
|
-
// for list, we need to deserialize the result to get an object
|
|
256
|
-
const itemList = this.serializer.deserializeList(
|
|
257
|
-
text,
|
|
258
|
-
this.metadata,
|
|
259
|
-
response
|
|
260
|
-
) as D['list'];
|
|
261
|
-
|
|
262
|
-
if (this.#isUnitOfWorkEnabled) {
|
|
263
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
264
|
-
for (const decodedItem of itemList) {
|
|
265
|
-
const identifier = this._getEntityIdentifier(decodedItem);
|
|
266
|
-
const normalizedItem = this.serializer.normalizeItem(
|
|
267
|
-
decodedItem,
|
|
268
|
-
this.metadata
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
// then we register the re-normalized item
|
|
272
|
-
if (identifier !== null) {
|
|
273
|
-
this.sdk.unitOfWork.registerClean(identifier, normalizedItem);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return itemList as D['list'];
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// for items, we can just decode the item (ie. transform it to JS object)
|
|
282
|
-
const decodedItem = this.serializer.decodeItem(
|
|
283
|
-
text,
|
|
284
|
-
this.metadata,
|
|
285
|
-
response
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
// and register it directy without deserializing + renormalizing
|
|
289
|
-
const identifier = this._getEntityIdentifier(decodedItem);
|
|
290
|
-
|
|
291
|
-
// and finally return the denormalized item
|
|
292
|
-
const item = this.serializer.denormalizeItem(
|
|
293
|
-
decodedItem,
|
|
294
|
-
this.metadata,
|
|
295
|
-
response
|
|
296
|
-
) as D['entity'];
|
|
297
|
-
|
|
298
|
-
if (this.#isUnitOfWorkEnabled && identifier !== null) {
|
|
299
|
-
this.sdk.unitOfWork.registerClean(
|
|
300
|
-
identifier,
|
|
301
|
-
this.serializer.normalizeItem(item, this.metadata)
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
return item as D['entity'];
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
makeUri(input: string | URI): URI {
|
|
310
|
-
const url = input instanceof URI ? input : new URI(input);
|
|
311
|
-
url.host(this.sdk.config.path).scheme(this.sdk.config.scheme);
|
|
312
|
-
|
|
313
|
-
if (this.sdk.config.port) {
|
|
314
|
-
url.port(String(this.sdk.config.port));
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (this.sdk.mapping.idPrefix) {
|
|
318
|
-
const segments = url.segment();
|
|
319
|
-
|
|
320
|
-
if (!url.pathname().startsWith(this.sdk.mapping.idPrefix)) {
|
|
321
|
-
segments.unshift(this.sdk.mapping.idPrefix);
|
|
322
|
-
url.segment(segments);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return url;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
authorizedFetch(input: string | URI, requestParams = {}): Promise<Response> {
|
|
330
|
-
const url = this.makeUri(input);
|
|
331
|
-
|
|
332
|
-
return this._fetchWithToken(url.toString(), requestParams);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
private _generateUrlFromParams(
|
|
336
|
-
queryParam: Record<string, unknown>,
|
|
337
|
-
pathParameters: Record<string, unknown> = {},
|
|
338
|
-
id: null | string | number = null
|
|
339
|
-
): URI {
|
|
340
|
-
const params = queryParam;
|
|
341
|
-
if (this.sdk.config.useDefaultParameters) {
|
|
342
|
-
Object.assign(params, this.getDefaultParameters());
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const pathBase = this.getPathBase(pathParameters);
|
|
346
|
-
|
|
347
|
-
let url = null;
|
|
348
|
-
if (id) {
|
|
349
|
-
const testPathBase = this.sdk.mapping.idPrefix
|
|
350
|
-
? `${this.sdk.mapping.idPrefix}${pathBase}`
|
|
351
|
-
: pathBase;
|
|
352
|
-
|
|
353
|
-
if (typeof id === 'string' && id.startsWith(testPathBase)) {
|
|
354
|
-
url = new URI(id);
|
|
355
|
-
} else {
|
|
356
|
-
url = new URI(`${pathBase}/${id}`);
|
|
357
|
-
}
|
|
358
|
-
} else {
|
|
359
|
-
url = new URI(pathBase);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (params) {
|
|
363
|
-
url.addSearch(params);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return url;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
private _fetchWithToken(
|
|
370
|
-
input: string,
|
|
371
|
-
requestParams = {}
|
|
372
|
-
): Promise<Response> {
|
|
373
|
-
if (!input) {
|
|
374
|
-
throw new Error('input is empty');
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (this.#tokenStorage) {
|
|
378
|
-
return this.#tokenStorage
|
|
379
|
-
.getCurrentTokenExpiresIn()
|
|
380
|
-
.then((accessTokenExpiresIn) => {
|
|
381
|
-
if (
|
|
382
|
-
accessTokenExpiresIn !== null &&
|
|
383
|
-
accessTokenExpiresIn <= EXPIRE_LIMIT_SECONDS
|
|
384
|
-
) {
|
|
385
|
-
return this.#tokenStorage
|
|
386
|
-
.refreshToken()
|
|
387
|
-
.then(
|
|
388
|
-
(refreshedTokenObject) => refreshedTokenObject.access_token
|
|
389
|
-
).catch((e) => {
|
|
390
|
-
this._handleRefreshTokenFailure(e);
|
|
391
|
-
|
|
392
|
-
throw e;
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
return this.#tokenStorage.getAccessToken();
|
|
397
|
-
})
|
|
398
|
-
.then((token) => this._doFetch(token, input, requestParams));
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
return this._doFetch(null, input, requestParams);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
private _refreshTokenAndRefetch(
|
|
405
|
-
input: string,
|
|
406
|
-
requestParams: RequestInit = {}
|
|
407
|
-
): Promise<Response> {
|
|
408
|
-
return this.#tokenStorage
|
|
409
|
-
.refreshToken()
|
|
410
|
-
.then(() => {
|
|
411
|
-
// eslint-disable-next-line prefer-const
|
|
412
|
-
let { headers, ...rest } = requestParams;
|
|
413
|
-
|
|
414
|
-
const updatedRequestParams: RequestInit = {
|
|
415
|
-
...rest,
|
|
416
|
-
headers: removeAuthorization(headers),
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
return this._fetchWithToken(input, updatedRequestParams);
|
|
420
|
-
})
|
|
421
|
-
.catch((e) => {
|
|
422
|
-
this._handleRefreshTokenFailure(e);
|
|
423
|
-
|
|
424
|
-
throw e;
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
private _manageUnauthorized(
|
|
429
|
-
response: Response,
|
|
430
|
-
input: string,
|
|
431
|
-
requestParams = {}
|
|
432
|
-
): Promise<Response> {
|
|
433
|
-
const error = getHttpErrorFromResponse(response);
|
|
434
|
-
|
|
435
|
-
// https://tools.ietf.org/html/rfc2617#section-1.2
|
|
436
|
-
const authorizationHeader = response.headers.get('www-authenticate');
|
|
437
|
-
if (authorizationHeader) {
|
|
438
|
-
const invalidGrant =
|
|
439
|
-
authorizationHeader.indexOf('error="invalid_grant"') > -1;
|
|
440
|
-
|
|
441
|
-
if (invalidGrant && this.#tokenStorage) {
|
|
442
|
-
return this._refreshTokenAndRefetch(input, requestParams);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
throw error;
|
|
446
|
-
} else {
|
|
447
|
-
// if www-authenticate header is missing, try and read json response
|
|
448
|
-
return response
|
|
449
|
-
.json()
|
|
450
|
-
.then((body) => {
|
|
451
|
-
if (
|
|
452
|
-
this.#tokenStorage &&
|
|
453
|
-
(body.error === 'invalid_scope' || body.error === 'access_denied')
|
|
454
|
-
) {
|
|
455
|
-
return this._refreshTokenAndRefetch(input, requestParams);
|
|
456
|
-
}
|
|
457
|
-
throw error;
|
|
458
|
-
})
|
|
459
|
-
.catch((err) => {
|
|
460
|
-
if (err instanceof OauthError) {
|
|
461
|
-
throw err;
|
|
462
|
-
}
|
|
463
|
-
throw error;
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
private _doFetch(
|
|
469
|
-
accessToken: null | string,
|
|
470
|
-
input: string,
|
|
471
|
-
requestParams: RequestInit
|
|
472
|
-
): Promise<Response> {
|
|
473
|
-
let params = requestParams;
|
|
474
|
-
let baseHeaders: HeadersInit = {};
|
|
475
|
-
|
|
476
|
-
if (params.method !== 'GET' || (params.method !== 'GET' && params.method !== 'DELETE')) {
|
|
477
|
-
baseHeaders = {
|
|
478
|
-
'Content-Type': 'application/json',
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
if (accessToken) {
|
|
483
|
-
baseHeaders.Authorization = `${this.sdk.config.authorizationType} ${accessToken}`;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
if (params) {
|
|
487
|
-
if (!params.headers) {
|
|
488
|
-
params.headers = {};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
params.headers = Object.assign(baseHeaders, params.headers);
|
|
492
|
-
} else {
|
|
493
|
-
params = { headers: baseHeaders };
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
if (params.headers) {
|
|
497
|
-
params.headers = removeUndefinedHeaders(params.headers);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
let logId: undefined | string;
|
|
501
|
-
if (this.sdk.logger) {
|
|
502
|
-
logId = this.sdk.logger.logRequest({ url: input, ...params });
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// eslint-disable-next-line consistent-return
|
|
506
|
-
return fetch(input, params).then((response) => {
|
|
507
|
-
if (this.sdk.logger) {
|
|
508
|
-
this.sdk.logger.logResponse(response, logId);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
if (response.status < 400) {
|
|
512
|
-
return response;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
if (response.status === 401) {
|
|
516
|
-
return this._manageUnauthorized(response, input, params);
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
const httpError = getHttpErrorFromResponse(response);
|
|
520
|
-
throw httpError;
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
private _getEntityIdentifier(
|
|
525
|
-
object: Record<string, unknown>
|
|
526
|
-
): null | string | number {
|
|
527
|
-
const idKey = this.metadata.getIdentifierAttribute().serializedKey;
|
|
528
|
-
|
|
529
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
530
|
-
// @ts-ignore
|
|
531
|
-
return object[idKey];
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
private _handleRefreshTokenFailure(error: Error): void {
|
|
535
|
-
if (error instanceof OauthError) {
|
|
536
|
-
this.#tokenStorage.logout().then(() => {
|
|
537
|
-
if (this.sdk.config.onRefreshTokenFailure) {
|
|
538
|
-
this.sdk.config.onRefreshTokenFailure(error);
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
export default AbstractClient;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/* eslint-disable consistent-return */
|
|
2
|
-
export function removeAuthorization(
|
|
3
|
-
headers: undefined | HeadersInit
|
|
4
|
-
): undefined | HeadersInit {
|
|
5
|
-
if (!headers) {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
if (headers instanceof Headers) {
|
|
10
|
-
headers.delete('Authorization');
|
|
11
|
-
return headers;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const filterAuthorization = (entries: string[][]): string[][] =>
|
|
15
|
-
entries.filter((header) => header[0] !== 'Authorization');
|
|
16
|
-
|
|
17
|
-
if (Array.isArray(headers)) {
|
|
18
|
-
return filterAuthorization(headers);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return Object.fromEntries(filterAuthorization(Object.entries(headers)));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* remove undefined key, usefull to remove Content-Type for example.
|
|
26
|
-
* Does not apply on "Headers" instance as values are string in there.
|
|
27
|
-
*/
|
|
28
|
-
export function removeUndefinedHeaders(headers: HeadersInit): HeadersInit {
|
|
29
|
-
if (headers instanceof Headers) {
|
|
30
|
-
return headers;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const filterEntries = (entries: string[][]): string[][] =>
|
|
34
|
-
entries.filter((header) => header[1] !== undefined);
|
|
35
|
-
|
|
36
|
-
if (Array.isArray(headers)) {
|
|
37
|
-
return filterEntries(headers);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return Object.fromEntries(filterEntries(Object.entries(headers)));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function convertToRecord(headers: HeadersInit): Record<string, string> {
|
|
44
|
-
if (headers instanceof Headers) {
|
|
45
|
-
return Object.fromEntries(headers.entries());
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (Array.isArray(headers)) {
|
|
49
|
-
return Object.fromEntries(headers);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return headers;
|
|
53
|
-
}
|
package/src/decorator.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
|
|
3
|
-
// memoize promise returning function so that it returns
|
|
4
|
-
// the same promise if called again before resolve / reject
|
|
5
|
-
|
|
6
|
-
interface MemoizableFunction<P, R> {
|
|
7
|
-
(...params: P[]): Promise<R> | false;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type FalsableCallbackReturn<R> = false | Promise<R>;
|
|
11
|
-
|
|
12
|
-
export function memoizePromise<P, R>(
|
|
13
|
-
callback: MemoizableFunction<P, R>
|
|
14
|
-
): () => Promise<R> {
|
|
15
|
-
const cache: { [key: string]: FalsableCallbackReturn<R> } = {};
|
|
16
|
-
|
|
17
|
-
function memoized(this: unknown, ...parameters: P[]): Promise<R> {
|
|
18
|
-
const cacheKey = JSON.stringify(parameters);
|
|
19
|
-
const cacheValue = cache[cacheKey];
|
|
20
|
-
|
|
21
|
-
if (cacheValue) {
|
|
22
|
-
return cacheValue;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Get and add the value to the cache
|
|
26
|
-
const value = callback.apply(this, parameters);
|
|
27
|
-
cache[cacheKey] = value;
|
|
28
|
-
|
|
29
|
-
if (!value || typeof value.then !== 'function') {
|
|
30
|
-
throw new Error(
|
|
31
|
-
'Memoization Error, Async function returned non-promise value'
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Delete the value regardless of whether it resolves or rejects
|
|
36
|
-
return value.then(
|
|
37
|
-
(internalValue: R) => {
|
|
38
|
-
cache[cacheKey] = false;
|
|
39
|
-
|
|
40
|
-
return internalValue;
|
|
41
|
-
},
|
|
42
|
-
(err: Error) => {
|
|
43
|
-
cache[cacheKey] = false;
|
|
44
|
-
|
|
45
|
-
throw err;
|
|
46
|
-
}
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
memoized.cache = cache;
|
|
51
|
-
|
|
52
|
-
return memoized;
|
|
53
|
-
}
|