@etsoo/appscript 1.5.58 → 1.5.60

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.
@@ -219,6 +219,10 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
219
219
  * @returns Result
220
220
  */
221
221
  createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi<any>;
222
+ /**
223
+ * Reset all APIs
224
+ */
225
+ protected resetApis(): void;
222
226
  /**
223
227
  * Update API token and expires
224
228
  * @param name Api name
@@ -631,7 +635,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
631
635
  protected setupTasks(): void;
632
636
  /**
633
637
  * Signout, with userLogout and toLoginPage
634
- * @param apiUrl Signout API URL
635
638
  */
636
639
  signout(): Promise<void>;
637
640
  /**
@@ -14,6 +14,8 @@ const UserRole_1 = require("./UserRole");
14
14
  const AuthApi_1 = require("../erp/AuthApi");
15
15
  let CJ;
16
16
  const loadCrypto = () => import('crypto-js');
17
+ // System API name
18
+ const systemApi = 'system';
17
19
  /**
18
20
  * Core application
19
21
  */
@@ -201,12 +203,12 @@ class CoreApp {
201
203
  if (api) {
202
204
  // Base URL of the API
203
205
  api.baseUrl = this.settings.endpoint;
204
- api.name = 'system';
206
+ api.name = systemApi;
205
207
  this.setApi(api, refresh);
206
208
  this.api = api;
207
209
  }
208
210
  else {
209
- this.api = this.createApi('system', {
211
+ this.api = this.createApi(systemApi, {
210
212
  endpoint: settings.endpoint,
211
213
  webUrl: settings.webUrl
212
214
  }, refresh);
@@ -374,6 +376,15 @@ class CoreApp {
374
376
  this.setApi(api, refresh);
375
377
  return api;
376
378
  }
379
+ /**
380
+ * Reset all APIs
381
+ */
382
+ resetApis() {
383
+ for (const name in this.apis) {
384
+ const data = this.apis[name];
385
+ this.updateApi(data, undefined, -1);
386
+ }
387
+ }
377
388
  updateApi(nameOrData, token, seconds) {
378
389
  const api = typeof nameOrData === 'string' ? this.apis[nameOrData] : nameOrData;
379
390
  if (api == null)
@@ -1476,6 +1487,10 @@ class CoreApp {
1476
1487
  * @returns Result
1477
1488
  */
1478
1489
  async exchangeToken(api, token) {
1490
+ // Avoid to call the system API
1491
+ if (api.name === systemApi) {
1492
+ throw new Error('System API is not allowed to exchange token');
1493
+ }
1479
1494
  // Call the API quietly, no loading bar and no error popup
1480
1495
  const data = await new AuthApi_1.AuthApi(this).exchangeToken({ token }, {
1481
1496
  showLoading: false,
@@ -1508,14 +1523,18 @@ class CoreApp {
1508
1523
  exchangeTokenAll(coreData, coreName) {
1509
1524
  coreName ?? (coreName = 'core');
1510
1525
  for (const name in this.apis) {
1511
- const api = this.apis[name];
1526
+ // Ignore the system API as it has its own logic with refreshToken
1527
+ if (name === systemApi)
1528
+ continue;
1529
+ const data = this.apis[name];
1530
+ const api = data[0];
1512
1531
  // The core API
1513
- if (api[0].name === coreName) {
1514
- api[0].authorize(coreData.tokenType, coreData.accessToken);
1515
- this.updateApi(api, coreData.refreshToken, coreData.expiresIn);
1532
+ if (name === coreName) {
1533
+ api.authorize(coreData.tokenType, coreData.accessToken);
1534
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
1516
1535
  }
1517
1536
  else {
1518
- this.exchangeToken(api[0], coreData.refreshToken);
1537
+ this.exchangeToken(api, coreData.refreshToken);
1519
1538
  }
1520
1539
  }
1521
1540
  }
@@ -1613,11 +1632,12 @@ class CoreApp {
1613
1632
  }
1614
1633
  /**
1615
1634
  * Signout, with userLogout and toLoginPage
1616
- * @param apiUrl Signout API URL
1617
1635
  */
1618
1636
  async signout() {
1619
1637
  // Clear the keep login status
1620
1638
  this.keepLogin = false;
1639
+ // Reset all APIs
1640
+ this.resetApis();
1621
1641
  const token = this.getCacheToken();
1622
1642
  if (token) {
1623
1643
  const result = await new AuthApi_1.AuthApi(this).signout({
@@ -219,6 +219,10 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
219
219
  * @returns Result
220
220
  */
221
221
  createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi<any>;
222
+ /**
223
+ * Reset all APIs
224
+ */
225
+ protected resetApis(): void;
222
226
  /**
223
227
  * Update API token and expires
224
228
  * @param name Api name
@@ -631,7 +635,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
631
635
  protected setupTasks(): void;
632
636
  /**
633
637
  * Signout, with userLogout and toLoginPage
634
- * @param apiUrl Signout API URL
635
638
  */
636
639
  signout(): Promise<void>;
637
640
  /**
@@ -11,6 +11,8 @@ import { UserRole } from './UserRole';
11
11
  import { AuthApi } from '../erp/AuthApi';
12
12
  let CJ;
13
13
  const loadCrypto = () => import('crypto-js');
14
+ // System API name
15
+ const systemApi = 'system';
14
16
  /**
15
17
  * Core application
16
18
  */
@@ -198,12 +200,12 @@ export class CoreApp {
198
200
  if (api) {
199
201
  // Base URL of the API
200
202
  api.baseUrl = this.settings.endpoint;
201
- api.name = 'system';
203
+ api.name = systemApi;
202
204
  this.setApi(api, refresh);
203
205
  this.api = api;
204
206
  }
205
207
  else {
206
- this.api = this.createApi('system', {
208
+ this.api = this.createApi(systemApi, {
207
209
  endpoint: settings.endpoint,
208
210
  webUrl: settings.webUrl
209
211
  }, refresh);
@@ -371,6 +373,15 @@ export class CoreApp {
371
373
  this.setApi(api, refresh);
372
374
  return api;
373
375
  }
376
+ /**
377
+ * Reset all APIs
378
+ */
379
+ resetApis() {
380
+ for (const name in this.apis) {
381
+ const data = this.apis[name];
382
+ this.updateApi(data, undefined, -1);
383
+ }
384
+ }
374
385
  updateApi(nameOrData, token, seconds) {
375
386
  const api = typeof nameOrData === 'string' ? this.apis[nameOrData] : nameOrData;
376
387
  if (api == null)
@@ -1473,6 +1484,10 @@ export class CoreApp {
1473
1484
  * @returns Result
1474
1485
  */
1475
1486
  async exchangeToken(api, token) {
1487
+ // Avoid to call the system API
1488
+ if (api.name === systemApi) {
1489
+ throw new Error('System API is not allowed to exchange token');
1490
+ }
1476
1491
  // Call the API quietly, no loading bar and no error popup
1477
1492
  const data = await new AuthApi(this).exchangeToken({ token }, {
1478
1493
  showLoading: false,
@@ -1505,14 +1520,18 @@ export class CoreApp {
1505
1520
  exchangeTokenAll(coreData, coreName) {
1506
1521
  coreName ?? (coreName = 'core');
1507
1522
  for (const name in this.apis) {
1508
- const api = this.apis[name];
1523
+ // Ignore the system API as it has its own logic with refreshToken
1524
+ if (name === systemApi)
1525
+ continue;
1526
+ const data = this.apis[name];
1527
+ const api = data[0];
1509
1528
  // The core API
1510
- if (api[0].name === coreName) {
1511
- api[0].authorize(coreData.tokenType, coreData.accessToken);
1512
- this.updateApi(api, coreData.refreshToken, coreData.expiresIn);
1529
+ if (name === coreName) {
1530
+ api.authorize(coreData.tokenType, coreData.accessToken);
1531
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
1513
1532
  }
1514
1533
  else {
1515
- this.exchangeToken(api[0], coreData.refreshToken);
1534
+ this.exchangeToken(api, coreData.refreshToken);
1516
1535
  }
1517
1536
  }
1518
1537
  }
@@ -1610,11 +1629,12 @@ export class CoreApp {
1610
1629
  }
1611
1630
  /**
1612
1631
  * Signout, with userLogout and toLoginPage
1613
- * @param apiUrl Signout API URL
1614
1632
  */
1615
1633
  async signout() {
1616
1634
  // Clear the keep login status
1617
1635
  this.keepLogin = false;
1636
+ // Reset all APIs
1637
+ this.resetApis();
1618
1638
  const token = this.getCacheToken();
1619
1639
  if (token) {
1620
1640
  const result = await new AuthApi(this).signout({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.5.58",
3
+ "version": "1.5.60",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -64,6 +64,9 @@ type ApiRefreshTokenFunction = (
64
64
  // API task data
65
65
  type ApiTaskData = [IApi, number, number, ApiRefreshTokenFunction, string?];
66
66
 
67
+ // System API name
68
+ const systemApi = 'system';
69
+
67
70
  /**
68
71
  * Core application interface
69
72
  */
@@ -375,12 +378,12 @@ export abstract class CoreApp<
375
378
  if (api) {
376
379
  // Base URL of the API
377
380
  api.baseUrl = this.settings.endpoint;
378
- api.name = 'system';
381
+ api.name = systemApi;
379
382
  this.setApi(api, refresh);
380
383
  this.api = api;
381
384
  } else {
382
385
  this.api = this.createApi(
383
- 'system',
386
+ systemApi,
384
387
  {
385
388
  endpoint: settings.endpoint,
386
389
  webUrl: settings.webUrl
@@ -617,6 +620,16 @@ export abstract class CoreApp<
617
620
  return api;
618
621
  }
619
622
 
623
+ /**
624
+ * Reset all APIs
625
+ */
626
+ protected resetApis() {
627
+ for (const name in this.apis) {
628
+ const data = this.apis[name];
629
+ this.updateApi(data, undefined, -1);
630
+ }
631
+ }
632
+
620
633
  /**
621
634
  * Update API token and expires
622
635
  * @param name Api name
@@ -2026,6 +2039,11 @@ export abstract class CoreApp<
2026
2039
  * @returns Result
2027
2040
  */
2028
2041
  async exchangeToken(api: IApi, token: string) {
2042
+ // Avoid to call the system API
2043
+ if (api.name === systemApi) {
2044
+ throw new Error('System API is not allowed to exchange token');
2045
+ }
2046
+
2029
2047
  // Call the API quietly, no loading bar and no error popup
2030
2048
  const data = await new AuthApi(this).exchangeToken(
2031
2049
  { token },
@@ -2071,14 +2089,18 @@ export abstract class CoreApp<
2071
2089
  coreName ??= 'core';
2072
2090
 
2073
2091
  for (const name in this.apis) {
2074
- const api = this.apis[name];
2092
+ // Ignore the system API as it has its own logic with refreshToken
2093
+ if (name === systemApi) continue;
2094
+
2095
+ const data = this.apis[name];
2096
+ const api = data[0];
2075
2097
 
2076
2098
  // The core API
2077
- if (api[0].name === coreName) {
2078
- api[0].authorize(coreData.tokenType, coreData.accessToken);
2079
- this.updateApi(api, coreData.refreshToken, coreData.expiresIn);
2099
+ if (name === coreName) {
2100
+ api.authorize(coreData.tokenType, coreData.accessToken);
2101
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
2080
2102
  } else {
2081
- this.exchangeToken(api[0], coreData.refreshToken);
2103
+ this.exchangeToken(api, coreData.refreshToken);
2082
2104
  }
2083
2105
  }
2084
2106
  }
@@ -2196,12 +2218,14 @@ export abstract class CoreApp<
2196
2218
 
2197
2219
  /**
2198
2220
  * Signout, with userLogout and toLoginPage
2199
- * @param apiUrl Signout API URL
2200
2221
  */
2201
2222
  async signout() {
2202
2223
  // Clear the keep login status
2203
2224
  this.keepLogin = false;
2204
2225
 
2226
+ // Reset all APIs
2227
+ this.resetApis();
2228
+
2205
2229
  const token = this.getCacheToken();
2206
2230
  if (token) {
2207
2231
  const result = await new AuthApi(this).signout(