@etsoo/appscript 1.5.58 → 1.5.59

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.
@@ -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);
@@ -1476,6 +1478,10 @@ class CoreApp {
1476
1478
  * @returns Result
1477
1479
  */
1478
1480
  async exchangeToken(api, token) {
1481
+ // Avoid to call the system API
1482
+ if (api.name === systemApi) {
1483
+ throw new Error('System API is not allowed to exchange token');
1484
+ }
1479
1485
  // Call the API quietly, no loading bar and no error popup
1480
1486
  const data = await new AuthApi_1.AuthApi(this).exchangeToken({ token }, {
1481
1487
  showLoading: false,
@@ -1508,14 +1514,18 @@ class CoreApp {
1508
1514
  exchangeTokenAll(coreData, coreName) {
1509
1515
  coreName ?? (coreName = 'core');
1510
1516
  for (const name in this.apis) {
1511
- const api = this.apis[name];
1517
+ // Ignore the system API as it has its own logic with refreshToken
1518
+ if (name === systemApi)
1519
+ continue;
1520
+ const data = this.apis[name];
1521
+ const api = data[0];
1512
1522
  // 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);
1523
+ if (name === coreName) {
1524
+ api.authorize(coreData.tokenType, coreData.accessToken);
1525
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
1516
1526
  }
1517
1527
  else {
1518
- this.exchangeToken(api[0], coreData.refreshToken);
1528
+ this.exchangeToken(api, coreData.refreshToken);
1519
1529
  }
1520
1530
  }
1521
1531
  }
@@ -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);
@@ -1473,6 +1475,10 @@ export class CoreApp {
1473
1475
  * @returns Result
1474
1476
  */
1475
1477
  async exchangeToken(api, token) {
1478
+ // Avoid to call the system API
1479
+ if (api.name === systemApi) {
1480
+ throw new Error('System API is not allowed to exchange token');
1481
+ }
1476
1482
  // Call the API quietly, no loading bar and no error popup
1477
1483
  const data = await new AuthApi(this).exchangeToken({ token }, {
1478
1484
  showLoading: false,
@@ -1505,14 +1511,18 @@ export class CoreApp {
1505
1511
  exchangeTokenAll(coreData, coreName) {
1506
1512
  coreName ?? (coreName = 'core');
1507
1513
  for (const name in this.apis) {
1508
- const api = this.apis[name];
1514
+ // Ignore the system API as it has its own logic with refreshToken
1515
+ if (name === systemApi)
1516
+ continue;
1517
+ const data = this.apis[name];
1518
+ const api = data[0];
1509
1519
  // 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);
1520
+ if (name === coreName) {
1521
+ api.authorize(coreData.tokenType, coreData.accessToken);
1522
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
1513
1523
  }
1514
1524
  else {
1515
- this.exchangeToken(api[0], coreData.refreshToken);
1525
+ this.exchangeToken(api, coreData.refreshToken);
1516
1526
  }
1517
1527
  }
1518
1528
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.5.58",
3
+ "version": "1.5.59",
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
@@ -2026,6 +2029,11 @@ export abstract class CoreApp<
2026
2029
  * @returns Result
2027
2030
  */
2028
2031
  async exchangeToken(api: IApi, token: string) {
2032
+ // Avoid to call the system API
2033
+ if (api.name === systemApi) {
2034
+ throw new Error('System API is not allowed to exchange token');
2035
+ }
2036
+
2029
2037
  // Call the API quietly, no loading bar and no error popup
2030
2038
  const data = await new AuthApi(this).exchangeToken(
2031
2039
  { token },
@@ -2071,14 +2079,18 @@ export abstract class CoreApp<
2071
2079
  coreName ??= 'core';
2072
2080
 
2073
2081
  for (const name in this.apis) {
2074
- const api = this.apis[name];
2082
+ // Ignore the system API as it has its own logic with refreshToken
2083
+ if (name === systemApi) continue;
2084
+
2085
+ const data = this.apis[name];
2086
+ const api = data[0];
2075
2087
 
2076
2088
  // 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);
2089
+ if (name === coreName) {
2090
+ api.authorize(coreData.tokenType, coreData.accessToken);
2091
+ this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
2080
2092
  } else {
2081
- this.exchangeToken(api[0], coreData.refreshToken);
2093
+ this.exchangeToken(api, coreData.refreshToken);
2082
2094
  }
2083
2095
  }
2084
2096
  }