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