@sambath999/localize-token 12.4.6 → 12.4.8

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.
@@ -668,8 +668,11 @@
668
668
  };
669
669
  LocalizeTokenService.prototype.tokensValid = function () {
670
670
  var _a, _b;
671
- return !!((_a = this.refreshToken) === null || _a === void 0 ? void 0 : _a.length)
672
- && (!this.config.tenantToken || !!((_b = this.tenantToken) === null || _b === void 0 ? void 0 : _b.length));
671
+ if (!((_a = this.refreshToken) === null || _a === void 0 ? void 0 : _a.length))
672
+ return false;
673
+ if (this.config.requiredTenant && !((_b = this.tenantToken) === null || _b === void 0 ? void 0 : _b.length))
674
+ return false;
675
+ return true;
673
676
  };
674
677
  Object.defineProperty(LocalizeTokenService.prototype, "decodeRefreshToken", {
675
678
  get: function () {
@@ -1069,8 +1072,8 @@
1069
1072
  });
1070
1073
  });
1071
1074
  };
1072
- LocalizeApiHelper.prototype.createRequest = function (instance, method, url, body, options) {
1073
- var request$ = instance.client.request(method, url, Object.assign(Object.assign({}, options), { body: body, observe: 'response' })).pipe(rxjs.takeUntil(instance.destroy$()), rxjs.catchError(function (error) {
1075
+ LocalizeApiHelper.prototype.createRequest = function (instance, method, url, body, options, onInvalidResponseBody) {
1076
+ var request$ = instance.client.request(method, url, Object.assign(Object.assign({}, options), { body: body })).pipe(rxjs.takeUntil(instance.destroy$()), rxjs.map(function (body) { return extractJsonFromResponse(body, onInvalidResponseBody); }), rxjs.catchError(function (error) {
1074
1077
  // Convert to a non-observable error to handle in the promise
1075
1078
  return rxjs.throwError(function () { return error; });
1076
1079
  }));
@@ -1145,6 +1148,27 @@
1145
1148
  };
1146
1149
  return LocalizeApiHelper;
1147
1150
  }());
1151
+ function extractJsonFromResponse(body, onError) {
1152
+ // If already an object, just return
1153
+ if (typeof body === 'object' && body !== null)
1154
+ return body;
1155
+ // If it's a string, try to extract JSON portion
1156
+ if (typeof body === 'string') {
1157
+ var firstBrace = body.indexOf('{');
1158
+ if (firstBrace !== -1) {
1159
+ var jsonString = body.substring(firstBrace);
1160
+ try {
1161
+ return JSON.parse(jsonString);
1162
+ }
1163
+ catch (e) {
1164
+ // If parsing fails, return original string
1165
+ onError === null || onError === void 0 ? void 0 : onError(body);
1166
+ return body;
1167
+ }
1168
+ }
1169
+ }
1170
+ return body;
1171
+ }
1148
1172
  var ApiHelper = new LocalizeApiHelper();
1149
1173
 
1150
1174
  var SCHEMES = LocalizeToken.httpHeaders;
@@ -1251,7 +1275,7 @@
1251
1275
  var _this = this;
1252
1276
  return __generator(this, function (_e) {
1253
1277
  switch (_e.label) {
1254
- case 0: return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 100)];
1278
+ case 0: return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 500)];
1255
1279
  case 1:
1256
1280
  _e.sent();
1257
1281
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onPrepareRequest)];
@@ -1313,6 +1337,7 @@
1313
1337
  });
1314
1338
  };
1315
1339
  LocalizeApiService.prototype.performRequest = function (options) {
1340
+ var _a;
1316
1341
  return __awaiter(this, void 0, void 0, function () {
1317
1342
  var buildOptions, request$, response;
1318
1343
  var _this = this;
@@ -1323,10 +1348,10 @@
1323
1348
  request$ = ApiHelper.createRequest({
1324
1349
  client: this.httpClient,
1325
1350
  destroy$: function () { return _this.destroy$; }
1326
- }, options.method, options.requestUrl, options.requestBody, buildOptions);
1351
+ }, options.method, options.requestUrl, options.requestBody, buildOptions, (_a = this.config.onResponseBodyInvalid) === null || _a === void 0 ? void 0 : _a.bind(this));
1327
1352
  // Set the isRequesting state to true before making the request
1328
1353
  this.isRequestingSubject.next(true);
1329
- return [4 /*yield*/, new Promise(function (resolve, reject) { return request$.subscribe({ next: function (res) { return resolve(res.body); }, error: reject }); })];
1354
+ return [4 /*yield*/, new Promise(function (resolve, reject) { return request$.subscribe({ next: resolve, error: reject }); })];
1330
1355
  case 1:
1331
1356
  response = _e.sent();
1332
1357
  // Reset the isRequesting state after the request completes
@@ -1364,7 +1389,7 @@
1364
1389
  case 5:
1365
1390
  // Handle the error, log it
1366
1391
  _e.sent();
1367
- return [3 /*break*/, 7];
1392
+ throw error_2;
1368
1393
  case 6:
1369
1394
  // Reset the revoking token state
1370
1395
  this.isRevokingToken = false;
@@ -1397,10 +1422,8 @@
1397
1422
  switch (_e.label) {
1398
1423
  case 0:
1399
1424
  if (!this.isRevokingToken) return [3 /*break*/, 2];
1400
- // console.warn('A token refresh is in progress. Request is waiting.');
1401
1425
  return [4 /*yield*/, waitUntil(function () { return !_this.isRevokingToken; })];
1402
1426
  case 1:
1403
- // console.warn('A token refresh is in progress. Request is waiting.');
1404
1427
  _e.sent();
1405
1428
  _e.label = 2;
1406
1429
  case 2:
@@ -1462,7 +1485,7 @@
1462
1485
  };
1463
1486
  LocalizeApiService.prototype.validateConfig = function () {
1464
1487
  var _a, _b, _c, _d;
1465
- if (this.localizeTokenService.config.tenantToken
1488
+ if (this.localizeTokenService.config.requiredTenant
1466
1489
  && !((_b = (_a = this.localizeTokenService.config.tenantToken) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.trim().length)) {
1467
1490
  throw Error('Tenant token is required but tenantTokenName is not configured');
1468
1491
  }