@etsoo/appscript 1.5.57 → 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.
- package/lib/cjs/app/CoreApp.d.ts +7 -6
- package/lib/cjs/app/CoreApp.js +28 -16
- package/lib/cjs/app/ExternalSettings.d.ts +5 -0
- package/lib/cjs/app/IApp.d.ts +2 -1
- package/lib/cjs/erp/rq/ApiRefreshTokenRQ.d.ts +13 -0
- package/lib/cjs/erp/rq/ApiRefreshTokenRQ.js +2 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/mjs/app/CoreApp.d.ts +7 -6
- package/lib/mjs/app/CoreApp.js +28 -16
- package/lib/mjs/app/ExternalSettings.d.ts +5 -0
- package/lib/mjs/app/IApp.d.ts +2 -1
- package/lib/mjs/erp/rq/ApiRefreshTokenRQ.d.ts +13 -0
- package/lib/mjs/erp/rq/ApiRefreshTokenRQ.js +1 -0
- package/lib/mjs/index.d.ts +1 -0
- package/lib/mjs/index.js +1 -0
- package/package.json +1 -1
- package/src/app/CoreApp.ts +43 -30
- package/src/app/ExternalSettings.ts +6 -0
- package/src/app/IApp.ts +2 -1
- package/src/erp/rq/ApiRefreshTokenRQ.ts +14 -0
- package/src/index.ts +1 -0
package/lib/cjs/app/CoreApp.d.ts
CHANGED
|
@@ -11,7 +11,8 @@ import { AppLoginParams, AppTryLoginParams, FormatResultCustomCallback, IApp, IA
|
|
|
11
11
|
import { UserRole } from './UserRole';
|
|
12
12
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
13
13
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
14
|
-
|
|
14
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
15
|
+
type ApiRefreshTokenFunction = (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>;
|
|
15
16
|
type ApiTaskData = [IApi, number, number, ApiRefreshTokenFunction, string?];
|
|
16
17
|
/**
|
|
17
18
|
* Core application interface
|
|
@@ -217,7 +218,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
|
|
|
217
218
|
* @param item External endpoint item
|
|
218
219
|
* @returns Result
|
|
219
220
|
*/
|
|
220
|
-
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi,
|
|
221
|
+
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi<any>;
|
|
221
222
|
/**
|
|
222
223
|
* Update API token and expires
|
|
223
224
|
* @param name Api name
|
|
@@ -613,17 +614,17 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
|
|
|
613
614
|
/**
|
|
614
615
|
* API refresh token data
|
|
615
616
|
* @param api Current API
|
|
616
|
-
* @param
|
|
617
|
+
* @param rq Request data
|
|
617
618
|
* @returns Result
|
|
618
619
|
*/
|
|
619
|
-
protected apiRefreshTokenData(api: IApi,
|
|
620
|
+
protected apiRefreshTokenData(api: IApi, rq: ApiRefreshTokenRQ): Promise<ApiRefreshTokenDto | undefined>;
|
|
620
621
|
/**
|
|
621
622
|
* API refresh token
|
|
622
623
|
* @param api Current API
|
|
623
|
-
* @param
|
|
624
|
+
* @param rq Request data
|
|
624
625
|
* @returns Result
|
|
625
626
|
*/
|
|
626
|
-
protected apiRefreshToken(api: IApi,
|
|
627
|
+
protected apiRefreshToken(api: IApi, rq: ApiRefreshTokenRQ): Promise<[string, number] | undefined>;
|
|
627
628
|
/**
|
|
628
629
|
* Setup tasks
|
|
629
630
|
*/
|
package/lib/cjs/app/CoreApp.js
CHANGED
|
@@ -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
|
*/
|
|
@@ -183,10 +185,10 @@ class CoreApp {
|
|
|
183
185
|
}
|
|
184
186
|
this.defaultRegion = region;
|
|
185
187
|
// Current system refresh token
|
|
186
|
-
const refresh = async (api,
|
|
188
|
+
const refresh = async (api, rq) => {
|
|
187
189
|
if (this.lastCalled) {
|
|
188
190
|
// Call refreshToken to update access token
|
|
189
|
-
await this.refreshToken({ token, showLoading: false }, (result) => {
|
|
191
|
+
await this.refreshToken({ token: rq.token, showLoading: false }, (result) => {
|
|
190
192
|
if (result === true)
|
|
191
193
|
return;
|
|
192
194
|
console.log(`CoreApp.${this.name}.RefreshToken`, result);
|
|
@@ -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 =
|
|
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(
|
|
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,26 +1514,32 @@ class CoreApp {
|
|
|
1508
1514
|
exchangeTokenAll(coreData, coreName) {
|
|
1509
1515
|
coreName ?? (coreName = 'core');
|
|
1510
1516
|
for (const name in this.apis) {
|
|
1511
|
-
|
|
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 (
|
|
1514
|
-
api
|
|
1515
|
-
this.updateApi(
|
|
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
|
|
1528
|
+
this.exchangeToken(api, coreData.refreshToken);
|
|
1519
1529
|
}
|
|
1520
1530
|
}
|
|
1521
1531
|
}
|
|
1522
1532
|
/**
|
|
1523
1533
|
* API refresh token data
|
|
1524
1534
|
* @param api Current API
|
|
1525
|
-
* @param
|
|
1535
|
+
* @param rq Request data
|
|
1526
1536
|
* @returns Result
|
|
1527
1537
|
*/
|
|
1528
|
-
async apiRefreshTokenData(api,
|
|
1538
|
+
async apiRefreshTokenData(api, rq) {
|
|
1539
|
+
// Default appId
|
|
1540
|
+
rq.appId ?? (rq.appId = this.settings.appId);
|
|
1529
1541
|
// Call the API quietly, no loading bar and no error popup
|
|
1530
|
-
return new AuthApi_1.AuthApi(this, api).apiRefreshToken(
|
|
1542
|
+
return new AuthApi_1.AuthApi(this, api).apiRefreshToken(rq, {
|
|
1531
1543
|
showLoading: false,
|
|
1532
1544
|
onError: (error) => {
|
|
1533
1545
|
console.error(`CoreApp.${api.name}.apiRefreshToken error`, error);
|
|
@@ -1539,12 +1551,12 @@ class CoreApp {
|
|
|
1539
1551
|
/**
|
|
1540
1552
|
* API refresh token
|
|
1541
1553
|
* @param api Current API
|
|
1542
|
-
* @param
|
|
1554
|
+
* @param rq Request data
|
|
1543
1555
|
* @returns Result
|
|
1544
1556
|
*/
|
|
1545
|
-
async apiRefreshToken(api,
|
|
1557
|
+
async apiRefreshToken(api, rq) {
|
|
1546
1558
|
// Call the API quietly, no loading bar and no error popup
|
|
1547
|
-
const data = await this.apiRefreshTokenData(api,
|
|
1559
|
+
const data = await this.apiRefreshTokenData(api, rq);
|
|
1548
1560
|
if (data == null)
|
|
1549
1561
|
return undefined;
|
|
1550
1562
|
// Update the access token
|
|
@@ -1572,7 +1584,7 @@ class CoreApp {
|
|
|
1572
1584
|
// Ready to trigger
|
|
1573
1585
|
if (api[2] === 0) {
|
|
1574
1586
|
// Refresh token
|
|
1575
|
-
api[3](api[0], api[4]).then((data) => {
|
|
1587
|
+
api[3](api[0], { token: api[4] }).then((data) => {
|
|
1576
1588
|
if (data == null) {
|
|
1577
1589
|
// Failed, try it again in 2 seconds
|
|
1578
1590
|
api[2] = 2;
|
package/lib/cjs/app/IApp.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { EntityStatus } from '../business/EntityStatus';
|
|
|
9
9
|
import { Currency } from '../business/Currency';
|
|
10
10
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
11
11
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
12
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
12
13
|
/**
|
|
13
14
|
* Detect IP callback interface
|
|
14
15
|
*/
|
|
@@ -282,7 +283,7 @@ export interface IApp {
|
|
|
282
283
|
* @param item External endpoint item
|
|
283
284
|
* @returns Result
|
|
284
285
|
*/
|
|
285
|
-
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi,
|
|
286
|
+
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi;
|
|
286
287
|
/**
|
|
287
288
|
* Decrypt message
|
|
288
289
|
* @param messageEncrypted Encrypted message
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -43,6 +43,7 @@ export * from './erp/dto/PinDto';
|
|
|
43
43
|
export * from './erp/dto/PlaceParsedDto';
|
|
44
44
|
export * from './erp/dto/ResponseActionMessageDto';
|
|
45
45
|
export * from './erp/dto/ResultPayload';
|
|
46
|
+
export * from './erp/rq/ApiRefreshTokenRQ';
|
|
46
47
|
export * from './erp/rq/AuthRequest';
|
|
47
48
|
export * from './erp/rq/GetLogInUrlRQ';
|
|
48
49
|
export * from './erp/rq/LoginIdRQ';
|
package/lib/cjs/index.js
CHANGED
|
@@ -68,6 +68,7 @@ __exportStar(require("./erp/dto/PlaceParsedDto"), exports);
|
|
|
68
68
|
__exportStar(require("./erp/dto/ResponseActionMessageDto"), exports);
|
|
69
69
|
__exportStar(require("./erp/dto/ResultPayload"), exports);
|
|
70
70
|
// erp rq
|
|
71
|
+
__exportStar(require("./erp/rq/ApiRefreshTokenRQ"), exports);
|
|
71
72
|
__exportStar(require("./erp/rq/AuthRequest"), exports);
|
|
72
73
|
__exportStar(require("./erp/rq/GetLogInUrlRQ"), exports);
|
|
73
74
|
__exportStar(require("./erp/rq/LoginIdRQ"), exports);
|
package/lib/mjs/app/CoreApp.d.ts
CHANGED
|
@@ -11,7 +11,8 @@ import { AppLoginParams, AppTryLoginParams, FormatResultCustomCallback, IApp, IA
|
|
|
11
11
|
import { UserRole } from './UserRole';
|
|
12
12
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
13
13
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
14
|
-
|
|
14
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
15
|
+
type ApiRefreshTokenFunction = (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>;
|
|
15
16
|
type ApiTaskData = [IApi, number, number, ApiRefreshTokenFunction, string?];
|
|
16
17
|
/**
|
|
17
18
|
* Core application interface
|
|
@@ -217,7 +218,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
|
|
|
217
218
|
* @param item External endpoint item
|
|
218
219
|
* @returns Result
|
|
219
220
|
*/
|
|
220
|
-
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi,
|
|
221
|
+
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi<any>;
|
|
221
222
|
/**
|
|
222
223
|
* Update API token and expires
|
|
223
224
|
* @param name Api name
|
|
@@ -613,17 +614,17 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
|
|
|
613
614
|
/**
|
|
614
615
|
* API refresh token data
|
|
615
616
|
* @param api Current API
|
|
616
|
-
* @param
|
|
617
|
+
* @param rq Request data
|
|
617
618
|
* @returns Result
|
|
618
619
|
*/
|
|
619
|
-
protected apiRefreshTokenData(api: IApi,
|
|
620
|
+
protected apiRefreshTokenData(api: IApi, rq: ApiRefreshTokenRQ): Promise<ApiRefreshTokenDto | undefined>;
|
|
620
621
|
/**
|
|
621
622
|
* API refresh token
|
|
622
623
|
* @param api Current API
|
|
623
|
-
* @param
|
|
624
|
+
* @param rq Request data
|
|
624
625
|
* @returns Result
|
|
625
626
|
*/
|
|
626
|
-
protected apiRefreshToken(api: IApi,
|
|
627
|
+
protected apiRefreshToken(api: IApi, rq: ApiRefreshTokenRQ): Promise<[string, number] | undefined>;
|
|
627
628
|
/**
|
|
628
629
|
* Setup tasks
|
|
629
630
|
*/
|
package/lib/mjs/app/CoreApp.js
CHANGED
|
@@ -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
|
*/
|
|
@@ -180,10 +182,10 @@ export class CoreApp {
|
|
|
180
182
|
}
|
|
181
183
|
this.defaultRegion = region;
|
|
182
184
|
// Current system refresh token
|
|
183
|
-
const refresh = async (api,
|
|
185
|
+
const refresh = async (api, rq) => {
|
|
184
186
|
if (this.lastCalled) {
|
|
185
187
|
// Call refreshToken to update access token
|
|
186
|
-
await this.refreshToken({ token, showLoading: false }, (result) => {
|
|
188
|
+
await this.refreshToken({ token: rq.token, showLoading: false }, (result) => {
|
|
187
189
|
if (result === true)
|
|
188
190
|
return;
|
|
189
191
|
console.log(`CoreApp.${this.name}.RefreshToken`, result);
|
|
@@ -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 =
|
|
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(
|
|
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,26 +1511,32 @@ export class CoreApp {
|
|
|
1505
1511
|
exchangeTokenAll(coreData, coreName) {
|
|
1506
1512
|
coreName ?? (coreName = 'core');
|
|
1507
1513
|
for (const name in this.apis) {
|
|
1508
|
-
|
|
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 (
|
|
1511
|
-
api
|
|
1512
|
-
this.updateApi(
|
|
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
|
|
1525
|
+
this.exchangeToken(api, coreData.refreshToken);
|
|
1516
1526
|
}
|
|
1517
1527
|
}
|
|
1518
1528
|
}
|
|
1519
1529
|
/**
|
|
1520
1530
|
* API refresh token data
|
|
1521
1531
|
* @param api Current API
|
|
1522
|
-
* @param
|
|
1532
|
+
* @param rq Request data
|
|
1523
1533
|
* @returns Result
|
|
1524
1534
|
*/
|
|
1525
|
-
async apiRefreshTokenData(api,
|
|
1535
|
+
async apiRefreshTokenData(api, rq) {
|
|
1536
|
+
// Default appId
|
|
1537
|
+
rq.appId ?? (rq.appId = this.settings.appId);
|
|
1526
1538
|
// Call the API quietly, no loading bar and no error popup
|
|
1527
|
-
return new AuthApi(this, api).apiRefreshToken(
|
|
1539
|
+
return new AuthApi(this, api).apiRefreshToken(rq, {
|
|
1528
1540
|
showLoading: false,
|
|
1529
1541
|
onError: (error) => {
|
|
1530
1542
|
console.error(`CoreApp.${api.name}.apiRefreshToken error`, error);
|
|
@@ -1536,12 +1548,12 @@ export class CoreApp {
|
|
|
1536
1548
|
/**
|
|
1537
1549
|
* API refresh token
|
|
1538
1550
|
* @param api Current API
|
|
1539
|
-
* @param
|
|
1551
|
+
* @param rq Request data
|
|
1540
1552
|
* @returns Result
|
|
1541
1553
|
*/
|
|
1542
|
-
async apiRefreshToken(api,
|
|
1554
|
+
async apiRefreshToken(api, rq) {
|
|
1543
1555
|
// Call the API quietly, no loading bar and no error popup
|
|
1544
|
-
const data = await this.apiRefreshTokenData(api,
|
|
1556
|
+
const data = await this.apiRefreshTokenData(api, rq);
|
|
1545
1557
|
if (data == null)
|
|
1546
1558
|
return undefined;
|
|
1547
1559
|
// Update the access token
|
|
@@ -1569,7 +1581,7 @@ export class CoreApp {
|
|
|
1569
1581
|
// Ready to trigger
|
|
1570
1582
|
if (api[2] === 0) {
|
|
1571
1583
|
// Refresh token
|
|
1572
|
-
api[3](api[0], api[4]).then((data) => {
|
|
1584
|
+
api[3](api[0], { token: api[4] }).then((data) => {
|
|
1573
1585
|
if (data == null) {
|
|
1574
1586
|
// Failed, try it again in 2 seconds
|
|
1575
1587
|
api[2] = 2;
|
package/lib/mjs/app/IApp.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { EntityStatus } from '../business/EntityStatus';
|
|
|
9
9
|
import { Currency } from '../business/Currency';
|
|
10
10
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
11
11
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
12
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
12
13
|
/**
|
|
13
14
|
* Detect IP callback interface
|
|
14
15
|
*/
|
|
@@ -282,7 +283,7 @@ export interface IApp {
|
|
|
282
283
|
* @param item External endpoint item
|
|
283
284
|
* @returns Result
|
|
284
285
|
*/
|
|
285
|
-
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi,
|
|
286
|
+
createApi(name: string, item: ExternalEndpoint, refresh?: (api: IApi, rq: ApiRefreshTokenRQ) => Promise<[string, number] | undefined>): IApi;
|
|
286
287
|
/**
|
|
287
288
|
* Decrypt message
|
|
288
289
|
* @param messageEncrypted Encrypted message
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/mjs/index.d.ts
CHANGED
|
@@ -43,6 +43,7 @@ export * from './erp/dto/PinDto';
|
|
|
43
43
|
export * from './erp/dto/PlaceParsedDto';
|
|
44
44
|
export * from './erp/dto/ResponseActionMessageDto';
|
|
45
45
|
export * from './erp/dto/ResultPayload';
|
|
46
|
+
export * from './erp/rq/ApiRefreshTokenRQ';
|
|
46
47
|
export * from './erp/rq/AuthRequest';
|
|
47
48
|
export * from './erp/rq/GetLogInUrlRQ';
|
|
48
49
|
export * from './erp/rq/LoginIdRQ';
|
package/lib/mjs/index.js
CHANGED
|
@@ -51,6 +51,7 @@ export * from './erp/dto/PlaceParsedDto';
|
|
|
51
51
|
export * from './erp/dto/ResponseActionMessageDto';
|
|
52
52
|
export * from './erp/dto/ResultPayload';
|
|
53
53
|
// erp rq
|
|
54
|
+
export * from './erp/rq/ApiRefreshTokenRQ';
|
|
54
55
|
export * from './erp/rq/AuthRequest';
|
|
55
56
|
export * from './erp/rq/GetLogInUrlRQ';
|
|
56
57
|
export * from './erp/rq/LoginIdRQ';
|
package/package.json
CHANGED
package/src/app/CoreApp.ts
CHANGED
|
@@ -48,6 +48,7 @@ import { Currency } from '../business/Currency';
|
|
|
48
48
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
49
49
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
50
50
|
import { AuthApi } from '../erp/AuthApi';
|
|
51
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
51
52
|
|
|
52
53
|
type CJType = typeof CryptoJS;
|
|
53
54
|
let CJ: CJType;
|
|
@@ -57,12 +58,15 @@ const loadCrypto = () => import('crypto-js');
|
|
|
57
58
|
// API refresh token function interface
|
|
58
59
|
type ApiRefreshTokenFunction = (
|
|
59
60
|
api: IApi,
|
|
60
|
-
|
|
61
|
+
rq: ApiRefreshTokenRQ
|
|
61
62
|
) => Promise<[string, number] | undefined>;
|
|
62
63
|
|
|
63
64
|
// API task data
|
|
64
65
|
type ApiTaskData = [IApi, number, number, ApiRefreshTokenFunction, string?];
|
|
65
66
|
|
|
67
|
+
// System API name
|
|
68
|
+
const systemApi = 'system';
|
|
69
|
+
|
|
66
70
|
/**
|
|
67
71
|
* Core application interface
|
|
68
72
|
*/
|
|
@@ -351,11 +355,11 @@ export abstract class CoreApp<
|
|
|
351
355
|
this.defaultRegion = region;
|
|
352
356
|
|
|
353
357
|
// Current system refresh token
|
|
354
|
-
const refresh: ApiRefreshTokenFunction = async (api,
|
|
358
|
+
const refresh: ApiRefreshTokenFunction = async (api, rq) => {
|
|
355
359
|
if (this.lastCalled) {
|
|
356
360
|
// Call refreshToken to update access token
|
|
357
361
|
await this.refreshToken(
|
|
358
|
-
{ token, showLoading: false },
|
|
362
|
+
{ token: rq.token, showLoading: false },
|
|
359
363
|
(result) => {
|
|
360
364
|
if (result === true) return;
|
|
361
365
|
console.log(
|
|
@@ -374,12 +378,12 @@ export abstract class CoreApp<
|
|
|
374
378
|
if (api) {
|
|
375
379
|
// Base URL of the API
|
|
376
380
|
api.baseUrl = this.settings.endpoint;
|
|
377
|
-
api.name =
|
|
381
|
+
api.name = systemApi;
|
|
378
382
|
this.setApi(api, refresh);
|
|
379
383
|
this.api = api;
|
|
380
384
|
} else {
|
|
381
385
|
this.api = this.createApi(
|
|
382
|
-
|
|
386
|
+
systemApi,
|
|
383
387
|
{
|
|
384
388
|
endpoint: settings.endpoint,
|
|
385
389
|
webUrl: settings.webUrl
|
|
@@ -602,7 +606,7 @@ export abstract class CoreApp<
|
|
|
602
606
|
item: ExternalEndpoint,
|
|
603
607
|
refresh?: (
|
|
604
608
|
api: IApi,
|
|
605
|
-
|
|
609
|
+
rq: ApiRefreshTokenRQ
|
|
606
610
|
) => Promise<[string, number] | undefined>
|
|
607
611
|
) {
|
|
608
612
|
if (this.apis[name] != null) {
|
|
@@ -2025,6 +2029,11 @@ export abstract class CoreApp<
|
|
|
2025
2029
|
* @returns Result
|
|
2026
2030
|
*/
|
|
2027
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
|
+
|
|
2028
2037
|
// Call the API quietly, no loading bar and no error popup
|
|
2029
2038
|
const data = await new AuthApi(this).exchangeToken(
|
|
2030
2039
|
{ token },
|
|
@@ -2070,14 +2079,18 @@ export abstract class CoreApp<
|
|
|
2070
2079
|
coreName ??= 'core';
|
|
2071
2080
|
|
|
2072
2081
|
for (const name in this.apis) {
|
|
2073
|
-
|
|
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];
|
|
2074
2087
|
|
|
2075
2088
|
// The core API
|
|
2076
|
-
if (
|
|
2077
|
-
api
|
|
2078
|
-
this.updateApi(
|
|
2089
|
+
if (name === coreName) {
|
|
2090
|
+
api.authorize(coreData.tokenType, coreData.accessToken);
|
|
2091
|
+
this.updateApi(data, coreData.refreshToken, coreData.expiresIn);
|
|
2079
2092
|
} else {
|
|
2080
|
-
this.exchangeToken(api
|
|
2093
|
+
this.exchangeToken(api, coreData.refreshToken);
|
|
2081
2094
|
}
|
|
2082
2095
|
}
|
|
2083
2096
|
}
|
|
@@ -2085,43 +2098,43 @@ export abstract class CoreApp<
|
|
|
2085
2098
|
/**
|
|
2086
2099
|
* API refresh token data
|
|
2087
2100
|
* @param api Current API
|
|
2088
|
-
* @param
|
|
2101
|
+
* @param rq Request data
|
|
2089
2102
|
* @returns Result
|
|
2090
2103
|
*/
|
|
2091
2104
|
protected async apiRefreshTokenData(
|
|
2092
2105
|
api: IApi,
|
|
2093
|
-
|
|
2106
|
+
rq: ApiRefreshTokenRQ
|
|
2094
2107
|
): Promise<ApiRefreshTokenDto | undefined> {
|
|
2108
|
+
// Default appId
|
|
2109
|
+
rq.appId ??= this.settings.appId;
|
|
2110
|
+
|
|
2095
2111
|
// Call the API quietly, no loading bar and no error popup
|
|
2096
|
-
return new AuthApi(this, api).apiRefreshToken(
|
|
2097
|
-
|
|
2098
|
-
{
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
error
|
|
2104
|
-
);
|
|
2112
|
+
return new AuthApi(this, api).apiRefreshToken(rq, {
|
|
2113
|
+
showLoading: false,
|
|
2114
|
+
onError: (error) => {
|
|
2115
|
+
console.error(
|
|
2116
|
+
`CoreApp.${api.name}.apiRefreshToken error`,
|
|
2117
|
+
error
|
|
2118
|
+
);
|
|
2105
2119
|
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
}
|
|
2120
|
+
// Prevent further processing
|
|
2121
|
+
return false;
|
|
2109
2122
|
}
|
|
2110
|
-
);
|
|
2123
|
+
});
|
|
2111
2124
|
}
|
|
2112
2125
|
|
|
2113
2126
|
/**
|
|
2114
2127
|
* API refresh token
|
|
2115
2128
|
* @param api Current API
|
|
2116
|
-
* @param
|
|
2129
|
+
* @param rq Request data
|
|
2117
2130
|
* @returns Result
|
|
2118
2131
|
*/
|
|
2119
2132
|
protected async apiRefreshToken(
|
|
2120
2133
|
api: IApi,
|
|
2121
|
-
|
|
2134
|
+
rq: ApiRefreshTokenRQ
|
|
2122
2135
|
): Promise<[string, number] | undefined> {
|
|
2123
2136
|
// Call the API quietly, no loading bar and no error popup
|
|
2124
|
-
const data = await this.apiRefreshTokenData(api,
|
|
2137
|
+
const data = await this.apiRefreshTokenData(api, rq);
|
|
2125
2138
|
if (data == null) return undefined;
|
|
2126
2139
|
|
|
2127
2140
|
// Update the access token
|
|
@@ -2153,7 +2166,7 @@ export abstract class CoreApp<
|
|
|
2153
2166
|
// Ready to trigger
|
|
2154
2167
|
if (api[2] === 0) {
|
|
2155
2168
|
// Refresh token
|
|
2156
|
-
api[3](api[0], api[4]).then((data) => {
|
|
2169
|
+
api[3](api[0], { token: api[4] }).then((data) => {
|
|
2157
2170
|
if (data == null) {
|
|
2158
2171
|
// Failed, try it again in 2 seconds
|
|
2159
2172
|
api[2] = 2;
|
package/src/app/IApp.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { EntityStatus } from '../business/EntityStatus';
|
|
|
24
24
|
import { Currency } from '../business/Currency';
|
|
25
25
|
import { ExternalEndpoint } from './ExternalSettings';
|
|
26
26
|
import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
|
|
27
|
+
import { ApiRefreshTokenRQ } from '../erp/rq/ApiRefreshTokenRQ';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* Detect IP callback interface
|
|
@@ -371,7 +372,7 @@ export interface IApp {
|
|
|
371
372
|
item: ExternalEndpoint,
|
|
372
373
|
refresh?: (
|
|
373
374
|
api: IApi,
|
|
374
|
-
|
|
375
|
+
rq: ApiRefreshTokenRQ
|
|
375
376
|
) => Promise<[string, number] | undefined>
|
|
376
377
|
): IApi;
|
|
377
378
|
|
package/src/index.ts
CHANGED
|
@@ -58,6 +58,7 @@ export * from './erp/dto/ResponseActionMessageDto';
|
|
|
58
58
|
export * from './erp/dto/ResultPayload';
|
|
59
59
|
|
|
60
60
|
// erp rq
|
|
61
|
+
export * from './erp/rq/ApiRefreshTokenRQ';
|
|
61
62
|
export * from './erp/rq/AuthRequest';
|
|
62
63
|
export * from './erp/rq/GetLogInUrlRQ';
|
|
63
64
|
export * from './erp/rq/LoginIdRQ';
|