@sambath999/localize-token 12.3.3 → 12.3.5

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.
@@ -482,19 +482,8 @@
482
482
  var LocalizeToken = /** @class */ (function () {
483
483
  function LocalizeToken() {
484
484
  }
485
- LocalizeToken.init = function (config) {
486
- LocalizeToken.config = Object.assign(Object.assign({}, LocalizeToken.config), config);
487
- };
488
485
  return LocalizeToken;
489
486
  }());
490
- LocalizeToken.config = {
491
- mainDomain: extractMainDomain(),
492
- authTokenName: 'auth-token',
493
- refreshTokenName: '_lze_rftkp_',
494
- needTenant: false,
495
- isProduction: false,
496
- revokeTokenUrl: ''
497
- };
498
487
  LocalizeToken.storage = new LocalizeTokenStorage();
499
488
  LocalizeToken.httpHeaders = {
500
489
  AUTHORIZATION: 'Authorization',
@@ -576,13 +565,33 @@
576
565
 
577
566
  var LocalizeTokenService = /** @class */ (function () {
578
567
  function LocalizeTokenService() {
568
+ this.configSubject = new rxjs.BehaviorSubject({});
579
569
  this.isRevokingTokenSubject = new rxjs.BehaviorSubject(false);
570
+ this.defaultConfig = {
571
+ mainDomain: extractMainDomain(),
572
+ authTokenName: 'auth-token',
573
+ refreshTokenName: '_lze_rftkp_',
574
+ };
575
+ this.isInitialized = false;
580
576
  this.decodeToken = function (token) { return jwt_decode__namespace.jwtDecode(token); };
581
- if (!LocalizeToken.config.authTokenName || !LocalizeToken.config.refreshTokenName) {
582
- throw new Error('authTokenName and refreshTokenName must be defined in LocalizeToken.config');
583
- }
584
- this.config = LocalizeToken.config;
585
577
  }
578
+ Object.defineProperty(LocalizeTokenService.prototype, "config", {
579
+ get: function () {
580
+ this.throwIfNotInitialized();
581
+ return this.configSubject.value;
582
+ },
583
+ enumerable: false,
584
+ configurable: true
585
+ });
586
+ LocalizeTokenService.prototype.init = function (config) {
587
+ console.log('LocalizeTokenService is initialized.');
588
+ this.configSubject.next(Object.assign(Object.assign({}, this.defaultConfig), config));
589
+ this.isInitialized = true;
590
+ };
591
+ LocalizeTokenService.prototype.ngOnDestroy = function () {
592
+ // this.configSubject.complete();
593
+ // this.isRevokingTokenSubject.complete();
594
+ };
586
595
  Object.defineProperty(LocalizeTokenService.prototype, "authToken", {
587
596
  get: function () { return this.storageGet(); },
588
597
  set: function (value) {
@@ -601,9 +610,7 @@
601
610
  Object.defineProperty(LocalizeTokenService.prototype, "accessToken", {
602
611
  get: function () { var _a; return (_a = this.authToken) === null || _a === void 0 ? void 0 : _a.token; },
603
612
  set: function (value) {
604
- if (value) {
605
- this.authToken = { token: value, revoke: false };
606
- }
613
+ value && (this.authToken = { token: value, revoke: false });
607
614
  },
608
615
  enumerable: false,
609
616
  configurable: true
@@ -612,9 +619,7 @@
612
619
  get: function () { return this.isRevokingTokenSubject.value; /* this.authToken?.revoke ?? false */ },
613
620
  set: function (value) {
614
621
  this.isRevokingTokenSubject.next(value);
615
- if (this.authToken) {
616
- this.authToken = Object.assign(Object.assign({}, this.authToken), { revoke: value });
617
- }
622
+ this.authToken && (this.authToken = Object.assign(Object.assign({}, this.authToken), { revoke: value }));
618
623
  },
619
624
  enumerable: false,
620
625
  configurable: true
@@ -646,20 +651,22 @@
646
651
  Object.defineProperty(LocalizeTokenService.prototype, "decodeRefreshToken", {
647
652
  get: function () {
648
653
  var token = LocalizeToken.storage.get(this.config.refreshTokenName);
649
- if (!token)
650
- return null;
651
- return this.decodeToken(token);
654
+ return !token ? null : this.decodeToken(token);
652
655
  },
653
656
  enumerable: false,
654
657
  configurable: true
655
658
  });
659
+ LocalizeTokenService.prototype.throwIfNotInitialized = function () {
660
+ if (!this.isInitialized) {
661
+ throw new Error('LocalizeTokenService is not initialized. Call init() method before using it.');
662
+ }
663
+ };
656
664
  return LocalizeTokenService;
657
665
  }());
658
666
  LocalizeTokenService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function LocalizeTokenService_Factory() { return new LocalizeTokenService(); }, token: LocalizeTokenService, providedIn: "root" });
659
667
  LocalizeTokenService.decorators = [
660
668
  { type: i0.Injectable, args: [{ providedIn: 'root' },] }
661
- ];
662
- LocalizeTokenService.ctorParameters = function () { return []; };
669
+ ];
663
670
 
664
671
  /**
665
672
  * Http method options
@@ -838,15 +845,6 @@
838
845
  });
839
846
  });
840
847
  };
841
- LocalizeApiHelper.prototype.validateConfig = function (config) {
842
- var _a;
843
- if (LocalizeToken.config.needTenant && !((_a = config.tenantTokenName) === null || _a === void 0 ? void 0 : _a.trim().length)) {
844
- throw Error('Tenant token is required but tenantTokenName is not configured');
845
- }
846
- if (!LocalizeToken.config.revokeTokenUrl.trim().length) {
847
- throw Error('Revoke token URL is not configured - token refresh will not work');
848
- }
849
- };
850
848
  LocalizeApiHelper.prototype.screenBlocker = function (optons, error, add) {
851
849
  if (add === void 0) { add = true; }
852
850
  var _a, _b, _c, _d;
@@ -892,10 +890,10 @@
892
890
  this.httpClient = httpClient;
893
891
  this.localizeTokenService = localizeTokenService;
894
892
  this.destroy$ = new rxjs.Subject();
893
+ this.configSubject = new rxjs.BehaviorSubject({});
895
894
  this.isRequestingSubject = new rxjs.BehaviorSubject(false);
896
- this.isResolvedStartupSubject = new rxjs.BehaviorSubject(false);
897
- this.needTenant = LocalizeToken.config.needTenant;
898
- this.config = {
895
+ this.isResolvingStartupSubject = new rxjs.BehaviorSubject(false);
896
+ this.defaultConfig = {
899
897
  waitEachRequest: { milliseconds: 0 },
900
898
  enableRequestCancellation: true,
901
899
  retryOptions: {
@@ -920,8 +918,13 @@
920
918
  return _this.request(baseUrl, path, method, reqBody, reqHeaders);
921
919
  }; };
922
920
  }
923
- Object.defineProperty(LocalizeApiService.prototype, "isResolvedStartup", {
924
- get: function () { return this.isResolvedStartupSubject.value; },
921
+ Object.defineProperty(LocalizeApiService.prototype, "isResolvingStartup", {
922
+ get: function () { return this.isResolvingStartupSubject.value; },
923
+ enumerable: false,
924
+ configurable: true
925
+ });
926
+ Object.defineProperty(LocalizeApiService.prototype, "needTenant", {
927
+ get: function () { return this.localizeTokenService.config.needTenant; },
925
928
  enumerable: false,
926
929
  configurable: true
927
930
  });
@@ -952,13 +955,21 @@
952
955
  enumerable: false,
953
956
  configurable: true
954
957
  });
958
+ Object.defineProperty(LocalizeApiService.prototype, "config", {
959
+ get: function () {
960
+ this.validateConfig(this.config);
961
+ return this.configSubject.value;
962
+ },
963
+ enumerable: false,
964
+ configurable: true
965
+ });
955
966
  /**
956
967
  * Initialize the API service.
957
968
  * @param apiConfigs - The API configurations.
958
969
  */
959
970
  LocalizeApiService.prototype.init = function (apiConfigs) {
960
- this.config = Object.assign(Object.assign({}, this.config), apiConfigs);
961
- ApiHelper.validateConfig(this.config);
971
+ console.log('LocalizeApiService is initialized.');
972
+ this.configSubject.next(Object.assign(Object.assign({}, this.defaultConfig), apiConfigs));
962
973
  };
963
974
  LocalizeApiService.prototype.cancelPendingRequests = function () {
964
975
  this.config.enableRequestCancellation
@@ -967,31 +978,38 @@
967
978
  LocalizeApiService.prototype.ngOnDestroy = function () {
968
979
  this.destroy$.next();
969
980
  this.destroy$.complete();
981
+ // this.isResolvingStartupSubject.complete();
982
+ // this.isRequestingSubject.complete();
983
+ // this.configSubject.complete();
970
984
  };
971
985
  LocalizeApiService.prototype.request = function (baseUrl, path, method, reqBody, reqHeaders) {
972
986
  if (method === void 0) { method = exports.EMethod.GET; }
973
987
  if (reqBody === void 0) { reqBody = null; }
974
988
  return __awaiter(this, void 0, void 0, function () {
975
989
  var apiOptions, error_1;
990
+ var _this = this;
976
991
  return __generator(this, function (_b) {
977
992
  switch (_b.label) {
978
- case 0: return [4 /*yield*/, ApiHelper.invokeHook(this.config.onPrepareRequest)];
993
+ case 0: return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 500)];
979
994
  case 1:
980
995
  _b.sent();
981
- apiOptions = this.buildApiOptions(baseUrl, path, method, reqBody, reqHeaders);
982
- _b.label = 2;
996
+ return [4 /*yield*/, ApiHelper.invokeHook(this.config.onPrepareRequest)];
983
997
  case 2:
984
- _b.trys.push([2, 5, , 7]);
985
- return [4 /*yield*/, this.toWaitForPreviousRequest()];
998
+ _b.sent();
999
+ apiOptions = this.buildApiOptions(baseUrl, path, method, reqBody, reqHeaders);
1000
+ _b.label = 3;
986
1001
  case 3:
1002
+ _b.trys.push([3, 6, , 8]);
1003
+ return [4 /*yield*/, this.toWaitForPreviousRequest()];
1004
+ case 4:
987
1005
  _b.sent();
988
1006
  return [4 /*yield*/, ApiHelper.performRequestWithRetry(apiOptions, this.config, this.performRequest.bind(this))];
989
- case 4: return [2 /*return*/, _b.sent()];
990
- case 5:
1007
+ case 5: return [2 /*return*/, _b.sent()];
1008
+ case 6:
991
1009
  error_1 = _b.sent();
992
1010
  return [4 /*yield*/, this.handleOnRequestError(error_1, apiOptions)];
993
- case 6: return [2 /*return*/, _b.sent()];
994
- case 7: return [2 /*return*/];
1011
+ case 7: return [2 /*return*/, _b.sent()];
1012
+ case 8: return [2 /*return*/];
995
1013
  }
996
1014
  });
997
1015
  });
@@ -1002,8 +1020,11 @@
1002
1020
  return __generator(this, function (_b) {
1003
1021
  switch (_b.label) {
1004
1022
  case 0:
1005
- if (error.status !== 401 || this.isResolvedStartup)
1023
+ if (error.status !== 401)
1006
1024
  throw error;
1025
+ return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 500)];
1026
+ case 1:
1027
+ _b.sent();
1007
1028
  return [4 /*yield*/, ApiHelper.performRetry({
1008
1029
  maxRetries: function () { return 1000; },
1009
1030
  delay: 500,
@@ -1025,7 +1046,7 @@
1025
1046
  });
1026
1047
  }); }
1027
1048
  })];
1028
- case 1: return [2 /*return*/, _b.sent()];
1049
+ case 2: return [2 /*return*/, _b.sent()];
1029
1050
  }
1030
1051
  });
1031
1052
  });
@@ -1066,7 +1087,7 @@
1066
1087
  if (_b.sent())
1067
1088
  return [2 /*return*/];
1068
1089
  this.isRevokingToken = true;
1069
- apiOptions = Object.assign(Object.assign({}, this.buildApiOptions(LocalizeToken.config.revokeTokenUrl)), { refreshToken: true });
1090
+ apiOptions = Object.assign(Object.assign({}, this.buildApiOptions(this.localizeTokenService.config.revokeTokenUrl)), { refreshToken: true });
1070
1091
  return [4 /*yield*/, ApiHelper.performRequestWithRetry(apiOptions, this.config, this.performRequest.bind(this))];
1071
1092
  case 2:
1072
1093
  revokeToken = _b.sent();
@@ -1184,6 +1205,15 @@
1184
1205
  });
1185
1206
  });
1186
1207
  };
1208
+ LocalizeApiService.prototype.validateConfig = function (config) {
1209
+ var _a;
1210
+ if (this.localizeTokenService.config.needTenant && !((_a = config.tenantTokenName) === null || _a === void 0 ? void 0 : _a.trim().length)) {
1211
+ throw Error('Tenant token is required but tenantTokenName is not configured');
1212
+ }
1213
+ if (!this.localizeTokenService.config.revokeTokenUrl.trim().length) {
1214
+ throw Error('Revoke token URL is not configured - token refresh will not work');
1215
+ }
1216
+ };
1187
1217
  return LocalizeApiService;
1188
1218
  }());
1189
1219
  LocalizeApiService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function LocalizeApiService_Factory() { return new LocalizeApiService(i0__namespace.ɵɵinject(i1__namespace.HttpClient), i0__namespace.ɵɵinject(LocalizeTokenService)); }, token: LocalizeApiService, providedIn: "root" });
@@ -1224,12 +1254,16 @@
1224
1254
  this.messageKey = "$login-dlg";
1225
1255
  this.loading = false;
1226
1256
  this.success = false;
1227
- this.config = LocalizeToken.config;
1228
1257
  this.clickLogout = function () { var _a; return (_a = _this.logout) === null || _a === void 0 ? void 0 : _a.call(_this); };
1229
1258
  this.decodeToken = this.tokenService.decodeRefreshToken;
1230
1259
  this.loginConfig = this.dlgConfig.data.loginConfig;
1231
1260
  this.properties = this.loginConfig.properties;
1232
1261
  }
1262
+ Object.defineProperty(LocalizeLogindlgComponent.prototype, "config", {
1263
+ get: function () { return this.tokenService.config; },
1264
+ enumerable: false,
1265
+ configurable: true
1266
+ });
1233
1267
  LocalizeLogindlgComponent.prototype.ngOnInit = function () {
1234
1268
  var _this = this;
1235
1269
  this.dlgConfig.closable = false;