@emilgroup/public-api-sdk 1.40.1-beta.27 → 1.40.1-beta.29

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/README.md CHANGED
@@ -17,11 +17,11 @@ Although this package can be used in both TypeScript and JavaScript, it is inten
17
17
  Navigate to the folder of your consuming project and run one of the following commands:
18
18
 
19
19
  ```
20
- npm install @emilgroup/public-api-sdk@1.40.1-beta.27 --save
20
+ npm install @emilgroup/public-api-sdk@1.40.1-beta.29 --save
21
21
  ```
22
22
  or
23
23
  ```
24
- yarn add @emilgroup/public-api-sdk@1.40.1-beta.27
24
+ yarn add @emilgroup/public-api-sdk@1.40.1-beta.29
25
25
  ```
26
26
 
27
27
  And then you can import `PublicApi`.
package/base.ts CHANGED
@@ -72,8 +72,9 @@ export enum Environment {
72
72
  let _retry_count = 0
73
73
  let _retry = null
74
74
 
75
- // Refreshes in flight per account (BaseAPI.shareRefresh).
76
- const refreshesInFlight = new Map<string, Promise<LoginClass>>();
75
+ // Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
76
+ // account refresh and resend it, never an object of another account open in the same browser.
77
+ const REQUEST_OWNER = '_emilSdkOwner';
77
78
 
78
79
  export function resetRetry() {
79
80
  _retry_count = 0
@@ -345,10 +346,7 @@ export class BaseAPI {
345
346
  const { username } = this.configuration;
346
347
 
347
348
  if (!username) {
348
- // This object cannot refresh at all, so the session it relied on is over for it.
349
- const cannotRefresh: any = new Error('Failed to refresh token.');
350
- cannotRefresh.sessionExpired = true;
351
- throw cannotRefresh;
349
+ throw new Error('Failed to refresh token.');
352
350
  }
353
351
 
354
352
  const refreshTokenAxios = globalAxios.create()
@@ -419,10 +417,6 @@ export class BaseAPI {
419
417
  this.storeTokenData(null);
420
418
  }
421
419
 
422
- // Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
423
- // account refresh and resend it, never an object of another account in the same app.
424
- private static readonly REQUEST_OWNER = '_emilSdkOwner';
425
-
426
420
  private ownAccount(): string {
427
421
  return (this.configuration.username || '').toLowerCase();
428
422
  }
@@ -432,70 +426,7 @@ export class BaseAPI {
432
426
  if (!this.ownAccount()) {
433
427
  return { ...options };
434
428
  }
435
- return { ...options, [BaseAPI.REQUEST_OWNER]: this.ownAccount() } as AxiosRequestConfig;
436
- }
437
-
438
- // Sent for another account: only that account's SDK objects handle it. Unmarked requests as before.
439
- private sentByOtherAccount(config?: AxiosRequestConfig): boolean {
440
- const owner = (config as Record<string, unknown> | undefined)?.[BaseAPI.REQUEST_OWNER];
441
- return owner !== undefined && owner !== this.ownAccount();
442
- }
443
-
444
-
445
- // One refresh per account; an object without a username keeps its own, which fails as before.
446
- private sharedRefresh(): Promise<LoginClass> {
447
- const account = this.ownAccount();
448
- if (!account) {
449
- return this.refreshTokenInternal();
450
- }
451
- return BaseAPI.shareRefresh(refreshesInFlight, account, () => this.refreshTokenInternal());
452
- }
453
-
454
- private refreshRunning(): boolean {
455
- const account = this.ownAccount();
456
- return !!account && refreshesInFlight.has(account);
457
- }
458
-
459
- // Joins the refresh already in flight for this key, or starts one: calls refused at the same time wait for
460
- // one refresh instead of each starting their own (a refresh token may be single-use). The entry is removed
461
- // when the refresh settles.
462
- private static shareRefresh<K, T>(inFlight: Map<K, Promise<T>>, key: K, start: () => Promise<T>): Promise<T> {
463
- const running = inFlight.get(key);
464
- if (running) {
465
- return running;
466
- }
467
- const refresh = start();
468
- inFlight.set(key, refresh);
469
- const settled = () => { inFlight.delete(key); };
470
- refresh.then(settled, settled);
471
- return refresh;
472
- }
473
-
474
- // A refused call that joins a refresh already running is not a new try: only started refreshes count toward
475
- // the limit, so every call refused at the same time is retried.
476
- private mayRefreshAfterNetworkError(): boolean {
477
- if (this.refreshRunning()) {
478
- return true;
479
- }
480
- if (_retry_count >= 4) {
481
- return false;
482
- }
483
- _retry_count++;
484
- return true;
485
- }
486
-
487
-
488
- // Rejects as before (the refresh's response body, or its error). Marks it as an ended session only when the
489
- // refresh was refused (401 or 403) or could not run (no username, marked where it is thrown); a server error
490
- // or a network error says nothing about the session, so an app must not log the user out for it. A body that
491
- // is not an object cannot carry the mark and is kept as it is.
492
- private rejectEndedSession(error: any): Promise<never> {
493
- const rejection = error.response?.data || error;
494
- const status = error.response?.status;
495
- if (typeof rejection === 'object' && (status === 401 || status === 403)) {
496
- rejection.sessionExpired = true;
497
- }
498
- return Promise.reject(rejection);
429
+ return { ...options, [REQUEST_OWNER]: this.ownAccount() } as AxiosRequestConfig;
499
430
  }
500
431
 
501
432
  private attachInterceptor(axios: AxiosInstance) {
@@ -505,7 +436,9 @@ export class BaseAPI {
505
436
  },
506
437
  async (err) => {
507
438
  let originalConfig = err.config;
508
- if (this.sentByOtherAccount(originalConfig)) {
439
+ // Sent for another account: only that account's SDK objects handle it. Unmarked requests as before.
440
+ const owner = originalConfig?.[REQUEST_OWNER];
441
+ if (owner !== undefined && owner !== this.ownAccount()) {
509
442
  return Promise.reject(err);
510
443
  }
511
444
  if (err.response && !(err.response instanceof XMLHttpRequest)) { // sometimes buggy and is of type request
@@ -514,7 +447,7 @@ export class BaseAPI {
514
447
  && !originalConfig._retry) {
515
448
  originalConfig._retry = true;
516
449
  try {
517
- const reply = await this.sharedRefresh();
450
+ const reply = await this.refreshTokenInternal();
518
451
  this.tokenData.permissions = reply.permissions;
519
452
  this.applyTokenReply(reply, true);
520
453
  this.storeTokenData(this.tokenData);
@@ -531,7 +464,10 @@ export class BaseAPI {
531
464
  return this.retryWithToken(axios, originalConfig);
532
465
  });
533
466
  } catch (_error) {
534
- return this.rejectEndedSession(_error);
467
+ if (_error.response && _error.response.data) {
468
+ return Promise.reject(_error.response.data);
469
+ }
470
+ return Promise.reject(_error);
535
471
  }
536
472
  }
537
473
  } else if (err.message === NETWORK_ERROR_MESSAGE && this.canRetryWithToken(originalConfig)) {
@@ -544,11 +480,12 @@ export class BaseAPI {
544
480
  } else if (err.message === NETWORK_ERROR_MESSAGE
545
481
  && (originalConfig.headers.hasOwnProperty('Authorization')
546
482
  || (this.configuration.cookieAuth && originalConfig.withCredentials && this.canResend(originalConfig)))
547
- && this.mayRefreshAfterNetworkError()
483
+ && _retry_count < 4
548
484
  ) {
485
+ _retry_count++;
549
486
  try {
550
487
  // In session mode an unreadable 401 (no CORS headers on the error reply) also lands here.
551
- const reply = await this.sharedRefresh();
488
+ const reply = await this.refreshTokenInternal();
552
489
  this.tokenData.permissions = reply.permissions;
553
490
 
554
491
  _retry = true;
@@ -564,7 +501,10 @@ export class BaseAPI {
564
501
  return retried;
565
502
  });
566
503
  } catch (_error) {
567
- return this.rejectEndedSession(_error);
504
+ if (_error.response && _error.response.data) {
505
+ return Promise.reject(_error.response.data);
506
+ }
507
+ return Promise.reject(_error);
568
508
  }
569
509
  }
570
510
  return Promise.reject(err);
package/dist/base.d.ts CHANGED
@@ -96,15 +96,8 @@ export declare class BaseAPI {
96
96
  private storeTokenData;
97
97
  loadTokenData(): void;
98
98
  cleanTokenData(): void;
99
- private static readonly REQUEST_OWNER;
100
99
  private ownAccount;
101
100
  protected withOwner(options?: AxiosRequestConfig): AxiosRequestConfig;
102
- private sentByOtherAccount;
103
- private sharedRefresh;
104
- private refreshRunning;
105
- private static shareRefresh;
106
- private mayRefreshAfterNetworkError;
107
- private rejectEndedSession;
108
101
  private attachInterceptor;
109
102
  }
110
103
  /**
package/dist/base.js CHANGED
@@ -117,8 +117,9 @@ var Environment;
117
117
  })(Environment = exports.Environment || (exports.Environment = {}));
118
118
  var _retry_count = 0;
119
119
  var _retry = null;
120
- // Refreshes in flight per account (BaseAPI.shareRefresh).
121
- var refreshesInFlight = new Map();
120
+ // Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
121
+ // account refresh and resend it, never an object of another account open in the same browser.
122
+ var REQUEST_OWNER = '_emilSdkOwner';
122
123
  function resetRetry() {
123
124
  _retry_count = 0;
124
125
  }
@@ -355,16 +356,14 @@ var BaseAPI = /** @class */ (function () {
355
356
  };
356
357
  BaseAPI.prototype.refreshTokenInternal = function () {
357
358
  return __awaiter(this, void 0, void 0, function () {
358
- var username, cannotRefresh, refreshTokenAxios, options, response;
359
+ var username, refreshTokenAxios, options, response;
359
360
  var _this = this;
360
361
  return __generator(this, function (_a) {
361
362
  switch (_a.label) {
362
363
  case 0:
363
364
  username = this.configuration.username;
364
365
  if (!username) {
365
- cannotRefresh = new Error('Failed to refresh token.');
366
- cannotRefresh.sessionExpired = true;
367
- throw cannotRefresh;
366
+ throw new Error('Failed to refresh token.');
368
367
  }
369
368
  refreshTokenAxios = axios_1.default.create();
370
369
  refreshTokenAxios.interceptors.response.use(function (response) {
@@ -440,77 +439,21 @@ var BaseAPI = /** @class */ (function () {
440
439
  if (!this.ownAccount()) {
441
440
  return __assign({}, options);
442
441
  }
443
- return __assign(__assign({}, options), (_a = {}, _a[BaseAPI.REQUEST_OWNER] = this.ownAccount(), _a));
444
- };
445
- // Sent for another account: only that account's SDK objects handle it. Unmarked requests as before.
446
- BaseAPI.prototype.sentByOtherAccount = function (config) {
447
- var owner = config === null || config === void 0 ? void 0 : config[BaseAPI.REQUEST_OWNER];
448
- return owner !== undefined && owner !== this.ownAccount();
449
- };
450
- // One refresh per account; an object without a username keeps its own, which fails as before.
451
- BaseAPI.prototype.sharedRefresh = function () {
452
- var _this = this;
453
- var account = this.ownAccount();
454
- if (!account) {
455
- return this.refreshTokenInternal();
456
- }
457
- return BaseAPI.shareRefresh(refreshesInFlight, account, function () { return _this.refreshTokenInternal(); });
458
- };
459
- BaseAPI.prototype.refreshRunning = function () {
460
- var account = this.ownAccount();
461
- return !!account && refreshesInFlight.has(account);
462
- };
463
- // Joins the refresh already in flight for this key, or starts one: calls refused at the same time wait for
464
- // one refresh instead of each starting their own (a refresh token may be single-use). The entry is removed
465
- // when the refresh settles.
466
- BaseAPI.shareRefresh = function (inFlight, key, start) {
467
- var running = inFlight.get(key);
468
- if (running) {
469
- return running;
470
- }
471
- var refresh = start();
472
- inFlight.set(key, refresh);
473
- var settled = function () { inFlight.delete(key); };
474
- refresh.then(settled, settled);
475
- return refresh;
476
- };
477
- // A refused call that joins a refresh already running is not a new try: only started refreshes count toward
478
- // the limit, so every call refused at the same time is retried.
479
- BaseAPI.prototype.mayRefreshAfterNetworkError = function () {
480
- if (this.refreshRunning()) {
481
- return true;
482
- }
483
- if (_retry_count >= 4) {
484
- return false;
485
- }
486
- _retry_count++;
487
- return true;
488
- };
489
- // Rejects as before (the refresh's response body, or its error). Marks it as an ended session only when the
490
- // refresh was refused (401 or 403) or could not run (no username, marked where it is thrown); a server error
491
- // or a network error says nothing about the session, so an app must not log the user out for it. A body that
492
- // is not an object cannot carry the mark and is kept as it is.
493
- BaseAPI.prototype.rejectEndedSession = function (error) {
494
- var _a, _b;
495
- var rejection = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || error;
496
- var status = (_b = error.response) === null || _b === void 0 ? void 0 : _b.status;
497
- if (typeof rejection === 'object' && (status === 401 || status === 403)) {
498
- rejection.sessionExpired = true;
499
- }
500
- return Promise.reject(rejection);
442
+ return __assign(__assign({}, options), (_a = {}, _a[REQUEST_OWNER] = this.ownAccount(), _a));
501
443
  };
502
444
  BaseAPI.prototype.attachInterceptor = function (axios) {
503
445
  var _this = this;
504
446
  axios.interceptors.response.use(function (res) {
505
447
  return res;
506
448
  }, function (err) { return __awaiter(_this, void 0, void 0, function () {
507
- var originalConfig, reply, _error_1, reply, _error_2;
449
+ var originalConfig, owner, reply, _error_1, reply, _error_2;
508
450
  var _this = this;
509
451
  return __generator(this, function (_a) {
510
452
  switch (_a.label) {
511
453
  case 0:
512
454
  originalConfig = err.config;
513
- if (this.sentByOtherAccount(originalConfig)) {
455
+ owner = originalConfig === null || originalConfig === void 0 ? void 0 : originalConfig[REQUEST_OWNER];
456
+ if (owner !== undefined && owner !== this.ownAccount()) {
514
457
  return [2 /*return*/, Promise.reject(err)];
515
458
  }
516
459
  if (!(err.response && !(err.response instanceof XMLHttpRequest))) return [3 /*break*/, 5];
@@ -520,7 +463,7 @@ var BaseAPI = /** @class */ (function () {
520
463
  _a.label = 1;
521
464
  case 1:
522
465
  _a.trys.push([1, 3, , 4]);
523
- return [4 /*yield*/, this.sharedRefresh()];
466
+ return [4 /*yield*/, this.refreshTokenInternal()];
524
467
  case 2:
525
468
  reply = _a.sent();
526
469
  this.tokenData.permissions = reply.permissions;
@@ -539,7 +482,10 @@ var BaseAPI = /** @class */ (function () {
539
482
  })];
540
483
  case 3:
541
484
  _error_1 = _a.sent();
542
- return [2 /*return*/, this.rejectEndedSession(_error_1)];
485
+ if (_error_1.response && _error_1.response.data) {
486
+ return [2 /*return*/, Promise.reject(_error_1.response.data)];
487
+ }
488
+ return [2 /*return*/, Promise.reject(_error_1)];
543
489
  case 4: return [3 /*break*/, 10];
544
490
  case 5:
545
491
  if (!(err.message === NETWORK_ERROR_MESSAGE && this.canRetryWithToken(originalConfig))) return [3 /*break*/, 6];
@@ -553,11 +499,12 @@ var BaseAPI = /** @class */ (function () {
553
499
  if (!(err.message === NETWORK_ERROR_MESSAGE
554
500
  && (originalConfig.headers.hasOwnProperty('Authorization')
555
501
  || (this.configuration.cookieAuth && originalConfig.withCredentials && this.canResend(originalConfig)))
556
- && this.mayRefreshAfterNetworkError())) return [3 /*break*/, 10];
502
+ && _retry_count < 4)) return [3 /*break*/, 10];
503
+ _retry_count++;
557
504
  _a.label = 7;
558
505
  case 7:
559
506
  _a.trys.push([7, 9, , 10]);
560
- return [4 /*yield*/, this.sharedRefresh()];
507
+ return [4 /*yield*/, this.refreshTokenInternal()];
561
508
  case 8:
562
509
  reply = _a.sent();
563
510
  this.tokenData.permissions = reply.permissions;
@@ -572,15 +519,15 @@ var BaseAPI = /** @class */ (function () {
572
519
  })];
573
520
  case 9:
574
521
  _error_2 = _a.sent();
575
- return [2 /*return*/, this.rejectEndedSession(_error_2)];
522
+ if (_error_2.response && _error_2.response.data) {
523
+ return [2 /*return*/, Promise.reject(_error_2.response.data)];
524
+ }
525
+ return [2 /*return*/, Promise.reject(_error_2)];
576
526
  case 10: return [2 /*return*/, Promise.reject(err)];
577
527
  }
578
528
  });
579
529
  }); });
580
530
  };
581
- // Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
582
- // account refresh and resend it, never an object of another account in the same app.
583
- BaseAPI.REQUEST_OWNER = '_emilSdkOwner';
584
531
  return BaseAPI;
585
532
  }());
586
533
  exports.BaseAPI = BaseAPI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emilgroup/public-api-sdk",
3
- "version": "1.40.1-beta.27",
3
+ "version": "1.40.1-beta.29",
4
4
  "description": "OpenAPI client for @emilgroup/public-api-sdk",
5
5
  "author": "OpenAPI-Generator Contributors",
6
6
  "keywords": [