rest-client-sdk 6.6.1 → 6.7.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.
@@ -6,6 +6,10 @@ import { ErrorBody, Token, TokenBody } from './TokenGenerator/types';
6
6
  declare class HttpError extends Error {
7
7
  baseResponse: Response;
8
8
  constructor(message: null | string, baseResponse: Response);
9
+ /**
10
+ * Get the JSON of the baseResponse directly, if possible
11
+ */
12
+ responseJson(): Promise<Record<string, unknown>>;
9
13
  }
10
14
  declare class OauthError extends Error {
11
15
  previousError: HttpError | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rest-client-sdk",
3
- "version": "6.6.1",
3
+ "version": "6.7.0",
4
4
  "description": "Rest Client SDK for API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -23,6 +23,13 @@ class HttpError extends Error {
23
23
  this.name = 'HttpError';
24
24
  this.baseResponse = baseResponse;
25
25
  }
26
+
27
+ /**
28
+ * Get the JSON of the baseResponse directly, if possible
29
+ */
30
+ public async responseJson(): Promise<Record<string, unknown>> {
31
+ return this.baseResponse.json();
32
+ }
26
33
  }
27
34
 
28
35
  class OauthError extends Error {
@@ -178,6 +178,7 @@ class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
178
178
  ): Promise<T> {
179
179
  if (isResponse(responseOrBody)) {
180
180
  const response = responseOrBody;
181
+ const originalResponse = response.clone();
181
182
  return response
182
183
  .json()
183
184
  .then((body) => {
@@ -186,14 +187,17 @@ class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
186
187
  manageOauthError(body, response);
187
188
  } else if (response.status >= 400) {
188
189
  // throw an error if response status code is an "error" status code
189
- throw getHttpErrorFromResponse(response);
190
+ throw getHttpErrorFromResponse(originalResponse);
190
191
  }
191
192
 
192
193
  return body;
193
194
  })
194
195
  .catch((err) => {
195
196
  if (!(err instanceof OauthError)) {
196
- throw new OauthError(err.type, getHttpErrorFromResponse(response));
197
+ throw new OauthError(
198
+ err.type,
199
+ getHttpErrorFromResponse(originalResponse)
200
+ );
197
201
  }
198
202
 
199
203
  throw err;
package/CHANGELOG.md DELETED
@@ -1,503 +0,0 @@
1
- # Changelog
2
-
3
- ## 6.6.1
4
-
5
- Do not use async / await to avoid regeneratorRuntime issue
6
-
7
- ## 6.6.0
8
-
9
- - Add Authorization code flow token generator
10
- - Better compatibility with the oauth spec: https://tools.ietf.org/html/rfc6749
11
- - handle oauth error even if the TokenGenerator does not extends `AbstractTokenGenator` or call `_manageOauthError`
12
- - [Minor BC] `TokenGenerator.generateToken` and `TokenGenerator.generateToken` might return a `Response` instead of a plain object. This should not break anything as the response is handled by the `TokenStorage` unless you call directly the `TokenGenerator` (which you should not). Returning an object has been deprecated and is only used in the `ProvidedTokenGenerator` (which is also deprecated)
13
- - [Minor BC] `OauthError.previousError` might be undefined
14
- - [Deprecation] Deprecate `ProvidedTokenGenerator`. You should use one of the oauth2 generator or implement your own custom generator.
15
- - [Deprecation] Calling `AbstractTokenGenerator._manageOauthError` is deprecated. You should return the `Response` instead :
16
-
17
- ```diff
18
- generateToken() {
19
- const body = // … generate body
20
-
21
- return fetch(url, {
22
- method: 'POST',
23
- body,
24
- + });
25
- - }).then((response) => {
26
- - if (response.status < 400) {
27
- - return response;
28
- - }
29
-
30
- - return this._manageOauthError(response);
31
- - });
32
- }
33
- ```
34
-
35
- ## 6.5.2
36
-
37
- Fix regression from `6.5.1` : `TokenStorage.refreshToken` does now throws an Error anymore if there is no token stored in the storage.
38
-
39
- ## 6.5.1
40
-
41
- - getItem from AsyncStorage interface might return null
42
- - `TokenStorage.refreshToken` now throws an Error if there is no token stored in the storage.
43
-
44
- ## 6.5.0
45
-
46
- ### Added
47
-
48
- Accept Relation.ONE_TO_ONE relation.
49
- It is basically handled the same way as MANY_TO_ONE, but has been added for semantic purpose.
50
- The comportment may differ later.
51
-
52
- ## 6.4.1
53
-
54
- ### Fixed
55
-
56
- - withUnitOfWork returns a clone of the base repository to avoid race condition on `isUnitOfWorkEnabled`
57
-
58
- ## 6.4.0
59
-
60
- - allow unitOfWork deactivation on find\* calls
61
-
62
- ## 6.3.0
63
-
64
- ### Added
65
-
66
- Accept Relation.MANY_TO_MANY relation.
67
- It is basically handled the same way as ONE_TO_MANY, but has been added for semantic purpose.
68
- The comportment may differ later.
69
-
70
- ## 6.2.0
71
-
72
- ### Changed
73
-
74
- - [TypeScript] better handle deserializeResponse method definition
75
-
76
- ## 6.1.0
77
-
78
- ### Added
79
-
80
- Export `MetadataDefinition` for AbstractClient inheritance
81
-
82
- ## 6.0.0
83
-
84
- ### Changed
85
-
86
- - [BREAKING] UnitOfWork is disabled by default, one must add a prop `unitOfWorkEnabled` with value true to the config object passed to the RestClientSdk's constructor in order to enable it
87
- - TokenStorage's `generateToken` and `refreshToken` methods are now memoized in order to avoid bugs when making concurrent calls
88
-
89
- ## 5.0.0
90
-
91
- This release is a migration to TypeScript, but the API stay the same. The only breaking changes are more fixed bugs that might be breaking in really improbable case.
92
-
93
- ### Changed
94
-
95
- - [BREAKING] TokenGenerator's `refreshToken` method signature changed from `refreshToken(accessToken, parameters)` to `refreshToken(accessToken)`.
96
- If you did extend the `AbstractToken` generator, you shouldn't relly on the second parameter.
97
- As a matter of fact, the auto-refreshed token in `AbstractClient` did not send any parameters either, so it should not have been working before !
98
- Accordingly The `refreshToken` method of `TokenStorage` signature changed from `refreshToken(parameters)` to `refreshToken()`
99
- - [Might Break] all pseudo-private methods and attributes (starting with `_`) are unavailable and are now really private (or protected).
100
- - `_storeAccessToken` should be replace by a `ProvidedTokenGenerator`
101
- - [Might Break] `ClassMetadata`, `Attribute` and `Relation` attributes are now `readonly`. You can not change them after initialization.
102
- - [Might Break] `AbstractTokenGenerator` is now abstract. It should not have been used directly anyway. It implies that the method that previously did thrown errors are not callable (`generateToken`, `refreshToken` and `checkTokenGeneratorConfig`). They should not have been called. They don't exist in JS land anymore.
103
- - [Might Break] `canAutogenerateToken` has been removed from the token storages and replaced by a `autogenerateToken()` method (it's not real POO, but I did not manage to deal with instance of for now)
104
- - [Might Break] AbstractClient.\_refreshTokenAndRefetch does not take the response as a first argument (it wan unused). The method was supposed to be private be the way.
105
-
106
- - Make `SerializerInterace` and `Serializer` input and output understandable
107
-
108
- ### Added
109
-
110
- - export (and implement) `RestClientSdkInterface` and `TokenStorageInterface`
111
- - export `TokenGeneratorInterface` and `AsyncStorageInterface`
112
-
113
- ## 4.1.4
114
-
115
- ### Changed
116
-
117
- - Passing a string as argument to `ProvidedTokenGenerator` is now deprecated. You should pass the token object instead. It should have had a weird comportment before by the way.
118
-
119
- ### Fixed
120
-
121
- - Fix issue when two token generation are send on the same time : the second one will not throw an error now.
122
-
123
- ## 4.1.3
124
-
125
- ### Fixed
126
-
127
- - Fix issue when no token is provided but the object is `{ expires_at: null }`
128
-
129
- ## 4.1.2
130
-
131
- ### Fixed
132
-
133
- - Fix issue with urijs 1.19.2
134
-
135
- ## 4.1.1
136
-
137
- ### Changed
138
-
139
- - replace object spread with Object.assign for build to work
140
-
141
- ## 4.1.0
142
-
143
- ### Changed
144
-
145
- - main AbstractClient CRUD methods accept additional requestParams for overriding fetch params like method, headers, etc.
146
-
147
- ## 4.0.1
148
-
149
- ### Changed
150
-
151
- - downgraded jest and babel-jest to ^23.6.0 in order to fix tests
152
-
153
- ## 4.0.0
154
-
155
- ### Changed
156
-
157
- - Do not generate token expires_at in \_storeAccessToken since it is sometimes used from outside, in tests for instance (even though it is supposed to be private)
158
- - [Breaking] throw new error types when oauth calls fail, they all wrap the originalError => InvalidGrantError, InvalidScopeError which both extend OauthError. Original error can be found at error.previousError. If it was an HttpError the response is at error.previousError.baseResponse
159
-
160
- ## 3.2.1
161
-
162
- ### Changed
163
-
164
- - Refactor error handling with error factory and handle 400's in refreshToken of PasswordGenerator
165
-
166
- ## 3.2.0
167
-
168
- ### Changed
169
-
170
- - Pre-emptively refresh the access token if it is close to expiration before doing any request
171
-
172
- ## 3.1.1
173
-
174
- ### Changed
175
-
176
- - Read json response in \_manageUnauthorized when www-authenticate header is not available
177
-
178
- ## 3.1.0
179
-
180
- ### Changed
181
-
182
- - 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`
183
- - Add `TokenStorage.getCurrentTokenExpiresIn` function to known how many seconds are remaining for the access token currently stored
184
-
185
- ## 3.0.0
186
-
187
- ### Changed
188
-
189
- - [Breaking] `AccessDeniedError` is now `UnauthorizedError`
190
- - [Breaking] Default error is now `HttpError`
191
- - Add `ConflictError` to react to 409
192
-
193
- ## 2.3.0
194
-
195
- ### Changed
196
-
197
- - `TokenStorage` add possibility to pass access_token async storage key as constructor param
198
- - `AbstractClient._manageAccessDenied` checks headers instead of json to ensure refreshing the token is required
199
-
200
- ## 2.2.1
201
-
202
- ### Changed
203
-
204
- - `TokenStorage.getAccessTokenObject()` returns an object and null if the stored token is not an object
205
- - [Might Break] `TokenStorage.getAccessTokenObject()` does not return `undefined` anymore but `null` if no token object is found
206
-
207
- ## 2.2.0
208
-
209
- ### Changed
210
-
211
- - Add possibility to pass null as tokenStorage argument of the sdk
212
- - switch method names \_doFetch and \_fetchWithToken in AbstractClient for coherence
213
-
214
- ## 2.1.0
215
-
216
- ### Changed
217
-
218
- - Add method getAccessTokenObject to TokenStorage
219
-
220
- ## 2.0.1
221
-
222
- ### Changed
223
-
224
- - Better packaging system [#48](https://github.com/mapado/rest-client-js-sdk/pull/48)
225
-
226
- ## 2.0.0
227
-
228
- ### Added
229
-
230
- - [Might break] Custom serializer can:
231
- - `serializeItem` has been splitted into `normalizeItem` + `encodeItem`
232
- - `deserializeItem` has been splitted into `denormalizeItem` + `decodeItem`
233
- - `deserializeList` has been splitted into `denormalizeList` + `decodeList`
234
-
235
- You must implement them if you have a custom serialization that do not use plain javascript objects.
236
-
237
- ### Changed
238
-
239
- - [Breaking] Calling `restClientSdk.foo.xxx` is deprecated, you must now call `restClientSdk.getRepository('foo').xxx`
240
- - [Breaking] RestClientSdk now takes a Mapping instance instead of a clientList. This instance is required
241
- - [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.
242
- Beware that now, the `key` is now used both to call the repository name, and passed to the serializer
243
- Before:
244
-
245
- ```js
246
- class SomeClient extends AbstractClient {
247
- getName() {
248
- return 'SomeClient';
249
- }
250
- }
251
-
252
- new RestClientSdk(
253
- // ...
254
- {
255
- foo: SomeClient,
256
- }
257
- );
258
- ```
259
-
260
- Now:
261
-
262
- ```js
263
- const metadata = new ClassMetadata('foo', 'some_endpoint');
264
- mapping = new Mapping();
265
- mapping.setMapping([ metadata ]);
266
-
267
- const sdk = new RestClientSdk(/* ... */, mapping); // `foo` will be used in serializer too
268
- sdk.foo.find();
269
- ```
270
-
271
- - [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.
272
-
273
- ### Removed
274
-
275
- - Dropped support for node 6
276
- - AbstractClient does not need to specify `getPathBase` anymore: this is generated via the classmetata. You can still override it.
277
- - `getEntityURI` is not mandatory anymore. it will be autogenerate by the AbstractClient. You can still override it
278
- - There is no need to create an empty client extending `AbstractClient` now, the default will be `AbstractClient`
279
- - config `prefix` is not used anymore, you should specify your prefix in the mapping: `new Mapping('/v1')`
280
-
281
- ## 2.0.0-rc.14
282
-
283
- Check relations in `isValidMapping`
284
-
285
- ## 2.0.0-rc.13
286
-
287
- Fix issue when setting a ManyToOne relatossue when setting a ManyToOne relation to `null`n to `null`
288
-
289
- ## 2.0.0-rc.12
290
-
291
- add a function to test mapping validity
292
-
293
- ## 2.0.0-rc.10 + 2.0.0-rc.11
294
-
295
- deserialize + normalize item in unitofwork
296
-
297
- ## 2.0.0-rc.9
298
-
299
- creating or updating an "complex" entity (not a basic JS object) would throw an error
300
-
301
- ## 2.0.0-rc.8
302
-
303
- remove `async` method (that already returned a Promise object)
304
-
305
- ## 2.0.0-rc.7
306
-
307
- Fix issue when posting a ManyToOne relation with only the id as string
308
-
309
- ## 2.0.0-rc.6
310
-
311
- Fix bug in delete call (not returning the response)
312
-
313
- ## 2.0.0-rc.5
314
-
315
- Fix bug in serializer
316
-
317
- ## 2.0.0-rc.4
318
-
319
- Fix issue with list denormalized oo objects
320
-
321
- ## 2.0.0-rc.3
322
-
323
- Cleaner relations, matches the Arguments attributes
324
-
325
- ## 2.0.0-rc.2
326
-
327
- ### Changed
328
-
329
- - `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
330
-
331
- ## 2.0.0-rc.1
332
-
333
- See release note for 2.0.0
334
-
335
- ## [1.3.2] - 2017-12-04
336
-
337
- ### Changed
338
-
339
- - fix simultaneous refresh token throwing error when trying to access to
340
- response body twice
341
-
342
- ## [1.3.1] - 2017-12-04
343
-
344
- ### Changed
345
-
346
- - add error message to AccessDeniedError in PasswordGenerator's refreshToken
347
-
348
- ## [1.3.0] - 2017-11-29
349
-
350
- ### Changed
351
-
352
- - throw AccessDeniedError only on BadRequestError in PasswordGenerator
353
- refreshToken
354
-
355
- ## [1.2.0] - 2017-11-10
356
-
357
- ### Changed
358
-
359
- - fix response passed as error message in Errors
360
- - throw AccessDeniedError on any Error in PasswordGenerator refreshToken
361
- - allow to pass scope param in tokenGeneratorConfig
362
-
363
- ## [1.1.1] - 2017-10-08
364
-
365
- ### Changed
366
-
367
- - upgrade rollup to 0.50.0
368
-
369
- ## [1.1.0] - 2017-08-30
370
-
371
- ### Changed
372
-
373
- - Pass on response to serializer
374
- - Upgrade dependencies (rollup 0.49, eslint 4, etc.)
375
- - Add prettier to project
376
-
377
- ## [1.0.5] - 2017-08-30
378
-
379
- ### Changed
380
-
381
- - Fix token loop when token is not valid anymore
382
-
383
- ## [1.0.4] - 2017-07-28
384
-
385
- ### Changed
386
-
387
- - Remove undefined headers
388
-
389
- ## [1.0.3] - 2017-07-27
390
-
391
- ### Changed
392
-
393
- - Allow overriding base headers
394
- [#24](https://github.com/mapado/rest-client-js-sdk/pull/24)
395
-
396
- ## [1.0.2] - 2017-07-04
397
-
398
- This release is the same as 0.15.0 and though fully compatible
399
-
400
- ### Changed
401
-
402
- - Rolled back to urijs because domurl use the default node implementation which
403
- is not compatible with the stack we use. The real future solution will be to
404
- use Javascript `URL` object but too early and unstable for now
405
-
406
- ## [1.0.1] - 2017-07-03 - [YANKED]
407
-
408
- ### Changed
409
-
410
- - Make urijs implementation work again but might be breaking
411
- - Url constructor passed with `noTransform = true` for better perf and avoid
412
- potential bugs
413
-
414
- ## [1.0.0] - 2017-07-03 - [YANKED]
415
-
416
- ### Changed
417
-
418
- - Replace [urijs](https://medialize.github.io/URI.js/) dependency by smaller
419
- [domurl](https://github.com/Mikhus/domurl)
420
-
421
- ## [0.15.0] - 2017-06-27
422
-
423
- ### Added
424
-
425
- - Added the current URI as referer for http calls
426
-
427
- ## [0.14.x]
428
-
429
- ### breaking changes
430
-
431
- Error responses in PasswordGenerator and ClientCredentialsGenerator now throw
432
- proper js errors instead of returning the response in the promise rejection
433
-
434
- ## [0.13.x]
435
-
436
- ### breaking changes
437
-
438
- The library does not depends on `immutablejs` anymore, so we needed to make a
439
- few breaking changes:
440
-
441
- - Do not depend on `@id` anymore: The `AbstractClient` need to implements a
442
- `getEntityURI(entity)` and return an query string from it. This is not a great
443
- pattern, but it will do for now.
444
- (https://github.com/mapado/rest-client-js-sdk/pull/19)
445
- - The `entityFactory` does not exists anymore. It has been replaced by a
446
- `Serializer`: (https://github.com/mapado/rest-client-js-sdk/pull/21)
447
- - If you used the default entityFactory, you will now receive plain Javascript
448
- objects instead of immutable's Map or List
449
- - If you previously overrided the `entityFactory`, you will need to switch to
450
- the new `Serializer` object: It is much more extensible but is a bit more
451
- complex to extend. See README to know how to do it.
452
- - the `createEntityFromJsonResponse` method has been renamed to
453
- `deserializeResponse`
454
- - The library moved from ES5 to bundling with rollupjs, that should not break
455
- anything and should improve size of bundle but I am not so sure of this, so
456
- patch may follow (https://github.com/mapado/rest-client-js-sdk/pull/20)
457
-
458
- ## [0.12.x]
459
-
460
- ### breaking changes
461
-
462
- - a `delete` does not call `createEntityFromJsonResponse` anymore as it should
463
- return a "204 No Content" or a "404 Not Found"
464
-
465
- ## [0.11.x]
466
-
467
- ### breaking changes
468
-
469
- - `ProviderTokenGenerator` does not accept a param object anymore on its second
470
- argument.
471
-
472
- ### new features
473
-
474
- - `ProviderTokenGenerator` now accept a function on its second argument which
475
- will be called on `refreshToken`, so you can fully customize the behavior. The
476
- function must return a Promise.
477
-
478
- ## [0.9.x]
479
-
480
- ### breaking changes
481
-
482
- - if the response status is 4xx or 5xx: an error is thrown
483
- [#13](https://github.com/mapado/rest-client-js-sdk/pull/13)
484
-
485
- ### new features
486
-
487
- - memoize token generation and refresh
488
- [#12](https://github.com/mapado/rest-client-js-sdk/pull/12)
489
-
490
- ## [0.6.x]
491
-
492
- ### breaking changes
493
-
494
- The `queryParam` argument was added where needed in the AbstractClient class:
495
-
496
- - `findAll(queryParam = {}, pathParameters = {})`
497
- - `create(entity, queryParam = {}, pathParameters = {})`
498
-
499
- ### new features
500
-
501
- The `queryParam` argument was added where needed in the AbstractClient class:
502
-
503
- - `update(entity, queryParam = {})`