rest-client-sdk 6.7.1 → 7.0.0-rc.10
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.es.js +1654 -1527
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +1657 -1529
- package/dist/index.js.map +1 -1
- package/dist/types/RestClientSdk.d.ts +7 -5
- package/dist/types/RestClientSdkInterface.d.ts +5 -2
- package/dist/types/TokenGenerator/AbstractTokenGenerator.d.ts +3 -1
- package/dist/types/TokenStorage.d.ts +3 -2
- package/dist/types/client/AbstractClient.d.ts +1 -1
- package/dist/types/client/headerUtils.d.ts +1 -0
- package/dist/types/index.d.ts +18 -17
- package/dist/types/serializer/JsSerializer.d.ts +1 -1
- package/dist/types/serializer/Serializer.d.ts +1 -1
- package/dist/types/utils/logging.d.ts +31 -0
- package/dist/types/utils/repositoryGenerator.d.ts +1 -1
- package/package.json +1 -1
- package/src/ErrorFactory.ts +1 -1
- package/src/RestClientSdk.ts +18 -7
- package/src/RestClientSdkInterface.ts +6 -2
- package/src/TokenGenerator/AbstractTokenGenerator.ts +7 -1
- package/src/TokenGenerator/AuthorizationCodeFlowTokenGenerator.ts +18 -6
- package/src/TokenGenerator/ClientCredentialsGenerator.ts +12 -6
- package/src/TokenGenerator/PasswordGenerator.ts +18 -6
- package/src/TokenStorage.ts +24 -15
- package/src/UnitOfWork.ts +2 -2
- package/src/client/AbstractClient.ts +29 -24
- package/src/client/headerUtils.ts +12 -0
- package/src/index.ts +21 -20
- package/src/serializer/JsSerializer.ts +1 -1
- package/src/serializer/Serializer.ts +1 -1
- package/src/utils/logging.ts +96 -0
- package/src/utils/repositoryGenerator.ts +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { diff } from 'deep-diff';
|
|
2
1
|
import URI from 'urijs';
|
|
2
|
+
import { diff } from 'deep-diff';
|
|
3
3
|
|
|
4
4
|
function _typeof(obj) {
|
|
5
5
|
"@babel/helpers - typeof";
|
|
@@ -17,42 +17,6 @@ function _typeof(obj) {
|
|
|
17
17
|
return _typeof(obj);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
21
|
-
try {
|
|
22
|
-
var info = gen[key](arg);
|
|
23
|
-
var value = info.value;
|
|
24
|
-
} catch (error) {
|
|
25
|
-
reject(error);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (info.done) {
|
|
30
|
-
resolve(value);
|
|
31
|
-
} else {
|
|
32
|
-
Promise.resolve(value).then(_next, _throw);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function _asyncToGenerator(fn) {
|
|
37
|
-
return function () {
|
|
38
|
-
var self = this,
|
|
39
|
-
args = arguments;
|
|
40
|
-
return new Promise(function (resolve, reject) {
|
|
41
|
-
var gen = fn.apply(self, args);
|
|
42
|
-
|
|
43
|
-
function _next(value) {
|
|
44
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function _throw(err) {
|
|
48
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
_next(undefined);
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
20
|
function _classCallCheck(instance, Constructor) {
|
|
57
21
|
if (!(instance instanceof Constructor)) {
|
|
58
22
|
throw new TypeError("Cannot call a class as a function");
|
|
@@ -402,451 +366,348 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
402
366
|
return value;
|
|
403
367
|
}
|
|
404
368
|
|
|
405
|
-
/* eslint-disable
|
|
406
|
-
var Serializer = /*#__PURE__*/function () {
|
|
407
|
-
function Serializer() {
|
|
408
|
-
_classCallCheck(this, Serializer);
|
|
409
|
-
}
|
|
369
|
+
/* eslint-disable max-classes-per-file, no-use-before-define */
|
|
410
370
|
|
|
411
|
-
|
|
412
|
-
|
|
371
|
+
/**
|
|
372
|
+
* It's a bit tricky to extends native errors
|
|
373
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
374
|
+
*/
|
|
375
|
+
var HttpError = /*#__PURE__*/function (_Error) {
|
|
376
|
+
_inherits(HttpError, _Error);
|
|
413
377
|
|
|
414
|
-
|
|
415
|
-
* convert an entity to a plain javascript object
|
|
416
|
-
* @param {object} entity - The entity to convert
|
|
417
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
418
|
-
* @return {object} the object to serialize
|
|
419
|
-
*/
|
|
420
|
-
value: function normalizeItem(entity, classMetadata) {
|
|
421
|
-
return entity;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* convert a plain javascript object to string
|
|
425
|
-
* @param {object} object - The object to convert to convert
|
|
426
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
427
|
-
* @return {string} the content of the request
|
|
428
|
-
*/
|
|
378
|
+
var _super = _createSuper(HttpError);
|
|
429
379
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
value: function encodeItem(object, classMetadata) {
|
|
433
|
-
throw new TypeError('`encodeItem` method must be implemented');
|
|
434
|
-
}
|
|
435
|
-
/**
|
|
436
|
-
* convert an entity to string that will be sent as the request content
|
|
437
|
-
* @param {any} entity - The entity to convert
|
|
438
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
439
|
-
* @return {string} the content of the request
|
|
440
|
-
*/
|
|
380
|
+
function HttpError(message, baseResponse) {
|
|
381
|
+
var _this;
|
|
441
382
|
|
|
442
|
-
|
|
443
|
-
key: "serializeItem",
|
|
444
|
-
value: function serializeItem(object, classMetadata) {
|
|
445
|
-
var noralizedData = this.normalizeItem(object, classMetadata);
|
|
446
|
-
return this.encodeItem(noralizedData, classMetadata);
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* convert a plain object to an entity
|
|
450
|
-
* @param {object} object - The plain javascript object
|
|
451
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
452
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
453
|
-
* @return {object} an entity
|
|
454
|
-
*/
|
|
383
|
+
_classCallCheck(this, HttpError);
|
|
455
384
|
|
|
456
|
-
|
|
457
|
-
key: "denormalizeItem",
|
|
458
|
-
value: function denormalizeItem(object, classMetadata, response) {
|
|
459
|
-
return object;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* convert a string containing an object to a plain javascript object
|
|
463
|
-
* @param {string} rawData - The string fetched from the response
|
|
464
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
465
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
466
|
-
* @return {object} the normalized object
|
|
467
|
-
*/
|
|
385
|
+
_this = _super.call(this, message || 'Http errort'); // Set the prototype explicitly.
|
|
468
386
|
|
|
469
|
-
|
|
470
|
-
key: "decodeItem",
|
|
471
|
-
value: function decodeItem(rawData, classMetadata, response) {
|
|
472
|
-
throw new TypeError('`decodeItem` method must be implemented');
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* convert a string containing an object to an entity
|
|
476
|
-
* @param {string} rawData - The string fetched from the response
|
|
477
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
478
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
479
|
-
* @return {object} the entity
|
|
480
|
-
*/
|
|
387
|
+
_defineProperty(_assertThisInitialized(_this), "baseResponse", void 0);
|
|
481
388
|
|
|
482
|
-
|
|
483
|
-
key: "deserializeItem",
|
|
484
|
-
value: function deserializeItem(rawData, classMetadata, response) {
|
|
485
|
-
var object = this.decodeItem(rawData, classMetadata, response);
|
|
486
|
-
return this.denormalizeItem(object, classMetadata, response);
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* convert a plain object list to an entity list
|
|
490
|
-
* @param {Iterable<object>} objectList - The plain javascript object list (or an iterable object)
|
|
491
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
492
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
493
|
-
* @return {Iterable<object>} a list of entities
|
|
494
|
-
*/
|
|
389
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), OauthError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
495
390
|
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
value: function denormalizeList(objectList, classMetadata, response) {
|
|
499
|
-
return objectList; // weird conversion as the declaration of list type is fuzzy
|
|
391
|
+
if (Error.captureStackTrace) {
|
|
392
|
+
Error.captureStackTrace(_assertThisInitialized(_this), OauthError);
|
|
500
393
|
}
|
|
501
|
-
/**
|
|
502
|
-
* convert a string containing a list of objects to a list of plain javascript objects
|
|
503
|
-
* @param {string} rawListData - The string fetched from the response
|
|
504
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
505
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
506
|
-
* @return {Iterable<object>} a list of normalized objects
|
|
507
|
-
*/
|
|
508
394
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
* @param {string} rawListData - The string fetched from the response
|
|
517
|
-
* @param {ClassMetadata} classMetadata - the class metadata
|
|
518
|
-
* @param {Record<string, unknown>} response - the HTTP response
|
|
519
|
-
* @return {Iterable<object>} a list of entities
|
|
520
|
-
*/
|
|
395
|
+
_this.name = 'HttpError';
|
|
396
|
+
_this.baseResponse = baseResponse;
|
|
397
|
+
return _this;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Get the JSON of the baseResponse directly, if possible
|
|
401
|
+
*/
|
|
521
402
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
return this.
|
|
403
|
+
|
|
404
|
+
_createClass(HttpError, [{
|
|
405
|
+
key: "responseJson",
|
|
406
|
+
value: function responseJson() {
|
|
407
|
+
return this.baseResponse.json();
|
|
527
408
|
}
|
|
528
409
|
}]);
|
|
529
410
|
|
|
530
|
-
return
|
|
531
|
-
}();
|
|
411
|
+
return HttpError;
|
|
412
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
532
413
|
|
|
533
|
-
var
|
|
534
|
-
_inherits(
|
|
414
|
+
var OauthError = /*#__PURE__*/function (_Error2) {
|
|
415
|
+
_inherits(OauthError, _Error2);
|
|
535
416
|
|
|
536
|
-
var
|
|
417
|
+
var _super2 = _createSuper(OauthError);
|
|
537
418
|
|
|
538
|
-
function
|
|
539
|
-
|
|
419
|
+
function OauthError(message, previousError) {
|
|
420
|
+
var _this2;
|
|
540
421
|
|
|
541
|
-
|
|
542
|
-
}
|
|
422
|
+
_classCallCheck(this, OauthError);
|
|
543
423
|
|
|
544
|
-
|
|
545
|
-
key: "encodeItem",
|
|
546
|
-
value: function encodeItem(object, classMetadata) {
|
|
547
|
-
return JSON.stringify(object);
|
|
548
|
-
}
|
|
549
|
-
}, {
|
|
550
|
-
key: "decodeItem",
|
|
551
|
-
value: function decodeItem(rawData, classMetadata, response) {
|
|
552
|
-
return JSON.parse(rawData);
|
|
553
|
-
}
|
|
554
|
-
}, {
|
|
555
|
-
key: "decodeList",
|
|
556
|
-
value: function decodeList(rawListData, classMetadata, response) {
|
|
557
|
-
return JSON.parse(rawListData);
|
|
558
|
-
}
|
|
559
|
-
}]);
|
|
424
|
+
_this2 = _super2.call(this, message || 'Oauth error'); // Set the prototype explicitly.
|
|
560
425
|
|
|
561
|
-
|
|
562
|
-
}(Serializer);
|
|
426
|
+
_defineProperty(_assertThisInitialized(_this2), "previousError", void 0);
|
|
563
427
|
|
|
564
|
-
|
|
565
|
-
// "fork" of https://github.com/facebook/immutable-js/blob/v4.0.0-rc.9/src/Predicates.js
|
|
566
|
-
// because tree-shaking does not seems to work as expected
|
|
428
|
+
Object.setPrototypeOf(_assertThisInitialized(_this2), OauthError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
567
429
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
* This source code is licensed under the MIT license found in the
|
|
572
|
-
* LICENSE file in the root directory of this source tree.
|
|
573
|
-
*/
|
|
574
|
-
var IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
|
|
575
|
-
var IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@';
|
|
576
|
-
function isImmutable(maybeImmutable) {
|
|
577
|
-
return isCollection(maybeImmutable) || isRecord(maybeImmutable);
|
|
578
|
-
}
|
|
579
|
-
function isCollection(maybeCollection) {
|
|
580
|
-
return !!(maybeCollection && maybeCollection[IS_ITERABLE_SENTINEL]);
|
|
581
|
-
}
|
|
582
|
-
function isRecord(maybeRecord) {
|
|
583
|
-
return !!(maybeRecord && maybeRecord[IS_RECORD_SENTINEL]);
|
|
584
|
-
}
|
|
430
|
+
if (Error.captureStackTrace) {
|
|
431
|
+
Error.captureStackTrace(_assertThisInitialized(_this2), OauthError);
|
|
432
|
+
}
|
|
585
433
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
var result = diff(left, right);
|
|
591
|
-
return result ? result.length > 0 : false;
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* get the id of an entity or return itself if string
|
|
595
|
-
*/
|
|
434
|
+
_this2.name = 'OauthError';
|
|
435
|
+
_this2.previousError = previousError;
|
|
436
|
+
return _this2;
|
|
437
|
+
}
|
|
596
438
|
|
|
439
|
+
return OauthError;
|
|
440
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
597
441
|
|
|
598
|
-
function
|
|
599
|
-
|
|
600
|
-
return stringOrEntity;
|
|
601
|
-
} // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
602
|
-
// @ts-ignore
|
|
442
|
+
var InvalidGrantError = /*#__PURE__*/function (_OauthError) {
|
|
443
|
+
_inherits(InvalidGrantError, _OauthError);
|
|
603
444
|
|
|
445
|
+
var _super3 = _createSuper(InvalidGrantError);
|
|
604
446
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
/**
|
|
608
|
-
* find old relation value from the new relation id
|
|
609
|
-
* if not found, returs the default serilazed model
|
|
610
|
-
*/
|
|
447
|
+
function InvalidGrantError(message, previousError) {
|
|
448
|
+
var _this3;
|
|
611
449
|
|
|
450
|
+
_classCallCheck(this, InvalidGrantError);
|
|
612
451
|
|
|
613
|
-
|
|
614
|
-
var idSerializedKey = classMetadata.getIdentifierAttribute().serializedKey; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
615
|
-
// @ts-ignore
|
|
616
|
-
|
|
617
|
-
var relationValueId = getEntityId(newRelationValue, idSerializedKey);
|
|
618
|
-
var foundValue = oldRelationValue && oldRelationValue.find(function (innerOldRelationValue) {
|
|
619
|
-
var oldRelationValueId = getEntityId(innerOldRelationValue, idSerializedKey);
|
|
620
|
-
return relationValueId === oldRelationValueId;
|
|
621
|
-
});
|
|
622
|
-
|
|
623
|
-
if (foundValue) {
|
|
624
|
-
return foundValue;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
return classMetadata.getDefaultSerializedModel();
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* add all identifier from one to many relation list
|
|
631
|
-
*/
|
|
452
|
+
_this3 = _super3.call(this, message || 'Invalid grant error', previousError); // Set the prototype explicitly.
|
|
632
453
|
|
|
454
|
+
_defineProperty(_assertThisInitialized(_this3), "previousError", void 0);
|
|
633
455
|
|
|
634
|
-
|
|
635
|
-
return newValue.map(function (value) {
|
|
636
|
-
if (Object.keys(value).includes(idSerializedKey)) {
|
|
637
|
-
return _defineProperty({}, idSerializedKey, value[idSerializedKey]);
|
|
638
|
-
}
|
|
456
|
+
Object.setPrototypeOf(_assertThisInitialized(_this3), InvalidGrantError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
639
457
|
|
|
640
|
-
if (
|
|
641
|
-
|
|
458
|
+
if (Error.captureStackTrace) {
|
|
459
|
+
Error.captureStackTrace(_assertThisInitialized(_this3), InvalidGrantError);
|
|
642
460
|
}
|
|
643
461
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
462
|
+
_this3.name = 'InvalidGrantError';
|
|
463
|
+
_this3.previousError = previousError;
|
|
464
|
+
return _this3;
|
|
465
|
+
}
|
|
647
466
|
|
|
648
|
-
|
|
467
|
+
return InvalidGrantError;
|
|
468
|
+
}(OauthError);
|
|
649
469
|
|
|
650
|
-
var
|
|
470
|
+
var InvalidScopeError = /*#__PURE__*/function (_OauthError2) {
|
|
471
|
+
_inherits(InvalidScopeError, _OauthError2);
|
|
651
472
|
|
|
652
|
-
var
|
|
653
|
-
function UnitOfWork(mapping) {
|
|
654
|
-
var enabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
473
|
+
var _super4 = _createSuper(InvalidScopeError);
|
|
655
474
|
|
|
656
|
-
|
|
475
|
+
function InvalidScopeError(message, previousError) {
|
|
476
|
+
var _this4;
|
|
657
477
|
|
|
658
|
-
|
|
478
|
+
_classCallCheck(this, InvalidScopeError);
|
|
659
479
|
|
|
660
|
-
|
|
661
|
-
writable: true,
|
|
662
|
-
value: void 0
|
|
663
|
-
});
|
|
480
|
+
_this4 = _super4.call(this, message || 'Invalid scope error', previousError); // Set the prototype explicitly.
|
|
664
481
|
|
|
665
|
-
|
|
666
|
-
writable: true,
|
|
667
|
-
value: void 0
|
|
668
|
-
});
|
|
482
|
+
_defineProperty(_assertThisInitialized(_this4), "previousError", void 0);
|
|
669
483
|
|
|
670
|
-
|
|
484
|
+
Object.setPrototypeOf(_assertThisInitialized(_this4), InvalidScopeError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
671
485
|
|
|
672
|
-
|
|
486
|
+
if (Error.captureStackTrace) {
|
|
487
|
+
Error.captureStackTrace(_assertThisInitialized(_this4), InvalidScopeError);
|
|
488
|
+
}
|
|
673
489
|
|
|
674
|
-
|
|
490
|
+
_this4.name = 'InvalidScopeError';
|
|
491
|
+
_this4.previousError = previousError;
|
|
492
|
+
return _this4;
|
|
675
493
|
}
|
|
676
494
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
value: function registerClean(id, entity) {
|
|
680
|
-
if (!_classPrivateFieldGet(this, _enabled)) {
|
|
681
|
-
return;
|
|
682
|
-
}
|
|
495
|
+
return InvalidScopeError;
|
|
496
|
+
}(OauthError); // 400
|
|
683
497
|
|
|
684
|
-
if (isImmutable(entity)) {
|
|
685
|
-
_classPrivateFieldGet(this, _storage)[id] = entity;
|
|
686
|
-
} else {
|
|
687
|
-
_classPrivateFieldGet(this, _storage)[id] = _objectSpread2({}, entity);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
}, {
|
|
691
|
-
key: "getDirtyEntity",
|
|
692
|
-
value: function getDirtyEntity(id) {
|
|
693
|
-
return _classPrivateFieldGet(this, _storage)[id];
|
|
694
|
-
}
|
|
695
|
-
}, {
|
|
696
|
-
key: "clear",
|
|
697
|
-
value: function clear(id) {
|
|
698
|
-
delete _classPrivateFieldGet(this, _storage)[id];
|
|
699
|
-
}
|
|
700
|
-
}, {
|
|
701
|
-
key: "getDirtyData",
|
|
702
|
-
value: function getDirtyData(newSerializedModel, oldSerializedModel, classMetadata) {
|
|
703
|
-
return this._getDirtyFields(newSerializedModel, oldSerializedModel, classMetadata);
|
|
704
|
-
}
|
|
705
|
-
}, {
|
|
706
|
-
key: "_getDirtyFieldsForAttribute",
|
|
707
|
-
value: function _getDirtyFieldsForAttribute(dirtyFieldsParam, key, attribute, oldValue, newValue) {
|
|
708
|
-
var dirtyFields = dirtyFieldsParam;
|
|
709
498
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
dirtyFields[key] = newValue;
|
|
713
|
-
}
|
|
714
|
-
} else if (oldValue !== newValue) {
|
|
715
|
-
dirtyFields[key] = newValue;
|
|
716
|
-
}
|
|
499
|
+
var BadRequestError = /*#__PURE__*/function (_HttpError) {
|
|
500
|
+
_inherits(BadRequestError, _HttpError);
|
|
717
501
|
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
}, {
|
|
721
|
-
key: "_getDirtyFieldsForManyToOne",
|
|
722
|
-
value: function _getDirtyFieldsForManyToOne(dirtyFieldsParam, key, oldValue, newValue, relationMetadata, idSerializedKey) {
|
|
723
|
-
var dirtyFields = dirtyFieldsParam;
|
|
502
|
+
var _super5 = _createSuper(BadRequestError);
|
|
724
503
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
dirtyFields[key] = null;
|
|
728
|
-
return dirtyFields;
|
|
729
|
-
}
|
|
504
|
+
function BadRequestError(message, baseResponse) {
|
|
505
|
+
var _this5;
|
|
730
506
|
|
|
731
|
-
|
|
732
|
-
dirtyFields[key] = newValue;
|
|
733
|
-
}
|
|
507
|
+
_classCallCheck(this, BadRequestError);
|
|
734
508
|
|
|
735
|
-
|
|
509
|
+
_this5 = _super5.call(this, message || 'Bad request', baseResponse); // Set the prototype explicitly.
|
|
736
510
|
|
|
737
|
-
|
|
738
|
-
recursiveDiff[idSerializedKey] = getEntityId(newValue, idSerializedKey);
|
|
739
|
-
dirtyFields[key] = recursiveDiff;
|
|
740
|
-
}
|
|
741
|
-
}
|
|
511
|
+
_defineProperty(_assertThisInitialized(_this5), "baseResponse", void 0);
|
|
742
512
|
|
|
743
|
-
|
|
513
|
+
Object.setPrototypeOf(_assertThisInitialized(_this5), BadRequestError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
514
|
+
|
|
515
|
+
if (Error.captureStackTrace) {
|
|
516
|
+
Error.captureStackTrace(_assertThisInitialized(_this5), BadRequestError);
|
|
744
517
|
}
|
|
745
|
-
}, {
|
|
746
|
-
key: "_getDirtyFieldsForOneToMany",
|
|
747
|
-
value: function _getDirtyFieldsForOneToMany(dirtyFieldsParam, key, idSerializedKey, relationMetadata, oldValue, newValue) {
|
|
748
|
-
var _this = this;
|
|
749
518
|
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
519
|
+
_this5.name = 'BadRequestError';
|
|
520
|
+
_this5.baseResponse = baseResponse;
|
|
521
|
+
return _this5;
|
|
522
|
+
}
|
|
753
523
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
}
|
|
524
|
+
return BadRequestError;
|
|
525
|
+
}(HttpError); // 401
|
|
757
526
|
|
|
758
|
-
if (newValue && newValue.length > 0) {
|
|
759
|
-
if (dirtyFields[key] === undefined) {
|
|
760
|
-
dirtyFields[key] = [];
|
|
761
|
-
}
|
|
762
527
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
var oldRelationValue = findOldRelation(newRelationValue, oldValue, relationMetadata);
|
|
528
|
+
var UnauthorizedError = /*#__PURE__*/function (_BadRequestError) {
|
|
529
|
+
_inherits(UnauthorizedError, _BadRequestError);
|
|
766
530
|
|
|
767
|
-
|
|
768
|
-
if (typeof newRelationValue === 'string' || typeof oldRelationValue === 'string') {
|
|
769
|
-
dirtyFields[key][relationAttributeName] = newRelationValue;
|
|
770
|
-
return;
|
|
771
|
-
}
|
|
531
|
+
var _super6 = _createSuper(UnauthorizedError);
|
|
772
532
|
|
|
773
|
-
|
|
533
|
+
function UnauthorizedError(message, baseResponse) {
|
|
534
|
+
var _this6;
|
|
774
535
|
|
|
775
|
-
|
|
776
|
-
var entityId = getEntityId(newRelationValue, idSerializedKey);
|
|
536
|
+
_classCallCheck(this, UnauthorizedError);
|
|
777
537
|
|
|
778
|
-
|
|
779
|
-
recursiveDiff[idSerializedKey] = entityId;
|
|
780
|
-
}
|
|
538
|
+
_this6 = _super6.call(this, message || 'Unauthorized', baseResponse); // Set the prototype explicitly.
|
|
781
539
|
|
|
782
|
-
|
|
783
|
-
Object.assign(dirtyFields[key][relationAttributeName], recursiveDiff);
|
|
784
|
-
} else {
|
|
785
|
-
dirtyFields[key][relationAttributeName] = recursiveDiff;
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
});
|
|
540
|
+
_defineProperty(_assertThisInitialized(_this6), "baseResponse", void 0);
|
|
790
541
|
|
|
791
|
-
|
|
792
|
-
// add identifier because one or more object linked has been updated
|
|
793
|
-
// see test: 'get dirty data many to one update item'
|
|
794
|
-
dirtyFields[key] = Object.assign(getIdentifierForList(newValue, idSerializedKey), dirtyFields[key]);
|
|
795
|
-
} else {
|
|
796
|
-
// no changes, no need to send the key with an empty array as value
|
|
797
|
-
delete dirtyFields[key];
|
|
798
|
-
}
|
|
799
|
-
}
|
|
542
|
+
Object.setPrototypeOf(_assertThisInitialized(_this6), UnauthorizedError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
800
543
|
|
|
801
|
-
|
|
544
|
+
if (Error.captureStackTrace) {
|
|
545
|
+
Error.captureStackTrace(_assertThisInitialized(_this6), UnauthorizedError);
|
|
802
546
|
}
|
|
803
|
-
}, {
|
|
804
|
-
key: "_getDirtyFields",
|
|
805
|
-
value: function _getDirtyFields(newSerializedModel, oldSerializedModel, classMetadata) {
|
|
806
|
-
var _this2 = this;
|
|
807
547
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
var oldValue = oldSerializedModel ? oldSerializedModel[key] : null;
|
|
813
|
-
var currentRelation = classMetadata.getRelation(attribute.serializedKey);
|
|
548
|
+
_this6.name = 'UnauthorizedError';
|
|
549
|
+
_this6.baseResponse = baseResponse;
|
|
550
|
+
return _this6;
|
|
551
|
+
}
|
|
814
552
|
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
} // not a relation
|
|
553
|
+
return UnauthorizedError;
|
|
554
|
+
}(BadRequestError); // 403
|
|
818
555
|
|
|
819
556
|
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
return;
|
|
823
|
-
}
|
|
557
|
+
var ForbiddenError = /*#__PURE__*/function (_BadRequestError2) {
|
|
558
|
+
_inherits(ForbiddenError, _BadRequestError2);
|
|
824
559
|
|
|
825
|
-
|
|
560
|
+
var _super7 = _createSuper(ForbiddenError);
|
|
826
561
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
}
|
|
562
|
+
function ForbiddenError(message, baseResponse) {
|
|
563
|
+
var _this7;
|
|
830
564
|
|
|
831
|
-
|
|
565
|
+
_classCallCheck(this, ForbiddenError);
|
|
832
566
|
|
|
833
|
-
|
|
834
|
-
dirtyFields = _this2._getDirtyFieldsForManyToOne(dirtyFields, key, oldValue, newValue, relationMetadata, idSerializedKey);
|
|
835
|
-
return;
|
|
836
|
-
} // *_TO_MANY relation
|
|
567
|
+
_this7 = _super7.call(this, message || 'Forbidden', baseResponse); // Set the prototype explicitly.
|
|
837
568
|
|
|
569
|
+
_defineProperty(_assertThisInitialized(_this7), "baseResponse", void 0);
|
|
838
570
|
|
|
839
|
-
|
|
840
|
-
});
|
|
841
|
-
return dirtyFields;
|
|
842
|
-
}
|
|
843
|
-
}]);
|
|
571
|
+
Object.setPrototypeOf(_assertThisInitialized(_this7), ForbiddenError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
844
572
|
|
|
845
|
-
|
|
846
|
-
|
|
573
|
+
if (Error.captureStackTrace) {
|
|
574
|
+
Error.captureStackTrace(_assertThisInitialized(_this7), ForbiddenError);
|
|
575
|
+
}
|
|
847
576
|
|
|
848
|
-
|
|
849
|
-
|
|
577
|
+
_this7.name = 'ForbiddenError';
|
|
578
|
+
_this7.baseResponse = baseResponse;
|
|
579
|
+
return _this7;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return ForbiddenError;
|
|
583
|
+
}(BadRequestError); // 404
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
var ResourceNotFoundError = /*#__PURE__*/function (_BadRequestError3) {
|
|
587
|
+
_inherits(ResourceNotFoundError, _BadRequestError3);
|
|
588
|
+
|
|
589
|
+
var _super8 = _createSuper(ResourceNotFoundError);
|
|
590
|
+
|
|
591
|
+
function ResourceNotFoundError(message, baseResponse) {
|
|
592
|
+
var _this8;
|
|
593
|
+
|
|
594
|
+
_classCallCheck(this, ResourceNotFoundError);
|
|
595
|
+
|
|
596
|
+
_this8 = _super8.call(this, message || 'Resource is not found', baseResponse); // Set the prototype explicitly.
|
|
597
|
+
|
|
598
|
+
_defineProperty(_assertThisInitialized(_this8), "baseResponse", void 0);
|
|
599
|
+
|
|
600
|
+
Object.setPrototypeOf(_assertThisInitialized(_this8), ResourceNotFoundError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
601
|
+
|
|
602
|
+
if (Error.captureStackTrace) {
|
|
603
|
+
Error.captureStackTrace(_assertThisInitialized(_this8), ResourceNotFoundError);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
_this8.name = 'ResourceNotFoundError';
|
|
607
|
+
_this8.baseResponse = baseResponse;
|
|
608
|
+
return _this8;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
return ResourceNotFoundError;
|
|
612
|
+
}(BadRequestError); // 409
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
var ConflictError = /*#__PURE__*/function (_BadRequestError4) {
|
|
616
|
+
_inherits(ConflictError, _BadRequestError4);
|
|
617
|
+
|
|
618
|
+
var _super9 = _createSuper(ConflictError);
|
|
619
|
+
|
|
620
|
+
function ConflictError(message, baseResponse) {
|
|
621
|
+
var _this9;
|
|
622
|
+
|
|
623
|
+
_classCallCheck(this, ConflictError);
|
|
624
|
+
|
|
625
|
+
_this9 = _super9.call(this, message || 'Conflict detected', baseResponse); // Set the prototype explicitly.
|
|
626
|
+
|
|
627
|
+
_defineProperty(_assertThisInitialized(_this9), "baseResponse", void 0);
|
|
628
|
+
|
|
629
|
+
Object.setPrototypeOf(_assertThisInitialized(_this9), ConflictError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
630
|
+
|
|
631
|
+
if (Error.captureStackTrace) {
|
|
632
|
+
Error.captureStackTrace(_assertThisInitialized(_this9), ConflictError);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
_this9.name = 'ConflictError';
|
|
636
|
+
_this9.baseResponse = baseResponse;
|
|
637
|
+
return _this9;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return ConflictError;
|
|
641
|
+
}(BadRequestError); // 500
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
var InternalServerError = /*#__PURE__*/function (_HttpError2) {
|
|
645
|
+
_inherits(InternalServerError, _HttpError2);
|
|
646
|
+
|
|
647
|
+
var _super10 = _createSuper(InternalServerError);
|
|
648
|
+
|
|
649
|
+
function InternalServerError(message, baseResponse) {
|
|
650
|
+
var _this10;
|
|
651
|
+
|
|
652
|
+
_classCallCheck(this, InternalServerError);
|
|
653
|
+
|
|
654
|
+
_this10 = _super10.call(this, message || 'Internal server error', baseResponse); // Set the prototype explicitly.
|
|
655
|
+
|
|
656
|
+
_defineProperty(_assertThisInitialized(_this10), "baseResponse", void 0);
|
|
657
|
+
|
|
658
|
+
Object.setPrototypeOf(_assertThisInitialized(_this10), InternalServerError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
659
|
+
|
|
660
|
+
if (Error.captureStackTrace) {
|
|
661
|
+
Error.captureStackTrace(_assertThisInitialized(_this10), InternalServerError);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
_this10.name = 'InternalServerError';
|
|
665
|
+
_this10.baseResponse = baseResponse;
|
|
666
|
+
return _this10;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
return InternalServerError;
|
|
670
|
+
}(HttpError);
|
|
671
|
+
/**
|
|
672
|
+
* @returns {HttpError}
|
|
673
|
+
*/
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
function getHttpErrorFromResponse(originalResponse) {
|
|
677
|
+
var response = originalResponse.clone();
|
|
678
|
+
|
|
679
|
+
switch (true) {
|
|
680
|
+
case response.status === 401:
|
|
681
|
+
return new UnauthorizedError(null, response);
|
|
682
|
+
|
|
683
|
+
case response.status === 403:
|
|
684
|
+
return new ForbiddenError(null, response);
|
|
685
|
+
|
|
686
|
+
case response.status === 404:
|
|
687
|
+
return new ResourceNotFoundError(null, response);
|
|
688
|
+
|
|
689
|
+
case response.status === 409:
|
|
690
|
+
return new ConflictError(null, response);
|
|
691
|
+
|
|
692
|
+
case response.status >= 400 && response.status < 500:
|
|
693
|
+
return new BadRequestError(null, response);
|
|
694
|
+
|
|
695
|
+
case response.status >= 500 && response.status < 600:
|
|
696
|
+
return new InternalServerError(null, response);
|
|
697
|
+
|
|
698
|
+
default:
|
|
699
|
+
return new HttpError(null, response);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function isOauthError(body) {
|
|
704
|
+
var _ref;
|
|
705
|
+
|
|
706
|
+
return typeof ((_ref = body) === null || _ref === void 0 ? void 0 : _ref.error) === 'string';
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
var DEFAULT_CONFIG = {
|
|
710
|
+
collectionKey: 'hydra:member'
|
|
850
711
|
};
|
|
851
712
|
|
|
852
713
|
var _config = new WeakMap();
|
|
@@ -980,6 +841,38 @@ var Mapping = /*#__PURE__*/function () {
|
|
|
980
841
|
return Mapping;
|
|
981
842
|
}();
|
|
982
843
|
|
|
844
|
+
var Attribute =
|
|
845
|
+
/**
|
|
846
|
+
* @param {string} serializedKey the key returned from your API
|
|
847
|
+
* @param {null|string} attributeName the name in your entity, default to the `serializedKey` attribute
|
|
848
|
+
* @param {string} type type of the attribute
|
|
849
|
+
* @param {boolean} isIdentifier is this attribute the entity identifier
|
|
850
|
+
*/
|
|
851
|
+
function Attribute(serializedKey) {
|
|
852
|
+
var attributeName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
853
|
+
var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'string';
|
|
854
|
+
var isIdentifier = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
855
|
+
|
|
856
|
+
_classCallCheck(this, Attribute);
|
|
857
|
+
|
|
858
|
+
_defineProperty(this, "serializedKey", void 0);
|
|
859
|
+
|
|
860
|
+
_defineProperty(this, "attributeName", void 0);
|
|
861
|
+
|
|
862
|
+
_defineProperty(this, "type", void 0);
|
|
863
|
+
|
|
864
|
+
_defineProperty(this, "isIdentifier", void 0);
|
|
865
|
+
|
|
866
|
+
if (!serializedKey) {
|
|
867
|
+
throw new TypeError('`serializedKey` must not be empty');
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
this.serializedKey = serializedKey;
|
|
871
|
+
this.attributeName = attributeName || this.serializedKey;
|
|
872
|
+
this.type = type;
|
|
873
|
+
this.isIdentifier = isIdentifier;
|
|
874
|
+
};
|
|
875
|
+
|
|
983
876
|
/* eslint-disable import/prefer-default-export */
|
|
984
877
|
// eslint-disable-next-line import/no-duplicates
|
|
985
878
|
// eslint-disable-next-line import/no-duplicates
|
|
@@ -989,1266 +882,1422 @@ function generateRepository(sdk, metadata) {
|
|
|
989
882
|
return new metadata.repositoryClass(sdk, metadata, isUnitOfWorkEnabled);
|
|
990
883
|
}
|
|
991
884
|
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
_classCallCheck(this, RestClientSdk);
|
|
885
|
+
/* eslint-disable consistent-return */
|
|
886
|
+
function removeAuthorization(headers) {
|
|
887
|
+
if (!headers) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
999
890
|
|
|
1000
|
-
|
|
891
|
+
if (headers instanceof Headers) {
|
|
892
|
+
headers["delete"]('Authorization');
|
|
893
|
+
return headers;
|
|
894
|
+
}
|
|
1001
895
|
|
|
1002
|
-
|
|
896
|
+
var filterAuthorization = function filterAuthorization(entries) {
|
|
897
|
+
return entries.filter(function (header) {
|
|
898
|
+
return header[0] !== 'Authorization';
|
|
899
|
+
});
|
|
900
|
+
};
|
|
1003
901
|
|
|
1004
|
-
|
|
902
|
+
if (Array.isArray(headers)) {
|
|
903
|
+
return filterAuthorization(headers);
|
|
904
|
+
}
|
|
1005
905
|
|
|
1006
|
-
|
|
906
|
+
return Object.fromEntries(filterAuthorization(Object.entries(headers)));
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* remove undefined key, usefull to remove Content-Type for example.
|
|
910
|
+
* Does not apply on "Headers" instance as values are string in there.
|
|
911
|
+
*/
|
|
1007
912
|
|
|
1008
|
-
|
|
913
|
+
function removeUndefinedHeaders(headers) {
|
|
914
|
+
if (headers instanceof Headers) {
|
|
915
|
+
return headers;
|
|
916
|
+
}
|
|
1009
917
|
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
918
|
+
var filterEntries = function filterEntries(entries) {
|
|
919
|
+
return entries.filter(function (header) {
|
|
920
|
+
return header[1] !== undefined;
|
|
1013
921
|
});
|
|
922
|
+
};
|
|
1014
923
|
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
throw new TypeError('mapping should be an instance of `Mapping`');
|
|
1019
|
-
}
|
|
924
|
+
if (Array.isArray(headers)) {
|
|
925
|
+
return filterEntries(headers);
|
|
926
|
+
}
|
|
1020
927
|
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
928
|
+
return Object.fromEntries(filterEntries(Object.entries(headers)));
|
|
929
|
+
}
|
|
930
|
+
function convertToRecord(headers) {
|
|
931
|
+
if (headers instanceof Headers) {
|
|
932
|
+
return Object.fromEntries(headers.entries());
|
|
933
|
+
}
|
|
1026
934
|
|
|
1027
|
-
|
|
935
|
+
if (Array.isArray(headers)) {
|
|
936
|
+
return Object.fromEntries(headers);
|
|
1028
937
|
}
|
|
1029
|
-
/**
|
|
1030
|
-
* get a repository by it's metadata "key" attribute
|
|
1031
|
-
*
|
|
1032
|
-
* @param {string} key the repository "key" (the first argument of `new ClassMetadata`)
|
|
1033
|
-
*/
|
|
1034
938
|
|
|
939
|
+
return headers;
|
|
940
|
+
}
|
|
1035
941
|
|
|
1036
|
-
|
|
1037
|
-
key: "getRepository",
|
|
1038
|
-
value: function getRepository(key) {
|
|
1039
|
-
if (!_classPrivateFieldGet(this, _repositoryList)[key]) {
|
|
1040
|
-
var metadata = this.mapping.getClassMetadataByKey(key);
|
|
942
|
+
var EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes
|
|
1041
943
|
|
|
1042
|
-
|
|
1043
|
-
throw new Error("Unable to get metadata for repository ".concat(key));
|
|
1044
|
-
} // eslint-disable-next-line new-cap
|
|
944
|
+
var _tokenStorage = new WeakMap();
|
|
1045
945
|
|
|
946
|
+
var _isUnitOfWorkEnabled = new WeakMap();
|
|
1046
947
|
|
|
1047
|
-
|
|
1048
|
-
|
|
948
|
+
var AbstractClient = /*#__PURE__*/function () {
|
|
949
|
+
function AbstractClient(sdk, metadata) {
|
|
950
|
+
var isUnitOfWorkEnabled = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
1049
951
|
|
|
952
|
+
_classCallCheck(this, AbstractClient);
|
|
1050
953
|
|
|
1051
|
-
|
|
1052
|
-
}
|
|
1053
|
-
}, {
|
|
1054
|
-
key: "_mergeWithBaseConfig",
|
|
1055
|
-
value: function _mergeWithBaseConfig(config) {
|
|
1056
|
-
var newConfig = config;
|
|
1057
|
-
newConfig.useDefaultParameters = config.useDefaultParameters === undefined ? true : config.useDefaultParameters;
|
|
1058
|
-
newConfig.authorizationType = config.authorizationType || 'Bearer';
|
|
1059
|
-
return newConfig;
|
|
1060
|
-
}
|
|
1061
|
-
}, {
|
|
1062
|
-
key: "checkConfigValidity",
|
|
1063
|
-
value: function checkConfigValidity(config) {
|
|
1064
|
-
if (!(config && config.path && config.scheme)) {
|
|
1065
|
-
throw new RangeError("SDK config is not valid, it should contain a \"path\", a \"scheme\" parameter,\n and may contain a \"port\" and a \"useDefaultParameters\" parameter");
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
}]);
|
|
1069
|
-
|
|
1070
|
-
return RestClientSdk;
|
|
1071
|
-
}();
|
|
954
|
+
_defineProperty(this, "sdk", void 0);
|
|
1072
955
|
|
|
1073
|
-
|
|
956
|
+
_tokenStorage.set(this, {
|
|
957
|
+
writable: true,
|
|
958
|
+
value: void 0
|
|
959
|
+
});
|
|
1074
960
|
|
|
1075
|
-
|
|
1076
|
-
* It's a bit tricky to extends native errors
|
|
1077
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
1078
|
-
*/
|
|
1079
|
-
var HttpError = /*#__PURE__*/function (_Error) {
|
|
1080
|
-
_inherits(HttpError, _Error);
|
|
961
|
+
_defineProperty(this, "serializer", void 0);
|
|
1081
962
|
|
|
1082
|
-
|
|
963
|
+
_defineProperty(this, "metadata", void 0);
|
|
1083
964
|
|
|
1084
|
-
|
|
1085
|
-
|
|
965
|
+
_isUnitOfWorkEnabled.set(this, {
|
|
966
|
+
writable: true,
|
|
967
|
+
value: void 0
|
|
968
|
+
});
|
|
1086
969
|
|
|
1087
|
-
|
|
970
|
+
this.sdk = sdk;
|
|
1088
971
|
|
|
1089
|
-
|
|
972
|
+
_classPrivateFieldSet(this, _tokenStorage, sdk.tokenStorage);
|
|
1090
973
|
|
|
1091
|
-
|
|
974
|
+
this.serializer = sdk.serializer;
|
|
975
|
+
this.metadata = metadata;
|
|
1092
976
|
|
|
1093
|
-
|
|
977
|
+
_classPrivateFieldSet(this, _isUnitOfWorkEnabled, isUnitOfWorkEnabled);
|
|
978
|
+
}
|
|
1094
979
|
|
|
1095
|
-
|
|
1096
|
-
|
|
980
|
+
_createClass(AbstractClient, [{
|
|
981
|
+
key: "withUnitOfWork",
|
|
982
|
+
value: function withUnitOfWork(enabled) {
|
|
983
|
+
// eslint-disable-next-line new-cap
|
|
984
|
+
return generateRepository(this.sdk, this.metadata, enabled);
|
|
1097
985
|
}
|
|
986
|
+
}, {
|
|
987
|
+
key: "getDefaultParameters",
|
|
988
|
+
value: function getDefaultParameters() {
|
|
989
|
+
return {};
|
|
990
|
+
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1098
991
|
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
992
|
+
}, {
|
|
993
|
+
key: "getPathBase",
|
|
994
|
+
value: function getPathBase(pathParameters) {
|
|
995
|
+
return "/".concat(this.metadata.pathRoot);
|
|
996
|
+
}
|
|
997
|
+
}, {
|
|
998
|
+
key: "getEntityURI",
|
|
999
|
+
value: function getEntityURI(entity) {
|
|
1000
|
+
var idValue = this._getEntityIdentifier(entity);
|
|
1107
1001
|
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
var _responseJson = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
1112
|
-
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
1113
|
-
while (1) {
|
|
1114
|
-
switch (_context.prev = _context.next) {
|
|
1115
|
-
case 0:
|
|
1116
|
-
return _context.abrupt("return", this.baseResponse.json());
|
|
1117
|
-
|
|
1118
|
-
case 1:
|
|
1119
|
-
case "end":
|
|
1120
|
-
return _context.stop();
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
}, _callee, this);
|
|
1124
|
-
}));
|
|
1002
|
+
if (idValue === null) {
|
|
1003
|
+
throw new Error('Cannot call `getEntityURI for entity without id.`');
|
|
1004
|
+
}
|
|
1125
1005
|
|
|
1126
|
-
|
|
1127
|
-
|
|
1006
|
+
if (typeof idValue === 'number') {
|
|
1007
|
+
if (Number.isFinite(idValue)) {
|
|
1008
|
+
idValue = idValue.toString();
|
|
1009
|
+
} else {
|
|
1010
|
+
throw new Error('Unable to handle non-finite number');
|
|
1011
|
+
}
|
|
1128
1012
|
}
|
|
1129
1013
|
|
|
1130
|
-
|
|
1131
|
-
}()
|
|
1132
|
-
}]);
|
|
1014
|
+
var pathBase = this.getPathBase({});
|
|
1133
1015
|
|
|
1134
|
-
|
|
1135
|
-
|
|
1016
|
+
if (idValue.indexOf(pathBase) > -1) {
|
|
1017
|
+
return idValue;
|
|
1018
|
+
}
|
|
1136
1019
|
|
|
1137
|
-
|
|
1138
|
-
|
|
1020
|
+
return "".concat(pathBase, "/").concat(idValue);
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* get an entity by its id
|
|
1024
|
+
*
|
|
1025
|
+
* @param {string|number} id the entity identifier
|
|
1026
|
+
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1027
|
+
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1028
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1029
|
+
*/
|
|
1139
1030
|
|
|
1140
|
-
|
|
1031
|
+
}, {
|
|
1032
|
+
key: "find",
|
|
1033
|
+
value: function find(id) {
|
|
1034
|
+
var queryParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1035
|
+
var pathParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1036
|
+
var requestParams = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
1141
1037
|
|
|
1142
|
-
|
|
1143
|
-
var _this2;
|
|
1038
|
+
var url = this._generateUrlFromParams(queryParam, pathParameters, id);
|
|
1144
1039
|
|
|
1145
|
-
|
|
1040
|
+
return this.deserializeResponse(this.authorizedFetch(url, requestParams), 'item');
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* get a list of entities by some parameters
|
|
1044
|
+
*
|
|
1045
|
+
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1046
|
+
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1047
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1048
|
+
*/
|
|
1146
1049
|
|
|
1147
|
-
|
|
1050
|
+
}, {
|
|
1051
|
+
key: "findBy",
|
|
1052
|
+
value: function findBy(queryParam) {
|
|
1053
|
+
var pathParameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1054
|
+
var requestParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1148
1055
|
|
|
1149
|
-
|
|
1056
|
+
var url = this._generateUrlFromParams(queryParam, pathParameters);
|
|
1150
1057
|
|
|
1151
|
-
|
|
1058
|
+
return this.deserializeResponse(this.authorizedFetch(url, requestParams), 'list');
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* get a list of all entities
|
|
1062
|
+
*
|
|
1063
|
+
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1064
|
+
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1065
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1066
|
+
*/
|
|
1152
1067
|
|
|
1153
|
-
|
|
1154
|
-
|
|
1068
|
+
}, {
|
|
1069
|
+
key: "findAll",
|
|
1070
|
+
value: function findAll() {
|
|
1071
|
+
var queryParam = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1072
|
+
var pathParameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1073
|
+
var requestParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1074
|
+
return this.findBy(queryParam, pathParameters, requestParams);
|
|
1155
1075
|
}
|
|
1076
|
+
/**
|
|
1077
|
+
* create an entity
|
|
1078
|
+
*
|
|
1079
|
+
* @param {Record<string, unknown>} entity the entity to persist
|
|
1080
|
+
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1081
|
+
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1082
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1083
|
+
*/
|
|
1156
1084
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1085
|
+
}, {
|
|
1086
|
+
key: "create",
|
|
1087
|
+
value: function create(entity) {
|
|
1088
|
+
var queryParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1089
|
+
var pathParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1090
|
+
var requestParams = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
1161
1091
|
|
|
1162
|
-
|
|
1163
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
1092
|
+
this._throwIfUnitOfWorkIsDisabled();
|
|
1164
1093
|
|
|
1165
|
-
var
|
|
1166
|
-
|
|
1094
|
+
var url = new URI(this.getPathBase(pathParameters));
|
|
1095
|
+
url.addSearch(queryParam);
|
|
1096
|
+
var oldSerializedModel = this.metadata.getDefaultSerializedModel();
|
|
1097
|
+
var newSerializedModel = this.serializer.normalizeItem(entity, this.metadata);
|
|
1098
|
+
var diff = this.sdk.unitOfWork.getDirtyData(newSerializedModel, oldSerializedModel, this.metadata);
|
|
1099
|
+
return this.deserializeResponse(this.authorizedFetch(url, _objectSpread2({
|
|
1100
|
+
method: 'POST',
|
|
1101
|
+
body: this.serializer.encodeItem(diff, this.metadata)
|
|
1102
|
+
}, requestParams)), 'item');
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* update an entity
|
|
1106
|
+
*
|
|
1107
|
+
* @param {Record<string, unknown>} entity the entity to update
|
|
1108
|
+
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1109
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1110
|
+
*/
|
|
1167
1111
|
|
|
1168
|
-
|
|
1112
|
+
}, {
|
|
1113
|
+
key: "update",
|
|
1114
|
+
value: function update(entity) {
|
|
1115
|
+
var queryParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1116
|
+
var requestParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1169
1117
|
|
|
1170
|
-
|
|
1171
|
-
var _this3;
|
|
1118
|
+
this._throwIfUnitOfWorkIsDisabled();
|
|
1172
1119
|
|
|
1173
|
-
|
|
1120
|
+
var url = new URI(this.getEntityURI(entity));
|
|
1121
|
+
url.addSearch(queryParam);
|
|
1122
|
+
var newSerializedModel = this.serializer.normalizeItem(entity, this.metadata);
|
|
1174
1123
|
|
|
1175
|
-
|
|
1124
|
+
var identifier = this._getEntityIdentifier(newSerializedModel);
|
|
1176
1125
|
|
|
1177
|
-
|
|
1126
|
+
var oldModel;
|
|
1178
1127
|
|
|
1179
|
-
|
|
1128
|
+
if (identifier !== null) {
|
|
1129
|
+
oldModel = this.sdk.unitOfWork.getDirtyEntity(identifier);
|
|
1130
|
+
}
|
|
1180
1131
|
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1132
|
+
if (oldModel) {
|
|
1133
|
+
newSerializedModel = this.sdk.unitOfWork.getDirtyData(newSerializedModel, oldModel, this.metadata);
|
|
1134
|
+
}
|
|
1184
1135
|
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1136
|
+
return this.deserializeResponse(this.authorizedFetch(url, _objectSpread2({
|
|
1137
|
+
method: 'PUT',
|
|
1138
|
+
body: this.serializer.encodeItem(newSerializedModel, this.metadata)
|
|
1139
|
+
}, requestParams)), 'item');
|
|
1140
|
+
}
|
|
1141
|
+
/**
|
|
1142
|
+
* delete an entity
|
|
1143
|
+
*
|
|
1144
|
+
* @param {Record<string, unknown>} the entity to delete
|
|
1145
|
+
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1146
|
+
*/
|
|
1189
1147
|
|
|
1190
|
-
|
|
1191
|
-
|
|
1148
|
+
}, {
|
|
1149
|
+
key: "delete",
|
|
1150
|
+
value: function _delete(entity) {
|
|
1151
|
+
var _this = this;
|
|
1192
1152
|
|
|
1193
|
-
var
|
|
1194
|
-
_inherits(InvalidScopeError, _OauthError2);
|
|
1153
|
+
var requestParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1195
1154
|
|
|
1196
|
-
|
|
1155
|
+
this._throwIfUnitOfWorkIsDisabled();
|
|
1197
1156
|
|
|
1198
|
-
|
|
1199
|
-
var _this4;
|
|
1157
|
+
var url = this.getEntityURI(entity);
|
|
1200
1158
|
|
|
1201
|
-
|
|
1159
|
+
var identifier = this._getEntityIdentifier(entity);
|
|
1202
1160
|
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1161
|
+
return this.authorizedFetch(url, _objectSpread2({
|
|
1162
|
+
method: 'DELETE'
|
|
1163
|
+
}, requestParams)).then(function (response) {
|
|
1164
|
+
if (identifier !== null) {
|
|
1165
|
+
_this.sdk.unitOfWork.clear(identifier);
|
|
1166
|
+
}
|
|
1208
1167
|
|
|
1209
|
-
|
|
1210
|
-
|
|
1168
|
+
return response;
|
|
1169
|
+
});
|
|
1211
1170
|
}
|
|
1171
|
+
}, {
|
|
1172
|
+
key: "deserializeResponse",
|
|
1173
|
+
value: function deserializeResponse(requestPromise, listOrItem) {
|
|
1174
|
+
var _this2 = this;
|
|
1212
1175
|
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
}
|
|
1176
|
+
return requestPromise.then(function (response) {
|
|
1177
|
+
return response.text().then(function (text) {
|
|
1178
|
+
return {
|
|
1179
|
+
response: response,
|
|
1180
|
+
text: text
|
|
1181
|
+
};
|
|
1182
|
+
});
|
|
1183
|
+
}).then(function (_ref) {
|
|
1184
|
+
var response = _ref.response,
|
|
1185
|
+
text = _ref.text;
|
|
1220
1186
|
|
|
1187
|
+
if (listOrItem === 'list') {
|
|
1188
|
+
// for list, we need to deserialize the result to get an object
|
|
1189
|
+
var itemList = _this2.serializer.deserializeList(text, _this2.metadata, response);
|
|
1221
1190
|
|
|
1222
|
-
|
|
1223
|
-
|
|
1191
|
+
if (_classPrivateFieldGet(_this2, _isUnitOfWorkEnabled)) {
|
|
1192
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
1193
|
+
var _iterator = _createForOfIteratorHelper(itemList),
|
|
1194
|
+
_step;
|
|
1224
1195
|
|
|
1225
|
-
|
|
1196
|
+
try {
|
|
1197
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1198
|
+
var _decodedItem = _step.value;
|
|
1226
1199
|
|
|
1227
|
-
|
|
1228
|
-
var _this5;
|
|
1200
|
+
var _identifier = _this2._getEntityIdentifier(_decodedItem);
|
|
1229
1201
|
|
|
1230
|
-
|
|
1202
|
+
var normalizedItem = _this2.serializer.normalizeItem(_decodedItem, _this2.metadata); // then we register the re-normalized item
|
|
1231
1203
|
|
|
1232
|
-
_this5 = _super5.call(this, message || 'Bad request', baseResponse); // Set the prototype explicitly.
|
|
1233
1204
|
|
|
1234
|
-
|
|
1205
|
+
if (_identifier !== null) {
|
|
1206
|
+
_this2.sdk.unitOfWork.registerClean(_identifier, normalizedItem);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
} catch (err) {
|
|
1210
|
+
_iterator.e(err);
|
|
1211
|
+
} finally {
|
|
1212
|
+
_iterator.f();
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1235
1215
|
|
|
1236
|
-
|
|
1216
|
+
return itemList;
|
|
1217
|
+
} // for items, we can just decode the item (ie. transform it to JS object)
|
|
1237
1218
|
|
|
1238
|
-
if (Error.captureStackTrace) {
|
|
1239
|
-
Error.captureStackTrace(_assertThisInitialized(_this5), BadRequestError);
|
|
1240
|
-
}
|
|
1241
1219
|
|
|
1242
|
-
|
|
1243
|
-
_this5.baseResponse = baseResponse;
|
|
1244
|
-
return _this5;
|
|
1245
|
-
}
|
|
1220
|
+
var decodedItem = _this2.serializer.decodeItem(text, _this2.metadata, response); // and register it directy without deserializing + renormalizing
|
|
1246
1221
|
|
|
1247
|
-
return BadRequestError;
|
|
1248
|
-
}(HttpError); // 401
|
|
1249
1222
|
|
|
1223
|
+
var identifier = _this2._getEntityIdentifier(decodedItem); // and finally return the denormalized item
|
|
1250
1224
|
|
|
1251
|
-
var UnauthorizedError = /*#__PURE__*/function (_BadRequestError) {
|
|
1252
|
-
_inherits(UnauthorizedError, _BadRequestError);
|
|
1253
1225
|
|
|
1254
|
-
|
|
1226
|
+
var item = _this2.serializer.denormalizeItem(decodedItem, _this2.metadata, response);
|
|
1255
1227
|
|
|
1256
|
-
|
|
1257
|
-
|
|
1228
|
+
if (_classPrivateFieldGet(_this2, _isUnitOfWorkEnabled) && identifier !== null) {
|
|
1229
|
+
_this2.sdk.unitOfWork.registerClean(identifier, _this2.serializer.normalizeItem(item, _this2.metadata));
|
|
1230
|
+
}
|
|
1258
1231
|
|
|
1259
|
-
|
|
1232
|
+
return item;
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
}, {
|
|
1236
|
+
key: "makeUri",
|
|
1237
|
+
value: function makeUri(input) {
|
|
1238
|
+
var url = input instanceof URI ? input : new URI(input);
|
|
1239
|
+
url.host(this.sdk.config.path).scheme(this.sdk.config.scheme);
|
|
1260
1240
|
|
|
1261
|
-
|
|
1241
|
+
if (this.sdk.config.port) {
|
|
1242
|
+
url.port(String(this.sdk.config.port));
|
|
1243
|
+
}
|
|
1262
1244
|
|
|
1263
|
-
|
|
1245
|
+
if (this.sdk.mapping.idPrefix) {
|
|
1246
|
+
var segments = url.segment();
|
|
1264
1247
|
|
|
1265
|
-
|
|
1248
|
+
if (!url.pathname().startsWith(this.sdk.mapping.idPrefix)) {
|
|
1249
|
+
segments.unshift(this.sdk.mapping.idPrefix);
|
|
1250
|
+
url.segment(segments);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1266
1253
|
|
|
1267
|
-
|
|
1268
|
-
|
|
1254
|
+
return url;
|
|
1255
|
+
}
|
|
1256
|
+
}, {
|
|
1257
|
+
key: "authorizedFetch",
|
|
1258
|
+
value: function authorizedFetch(input) {
|
|
1259
|
+
var requestParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1260
|
+
var url = this.makeUri(input);
|
|
1261
|
+
return this._fetchWithToken(url.toString(), requestParams);
|
|
1269
1262
|
}
|
|
1263
|
+
}, {
|
|
1264
|
+
key: "_generateUrlFromParams",
|
|
1265
|
+
value: function _generateUrlFromParams(queryParam) {
|
|
1266
|
+
var pathParameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1267
|
+
var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1268
|
+
var params = queryParam;
|
|
1270
1269
|
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
}
|
|
1270
|
+
if (this.sdk.config.useDefaultParameters) {
|
|
1271
|
+
Object.assign(params, this.getDefaultParameters());
|
|
1272
|
+
}
|
|
1275
1273
|
|
|
1276
|
-
|
|
1277
|
-
|
|
1274
|
+
var pathBase = this.getPathBase(pathParameters);
|
|
1275
|
+
var url = null;
|
|
1278
1276
|
|
|
1277
|
+
if (id) {
|
|
1278
|
+
var testPathBase = this.sdk.mapping.idPrefix ? "".concat(this.sdk.mapping.idPrefix).concat(pathBase) : pathBase;
|
|
1279
1279
|
|
|
1280
|
-
|
|
1281
|
-
|
|
1280
|
+
if (typeof id === 'string' && id.startsWith(testPathBase)) {
|
|
1281
|
+
url = new URI(id);
|
|
1282
|
+
} else {
|
|
1283
|
+
url = new URI("".concat(pathBase, "/").concat(id));
|
|
1284
|
+
}
|
|
1285
|
+
} else {
|
|
1286
|
+
url = new URI(pathBase);
|
|
1287
|
+
}
|
|
1282
1288
|
|
|
1283
|
-
|
|
1289
|
+
if (params) {
|
|
1290
|
+
url.addSearch(params);
|
|
1291
|
+
}
|
|
1284
1292
|
|
|
1285
|
-
|
|
1286
|
-
|
|
1293
|
+
return url;
|
|
1294
|
+
}
|
|
1295
|
+
}, {
|
|
1296
|
+
key: "_fetchWithToken",
|
|
1297
|
+
value: function _fetchWithToken(input) {
|
|
1298
|
+
var _this3 = this;
|
|
1287
1299
|
|
|
1288
|
-
|
|
1300
|
+
var requestParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1289
1301
|
|
|
1290
|
-
|
|
1302
|
+
if (!input) {
|
|
1303
|
+
throw new Error('input is empty');
|
|
1304
|
+
}
|
|
1291
1305
|
|
|
1292
|
-
|
|
1306
|
+
if (_classPrivateFieldGet(this, _tokenStorage)) {
|
|
1307
|
+
return _classPrivateFieldGet(this, _tokenStorage).getCurrentTokenExpiresIn().then(function (accessTokenExpiresIn) {
|
|
1308
|
+
if (accessTokenExpiresIn !== null && accessTokenExpiresIn <= EXPIRE_LIMIT_SECONDS) {
|
|
1309
|
+
return _classPrivateFieldGet(_this3, _tokenStorage).refreshToken().then(function (refreshedTokenObject) {
|
|
1310
|
+
return refreshedTokenObject.access_token;
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1293
1313
|
|
|
1294
|
-
|
|
1314
|
+
return _classPrivateFieldGet(_this3, _tokenStorage).getAccessToken();
|
|
1315
|
+
}).then(function (token) {
|
|
1316
|
+
return _this3._doFetch(token, input, requestParams);
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1295
1319
|
|
|
1296
|
-
|
|
1297
|
-
Error.captureStackTrace(_assertThisInitialized(_this7), ForbiddenError);
|
|
1320
|
+
return this._doFetch(null, input, requestParams);
|
|
1298
1321
|
}
|
|
1322
|
+
}, {
|
|
1323
|
+
key: "_refreshTokenAndRefetch",
|
|
1324
|
+
value: function _refreshTokenAndRefetch(input) {
|
|
1325
|
+
var _this4 = this;
|
|
1299
1326
|
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
return ForbiddenError;
|
|
1306
|
-
}(BadRequestError); // 404
|
|
1327
|
+
var requestParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1328
|
+
return _classPrivateFieldGet(this, _tokenStorage).refreshToken().then(function () {
|
|
1329
|
+
// eslint-disable-next-line prefer-const
|
|
1330
|
+
var headers = requestParams.headers,
|
|
1331
|
+
rest = _objectWithoutProperties(requestParams, ["headers"]);
|
|
1307
1332
|
|
|
1333
|
+
var updatedRequestParams = _objectSpread2(_objectSpread2({}, rest), {}, {
|
|
1334
|
+
headers: removeAuthorization(headers)
|
|
1335
|
+
});
|
|
1308
1336
|
|
|
1309
|
-
|
|
1310
|
-
|
|
1337
|
+
return _this4._fetchWithToken(input, updatedRequestParams);
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
}, {
|
|
1341
|
+
key: "_manageUnauthorized",
|
|
1342
|
+
value: function _manageUnauthorized(response, input) {
|
|
1343
|
+
var _this5 = this;
|
|
1311
1344
|
|
|
1312
|
-
|
|
1345
|
+
var requestParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1346
|
+
var error = getHttpErrorFromResponse(response); // https://tools.ietf.org/html/rfc2617#section-1.2
|
|
1313
1347
|
|
|
1314
|
-
|
|
1315
|
-
var _this8;
|
|
1348
|
+
var authorizationHeader = response.headers.get('www-authenticate');
|
|
1316
1349
|
|
|
1317
|
-
|
|
1350
|
+
if (authorizationHeader) {
|
|
1351
|
+
var invalidGrant = authorizationHeader.indexOf('error="invalid_grant"') > -1;
|
|
1318
1352
|
|
|
1319
|
-
|
|
1353
|
+
if (invalidGrant && _classPrivateFieldGet(this, _tokenStorage)) {
|
|
1354
|
+
return this._refreshTokenAndRefetch(input, requestParams);
|
|
1355
|
+
}
|
|
1320
1356
|
|
|
1321
|
-
|
|
1357
|
+
throw error;
|
|
1358
|
+
} else {
|
|
1359
|
+
// if www-authenticate header is missing, try and read json response
|
|
1360
|
+
return response.json().then(function (body) {
|
|
1361
|
+
if (_classPrivateFieldGet(_this5, _tokenStorage) && body.error === 'invalid_grant') {
|
|
1362
|
+
return _this5._refreshTokenAndRefetch(input, requestParams);
|
|
1363
|
+
}
|
|
1322
1364
|
|
|
1323
|
-
|
|
1365
|
+
throw error;
|
|
1366
|
+
})["catch"](function (err) {
|
|
1367
|
+
if (err instanceof OauthError) {
|
|
1368
|
+
throw err;
|
|
1369
|
+
}
|
|
1324
1370
|
|
|
1325
|
-
|
|
1326
|
-
|
|
1371
|
+
throw error;
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1327
1374
|
}
|
|
1375
|
+
}, {
|
|
1376
|
+
key: "_doFetch",
|
|
1377
|
+
value: function _doFetch(accessToken, input, requestParams) {
|
|
1378
|
+
var _this6 = this;
|
|
1328
1379
|
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1380
|
+
var params = requestParams;
|
|
1381
|
+
var baseHeaders = {
|
|
1382
|
+
'Content-Type': 'application/json'
|
|
1383
|
+
};
|
|
1333
1384
|
|
|
1334
|
-
|
|
1335
|
-
|
|
1385
|
+
if (accessToken) {
|
|
1386
|
+
baseHeaders.Authorization = "".concat(this.sdk.config.authorizationType, " ").concat(accessToken);
|
|
1387
|
+
}
|
|
1336
1388
|
|
|
1389
|
+
if (params) {
|
|
1390
|
+
if (!params.headers) {
|
|
1391
|
+
params.headers = {};
|
|
1392
|
+
}
|
|
1337
1393
|
|
|
1338
|
-
|
|
1339
|
-
|
|
1394
|
+
params.headers = Object.assign(baseHeaders, params.headers);
|
|
1395
|
+
} else {
|
|
1396
|
+
params = {
|
|
1397
|
+
headers: baseHeaders
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1340
1400
|
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
var _this9;
|
|
1345
|
-
|
|
1346
|
-
_classCallCheck(this, ConflictError);
|
|
1347
|
-
|
|
1348
|
-
_this9 = _super9.call(this, message || 'Conflict detected', baseResponse); // Set the prototype explicitly.
|
|
1349
|
-
|
|
1350
|
-
_defineProperty(_assertThisInitialized(_this9), "baseResponse", void 0);
|
|
1351
|
-
|
|
1352
|
-
Object.setPrototypeOf(_assertThisInitialized(_this9), ConflictError.prototype); // Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
1353
|
-
|
|
1354
|
-
if (Error.captureStackTrace) {
|
|
1355
|
-
Error.captureStackTrace(_assertThisInitialized(_this9), ConflictError);
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
_this9.name = 'ConflictError';
|
|
1359
|
-
_this9.baseResponse = baseResponse;
|
|
1360
|
-
return _this9;
|
|
1361
|
-
}
|
|
1362
|
-
|
|
1363
|
-
return ConflictError;
|
|
1364
|
-
}(BadRequestError); // 500
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
var InternalServerError = /*#__PURE__*/function (_HttpError2) {
|
|
1368
|
-
_inherits(InternalServerError, _HttpError2);
|
|
1401
|
+
if (params.headers) {
|
|
1402
|
+
params.headers = removeUndefinedHeaders(params.headers);
|
|
1403
|
+
}
|
|
1369
1404
|
|
|
1370
|
-
|
|
1405
|
+
var logId;
|
|
1371
1406
|
|
|
1372
|
-
|
|
1373
|
-
|
|
1407
|
+
if (this.sdk.logger) {
|
|
1408
|
+
logId = this.sdk.logger.logRequest(_objectSpread2({
|
|
1409
|
+
url: input
|
|
1410
|
+
}, params));
|
|
1411
|
+
} // eslint-disable-next-line consistent-return
|
|
1374
1412
|
|
|
1375
|
-
_classCallCheck(this, InternalServerError);
|
|
1376
1413
|
|
|
1377
|
-
|
|
1414
|
+
return fetch(input, params).then(function (response) {
|
|
1415
|
+
if (_this6.sdk.logger) {
|
|
1416
|
+
_this6.sdk.logger.logResponse(response, logId);
|
|
1417
|
+
}
|
|
1378
1418
|
|
|
1379
|
-
|
|
1419
|
+
if (response.status < 400) {
|
|
1420
|
+
return response;
|
|
1421
|
+
}
|
|
1380
1422
|
|
|
1381
|
-
|
|
1423
|
+
if (response.status === 401) {
|
|
1424
|
+
return _this6._manageUnauthorized(response, input, params);
|
|
1425
|
+
}
|
|
1382
1426
|
|
|
1383
|
-
|
|
1384
|
-
|
|
1427
|
+
var httpError = getHttpErrorFromResponse(response);
|
|
1428
|
+
throw httpError;
|
|
1429
|
+
});
|
|
1385
1430
|
}
|
|
1431
|
+
}, {
|
|
1432
|
+
key: "_getEntityIdentifier",
|
|
1433
|
+
value: function _getEntityIdentifier(object) {
|
|
1434
|
+
var idKey = this.metadata.getIdentifierAttribute().serializedKey; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
1435
|
+
// @ts-ignore
|
|
1386
1436
|
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
function getHttpErrorFromResponse(originalResponse) {
|
|
1400
|
-
var response = originalResponse.clone();
|
|
1401
|
-
|
|
1402
|
-
switch (true) {
|
|
1403
|
-
case response.status === 401:
|
|
1404
|
-
return new UnauthorizedError(null, response);
|
|
1405
|
-
|
|
1406
|
-
case response.status === 403:
|
|
1407
|
-
return new ForbiddenError(null, response);
|
|
1408
|
-
|
|
1409
|
-
case response.status === 404:
|
|
1410
|
-
return new ResourceNotFoundError(null, response);
|
|
1411
|
-
|
|
1412
|
-
case response.status === 409:
|
|
1413
|
-
return new ConflictError(null, response);
|
|
1414
|
-
|
|
1415
|
-
case response.status >= 400 && response.status < 500:
|
|
1416
|
-
return new BadRequestError(null, response);
|
|
1417
|
-
|
|
1418
|
-
case response.status >= 500 && response.status < 600:
|
|
1419
|
-
return new InternalServerError(null, response);
|
|
1437
|
+
return object[idKey];
|
|
1438
|
+
}
|
|
1439
|
+
}, {
|
|
1440
|
+
key: "_throwIfUnitOfWorkIsDisabled",
|
|
1441
|
+
value: function _throwIfUnitOfWorkIsDisabled() {
|
|
1442
|
+
if (!_classPrivateFieldGet(this, _isUnitOfWorkEnabled)) {
|
|
1443
|
+
throw new Error('UnitOfWork can be deactivated only on find* methods (for now). If you think this should be authorized, please report in https://git.io/JkYTO');
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
}]);
|
|
1420
1447
|
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1448
|
+
return AbstractClient;
|
|
1449
|
+
}();
|
|
1425
1450
|
|
|
1426
|
-
|
|
1427
|
-
var _ref;
|
|
1451
|
+
var _attributeList = new WeakMap();
|
|
1428
1452
|
|
|
1429
|
-
|
|
1430
|
-
}
|
|
1453
|
+
var _relationList = new WeakMap();
|
|
1431
1454
|
|
|
1432
|
-
|
|
1433
|
-
function removeAuthorization(headers) {
|
|
1434
|
-
if (!headers) {
|
|
1435
|
-
return;
|
|
1436
|
-
}
|
|
1455
|
+
var _identifierAttribute = new WeakMap();
|
|
1437
1456
|
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1457
|
+
var ClassMetadata = /*#__PURE__*/function () {
|
|
1458
|
+
/**
|
|
1459
|
+
* @param {string} key mandatory, will be passed in your serializer
|
|
1460
|
+
* @param {string|null} pathRoot the endpoint of your API: will be added to the mapping prefix ('/v1' here)
|
|
1461
|
+
* @param {typeof AbstractClient} repositoryClass [Overriding repository]{@link https://github.com/mapado/rest-client-js-sdk#overriding-repository} for more detail
|
|
1462
|
+
*/
|
|
1463
|
+
function ClassMetadata(key) {
|
|
1464
|
+
var pathRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
1465
|
+
var repositoryClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : AbstractClient;
|
|
1442
1466
|
|
|
1443
|
-
|
|
1444
|
-
return entries.filter(function (header) {
|
|
1445
|
-
return header[0] !== 'Authorization';
|
|
1446
|
-
});
|
|
1447
|
-
};
|
|
1467
|
+
_classCallCheck(this, ClassMetadata);
|
|
1448
1468
|
|
|
1449
|
-
|
|
1450
|
-
return filterAuthorization(headers);
|
|
1451
|
-
}
|
|
1469
|
+
_defineProperty(this, "key", void 0);
|
|
1452
1470
|
|
|
1453
|
-
|
|
1454
|
-
}
|
|
1455
|
-
/**
|
|
1456
|
-
* remove undefined key, usefull to remove Content-Type for example.
|
|
1457
|
-
* Does not apply on "Headers" instance as values are string in there.
|
|
1458
|
-
*/
|
|
1471
|
+
_defineProperty(this, "pathRoot", void 0);
|
|
1459
1472
|
|
|
1460
|
-
|
|
1461
|
-
if (headers instanceof Headers) {
|
|
1462
|
-
return headers;
|
|
1463
|
-
}
|
|
1473
|
+
_defineProperty(this, "repositoryClass", void 0);
|
|
1464
1474
|
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1475
|
+
_attributeList.set(this, {
|
|
1476
|
+
writable: true,
|
|
1477
|
+
value: void 0
|
|
1468
1478
|
});
|
|
1469
|
-
};
|
|
1470
1479
|
|
|
1471
|
-
|
|
1472
|
-
return filterEntries(headers);
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1475
|
-
return Object.fromEntries(filterEntries(Object.entries(headers)));
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
var EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes
|
|
1479
|
-
|
|
1480
|
-
var _tokenStorage = new WeakMap();
|
|
1481
|
-
|
|
1482
|
-
var _isUnitOfWorkEnabled = new WeakMap();
|
|
1483
|
-
|
|
1484
|
-
var AbstractClient = /*#__PURE__*/function () {
|
|
1485
|
-
function AbstractClient(sdk, metadata) {
|
|
1486
|
-
var isUnitOfWorkEnabled = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
1487
|
-
|
|
1488
|
-
_classCallCheck(this, AbstractClient);
|
|
1489
|
-
|
|
1490
|
-
_defineProperty(this, "sdk", void 0);
|
|
1491
|
-
|
|
1492
|
-
_tokenStorage.set(this, {
|
|
1480
|
+
_relationList.set(this, {
|
|
1493
1481
|
writable: true,
|
|
1494
1482
|
value: void 0
|
|
1495
1483
|
});
|
|
1496
1484
|
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
_defineProperty(this, "metadata", void 0);
|
|
1500
|
-
|
|
1501
|
-
_isUnitOfWorkEnabled.set(this, {
|
|
1485
|
+
_identifierAttribute.set(this, {
|
|
1502
1486
|
writable: true,
|
|
1503
1487
|
value: void 0
|
|
1504
1488
|
});
|
|
1505
1489
|
|
|
1506
|
-
|
|
1490
|
+
if (!key) {
|
|
1491
|
+
throw new TypeError('key attribute are required');
|
|
1492
|
+
}
|
|
1507
1493
|
|
|
1508
|
-
|
|
1494
|
+
this.key = key;
|
|
1495
|
+
this.pathRoot = pathRoot || key; // this.modelName = modelName;
|
|
1509
1496
|
|
|
1510
|
-
this.
|
|
1511
|
-
this.metadata = metadata;
|
|
1497
|
+
this.repositoryClass = repositoryClass;
|
|
1512
1498
|
|
|
1513
|
-
_classPrivateFieldSet(this,
|
|
1499
|
+
_classPrivateFieldSet(this, _attributeList, {});
|
|
1500
|
+
|
|
1501
|
+
_classPrivateFieldSet(this, _relationList, {});
|
|
1514
1502
|
}
|
|
1515
1503
|
|
|
1516
|
-
_createClass(
|
|
1517
|
-
key: "
|
|
1518
|
-
value: function
|
|
1519
|
-
|
|
1520
|
-
return generateRepository(this.sdk, this.metadata, enabled);
|
|
1504
|
+
_createClass(ClassMetadata, [{
|
|
1505
|
+
key: "getAttribute",
|
|
1506
|
+
value: function getAttribute(name) {
|
|
1507
|
+
return _classPrivateFieldGet(this, _attributeList)[name];
|
|
1521
1508
|
}
|
|
1522
1509
|
}, {
|
|
1523
|
-
key: "
|
|
1524
|
-
value: function
|
|
1525
|
-
return
|
|
1526
|
-
}
|
|
1510
|
+
key: "hasIdentifierAttribute",
|
|
1511
|
+
value: function hasIdentifierAttribute() {
|
|
1512
|
+
return !!_classPrivateFieldGet(this, _identifierAttribute);
|
|
1513
|
+
}
|
|
1514
|
+
}, {
|
|
1515
|
+
key: "getIdentifierAttribute",
|
|
1516
|
+
value: function getIdentifierAttribute() {
|
|
1517
|
+
if (!_classPrivateFieldGet(this, _identifierAttribute)) {
|
|
1518
|
+
throw new TypeError("\"".concat(this.key, "\" has no identifier attribute set. Did you call \"setAttributeList\" first ?"));
|
|
1519
|
+
}
|
|
1527
1520
|
|
|
1521
|
+
return _classPrivateFieldGet(this, _identifierAttribute);
|
|
1522
|
+
}
|
|
1528
1523
|
}, {
|
|
1529
|
-
key: "
|
|
1530
|
-
value: function
|
|
1531
|
-
return
|
|
1524
|
+
key: "getAttributeList",
|
|
1525
|
+
value: function getAttributeList() {
|
|
1526
|
+
return _classPrivateFieldGet(this, _attributeList);
|
|
1532
1527
|
}
|
|
1533
1528
|
}, {
|
|
1534
|
-
key: "
|
|
1535
|
-
value: function
|
|
1536
|
-
var
|
|
1529
|
+
key: "setAttributeList",
|
|
1530
|
+
value: function setAttributeList(attributeList) {
|
|
1531
|
+
var _this = this;
|
|
1537
1532
|
|
|
1538
|
-
|
|
1539
|
-
throw new Error('Cannot call `getEntityURI for entity without id.`');
|
|
1540
|
-
}
|
|
1533
|
+
_classPrivateFieldSet(this, _attributeList, {});
|
|
1541
1534
|
|
|
1542
|
-
|
|
1543
|
-
if (Number.isFinite(idValue)) {
|
|
1544
|
-
idValue = idValue.toString();
|
|
1545
|
-
} else {
|
|
1546
|
-
throw new Error('Unable to handle non-finite number');
|
|
1547
|
-
}
|
|
1548
|
-
}
|
|
1535
|
+
_classPrivateFieldSet(this, _identifierAttribute, undefined);
|
|
1549
1536
|
|
|
1550
|
-
|
|
1537
|
+
attributeList.forEach(function (attribute) {
|
|
1538
|
+
_classPrivateFieldGet(_this, _attributeList)[attribute.serializedKey] = attribute;
|
|
1551
1539
|
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1540
|
+
if (attribute.isIdentifier) {
|
|
1541
|
+
_classPrivateFieldSet(_this, _identifierAttribute, attribute);
|
|
1542
|
+
}
|
|
1543
|
+
});
|
|
1555
1544
|
|
|
1556
|
-
|
|
1545
|
+
if (!_classPrivateFieldGet(this, _identifierAttribute)) {
|
|
1546
|
+
throw new TypeError("\"".concat(this.key, "\" has no identifier attribute set. You must set all your attributes in one time and send an attribute with \"isIdentifier=true\""));
|
|
1547
|
+
}
|
|
1557
1548
|
}
|
|
1558
|
-
/**
|
|
1559
|
-
* get an entity by its id
|
|
1560
|
-
*
|
|
1561
|
-
* @param {string|number} id the entity identifier
|
|
1562
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1563
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1564
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1565
|
-
*/
|
|
1566
|
-
|
|
1567
1549
|
}, {
|
|
1568
|
-
key: "
|
|
1569
|
-
value: function
|
|
1570
|
-
var
|
|
1571
|
-
var pathParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1572
|
-
var requestParams = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
1550
|
+
key: "setRelationList",
|
|
1551
|
+
value: function setRelationList(relationList) {
|
|
1552
|
+
var _this2 = this;
|
|
1573
1553
|
|
|
1574
|
-
|
|
1554
|
+
_classPrivateFieldSet(this, _relationList, {});
|
|
1575
1555
|
|
|
1576
|
-
|
|
1556
|
+
relationList.forEach(function (relation) {
|
|
1557
|
+
_classPrivateFieldGet(_this2, _relationList)[relation.serializedKey] = relation;
|
|
1558
|
+
_classPrivateFieldGet(_this2, _attributeList)[relation.serializedKey] = new Attribute(relation.serializedKey, relation.serializedKey, relation.isRelationToMany() ? 'array' : 'object');
|
|
1559
|
+
});
|
|
1577
1560
|
}
|
|
1578
|
-
/**
|
|
1579
|
-
* get a list of entities by some parameters
|
|
1580
|
-
*
|
|
1581
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1582
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1583
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1584
|
-
*/
|
|
1585
|
-
|
|
1586
1561
|
}, {
|
|
1587
|
-
key: "
|
|
1588
|
-
value: function
|
|
1589
|
-
|
|
1590
|
-
var requestParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1591
|
-
|
|
1592
|
-
var url = this._generateUrlFromParams(queryParam, pathParameters);
|
|
1593
|
-
|
|
1594
|
-
return this.deserializeResponse(this.authorizedFetch(url, requestParams), 'list');
|
|
1562
|
+
key: "getRelation",
|
|
1563
|
+
value: function getRelation(key) {
|
|
1564
|
+
return _classPrivateFieldGet(this, _relationList)[key];
|
|
1595
1565
|
}
|
|
1596
|
-
/**
|
|
1597
|
-
* get a list of all entities
|
|
1598
|
-
*
|
|
1599
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1600
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1601
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1602
|
-
*/
|
|
1603
|
-
|
|
1604
1566
|
}, {
|
|
1605
|
-
key: "
|
|
1606
|
-
value: function
|
|
1607
|
-
var
|
|
1608
|
-
|
|
1609
|
-
var
|
|
1610
|
-
|
|
1567
|
+
key: "getDefaultSerializedModel",
|
|
1568
|
+
value: function getDefaultSerializedModel() {
|
|
1569
|
+
var _this3 = this;
|
|
1570
|
+
|
|
1571
|
+
var out = {};
|
|
1572
|
+
Object.keys(_classPrivateFieldGet(this, _attributeList)).forEach(function (serializedKey) {
|
|
1573
|
+
out[serializedKey] = null;
|
|
1574
|
+
});
|
|
1575
|
+
Object.keys(_classPrivateFieldGet(this, _relationList)).forEach(function (serializedKey) {
|
|
1576
|
+
var relation = _classPrivateFieldGet(_this3, _relationList)[serializedKey];
|
|
1577
|
+
|
|
1578
|
+
if (relation.isRelationToMany()) {
|
|
1579
|
+
out[serializedKey] = [];
|
|
1580
|
+
}
|
|
1581
|
+
});
|
|
1582
|
+
return out;
|
|
1611
1583
|
}
|
|
1612
|
-
|
|
1613
|
-
* create an entity
|
|
1614
|
-
*
|
|
1615
|
-
* @param {Record<string, unknown>} entity the entity to persist
|
|
1616
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1617
|
-
* @param {Record<string, unknown>} pathParameters path parameters, will be pass to the `getPathBase` method
|
|
1618
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1619
|
-
*/
|
|
1584
|
+
}]);
|
|
1620
1585
|
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
value: function create(entity) {
|
|
1624
|
-
var queryParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1625
|
-
var pathParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1626
|
-
var requestParams = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
1586
|
+
return ClassMetadata;
|
|
1587
|
+
}();
|
|
1627
1588
|
|
|
1628
|
-
|
|
1589
|
+
var RelationTypes;
|
|
1629
1590
|
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
method: 'POST',
|
|
1637
|
-
body: this.serializer.encodeItem(diff, this.metadata)
|
|
1638
|
-
}, requestParams)), 'item');
|
|
1639
|
-
}
|
|
1640
|
-
/**
|
|
1641
|
-
* update an entity
|
|
1642
|
-
*
|
|
1643
|
-
* @param {Record<string, unknown>} entity the entity to update
|
|
1644
|
-
* @param {Record<string, unknown>} queryParam query parameters that will be added to the request
|
|
1645
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1646
|
-
*/
|
|
1591
|
+
(function (RelationTypes) {
|
|
1592
|
+
RelationTypes["ONE_TO_ONE"] = "ONE_TO_ONE";
|
|
1593
|
+
RelationTypes["ONE_TO_MANY"] = "ONE_TO_MANY";
|
|
1594
|
+
RelationTypes["MANY_TO_MANY"] = "MANY_TO_MANY";
|
|
1595
|
+
RelationTypes["MANY_TO_ONE"] = "MANY_TO_ONE";
|
|
1596
|
+
})(RelationTypes || (RelationTypes = {}));
|
|
1647
1597
|
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1598
|
+
var Relation = /*#__PURE__*/function () {
|
|
1599
|
+
/**
|
|
1600
|
+
* @param {RelationTypes} type the type of relation
|
|
1601
|
+
* @param {string} targetMetadataKey must match the first argument of `ClassMetadata` constructor of the target entity
|
|
1602
|
+
* @param {string} serializedKey the key returned from your API
|
|
1603
|
+
* @param {string|null} attributeName the name in your entity, default to the `serializedKey` attribute
|
|
1604
|
+
*/
|
|
1605
|
+
function Relation(type, targetMetadataKey, serializedKey) {
|
|
1606
|
+
var attributeName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
|
|
1653
1607
|
|
|
1654
|
-
|
|
1608
|
+
_classCallCheck(this, Relation);
|
|
1655
1609
|
|
|
1656
|
-
|
|
1657
|
-
url.addSearch(queryParam);
|
|
1658
|
-
var newSerializedModel = this.serializer.normalizeItem(entity, this.metadata);
|
|
1610
|
+
_defineProperty(this, "type", void 0);
|
|
1659
1611
|
|
|
1660
|
-
|
|
1612
|
+
_defineProperty(this, "targetMetadataKey", void 0);
|
|
1661
1613
|
|
|
1662
|
-
|
|
1614
|
+
_defineProperty(this, "serializedKey", void 0);
|
|
1663
1615
|
|
|
1664
|
-
|
|
1665
|
-
oldModel = this.sdk.unitOfWork.getDirtyEntity(identifier);
|
|
1666
|
-
}
|
|
1616
|
+
_defineProperty(this, "attributeName", void 0);
|
|
1667
1617
|
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1618
|
+
this.type = type;
|
|
1619
|
+
this.targetMetadataKey = targetMetadataKey;
|
|
1620
|
+
this.serializedKey = serializedKey;
|
|
1621
|
+
this.attributeName = attributeName || this.serializedKey;
|
|
1622
|
+
}
|
|
1671
1623
|
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1624
|
+
_createClass(Relation, [{
|
|
1625
|
+
key: "isOneToOne",
|
|
1626
|
+
value: function isOneToOne() {
|
|
1627
|
+
return this.type === Relation.ONE_TO_ONE;
|
|
1676
1628
|
}
|
|
1677
|
-
/**
|
|
1678
|
-
* delete an entity
|
|
1679
|
-
*
|
|
1680
|
-
* @param {Record<string, unknown>} the entity to delete
|
|
1681
|
-
* @param {Record<string, unknown>} requestParams parameters that will be send as second parameter to the `fetch` call
|
|
1682
|
-
*/
|
|
1683
|
-
|
|
1684
1629
|
}, {
|
|
1685
|
-
key: "
|
|
1686
|
-
value: function
|
|
1687
|
-
|
|
1630
|
+
key: "isOneToMany",
|
|
1631
|
+
value: function isOneToMany() {
|
|
1632
|
+
return this.type === Relation.ONE_TO_MANY;
|
|
1633
|
+
}
|
|
1634
|
+
}, {
|
|
1635
|
+
key: "isManyToOne",
|
|
1636
|
+
value: function isManyToOne() {
|
|
1637
|
+
return this.type === Relation.MANY_TO_ONE;
|
|
1638
|
+
}
|
|
1639
|
+
}, {
|
|
1640
|
+
key: "isManyToMany",
|
|
1641
|
+
value: function isManyToMany() {
|
|
1642
|
+
return this.type === Relation.MANY_TO_MANY;
|
|
1643
|
+
}
|
|
1644
|
+
}, {
|
|
1645
|
+
key: "isRelationToMany",
|
|
1646
|
+
value: function isRelationToMany() {
|
|
1647
|
+
return this.isManyToMany() || this.isOneToMany();
|
|
1648
|
+
}
|
|
1649
|
+
}, {
|
|
1650
|
+
key: "isRelationToOne",
|
|
1651
|
+
value: function isRelationToOne() {
|
|
1652
|
+
return this.isManyToOne() || this.isOneToOne();
|
|
1653
|
+
}
|
|
1654
|
+
}]);
|
|
1688
1655
|
|
|
1689
|
-
|
|
1656
|
+
return Relation;
|
|
1657
|
+
}();
|
|
1690
1658
|
|
|
1691
|
-
|
|
1659
|
+
_defineProperty(Relation, "ONE_TO_ONE", RelationTypes.ONE_TO_ONE);
|
|
1692
1660
|
|
|
1693
|
-
|
|
1661
|
+
_defineProperty(Relation, "ONE_TO_MANY", RelationTypes.ONE_TO_MANY);
|
|
1694
1662
|
|
|
1695
|
-
|
|
1663
|
+
_defineProperty(Relation, "MANY_TO_ONE", RelationTypes.MANY_TO_ONE);
|
|
1696
1664
|
|
|
1697
|
-
|
|
1698
|
-
method: 'DELETE'
|
|
1699
|
-
}, requestParams)).then(function (response) {
|
|
1700
|
-
if (identifier !== null) {
|
|
1701
|
-
_this.sdk.unitOfWork.clear(identifier);
|
|
1702
|
-
}
|
|
1665
|
+
_defineProperty(Relation, "MANY_TO_MANY", RelationTypes.MANY_TO_MANY);
|
|
1703
1666
|
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
}, {
|
|
1708
|
-
key: "deserializeResponse",
|
|
1709
|
-
value: function deserializeResponse(requestPromise, listOrItem) {
|
|
1710
|
-
var _this2 = this;
|
|
1667
|
+
/* eslint-disable */
|
|
1668
|
+
// "fork" of https://github.com/facebook/immutable-js/blob/v4.0.0-rc.9/src/Predicates.js
|
|
1669
|
+
// because tree-shaking does not seems to work as expected
|
|
1711
1670
|
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1671
|
+
/**
|
|
1672
|
+
* Copyright (c) 2014-present, Facebook, Inc.
|
|
1673
|
+
*
|
|
1674
|
+
* This source code is licensed under the MIT license found in the
|
|
1675
|
+
* LICENSE file in the root directory of this source tree.
|
|
1676
|
+
*/
|
|
1677
|
+
var IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
|
|
1678
|
+
var IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@';
|
|
1679
|
+
function isImmutable(maybeImmutable) {
|
|
1680
|
+
return isCollection(maybeImmutable) || isRecord(maybeImmutable);
|
|
1681
|
+
}
|
|
1682
|
+
function isCollection(maybeCollection) {
|
|
1683
|
+
return !!(maybeCollection && maybeCollection[IS_ITERABLE_SENTINEL]);
|
|
1684
|
+
}
|
|
1685
|
+
function isRecord(maybeRecord) {
|
|
1686
|
+
return !!(maybeRecord && maybeRecord[IS_RECORD_SENTINEL]);
|
|
1687
|
+
}
|
|
1722
1688
|
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1689
|
+
/**
|
|
1690
|
+
* deep comparaison between objects
|
|
1691
|
+
*/
|
|
1692
|
+
function objectDiffers(left, right) {
|
|
1693
|
+
var result = diff(left, right);
|
|
1694
|
+
return result ? result.length > 0 : false;
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* get the id of an entity or return itself if string
|
|
1698
|
+
*/
|
|
1726
1699
|
|
|
1727
1700
|
|
|
1728
|
-
|
|
1729
|
-
|
|
1701
|
+
function getEntityId(stringOrEntity, idSerializedKey) {
|
|
1702
|
+
if (_typeof(stringOrEntity) !== 'object') {
|
|
1703
|
+
return stringOrEntity;
|
|
1704
|
+
} // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
1705
|
+
// @ts-ignore
|
|
1730
1706
|
|
|
1731
|
-
try {
|
|
1732
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1733
|
-
var _decodedItem = _step.value;
|
|
1734
1707
|
|
|
1735
|
-
|
|
1708
|
+
return stringOrEntity[idSerializedKey];
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* find old relation value from the new relation id
|
|
1712
|
+
* if not found, returs the default serilazed model
|
|
1713
|
+
*/
|
|
1736
1714
|
|
|
1737
|
-
var normalizedItem = _this2.serializer.normalizeItem(_decodedItem, _this2.metadata); // then we register the re-normalized item
|
|
1738
1715
|
|
|
1716
|
+
function findOldRelation(newRelationValue, oldRelationValue, classMetadata) {
|
|
1717
|
+
var idSerializedKey = classMetadata.getIdentifierAttribute().serializedKey; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
1718
|
+
// @ts-ignore
|
|
1739
1719
|
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
_iterator.e(err);
|
|
1746
|
-
} finally {
|
|
1747
|
-
_iterator.f();
|
|
1748
|
-
}
|
|
1720
|
+
var relationValueId = getEntityId(newRelationValue, idSerializedKey);
|
|
1721
|
+
var foundValue = oldRelationValue && oldRelationValue.find(function (innerOldRelationValue) {
|
|
1722
|
+
var oldRelationValueId = getEntityId(innerOldRelationValue, idSerializedKey);
|
|
1723
|
+
return relationValueId === oldRelationValueId;
|
|
1724
|
+
});
|
|
1749
1725
|
|
|
1750
|
-
|
|
1751
|
-
|
|
1726
|
+
if (foundValue) {
|
|
1727
|
+
return foundValue;
|
|
1728
|
+
}
|
|
1752
1729
|
|
|
1730
|
+
return classMetadata.getDefaultSerializedModel();
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* add all identifier from one to many relation list
|
|
1734
|
+
*/
|
|
1753
1735
|
|
|
1754
|
-
var decodedItem = _this2.serializer.decodeItem(text, _this2.metadata, response); // and register it directy without deserializing + renormalizing
|
|
1755
1736
|
|
|
1737
|
+
function getIdentifierForList(newValue, idSerializedKey) {
|
|
1738
|
+
return newValue.map(function (value) {
|
|
1739
|
+
if (Object.keys(value).includes(idSerializedKey)) {
|
|
1740
|
+
return _defineProperty({}, idSerializedKey, value[idSerializedKey]);
|
|
1741
|
+
}
|
|
1756
1742
|
|
|
1757
|
-
|
|
1743
|
+
if (typeof value === 'string') {
|
|
1744
|
+
return value;
|
|
1745
|
+
}
|
|
1758
1746
|
|
|
1747
|
+
throw new TypeError('new value should include a list of string or objects containing the serialized key');
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1759
1750
|
|
|
1760
|
-
|
|
1751
|
+
var _storage = new WeakMap();
|
|
1761
1752
|
|
|
1762
|
-
|
|
1763
|
-
_this2.sdk.unitOfWork.registerClean(identifier, _this2.serializer.normalizeItem(item, _this2.metadata));
|
|
1764
|
-
}
|
|
1753
|
+
var _enabled = new WeakMap();
|
|
1765
1754
|
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
}, {
|
|
1770
|
-
key: "makeUri",
|
|
1771
|
-
value: function makeUri(input) {
|
|
1772
|
-
var url = input instanceof URI ? input : new URI(input);
|
|
1773
|
-
url.host(this.sdk.config.path).scheme(this.sdk.config.scheme);
|
|
1755
|
+
var UnitOfWork = /*#__PURE__*/function () {
|
|
1756
|
+
function UnitOfWork(mapping) {
|
|
1757
|
+
var enabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
1774
1758
|
|
|
1775
|
-
|
|
1776
|
-
url.port(String(this.sdk.config.port));
|
|
1777
|
-
}
|
|
1759
|
+
_classCallCheck(this, UnitOfWork);
|
|
1778
1760
|
|
|
1779
|
-
|
|
1780
|
-
var segments = url.segment();
|
|
1761
|
+
_defineProperty(this, "mapping", void 0);
|
|
1781
1762
|
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1763
|
+
_storage.set(this, {
|
|
1764
|
+
writable: true,
|
|
1765
|
+
value: void 0
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
_enabled.set(this, {
|
|
1769
|
+
writable: true,
|
|
1770
|
+
value: void 0
|
|
1771
|
+
});
|
|
1772
|
+
|
|
1773
|
+
this.mapping = mapping;
|
|
1774
|
+
|
|
1775
|
+
_classPrivateFieldSet(this, _enabled, enabled);
|
|
1776
|
+
|
|
1777
|
+
_classPrivateFieldSet(this, _storage, {});
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
_createClass(UnitOfWork, [{
|
|
1781
|
+
key: "registerClean",
|
|
1782
|
+
value: function registerClean(id, entity) {
|
|
1783
|
+
if (!_classPrivateFieldGet(this, _enabled)) {
|
|
1784
|
+
return;
|
|
1786
1785
|
}
|
|
1787
1786
|
|
|
1788
|
-
|
|
1787
|
+
if (isImmutable(entity)) {
|
|
1788
|
+
_classPrivateFieldGet(this, _storage)[id] = entity;
|
|
1789
|
+
} else {
|
|
1790
|
+
_classPrivateFieldGet(this, _storage)[id] = _objectSpread2({}, entity);
|
|
1791
|
+
}
|
|
1789
1792
|
}
|
|
1790
1793
|
}, {
|
|
1791
|
-
key: "
|
|
1792
|
-
value: function
|
|
1793
|
-
|
|
1794
|
-
var url = this.makeUri(input);
|
|
1795
|
-
return this._fetchWithToken(url.toString(), requestParams);
|
|
1794
|
+
key: "getDirtyEntity",
|
|
1795
|
+
value: function getDirtyEntity(id) {
|
|
1796
|
+
return _classPrivateFieldGet(this, _storage)[id];
|
|
1796
1797
|
}
|
|
1797
1798
|
}, {
|
|
1798
|
-
key: "
|
|
1799
|
-
value: function
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1799
|
+
key: "clear",
|
|
1800
|
+
value: function clear(id) {
|
|
1801
|
+
delete _classPrivateFieldGet(this, _storage)[id];
|
|
1802
|
+
}
|
|
1803
|
+
}, {
|
|
1804
|
+
key: "getDirtyData",
|
|
1805
|
+
value: function getDirtyData(newSerializedModel, oldSerializedModel, classMetadata) {
|
|
1806
|
+
return this._getDirtyFields(newSerializedModel, oldSerializedModel, classMetadata);
|
|
1807
|
+
}
|
|
1808
|
+
}, {
|
|
1809
|
+
key: "_getDirtyFieldsForAttribute",
|
|
1810
|
+
value: function _getDirtyFieldsForAttribute(dirtyFieldsParam, key, attribute, oldValue, newValue) {
|
|
1811
|
+
var dirtyFields = dirtyFieldsParam;
|
|
1803
1812
|
|
|
1804
|
-
if (
|
|
1805
|
-
|
|
1813
|
+
if (attribute.type === 'object') {
|
|
1814
|
+
if (oldValue === undefined || objectDiffers(oldValue, newValue)) {
|
|
1815
|
+
dirtyFields[key] = newValue;
|
|
1816
|
+
}
|
|
1817
|
+
} else if (oldValue !== newValue) {
|
|
1818
|
+
dirtyFields[key] = newValue;
|
|
1806
1819
|
}
|
|
1807
1820
|
|
|
1808
|
-
|
|
1809
|
-
|
|
1821
|
+
return dirtyFields;
|
|
1822
|
+
}
|
|
1823
|
+
}, {
|
|
1824
|
+
key: "_getDirtyFieldsForManyToOne",
|
|
1825
|
+
value: function _getDirtyFieldsForManyToOne(dirtyFieldsParam, key, oldValue, newValue, relationMetadata, idSerializedKey) {
|
|
1826
|
+
var dirtyFields = dirtyFieldsParam;
|
|
1810
1827
|
|
|
1811
|
-
if (
|
|
1812
|
-
|
|
1828
|
+
if (oldValue !== newValue) {
|
|
1829
|
+
if (newValue === null) {
|
|
1830
|
+
dirtyFields[key] = null;
|
|
1831
|
+
return dirtyFields;
|
|
1832
|
+
}
|
|
1813
1833
|
|
|
1814
|
-
if (typeof
|
|
1815
|
-
|
|
1816
|
-
} else {
|
|
1817
|
-
url = new URI("".concat(pathBase, "/").concat(id));
|
|
1834
|
+
if (typeof oldValue === 'string' || typeof newValue === 'string') {
|
|
1835
|
+
dirtyFields[key] = newValue;
|
|
1818
1836
|
}
|
|
1819
|
-
} else {
|
|
1820
|
-
url = new URI(pathBase);
|
|
1821
|
-
}
|
|
1822
1837
|
|
|
1823
|
-
|
|
1824
|
-
|
|
1838
|
+
var recursiveDiff = this._getDirtyFields(newValue, oldValue, relationMetadata);
|
|
1839
|
+
|
|
1840
|
+
if (Object.keys(recursiveDiff).length > 0) {
|
|
1841
|
+
recursiveDiff[idSerializedKey] = getEntityId(newValue, idSerializedKey);
|
|
1842
|
+
dirtyFields[key] = recursiveDiff;
|
|
1843
|
+
}
|
|
1825
1844
|
}
|
|
1826
1845
|
|
|
1827
|
-
return
|
|
1846
|
+
return dirtyFields;
|
|
1828
1847
|
}
|
|
1829
1848
|
}, {
|
|
1830
|
-
key: "
|
|
1831
|
-
value: function
|
|
1832
|
-
var
|
|
1849
|
+
key: "_getDirtyFieldsForOneToMany",
|
|
1850
|
+
value: function _getDirtyFieldsForOneToMany(dirtyFieldsParam, key, idSerializedKey, relationMetadata, oldValue, newValue) {
|
|
1851
|
+
var _this = this;
|
|
1833
1852
|
|
|
1834
|
-
var
|
|
1853
|
+
var dirtyFields = dirtyFieldsParam;
|
|
1854
|
+
var newValueLength = newValue ? newValue.length : 0;
|
|
1855
|
+
var oldValueLength = oldValue ? oldValue.length : 0; // number of items changed
|
|
1835
1856
|
|
|
1836
|
-
if (
|
|
1837
|
-
|
|
1857
|
+
if (newValueLength !== oldValueLength) {
|
|
1858
|
+
dirtyFields[key] = getIdentifierForList(newValue, idSerializedKey);
|
|
1838
1859
|
}
|
|
1839
1860
|
|
|
1840
|
-
if (
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
return refreshedTokenObject.access_token;
|
|
1845
|
-
});
|
|
1846
|
-
}
|
|
1861
|
+
if (newValue && newValue.length > 0) {
|
|
1862
|
+
if (dirtyFields[key] === undefined) {
|
|
1863
|
+
dirtyFields[key] = [];
|
|
1864
|
+
}
|
|
1847
1865
|
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
});
|
|
1852
|
-
}
|
|
1866
|
+
var relationList = newValue;
|
|
1867
|
+
relationList.forEach(function (newRelationValue, relationAttributeName) {
|
|
1868
|
+
var oldRelationValue = findOldRelation(newRelationValue, oldValue, relationMetadata);
|
|
1853
1869
|
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
var _this4 = this;
|
|
1870
|
+
if (newRelationValue !== oldRelationValue) {
|
|
1871
|
+
if (typeof newRelationValue === 'string' || typeof oldRelationValue === 'string') {
|
|
1872
|
+
dirtyFields[key][relationAttributeName] = newRelationValue;
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1860
1875
|
|
|
1861
|
-
|
|
1862
|
-
return _classPrivateFieldGet(this, _tokenStorage).refreshToken().then(function () {
|
|
1863
|
-
// eslint-disable-next-line prefer-const
|
|
1864
|
-
var headers = requestParams.headers,
|
|
1865
|
-
rest = _objectWithoutProperties(requestParams, ["headers"]);
|
|
1876
|
+
var recursiveDiff = _this._getDirtyFields(newRelationValue, oldRelationValue, relationMetadata);
|
|
1866
1877
|
|
|
1867
|
-
|
|
1868
|
-
|
|
1878
|
+
if (Object.keys(recursiveDiff).length > 0) {
|
|
1879
|
+
var entityId = getEntityId(newRelationValue, idSerializedKey);
|
|
1880
|
+
|
|
1881
|
+
if (entityId !== null) {
|
|
1882
|
+
recursiveDiff[idSerializedKey] = entityId;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
if (dirtyFields[key][relationAttributeName]) {
|
|
1886
|
+
Object.assign(dirtyFields[key][relationAttributeName], recursiveDiff);
|
|
1887
|
+
} else {
|
|
1888
|
+
dirtyFields[key][relationAttributeName] = recursiveDiff;
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1869
1892
|
});
|
|
1870
1893
|
|
|
1871
|
-
|
|
1872
|
-
|
|
1894
|
+
if (dirtyFields[key].length > 0) {
|
|
1895
|
+
// add identifier because one or more object linked has been updated
|
|
1896
|
+
// see test: 'get dirty data many to one update item'
|
|
1897
|
+
dirtyFields[key] = Object.assign(getIdentifierForList(newValue, idSerializedKey), dirtyFields[key]);
|
|
1898
|
+
} else {
|
|
1899
|
+
// no changes, no need to send the key with an empty array as value
|
|
1900
|
+
delete dirtyFields[key];
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
return dirtyFields;
|
|
1873
1905
|
}
|
|
1874
1906
|
}, {
|
|
1875
|
-
key: "
|
|
1876
|
-
value: function
|
|
1877
|
-
var
|
|
1907
|
+
key: "_getDirtyFields",
|
|
1908
|
+
value: function _getDirtyFields(newSerializedModel, oldSerializedModel, classMetadata) {
|
|
1909
|
+
var _this2 = this;
|
|
1878
1910
|
|
|
1879
|
-
var
|
|
1880
|
-
|
|
1911
|
+
var dirtyFields = {};
|
|
1912
|
+
Object.values(classMetadata.getAttributeList()).forEach(function (attribute) {
|
|
1913
|
+
var key = attribute.attributeName;
|
|
1914
|
+
var newValue = newSerializedModel[key];
|
|
1915
|
+
var oldValue = oldSerializedModel ? oldSerializedModel[key] : null;
|
|
1916
|
+
var currentRelation = classMetadata.getRelation(attribute.serializedKey);
|
|
1881
1917
|
|
|
1882
|
-
|
|
1918
|
+
if (newValue === undefined) {
|
|
1919
|
+
return;
|
|
1920
|
+
} // not a relation
|
|
1883
1921
|
|
|
1884
|
-
if (authorizationHeader) {
|
|
1885
|
-
var invalidGrant = authorizationHeader.indexOf('error = "invalid_grant"');
|
|
1886
1922
|
|
|
1887
|
-
if (
|
|
1888
|
-
|
|
1923
|
+
if (!currentRelation) {
|
|
1924
|
+
dirtyFields = _this2._getDirtyFieldsForAttribute(dirtyFields, key, attribute, oldValue, newValue);
|
|
1925
|
+
return;
|
|
1889
1926
|
}
|
|
1890
1927
|
|
|
1891
|
-
|
|
1892
|
-
} else {
|
|
1893
|
-
// if www-authenticate header is missing, try and read json response
|
|
1894
|
-
return response.json().then(function (body) {
|
|
1895
|
-
if (_classPrivateFieldGet(_this5, _tokenStorage) && body.error === 'invalid_grant') {
|
|
1896
|
-
return _this5._refreshTokenAndRefetch(input, requestParams);
|
|
1897
|
-
}
|
|
1928
|
+
var relationMetadata = _this2.mapping.getClassMetadataByKey(currentRelation.targetMetadataKey);
|
|
1898
1929
|
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
throw err;
|
|
1903
|
-
}
|
|
1930
|
+
if (!relationMetadata) {
|
|
1931
|
+
throw new TypeError("relation metadata is not set for relation ".concat(classMetadata.key, ".").concat(currentRelation.targetMetadataKey));
|
|
1932
|
+
}
|
|
1904
1933
|
|
|
1905
|
-
|
|
1906
|
-
});
|
|
1907
|
-
}
|
|
1908
|
-
}
|
|
1909
|
-
}, {
|
|
1910
|
-
key: "_doFetch",
|
|
1911
|
-
value: function _doFetch(accessToken, input, requestParams) {
|
|
1912
|
-
var _this6 = this;
|
|
1934
|
+
var idSerializedKey = relationMetadata.getIdentifierAttribute().serializedKey; // MANY_TO_ONE relation
|
|
1913
1935
|
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1936
|
+
if (currentRelation.isRelationToOne()) {
|
|
1937
|
+
dirtyFields = _this2._getDirtyFieldsForManyToOne(dirtyFields, key, oldValue, newValue, relationMetadata, idSerializedKey);
|
|
1938
|
+
return;
|
|
1939
|
+
} // *_TO_MANY relation
|
|
1918
1940
|
|
|
1919
|
-
if (accessToken) {
|
|
1920
|
-
baseHeaders.Authorization = "".concat(this.sdk.config.authorizationType, " ").concat(accessToken);
|
|
1921
|
-
}
|
|
1922
1941
|
|
|
1923
|
-
|
|
1942
|
+
dirtyFields = _this2._getDirtyFieldsForOneToMany(dirtyFields, key, idSerializedKey, relationMetadata, oldValue, newValue);
|
|
1943
|
+
});
|
|
1944
|
+
return dirtyFields;
|
|
1945
|
+
}
|
|
1946
|
+
}]);
|
|
1924
1947
|
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
}
|
|
1948
|
+
return UnitOfWork;
|
|
1949
|
+
}();
|
|
1928
1950
|
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1951
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
1952
|
+
var Serializer = /*#__PURE__*/function () {
|
|
1953
|
+
function Serializer() {
|
|
1954
|
+
_classCallCheck(this, Serializer);
|
|
1955
|
+
}
|
|
1933
1956
|
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
params = {
|
|
1937
|
-
headers: baseHeaders
|
|
1938
|
-
};
|
|
1939
|
-
}
|
|
1957
|
+
_createClass(Serializer, [{
|
|
1958
|
+
key: "normalizeItem",
|
|
1940
1959
|
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1960
|
+
/**
|
|
1961
|
+
* convert an entity to a plain javascript object
|
|
1962
|
+
* @param {object} entity - The entity to convert
|
|
1963
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
1964
|
+
* @return {object} the object to serialize
|
|
1965
|
+
*/
|
|
1966
|
+
value: function normalizeItem(entity, classMetadata) {
|
|
1967
|
+
return entity;
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* convert a plain javascript object to string
|
|
1971
|
+
* @param {object} object - The object to convert to convert
|
|
1972
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
1973
|
+
* @return {string} the content of the request
|
|
1974
|
+
*/
|
|
1944
1975
|
|
|
1976
|
+
}, {
|
|
1977
|
+
key: "encodeItem",
|
|
1978
|
+
value: function encodeItem(object, classMetadata) {
|
|
1979
|
+
throw new TypeError('`encodeItem` method must be implemented');
|
|
1980
|
+
}
|
|
1981
|
+
/**
|
|
1982
|
+
* convert an entity to string that will be sent as the request content
|
|
1983
|
+
* @param {any} entity - The entity to convert
|
|
1984
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
1985
|
+
* @return {string} the content of the request
|
|
1986
|
+
*/
|
|
1945
1987
|
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1988
|
+
}, {
|
|
1989
|
+
key: "serializeItem",
|
|
1990
|
+
value: function serializeItem(object, classMetadata) {
|
|
1991
|
+
var noralizedData = this.normalizeItem(object, classMetadata);
|
|
1992
|
+
return this.encodeItem(noralizedData, classMetadata);
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* convert a plain object to an entity
|
|
1996
|
+
* @param {object} object - The plain javascript object
|
|
1997
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
1998
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
1999
|
+
* @return {object} an entity
|
|
2000
|
+
*/
|
|
1950
2001
|
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
2002
|
+
}, {
|
|
2003
|
+
key: "denormalizeItem",
|
|
2004
|
+
value: function denormalizeItem(object, classMetadata, response) {
|
|
2005
|
+
return object;
|
|
2006
|
+
}
|
|
2007
|
+
/**
|
|
2008
|
+
* convert a string containing an object to a plain javascript object
|
|
2009
|
+
* @param {string} rawData - The string fetched from the response
|
|
2010
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
2011
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
2012
|
+
* @return {object} the normalized object
|
|
2013
|
+
*/
|
|
2014
|
+
|
|
2015
|
+
}, {
|
|
2016
|
+
key: "decodeItem",
|
|
2017
|
+
value: function decodeItem(rawData, classMetadata, response) {
|
|
2018
|
+
throw new TypeError('`decodeItem` method must be implemented');
|
|
2019
|
+
}
|
|
2020
|
+
/**
|
|
2021
|
+
* convert a string containing an object to an entity
|
|
2022
|
+
* @param {string} rawData - The string fetched from the response
|
|
2023
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
2024
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
2025
|
+
* @return {object} the entity
|
|
2026
|
+
*/
|
|
1954
2027
|
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
2028
|
+
}, {
|
|
2029
|
+
key: "deserializeItem",
|
|
2030
|
+
value: function deserializeItem(rawData, classMetadata, response) {
|
|
2031
|
+
var object = this.decodeItem(rawData, classMetadata, response);
|
|
2032
|
+
return this.denormalizeItem(object, classMetadata, response);
|
|
1958
2033
|
}
|
|
2034
|
+
/**
|
|
2035
|
+
* convert a plain object list to an entity list
|
|
2036
|
+
* @param {Iterable<object>} objectList - The plain javascript object list (or an iterable object)
|
|
2037
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
2038
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
2039
|
+
* @return {Iterable<object>} a list of entities
|
|
2040
|
+
*/
|
|
2041
|
+
|
|
1959
2042
|
}, {
|
|
1960
|
-
key: "
|
|
1961
|
-
value: function
|
|
1962
|
-
|
|
1963
|
-
|
|
2043
|
+
key: "denormalizeList",
|
|
2044
|
+
value: function denormalizeList(objectList, classMetadata, response) {
|
|
2045
|
+
return objectList; // weird conversion as the declaration of list type is fuzzy
|
|
2046
|
+
}
|
|
2047
|
+
/**
|
|
2048
|
+
* convert a string containing a list of objects to a list of plain javascript objects
|
|
2049
|
+
* @param {string} rawListData - The string fetched from the response
|
|
2050
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
2051
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
2052
|
+
* @return {Iterable<object>} a list of normalized objects
|
|
2053
|
+
*/
|
|
1964
2054
|
|
|
1965
|
-
|
|
2055
|
+
}, {
|
|
2056
|
+
key: "decodeList",
|
|
2057
|
+
value: function decodeList(rawListData, classMetadata, response) {
|
|
2058
|
+
throw new TypeError('`deserializeList` method must be implemented');
|
|
1966
2059
|
}
|
|
2060
|
+
/**
|
|
2061
|
+
* convert a string containing a list of objects to a list of entities
|
|
2062
|
+
* @param {string} rawListData - The string fetched from the response
|
|
2063
|
+
* @param {ClassMetadata} classMetadata - the class metadata
|
|
2064
|
+
* @param {Record<string, unknown>} response - the HTTP response
|
|
2065
|
+
* @return {Iterable<object>} a list of entities
|
|
2066
|
+
*/
|
|
2067
|
+
|
|
1967
2068
|
}, {
|
|
1968
|
-
key: "
|
|
1969
|
-
value: function
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
}
|
|
2069
|
+
key: "deserializeList",
|
|
2070
|
+
value: function deserializeList(rawListData, classMetadata, response) {
|
|
2071
|
+
var objectList = this.decodeList(rawListData, classMetadata, response);
|
|
2072
|
+
return this.denormalizeList(objectList, classMetadata, response);
|
|
1973
2073
|
}
|
|
1974
2074
|
}]);
|
|
1975
2075
|
|
|
1976
|
-
return
|
|
2076
|
+
return Serializer;
|
|
1977
2077
|
}();
|
|
1978
2078
|
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
// the same promise if called again before resolve / reject
|
|
1982
|
-
function memoizePromise(callback) {
|
|
1983
|
-
var cache = {};
|
|
1984
|
-
|
|
1985
|
-
function memoized() {
|
|
1986
|
-
for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
1987
|
-
parameters[_key] = arguments[_key];
|
|
1988
|
-
}
|
|
1989
|
-
|
|
1990
|
-
var cacheKey = JSON.stringify(parameters);
|
|
1991
|
-
var cacheValue = cache[cacheKey];
|
|
1992
|
-
|
|
1993
|
-
if (cacheValue) {
|
|
1994
|
-
return cacheValue;
|
|
1995
|
-
} // Get and add the value to the cache
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
var value = callback.apply(this, parameters);
|
|
1999
|
-
cache[cacheKey] = value;
|
|
2000
|
-
|
|
2001
|
-
if (!value || typeof value.then !== 'function') {
|
|
2002
|
-
throw new Error('Memoization Error, Async function returned non-promise value');
|
|
2003
|
-
} // Delete the value regardless of whether it resolves or rejects
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
return value.then(function (internalValue) {
|
|
2007
|
-
cache[cacheKey] = false;
|
|
2008
|
-
return internalValue;
|
|
2009
|
-
}, function (err) {
|
|
2010
|
-
cache[cacheKey] = false;
|
|
2011
|
-
throw err;
|
|
2012
|
-
});
|
|
2013
|
-
}
|
|
2014
|
-
|
|
2015
|
-
memoized.cache = cache;
|
|
2016
|
-
return memoized;
|
|
2017
|
-
}
|
|
2018
|
-
|
|
2019
|
-
function isResponse(response) {
|
|
2020
|
-
return typeof (response === null || response === void 0 ? void 0 : response.json) === 'function';
|
|
2021
|
-
}
|
|
2079
|
+
var JsSerializer = /*#__PURE__*/function (_Serializer) {
|
|
2080
|
+
_inherits(JsSerializer, _Serializer);
|
|
2022
2081
|
|
|
2023
|
-
|
|
2024
|
-
var error;
|
|
2082
|
+
var _super = _createSuper(JsSerializer);
|
|
2025
2083
|
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
}
|
|
2084
|
+
function JsSerializer() {
|
|
2085
|
+
_classCallCheck(this, JsSerializer);
|
|
2029
2086
|
|
|
2030
|
-
|
|
2031
|
-
throw new InvalidGrantError(body.error, error);
|
|
2087
|
+
return _super.apply(this, arguments);
|
|
2032
2088
|
}
|
|
2033
2089
|
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2090
|
+
_createClass(JsSerializer, [{
|
|
2091
|
+
key: "encodeItem",
|
|
2092
|
+
value: function encodeItem(object, classMetadata) {
|
|
2093
|
+
return JSON.stringify(object);
|
|
2094
|
+
}
|
|
2095
|
+
}, {
|
|
2096
|
+
key: "decodeItem",
|
|
2097
|
+
value: function decodeItem(rawData, classMetadata, response) {
|
|
2098
|
+
return JSON.parse(rawData);
|
|
2099
|
+
}
|
|
2100
|
+
}, {
|
|
2101
|
+
key: "decodeList",
|
|
2102
|
+
value: function decodeList(rawListData, classMetadata, response) {
|
|
2103
|
+
return JSON.parse(rawListData);
|
|
2104
|
+
}
|
|
2105
|
+
}]);
|
|
2037
2106
|
|
|
2038
|
-
|
|
2039
|
-
}
|
|
2107
|
+
return JsSerializer;
|
|
2108
|
+
}(Serializer);
|
|
2040
2109
|
|
|
2041
|
-
var
|
|
2110
|
+
var generateId = function generateId() {
|
|
2111
|
+
return "rest-client-sdk|".concat(Date.now(), "-").concat(Math.floor(Math.random() * (9e12 - 1)) + 1e12);
|
|
2112
|
+
};
|
|
2042
2113
|
|
|
2043
|
-
|
|
2114
|
+
function serializeRequest(req) {
|
|
2115
|
+
var request = _objectSpread2({}, req); // Make a clone, useful for doing destructive things
|
|
2044
2116
|
|
|
2045
|
-
var _asyncStorage = new WeakMap();
|
|
2046
2117
|
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2118
|
+
return {
|
|
2119
|
+
method: request.method,
|
|
2120
|
+
// The Request Method, e.g. GET, POST, DELETE
|
|
2121
|
+
url: request.url,
|
|
2122
|
+
// The URL
|
|
2123
|
+
headers: request.headers ? convertToRecord(request.headers) : undefined,
|
|
2124
|
+
body: typeof request.body === 'string' ? request.body : undefined
|
|
2125
|
+
};
|
|
2126
|
+
} // eslint-disable-next-line import/prefer-default-export
|
|
2050
2127
|
|
|
2051
|
-
_classCallCheck(this, TokenStorage);
|
|
2052
2128
|
|
|
2053
|
-
|
|
2054
|
-
writable: true,
|
|
2055
|
-
value: void 0
|
|
2056
|
-
});
|
|
2129
|
+
var _history = new WeakMap();
|
|
2057
2130
|
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2131
|
+
var Logger = /*#__PURE__*/function () {
|
|
2132
|
+
_createClass(Logger, [{
|
|
2133
|
+
key: "history",
|
|
2134
|
+
get: function get() {
|
|
2135
|
+
return _classPrivateFieldGet(this, _history);
|
|
2136
|
+
}
|
|
2137
|
+
}]);
|
|
2062
2138
|
|
|
2063
|
-
|
|
2139
|
+
function Logger() {
|
|
2140
|
+
_classCallCheck(this, Logger);
|
|
2064
2141
|
|
|
2065
|
-
|
|
2142
|
+
_history.set(this, {
|
|
2066
2143
|
writable: true,
|
|
2067
2144
|
value: void 0
|
|
2068
2145
|
});
|
|
2069
2146
|
|
|
2070
|
-
_classPrivateFieldSet(this,
|
|
2147
|
+
_classPrivateFieldSet(this, _history, []);
|
|
2148
|
+
}
|
|
2071
2149
|
|
|
2072
|
-
|
|
2150
|
+
_createClass(Logger, [{
|
|
2151
|
+
key: "logRequest",
|
|
2152
|
+
value: function logRequest(params) {
|
|
2153
|
+
var log = {
|
|
2154
|
+
id: generateId(),
|
|
2155
|
+
request: serializeRequest(params),
|
|
2156
|
+
logTimes: {
|
|
2157
|
+
request: Date.now()
|
|
2158
|
+
}
|
|
2159
|
+
};
|
|
2073
2160
|
|
|
2074
|
-
|
|
2075
|
-
this.accessTokenKey = accessTokenKey;
|
|
2076
|
-
this.generateToken = memoizePromise(this.generateToken);
|
|
2077
|
-
this.refreshToken = memoizePromise(this.refreshToken);
|
|
2078
|
-
}
|
|
2161
|
+
_classPrivateFieldGet(this, _history).push(log);
|
|
2079
2162
|
|
|
2080
|
-
|
|
2081
|
-
key: "setAsyncStorage",
|
|
2082
|
-
value: function setAsyncStorage(asyncStorage) {
|
|
2083
|
-
_classPrivateFieldSet(this, _asyncStorage, asyncStorage);
|
|
2084
|
-
}
|
|
2085
|
-
}, {
|
|
2086
|
-
key: "hasAccessToken",
|
|
2087
|
-
value: function hasAccessToken() {
|
|
2088
|
-
return _classPrivateFieldGet(this, _asyncStorage).getItem(this.accessTokenKey).then(function (accessToken) {
|
|
2089
|
-
return !!accessToken;
|
|
2090
|
-
});
|
|
2163
|
+
return log.id;
|
|
2091
2164
|
}
|
|
2092
2165
|
}, {
|
|
2093
|
-
key: "
|
|
2094
|
-
value: function
|
|
2095
|
-
|
|
2096
|
-
|
|
2166
|
+
key: "logResponse",
|
|
2167
|
+
value: function logResponse(response, requestId) {
|
|
2168
|
+
var id = requestId || generateId(); // console.log('clone');
|
|
2169
|
+
// const clonedResponse = response.clone(); // THIS IS THE LINE THAT CAUSES THE PROBLEM
|
|
2170
|
+
// console.log('after clone', clonedResponse.status);
|
|
2171
|
+
|
|
2172
|
+
var logContent = {
|
|
2173
|
+
status: response.status,
|
|
2174
|
+
// clonedResponse.status,
|
|
2175
|
+
headers: Object.fromEntries(response.headers)
|
|
2176
|
+
};
|
|
2177
|
+
|
|
2178
|
+
var foundLogIndex = _classPrivateFieldGet(this, _history).findIndex(function (log) {
|
|
2179
|
+
return log.id === id;
|
|
2097
2180
|
});
|
|
2181
|
+
|
|
2182
|
+
if (foundLogIndex > -1) {
|
|
2183
|
+
_classPrivateFieldGet(this, _history)[foundLogIndex].response = logContent;
|
|
2184
|
+
_classPrivateFieldGet(this, _history)[foundLogIndex].logTimes.response = Date.now();
|
|
2185
|
+
} else {
|
|
2186
|
+
_classPrivateFieldGet(this, _history).push({
|
|
2187
|
+
id: id,
|
|
2188
|
+
response: logContent,
|
|
2189
|
+
logTimes: {
|
|
2190
|
+
response: Date.now()
|
|
2191
|
+
}
|
|
2192
|
+
});
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
return id;
|
|
2098
2196
|
}
|
|
2099
|
-
}
|
|
2100
|
-
key: "getAccessTokenObject",
|
|
2101
|
-
value: function getAccessTokenObject() {
|
|
2102
|
-
var _this = this;
|
|
2197
|
+
}]);
|
|
2103
2198
|
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
var tokenObject = JSON.parse(token);
|
|
2199
|
+
return Logger;
|
|
2200
|
+
}();
|
|
2107
2201
|
|
|
2108
|
-
|
|
2109
|
-
return null;
|
|
2110
|
-
}
|
|
2202
|
+
var _repositoryList = new WeakMap();
|
|
2111
2203
|
|
|
2112
|
-
|
|
2113
|
-
|
|
2204
|
+
var RestClientSdk = /*#__PURE__*/function () {
|
|
2205
|
+
function RestClientSdk(tokenStorage, config, mapping) {
|
|
2206
|
+
var serializer = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new JsSerializer();
|
|
2114
2207
|
|
|
2115
|
-
|
|
2116
|
-
return null;
|
|
2117
|
-
}
|
|
2208
|
+
_classCallCheck(this, RestClientSdk);
|
|
2118
2209
|
|
|
2119
|
-
|
|
2120
|
-
return _classPrivateFieldGet(_this, _tokenGenerator).autoGenerateToken();
|
|
2121
|
-
}
|
|
2210
|
+
_defineProperty(this, "config", void 0);
|
|
2122
2211
|
|
|
2123
|
-
|
|
2124
|
-
});
|
|
2125
|
-
}
|
|
2126
|
-
}, {
|
|
2127
|
-
key: "logout",
|
|
2128
|
-
value: function logout() {
|
|
2129
|
-
return _classPrivateFieldGet(this, _asyncStorage).removeItem(this.accessTokenKey);
|
|
2130
|
-
}
|
|
2131
|
-
}, {
|
|
2132
|
-
key: "_addExpiresAtToResponseData",
|
|
2133
|
-
value: function _addExpiresAtToResponseData(responseData, callTimestamp) {
|
|
2134
|
-
if (!responseData) {
|
|
2135
|
-
return responseData;
|
|
2136
|
-
}
|
|
2212
|
+
_defineProperty(this, "tokenStorage", void 0);
|
|
2137
2213
|
|
|
2138
|
-
|
|
2139
|
-
expires_at: null
|
|
2140
|
-
});
|
|
2214
|
+
_defineProperty(this, "serializer", void 0);
|
|
2141
2215
|
|
|
2142
|
-
|
|
2143
|
-
) {
|
|
2144
|
-
updatedResponseData.expires_at = Math.round(callTimestamp / 1000) + updatedResponseData.expires_in;
|
|
2145
|
-
}
|
|
2216
|
+
_defineProperty(this, "mapping", void 0);
|
|
2146
2217
|
|
|
2147
|
-
|
|
2148
|
-
}
|
|
2149
|
-
}, {
|
|
2150
|
-
key: "generateToken",
|
|
2151
|
-
value: function generateToken(parameters) {
|
|
2152
|
-
var _this2 = this;
|
|
2218
|
+
_defineProperty(this, "unitOfWork", void 0);
|
|
2153
2219
|
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2220
|
+
_repositoryList.set(this, {
|
|
2221
|
+
writable: true,
|
|
2222
|
+
value: void 0
|
|
2223
|
+
});
|
|
2224
|
+
|
|
2225
|
+
_defineProperty(this, "logger", void 0);
|
|
2157
2226
|
|
|
2158
|
-
|
|
2159
|
-
_classPrivateFieldSet(_this2, _hasATokenBeenGenerated, true);
|
|
2227
|
+
this.checkConfigValidity(config);
|
|
2160
2228
|
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
});
|
|
2229
|
+
if (!(mapping instanceof Mapping)) {
|
|
2230
|
+
throw new TypeError('mapping should be an instance of `Mapping`');
|
|
2164
2231
|
}
|
|
2165
|
-
}, {
|
|
2166
|
-
key: "refreshToken",
|
|
2167
|
-
value: function refreshToken() {
|
|
2168
|
-
var _this3 = this;
|
|
2169
2232
|
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2233
|
+
this.config = this._mergeWithBaseConfig(config);
|
|
2234
|
+
this.tokenStorage = tokenStorage;
|
|
2235
|
+
this.serializer = serializer;
|
|
2236
|
+
this.mapping = mapping;
|
|
2237
|
+
this.unitOfWork = new UnitOfWork(this.mapping, this.config.unitOfWorkEnabled);
|
|
2174
2238
|
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
});
|
|
2178
|
-
});
|
|
2179
|
-
});
|
|
2239
|
+
if (config.loggerEnabled) {
|
|
2240
|
+
this.logger = new Logger();
|
|
2180
2241
|
}
|
|
2181
|
-
}, {
|
|
2182
|
-
key: "handleTokenResponse",
|
|
2183
|
-
value: function handleTokenResponse(responseOrBody) {
|
|
2184
|
-
if (isResponse(responseOrBody)) {
|
|
2185
|
-
var response = responseOrBody;
|
|
2186
|
-
var originalResponse = response.clone();
|
|
2187
|
-
return response.json().then(function (body) {
|
|
2188
|
-
if (isOauthError(body)) {
|
|
2189
|
-
// throw error if response body is an oauth error
|
|
2190
|
-
manageOauthError(body, response);
|
|
2191
|
-
} else if (response.status >= 400) {
|
|
2192
|
-
// throw an error if response status code is an "error" status code
|
|
2193
|
-
throw getHttpErrorFromResponse(originalResponse);
|
|
2194
|
-
}
|
|
2195
2242
|
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2243
|
+
_classPrivateFieldSet(this, _repositoryList, {});
|
|
2244
|
+
}
|
|
2245
|
+
/**
|
|
2246
|
+
* get a repository by it's metadata "key" attribute
|
|
2247
|
+
*
|
|
2248
|
+
* @param {string} key the repository "key" (the first argument of `new ClassMetadata`)
|
|
2249
|
+
*/
|
|
2201
2250
|
|
|
2202
|
-
throw err;
|
|
2203
|
-
});
|
|
2204
|
-
}
|
|
2205
2251
|
|
|
2206
|
-
|
|
2252
|
+
_createClass(RestClientSdk, [{
|
|
2253
|
+
key: "getRepository",
|
|
2254
|
+
value: function getRepository(key) {
|
|
2255
|
+
if (!_classPrivateFieldGet(this, _repositoryList)[key]) {
|
|
2256
|
+
var metadata = this.mapping.getClassMetadataByKey(key);
|
|
2207
2257
|
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
}
|
|
2258
|
+
if (!metadata) {
|
|
2259
|
+
throw new Error("Unable to get metadata for repository ".concat(key));
|
|
2260
|
+
} // eslint-disable-next-line new-cap
|
|
2212
2261
|
|
|
2213
|
-
return Promise.resolve(body);
|
|
2214
|
-
}
|
|
2215
|
-
/**
|
|
2216
|
-
* Return the number of second when the token will expire
|
|
2217
|
-
* return value can be negative if the token is already expired
|
|
2218
|
-
*/
|
|
2219
2262
|
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
value: function getCurrentTokenExpiresIn() {
|
|
2223
|
-
return this.getAccessTokenObject().then(function (accessTokenObject) {
|
|
2224
|
-
if (accessTokenObject === null) {
|
|
2225
|
-
throw new Error('No token has been stored.');
|
|
2226
|
-
}
|
|
2263
|
+
_classPrivateFieldGet(this, _repositoryList)[key] = generateRepository(this, metadata, this.config.unitOfWorkEnabled);
|
|
2264
|
+
} // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2227
2265
|
|
|
2228
|
-
if (typeof accessTokenObject.expires_at === 'undefined' || accessTokenObject.expires_at === null) {
|
|
2229
|
-
return null;
|
|
2230
|
-
}
|
|
2231
2266
|
|
|
2232
|
-
|
|
2233
|
-
return accessTokenObject.expires_at - now;
|
|
2234
|
-
});
|
|
2267
|
+
return _classPrivateFieldGet(this, _repositoryList)[key];
|
|
2235
2268
|
}
|
|
2236
2269
|
}, {
|
|
2237
|
-
key: "
|
|
2238
|
-
value: function
|
|
2239
|
-
|
|
2270
|
+
key: "_mergeWithBaseConfig",
|
|
2271
|
+
value: function _mergeWithBaseConfig(config) {
|
|
2272
|
+
var newConfig = config;
|
|
2273
|
+
newConfig.useDefaultParameters = config.useDefaultParameters === undefined ? true : config.useDefaultParameters;
|
|
2274
|
+
newConfig.authorizationType = config.authorizationType || 'Bearer';
|
|
2275
|
+
return newConfig;
|
|
2276
|
+
}
|
|
2277
|
+
}, {
|
|
2278
|
+
key: "checkConfigValidity",
|
|
2279
|
+
value: function checkConfigValidity(config) {
|
|
2280
|
+
if (!(config && config.path && config.scheme)) {
|
|
2281
|
+
throw new RangeError("SDK config is not valid, it should contain a \"path\", a \"scheme\" parameter,\n and may contain a \"port\" and a \"useDefaultParameters\" parameter");
|
|
2282
|
+
}
|
|
2240
2283
|
}
|
|
2241
2284
|
}]);
|
|
2242
2285
|
|
|
2243
|
-
return
|
|
2286
|
+
return RestClientSdk;
|
|
2244
2287
|
}();
|
|
2245
2288
|
|
|
2246
2289
|
var AbstractTokenGenerator = /*#__PURE__*/function () {
|
|
2247
|
-
function AbstractTokenGenerator(tokenGeneratorConfig) {
|
|
2290
|
+
function AbstractTokenGenerator(tokenGeneratorConfig, logger) {
|
|
2248
2291
|
_classCallCheck(this, AbstractTokenGenerator);
|
|
2249
2292
|
|
|
2250
2293
|
_defineProperty(this, "tokenGeneratorConfig", void 0);
|
|
2251
2294
|
|
|
2295
|
+
_defineProperty(this, "logger", void 0);
|
|
2296
|
+
|
|
2297
|
+
if (logger) {
|
|
2298
|
+
this.logger = logger;
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2252
2301
|
this.tokenGeneratorConfig = tokenGeneratorConfig;
|
|
2253
2302
|
|
|
2254
2303
|
if (typeof this.checkTokenGeneratorConfig === 'function') {
|
|
@@ -2321,7 +2370,7 @@ var AuthorizationCodeFlowTokenGenerator = /*#__PURE__*/function (_AbstractTokenG
|
|
|
2321
2370
|
this._checkGenerateParameters(parameters);
|
|
2322
2371
|
|
|
2323
2372
|
var url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
2324
|
-
|
|
2373
|
+
var params = {
|
|
2325
2374
|
method: 'POST',
|
|
2326
2375
|
headers: {
|
|
2327
2376
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
@@ -2333,8 +2382,16 @@ var AuthorizationCodeFlowTokenGenerator = /*#__PURE__*/function (_AbstractTokenG
|
|
|
2333
2382
|
// TODO : secure this ?
|
|
2334
2383
|
redirect_uri: this.tokenGeneratorConfig.redirectUri,
|
|
2335
2384
|
code: parameters.code
|
|
2336
|
-
})
|
|
2337
|
-
}
|
|
2385
|
+
}).toString()
|
|
2386
|
+
};
|
|
2387
|
+
|
|
2388
|
+
if (this.logger) {
|
|
2389
|
+
this.logger.logRequest(_objectSpread2({
|
|
2390
|
+
url: url
|
|
2391
|
+
}, params));
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
return fetch(url, params);
|
|
2338
2395
|
}
|
|
2339
2396
|
/**
|
|
2340
2397
|
* This function needs to refresh the current possibly expired access token
|
|
@@ -2353,7 +2410,7 @@ var AuthorizationCodeFlowTokenGenerator = /*#__PURE__*/function (_AbstractTokenG
|
|
|
2353
2410
|
}
|
|
2354
2411
|
|
|
2355
2412
|
var url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
2356
|
-
|
|
2413
|
+
var params = {
|
|
2357
2414
|
method: 'POST',
|
|
2358
2415
|
headers: {
|
|
2359
2416
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
@@ -2363,8 +2420,16 @@ var AuthorizationCodeFlowTokenGenerator = /*#__PURE__*/function (_AbstractTokenG
|
|
|
2363
2420
|
client_id: this.tokenGeneratorConfig.clientId,
|
|
2364
2421
|
client_secret: this.tokenGeneratorConfig.clientSecret,
|
|
2365
2422
|
refresh_token: oldAccessToken.refresh_token
|
|
2366
|
-
})
|
|
2367
|
-
}
|
|
2423
|
+
}).toString()
|
|
2424
|
+
};
|
|
2425
|
+
|
|
2426
|
+
if (this.logger) {
|
|
2427
|
+
this.logger.logRequest(_objectSpread2({
|
|
2428
|
+
url: url
|
|
2429
|
+
}, params));
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
return fetch(url, params);
|
|
2368
2433
|
}
|
|
2369
2434
|
}, {
|
|
2370
2435
|
key: "checkTokenGeneratorConfig",
|
|
@@ -2401,6 +2466,46 @@ var AuthorizationCodeFlowTokenGenerator = /*#__PURE__*/function (_AbstractTokenG
|
|
|
2401
2466
|
return AuthorizationCodeFlowTokenGenerator;
|
|
2402
2467
|
}(AbstractTokenGenerator);
|
|
2403
2468
|
|
|
2469
|
+
/* eslint-disable import/prefer-default-export */
|
|
2470
|
+
// memoize promise returning function so that it returns
|
|
2471
|
+
// the same promise if called again before resolve / reject
|
|
2472
|
+
function memoizePromise(callback) {
|
|
2473
|
+
var cache = {};
|
|
2474
|
+
|
|
2475
|
+
function memoized() {
|
|
2476
|
+
for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2477
|
+
parameters[_key] = arguments[_key];
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2480
|
+
var cacheKey = JSON.stringify(parameters);
|
|
2481
|
+
var cacheValue = cache[cacheKey];
|
|
2482
|
+
|
|
2483
|
+
if (cacheValue) {
|
|
2484
|
+
return cacheValue;
|
|
2485
|
+
} // Get and add the value to the cache
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
var value = callback.apply(this, parameters);
|
|
2489
|
+
cache[cacheKey] = value;
|
|
2490
|
+
|
|
2491
|
+
if (!value || typeof value.then !== 'function') {
|
|
2492
|
+
throw new Error('Memoization Error, Async function returned non-promise value');
|
|
2493
|
+
} // Delete the value regardless of whether it resolves or rejects
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
return value.then(function (internalValue) {
|
|
2497
|
+
cache[cacheKey] = false;
|
|
2498
|
+
return internalValue;
|
|
2499
|
+
}, function (err) {
|
|
2500
|
+
cache[cacheKey] = false;
|
|
2501
|
+
throw err;
|
|
2502
|
+
});
|
|
2503
|
+
}
|
|
2504
|
+
|
|
2505
|
+
memoized.cache = cache;
|
|
2506
|
+
return memoized;
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2404
2509
|
var ERROR_CONFIG_EMPTY = 'TokenGenerator config must be set';
|
|
2405
2510
|
var ERROR_CONFIG_PATH_SCHEME = 'TokenGenerator config is not valid, it should contain a "path", a "scheme" parameter';
|
|
2406
2511
|
var ERROR_CONFIG_CLIENT_INFORMATIONS = 'TokenGenerator config is not valid, it should contain a "clientId", a "clientSecret" parameter';
|
|
@@ -2436,14 +2541,22 @@ var ClientCredentialsGenerator = /*#__PURE__*/function (_AbstractTokenGenerat) {
|
|
|
2436
2541
|
body.append('scope', this.tokenGeneratorConfig.scope);
|
|
2437
2542
|
}
|
|
2438
2543
|
|
|
2439
|
-
var
|
|
2440
|
-
return fetch(url, {
|
|
2544
|
+
var params = {
|
|
2441
2545
|
method: 'POST',
|
|
2442
2546
|
headers: {
|
|
2443
2547
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
2444
2548
|
},
|
|
2445
|
-
body: body
|
|
2446
|
-
}
|
|
2549
|
+
body: body.toString()
|
|
2550
|
+
};
|
|
2551
|
+
var url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
2552
|
+
|
|
2553
|
+
if (this.logger) {
|
|
2554
|
+
this.logger.logRequest(_objectSpread2({
|
|
2555
|
+
url: url
|
|
2556
|
+
}, params));
|
|
2557
|
+
}
|
|
2558
|
+
|
|
2559
|
+
return fetch(url, params);
|
|
2447
2560
|
}
|
|
2448
2561
|
}, {
|
|
2449
2562
|
key: "refreshToken",
|
|
@@ -2508,13 +2621,21 @@ var PasswordGenerator = /*#__PURE__*/function (_AbstractTokenGenerat) {
|
|
|
2508
2621
|
}
|
|
2509
2622
|
|
|
2510
2623
|
var url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
2511
|
-
|
|
2624
|
+
var params = {
|
|
2512
2625
|
method: 'POST',
|
|
2513
2626
|
headers: {
|
|
2514
2627
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
2515
2628
|
},
|
|
2516
|
-
body: body
|
|
2517
|
-
}
|
|
2629
|
+
body: body.toString()
|
|
2630
|
+
};
|
|
2631
|
+
|
|
2632
|
+
if (this.logger) {
|
|
2633
|
+
this.logger.logRequest(_objectSpread2({
|
|
2634
|
+
url: url
|
|
2635
|
+
}, params));
|
|
2636
|
+
}
|
|
2637
|
+
|
|
2638
|
+
return fetch(url, params);
|
|
2518
2639
|
}
|
|
2519
2640
|
}, {
|
|
2520
2641
|
key: "refreshToken",
|
|
@@ -2537,13 +2658,21 @@ var PasswordGenerator = /*#__PURE__*/function (_AbstractTokenGenerat) {
|
|
|
2537
2658
|
}
|
|
2538
2659
|
|
|
2539
2660
|
var url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
2540
|
-
|
|
2661
|
+
var params = {
|
|
2541
2662
|
method: 'POST',
|
|
2542
2663
|
headers: {
|
|
2543
2664
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
2544
2665
|
},
|
|
2545
|
-
body: body
|
|
2546
|
-
}
|
|
2666
|
+
body: body.toString()
|
|
2667
|
+
};
|
|
2668
|
+
|
|
2669
|
+
if (this.logger) {
|
|
2670
|
+
this.logger.logRequest(_objectSpread2({
|
|
2671
|
+
url: url
|
|
2672
|
+
}, params));
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
return fetch(url, params);
|
|
2547
2676
|
}
|
|
2548
2677
|
}, {
|
|
2549
2678
|
key: "checkTokenGeneratorConfig",
|
|
@@ -2638,254 +2767,252 @@ var ProvidedTokenGenerator = /*#__PURE__*/function () {
|
|
|
2638
2767
|
return ProvidedTokenGenerator;
|
|
2639
2768
|
}();
|
|
2640
2769
|
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
* @param {string} type type of the attribute
|
|
2646
|
-
* @param {boolean} isIdentifier is this attribute the entity identifier
|
|
2647
|
-
*/
|
|
2648
|
-
function Attribute(serializedKey) {
|
|
2649
|
-
var attributeName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
2650
|
-
var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'string';
|
|
2651
|
-
var isIdentifier = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
2652
|
-
|
|
2653
|
-
_classCallCheck(this, Attribute);
|
|
2654
|
-
|
|
2655
|
-
_defineProperty(this, "serializedKey", void 0);
|
|
2656
|
-
|
|
2657
|
-
_defineProperty(this, "attributeName", void 0);
|
|
2658
|
-
|
|
2659
|
-
_defineProperty(this, "type", void 0);
|
|
2770
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2771
|
+
function isResponse(response) {
|
|
2772
|
+
return typeof (response === null || response === void 0 ? void 0 : response.json) === 'function';
|
|
2773
|
+
}
|
|
2660
2774
|
|
|
2661
|
-
|
|
2775
|
+
function manageOauthError(body, response) {
|
|
2776
|
+
var error;
|
|
2662
2777
|
|
|
2663
|
-
if (
|
|
2664
|
-
|
|
2778
|
+
if (response) {
|
|
2779
|
+
error = getHttpErrorFromResponse(response);
|
|
2665
2780
|
}
|
|
2666
2781
|
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
this.isIdentifier = isIdentifier;
|
|
2671
|
-
};
|
|
2782
|
+
if (body.error === 'invalid_grant') {
|
|
2783
|
+
throw new InvalidGrantError(body.error, error);
|
|
2784
|
+
}
|
|
2672
2785
|
|
|
2673
|
-
|
|
2786
|
+
if (body.error === 'invalid_scope') {
|
|
2787
|
+
throw new InvalidScopeError(body.error, error);
|
|
2788
|
+
}
|
|
2674
2789
|
|
|
2675
|
-
|
|
2790
|
+
throw new OauthError(body.error, error);
|
|
2791
|
+
}
|
|
2676
2792
|
|
|
2677
|
-
var
|
|
2793
|
+
var _tokenGenerator = new WeakMap();
|
|
2678
2794
|
|
|
2679
|
-
var
|
|
2680
|
-
/**
|
|
2681
|
-
* @param {string} key mandatory, will be passed in your serializer
|
|
2682
|
-
* @param {string|null} pathRoot the endpoint of your API: will be added to the mapping prefix ('/v1' here)
|
|
2683
|
-
* @param {typeof AbstractClient} repositoryClass [Overriding repository]{@link https://github.com/mapado/rest-client-js-sdk#overriding-repository} for more detail
|
|
2684
|
-
*/
|
|
2685
|
-
function ClassMetadata(key) {
|
|
2686
|
-
var pathRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
2687
|
-
var repositoryClass = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : AbstractClient;
|
|
2795
|
+
var _hasATokenBeenGenerated = new WeakMap();
|
|
2688
2796
|
|
|
2689
|
-
|
|
2797
|
+
var _asyncStorage = new WeakMap();
|
|
2690
2798
|
|
|
2691
|
-
|
|
2799
|
+
var _logger = new WeakMap();
|
|
2692
2800
|
|
|
2693
|
-
|
|
2801
|
+
var TokenStorage = /*#__PURE__*/function () {
|
|
2802
|
+
function TokenStorage(tokenGenerator, asyncStorage) {
|
|
2803
|
+
var accessTokenKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'rest_client_sdk.api.access_token';
|
|
2804
|
+
var logger = arguments.length > 3 ? arguments[3] : undefined;
|
|
2694
2805
|
|
|
2695
|
-
|
|
2806
|
+
_classCallCheck(this, TokenStorage);
|
|
2696
2807
|
|
|
2697
|
-
|
|
2808
|
+
_tokenGenerator.set(this, {
|
|
2698
2809
|
writable: true,
|
|
2699
2810
|
value: void 0
|
|
2700
2811
|
});
|
|
2701
2812
|
|
|
2702
|
-
|
|
2813
|
+
_hasATokenBeenGenerated.set(this, {
|
|
2703
2814
|
writable: true,
|
|
2704
2815
|
value: void 0
|
|
2705
2816
|
});
|
|
2706
2817
|
|
|
2707
|
-
|
|
2818
|
+
_defineProperty(this, "accessTokenKey", void 0);
|
|
2819
|
+
|
|
2820
|
+
_asyncStorage.set(this, {
|
|
2708
2821
|
writable: true,
|
|
2709
2822
|
value: void 0
|
|
2710
2823
|
});
|
|
2711
2824
|
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2825
|
+
_logger.set(this, {
|
|
2826
|
+
writable: true,
|
|
2827
|
+
value: void 0
|
|
2828
|
+
});
|
|
2715
2829
|
|
|
2716
|
-
|
|
2717
|
-
|
|
2830
|
+
if (logger) {
|
|
2831
|
+
_classPrivateFieldSet(this, _logger, logger);
|
|
2832
|
+
}
|
|
2718
2833
|
|
|
2719
|
-
this
|
|
2834
|
+
_classPrivateFieldSet(this, _tokenGenerator, tokenGenerator);
|
|
2720
2835
|
|
|
2721
|
-
_classPrivateFieldSet(this,
|
|
2836
|
+
_classPrivateFieldSet(this, _hasATokenBeenGenerated, false);
|
|
2722
2837
|
|
|
2723
|
-
|
|
2838
|
+
this.setAsyncStorage(asyncStorage);
|
|
2839
|
+
this.accessTokenKey = accessTokenKey;
|
|
2840
|
+
this.handleTokenResponse = this.handleTokenResponse.bind(this);
|
|
2841
|
+
this.generateToken = memoizePromise(this.generateToken);
|
|
2842
|
+
this.refreshToken = memoizePromise(this.refreshToken);
|
|
2724
2843
|
}
|
|
2725
2844
|
|
|
2726
|
-
_createClass(
|
|
2727
|
-
key: "
|
|
2728
|
-
value: function
|
|
2729
|
-
|
|
2730
|
-
}
|
|
2731
|
-
}, {
|
|
2732
|
-
key: "hasIdentifierAttribute",
|
|
2733
|
-
value: function hasIdentifierAttribute() {
|
|
2734
|
-
return !!_classPrivateFieldGet(this, _identifierAttribute);
|
|
2845
|
+
_createClass(TokenStorage, [{
|
|
2846
|
+
key: "setAsyncStorage",
|
|
2847
|
+
value: function setAsyncStorage(asyncStorage) {
|
|
2848
|
+
_classPrivateFieldSet(this, _asyncStorage, asyncStorage);
|
|
2735
2849
|
}
|
|
2736
2850
|
}, {
|
|
2737
|
-
key: "
|
|
2738
|
-
value: function
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
}
|
|
2742
|
-
|
|
2743
|
-
return _classPrivateFieldGet(this, _identifierAttribute);
|
|
2851
|
+
key: "hasAccessToken",
|
|
2852
|
+
value: function hasAccessToken() {
|
|
2853
|
+
return _classPrivateFieldGet(this, _asyncStorage).getItem(this.accessTokenKey).then(function (accessToken) {
|
|
2854
|
+
return !!accessToken;
|
|
2855
|
+
});
|
|
2744
2856
|
}
|
|
2745
2857
|
}, {
|
|
2746
|
-
key: "
|
|
2747
|
-
value: function
|
|
2748
|
-
return
|
|
2858
|
+
key: "getAccessToken",
|
|
2859
|
+
value: function getAccessToken() {
|
|
2860
|
+
return this.getAccessTokenObject().then(function (token) {
|
|
2861
|
+
return token && token.access_token;
|
|
2862
|
+
});
|
|
2749
2863
|
}
|
|
2750
2864
|
}, {
|
|
2751
|
-
key: "
|
|
2752
|
-
value: function
|
|
2865
|
+
key: "getAccessTokenObject",
|
|
2866
|
+
value: function getAccessTokenObject() {
|
|
2753
2867
|
var _this = this;
|
|
2754
2868
|
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2869
|
+
return _classPrivateFieldGet(this, _asyncStorage).getItem(this.accessTokenKey).then(function (token) {
|
|
2870
|
+
if (token) {
|
|
2871
|
+
var tokenObject = JSON.parse(token);
|
|
2758
2872
|
|
|
2759
|
-
|
|
2760
|
-
|
|
2873
|
+
if (_typeof(tokenObject) !== 'object') {
|
|
2874
|
+
return null;
|
|
2875
|
+
}
|
|
2761
2876
|
|
|
2762
|
-
|
|
2763
|
-
_classPrivateFieldSet(_this, _identifierAttribute, attribute);
|
|
2877
|
+
return tokenObject;
|
|
2764
2878
|
}
|
|
2765
|
-
});
|
|
2766
2879
|
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
}
|
|
2771
|
-
}, {
|
|
2772
|
-
key: "setRelationList",
|
|
2773
|
-
value: function setRelationList(relationList) {
|
|
2774
|
-
var _this2 = this;
|
|
2880
|
+
if (_classPrivateFieldGet(_this, _hasATokenBeenGenerated)) {
|
|
2881
|
+
return null;
|
|
2882
|
+
}
|
|
2775
2883
|
|
|
2776
|
-
|
|
2884
|
+
if (typeof _classPrivateFieldGet(_this, _tokenGenerator).autoGenerateToken === 'function') {
|
|
2885
|
+
return _classPrivateFieldGet(_this, _tokenGenerator).autoGenerateToken();
|
|
2886
|
+
}
|
|
2777
2887
|
|
|
2778
|
-
|
|
2779
|
-
_classPrivateFieldGet(_this2, _relationList)[relation.serializedKey] = relation;
|
|
2780
|
-
_classPrivateFieldGet(_this2, _attributeList)[relation.serializedKey] = new Attribute(relation.serializedKey, relation.serializedKey, relation.isRelationToMany() ? 'array' : 'object');
|
|
2888
|
+
throw new Error('No token has been generated yet.');
|
|
2781
2889
|
});
|
|
2782
2890
|
}
|
|
2783
2891
|
}, {
|
|
2784
|
-
key: "
|
|
2785
|
-
value: function
|
|
2786
|
-
return _classPrivateFieldGet(this,
|
|
2892
|
+
key: "logout",
|
|
2893
|
+
value: function logout() {
|
|
2894
|
+
return _classPrivateFieldGet(this, _asyncStorage).removeItem(this.accessTokenKey);
|
|
2787
2895
|
}
|
|
2788
2896
|
}, {
|
|
2789
|
-
key: "
|
|
2790
|
-
value: function
|
|
2791
|
-
|
|
2897
|
+
key: "_addExpiresAtToResponseData",
|
|
2898
|
+
value: function _addExpiresAtToResponseData(responseData, callTimestamp) {
|
|
2899
|
+
if (!responseData) {
|
|
2900
|
+
return responseData;
|
|
2901
|
+
}
|
|
2792
2902
|
|
|
2793
|
-
var
|
|
2794
|
-
|
|
2795
|
-
out[serializedKey] = null;
|
|
2903
|
+
var updatedResponseData = _objectSpread2(_objectSpread2({}, responseData), {}, {
|
|
2904
|
+
expires_at: null
|
|
2796
2905
|
});
|
|
2797
|
-
Object.keys(_classPrivateFieldGet(this, _relationList)).forEach(function (serializedKey) {
|
|
2798
|
-
var relation = _classPrivateFieldGet(_this3, _relationList)[serializedKey];
|
|
2799
2906
|
|
|
2800
|
-
|
|
2801
|
-
|
|
2907
|
+
if (typeof updatedResponseData.expires_in !== 'undefined' && updatedResponseData.expires_in !== null // && updatedResponseData.expires_in >= 0
|
|
2908
|
+
) {
|
|
2909
|
+
updatedResponseData.expires_at = Math.round(callTimestamp / 1000) + updatedResponseData.expires_in;
|
|
2802
2910
|
}
|
|
2803
|
-
|
|
2804
|
-
return
|
|
2911
|
+
|
|
2912
|
+
return updatedResponseData;
|
|
2805
2913
|
}
|
|
2806
|
-
}
|
|
2914
|
+
}, {
|
|
2915
|
+
key: "generateToken",
|
|
2916
|
+
value: function generateToken(parameters) {
|
|
2917
|
+
var _this2 = this;
|
|
2807
2918
|
|
|
2808
|
-
|
|
2809
|
-
|
|
2919
|
+
var callTimestamp = Date.now();
|
|
2920
|
+
return _classPrivateFieldGet(this, _tokenGenerator).generateToken(parameters).then(this.handleTokenResponse).then(function (body) {
|
|
2921
|
+
var updatedResponseData = _this2._addExpiresAtToResponseData(body, callTimestamp);
|
|
2810
2922
|
|
|
2811
|
-
|
|
2923
|
+
return _this2._storeAccessToken(updatedResponseData).then(function () {
|
|
2924
|
+
_classPrivateFieldSet(_this2, _hasATokenBeenGenerated, true);
|
|
2812
2925
|
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2926
|
+
return updatedResponseData;
|
|
2927
|
+
});
|
|
2928
|
+
});
|
|
2929
|
+
}
|
|
2930
|
+
}, {
|
|
2931
|
+
key: "refreshToken",
|
|
2932
|
+
value: function refreshToken() {
|
|
2933
|
+
var _this3 = this;
|
|
2819
2934
|
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
* @param {string} serializedKey the key returned from your API
|
|
2825
|
-
* @param {string|null} attributeName the name in your entity, default to the `serializedKey` attribute
|
|
2826
|
-
*/
|
|
2827
|
-
function Relation(type, targetMetadataKey, serializedKey) {
|
|
2828
|
-
var attributeName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
|
|
2935
|
+
return _classPrivateFieldGet(this, _asyncStorage).getItem(this.accessTokenKey).then(function (token) {
|
|
2936
|
+
var callTimestamp = Date.now();
|
|
2937
|
+
return _classPrivateFieldGet(_this3, _tokenGenerator).refreshToken(token ? JSON.parse(token) : null).then(_this3.handleTokenResponse).then(function (body) {
|
|
2938
|
+
var updatedResponseData = _this3._addExpiresAtToResponseData(body, callTimestamp);
|
|
2829
2939
|
|
|
2830
|
-
|
|
2940
|
+
return _this3._storeAccessToken(updatedResponseData).then(function () {
|
|
2941
|
+
return updatedResponseData;
|
|
2942
|
+
});
|
|
2943
|
+
});
|
|
2944
|
+
});
|
|
2945
|
+
}
|
|
2946
|
+
}, {
|
|
2947
|
+
key: "handleTokenResponse",
|
|
2948
|
+
value: function handleTokenResponse(responseOrBody) {
|
|
2949
|
+
if (isResponse(responseOrBody)) {
|
|
2950
|
+
var response = responseOrBody;
|
|
2951
|
+
var originalResponse = response.clone();
|
|
2831
2952
|
|
|
2832
|
-
|
|
2953
|
+
if (_classPrivateFieldGet(this, _logger)) {
|
|
2954
|
+
_classPrivateFieldGet(this, _logger).logResponse(response);
|
|
2955
|
+
}
|
|
2833
2956
|
|
|
2834
|
-
|
|
2957
|
+
return response.json().then(function (body) {
|
|
2958
|
+
if (isOauthError(body)) {
|
|
2959
|
+
// throw error if response body is an oauth error
|
|
2960
|
+
manageOauthError(body, response);
|
|
2961
|
+
} else if (response.status >= 400) {
|
|
2962
|
+
// throw an error if response status code is an "error" status code
|
|
2963
|
+
throw getHttpErrorFromResponse(originalResponse);
|
|
2964
|
+
}
|
|
2835
2965
|
|
|
2836
|
-
|
|
2966
|
+
return body;
|
|
2967
|
+
})["catch"](function (err) {
|
|
2968
|
+
if (!(err instanceof OauthError)) {
|
|
2969
|
+
throw new OauthError(err.type, getHttpErrorFromResponse(originalResponse));
|
|
2970
|
+
}
|
|
2837
2971
|
|
|
2838
|
-
|
|
2972
|
+
throw err;
|
|
2973
|
+
});
|
|
2974
|
+
}
|
|
2839
2975
|
|
|
2840
|
-
|
|
2841
|
-
this.targetMetadataKey = targetMetadataKey;
|
|
2842
|
-
this.serializedKey = serializedKey;
|
|
2843
|
-
this.attributeName = attributeName || this.serializedKey;
|
|
2844
|
-
}
|
|
2976
|
+
var body = responseOrBody;
|
|
2845
2977
|
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
key: "isOneToMany",
|
|
2853
|
-
value: function isOneToMany() {
|
|
2854
|
-
return this.type === Relation.ONE_TO_MANY;
|
|
2855
|
-
}
|
|
2856
|
-
}, {
|
|
2857
|
-
key: "isManyToOne",
|
|
2858
|
-
value: function isManyToOne() {
|
|
2859
|
-
return this.type === Relation.MANY_TO_ONE;
|
|
2860
|
-
}
|
|
2861
|
-
}, {
|
|
2862
|
-
key: "isManyToMany",
|
|
2863
|
-
value: function isManyToMany() {
|
|
2864
|
-
return this.type === Relation.MANY_TO_MANY;
|
|
2978
|
+
if (isOauthError(body)) {
|
|
2979
|
+
// throw error if response body is an oauth error
|
|
2980
|
+
manageOauthError(body);
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
return Promise.resolve(body);
|
|
2865
2984
|
}
|
|
2985
|
+
/**
|
|
2986
|
+
* Return the number of second when the token will expire
|
|
2987
|
+
* return value can be negative if the token is already expired
|
|
2988
|
+
*/
|
|
2989
|
+
|
|
2866
2990
|
}, {
|
|
2867
|
-
key: "
|
|
2868
|
-
value: function
|
|
2869
|
-
return this.
|
|
2991
|
+
key: "getCurrentTokenExpiresIn",
|
|
2992
|
+
value: function getCurrentTokenExpiresIn() {
|
|
2993
|
+
return this.getAccessTokenObject().then(function (accessTokenObject) {
|
|
2994
|
+
if (accessTokenObject === null) {
|
|
2995
|
+
throw new Error('No token has been stored.');
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
if (typeof accessTokenObject.expires_at === 'undefined' || accessTokenObject.expires_at === null) {
|
|
2999
|
+
return null;
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
var now = Math.round(Date.now() / 1000);
|
|
3003
|
+
return accessTokenObject.expires_at - now;
|
|
3004
|
+
});
|
|
2870
3005
|
}
|
|
2871
3006
|
}, {
|
|
2872
|
-
key: "
|
|
2873
|
-
value: function
|
|
2874
|
-
return this.
|
|
3007
|
+
key: "_storeAccessToken",
|
|
3008
|
+
value: function _storeAccessToken(responseData) {
|
|
3009
|
+
return _classPrivateFieldGet(this, _asyncStorage).setItem(this.accessTokenKey, JSON.stringify(responseData));
|
|
2875
3010
|
}
|
|
2876
3011
|
}]);
|
|
2877
3012
|
|
|
2878
|
-
return
|
|
3013
|
+
return TokenStorage;
|
|
2879
3014
|
}();
|
|
2880
3015
|
|
|
2881
|
-
_defineProperty(Relation, "ONE_TO_ONE", RelationTypes.ONE_TO_ONE);
|
|
2882
|
-
|
|
2883
|
-
_defineProperty(Relation, "ONE_TO_MANY", RelationTypes.ONE_TO_MANY);
|
|
2884
|
-
|
|
2885
|
-
_defineProperty(Relation, "MANY_TO_ONE", RelationTypes.MANY_TO_ONE);
|
|
2886
|
-
|
|
2887
|
-
_defineProperty(Relation, "MANY_TO_MANY", RelationTypes.MANY_TO_MANY);
|
|
2888
|
-
|
|
2889
3016
|
export default RestClientSdk;
|
|
2890
|
-
export { AbstractClient, AbstractTokenGenerator, Attribute, AuthorizationCodeFlowTokenGenerator, BadRequestError, ClassMetadata, ClientCredentialsGenerator, ConflictError, ForbiddenError, HttpError, InternalServerError, InvalidGrantError, InvalidScopeError, Mapping, OauthError, PasswordGenerator, ProvidedTokenGenerator, Relation, ResourceNotFoundError, Serializer, TokenStorage, UnauthorizedError };
|
|
3017
|
+
export { AbstractClient, AbstractTokenGenerator, Attribute, AuthorizationCodeFlowTokenGenerator, BadRequestError, ClassMetadata, ClientCredentialsGenerator, ConflictError, ForbiddenError, HttpError, InternalServerError, InvalidGrantError, InvalidScopeError, Logger, Mapping, OauthError, PasswordGenerator, ProvidedTokenGenerator, Relation, ResourceNotFoundError, Serializer, TokenStorage, UnauthorizedError };
|
|
2891
3018
|
//# sourceMappingURL=index.es.js.map
|