@etsoo/appscript 1.1.41 → 1.1.45

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.
@@ -22,6 +22,10 @@ export interface IAppSettings extends IExternalSettings {
22
22
  * Detected culture
23
23
  */
24
24
  readonly detectedCulture: string;
25
+ /**
26
+ * Service id
27
+ */
28
+ readonly serviceId?: string;
25
29
  /**
26
30
  * Time zone
27
31
  */
@@ -262,8 +262,14 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
262
262
  get authorized(): boolean;
263
263
  private set authorized(value);
264
264
  private _isTryingLogin;
265
- private _lastCalled;
266
- private _refreshCountdownSeed;
265
+ /**
266
+ * Last called with token refresh
267
+ */
268
+ protected lastCalled: boolean;
269
+ /**
270
+ * Token refresh count down seed
271
+ */
272
+ protected refreshCountdownSeed: number;
267
273
  /**
268
274
  * Protected constructor
269
275
  * @param settings Settings
@@ -272,6 +278,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
272
278
  * @param name Application name
273
279
  */
274
280
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, name: string);
281
+ protected setApi(api: IApi): void;
275
282
  /**
276
283
  * Alert action result
277
284
  * @param result Action result
@@ -377,8 +384,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
377
384
  * Refresh countdown
378
385
  * @param seconds Seconds
379
386
  */
380
- private refreshCountdown;
381
- private refreshCountdownClear;
387
+ protected refreshCountdown(seconds: number): void;
388
+ protected refreshCountdownClear(): void;
382
389
  /**
383
390
  * Fresh countdown UI
384
391
  * @param callback Callback
@@ -24,40 +24,19 @@ class CoreApp {
24
24
  this.headerTokenField = 'SmartERPRefreshToken';
25
25
  this._authorized = false;
26
26
  this._isTryingLogin = false;
27
- this._lastCalled = false;
28
- this._refreshCountdownSeed = 0;
29
- // onRequest, show loading or not, rewrite the property to override default action
30
- api.onRequest = (data) => {
31
- if (data.showLoading == null || data.showLoading) {
32
- notifier.showLoading();
33
- }
34
- };
35
- // onComplete, hide loading, rewrite the property to override default action
36
- api.onComplete = (data) => {
37
- if (data.showLoading == null || data.showLoading) {
38
- notifier.hideLoading();
39
- }
40
- this._lastCalled = true;
41
- };
42
- // Global API error handler
43
- api.onError = (error) => {
44
- // Error code
45
- const status = error.response
46
- ? api.transformResponse(error.response).status
47
- : undefined;
48
- if (status === 401) {
49
- // When status is equal to 401, unauthorized, try login
50
- this.tryLogin();
51
- }
52
- else {
53
- // Report the error
54
- notifier.alert(this.formatError(error));
55
- }
56
- };
27
+ /**
28
+ * Last called with token refresh
29
+ */
30
+ this.lastCalled = false;
31
+ /**
32
+ * Token refresh count down seed
33
+ */
34
+ this.refreshCountdownSeed = 0;
57
35
  this.settings = settings;
58
36
  this.api = api;
59
37
  this.notifier = notifier;
60
38
  this.name = name;
39
+ this.setApi(api);
61
40
  const { currentCulture, currentRegion } = settings;
62
41
  this.changeCulture(currentCulture);
63
42
  this.changeRegion(currentRegion);
@@ -97,6 +76,36 @@ class CoreApp {
97
76
  set authorized(value) {
98
77
  this._authorized = value;
99
78
  }
79
+ setApi(api) {
80
+ // onRequest, show loading or not, rewrite the property to override default action
81
+ api.onRequest = (data) => {
82
+ if (data.showLoading == null || data.showLoading) {
83
+ this.notifier.showLoading();
84
+ }
85
+ };
86
+ // onComplete, hide loading, rewrite the property to override default action
87
+ api.onComplete = (data) => {
88
+ if (data.showLoading == null || data.showLoading) {
89
+ this.notifier.hideLoading();
90
+ }
91
+ this.lastCalled = true;
92
+ };
93
+ // Global API error handler
94
+ api.onError = (error) => {
95
+ // Error code
96
+ const status = error.response
97
+ ? api.transformResponse(error.response).status
98
+ : undefined;
99
+ if (status === 401) {
100
+ // When status is equal to 401, unauthorized, try login
101
+ this.tryLogin();
102
+ }
103
+ else {
104
+ // Report the error
105
+ this.notifier.alert(this.formatError(error));
106
+ }
107
+ };
108
+ }
100
109
  /**
101
110
  * Alert action result
102
111
  * @param result Action result
@@ -351,23 +360,23 @@ class CoreApp {
351
360
  // Reset last call flag
352
361
  // Any success call will update it to true
353
362
  // So first time after login will be always silent
354
- this._lastCalled = false;
355
- this._refreshCountdownSeed = window.setTimeout(() => {
356
- if (this._lastCalled) {
363
+ this.lastCalled = false;
364
+ this.refreshCountdownSeed = window.setTimeout(() => {
365
+ if (this.lastCalled) {
357
366
  // Call refreshToken to update access token
358
367
  this.refreshToken();
359
368
  }
360
369
  else {
361
370
  // Popup countdown for user action
362
- this.freshCountdownUI(this.refreshToken);
371
+ this.freshCountdownUI();
363
372
  }
364
373
  }, 1000 * seconds);
365
374
  }
366
375
  refreshCountdownClear() {
367
376
  // Clear the current timeout seed
368
- if (this._refreshCountdownSeed > 0) {
369
- window.clearTimeout(this._refreshCountdownSeed);
370
- this._refreshCountdownSeed = 0;
377
+ if (this.refreshCountdownSeed > 0) {
378
+ window.clearTimeout(this.refreshCountdownSeed);
379
+ this.refreshCountdownSeed = 0;
371
380
  }
372
381
  }
373
382
  /**
@@ -14,6 +14,14 @@ export interface IExternalSettings {
14
14
  * Cloud web url
15
15
  */
16
16
  readonly webUrl: string;
17
+ /**
18
+ * Core system Url
19
+ */
20
+ readonly coreUrl?: string;
21
+ /**
22
+ * Core system API
23
+ */
24
+ readonly coreApi?: string;
17
25
  }
18
26
  /**
19
27
  * External settings host
@@ -19,6 +19,8 @@ export * from './dto/UpdateDto';
19
19
  export * from './i18n/enUS';
20
20
  export * from './i18n/zhCN';
21
21
  export * from './i18n/zhHK';
22
+ export { ApiAuthorizationScheme, createClient } from '@etsoo/restclient';
23
+ export type { IApi, IApiPayload } from '@etsoo/restclient';
22
24
  export * from './result/ActionResult';
23
25
  export * from './result/ActionResultError';
24
26
  export * from './result/IActionResult';
package/lib/cjs/index.js CHANGED
@@ -10,6 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.createClient = exports.ApiAuthorizationScheme = void 0;
13
14
  // address
14
15
  __exportStar(require("./address/AddressContinent"), exports);
15
16
  __exportStar(require("./address/AddressRegion"), exports);
@@ -38,6 +39,10 @@ __exportStar(require("./dto/UpdateDto"), exports);
38
39
  __exportStar(require("./i18n/enUS"), exports);
39
40
  __exportStar(require("./i18n/zhCN"), exports);
40
41
  __exportStar(require("./i18n/zhHK"), exports);
42
+ // @etsoo/restclient
43
+ var restclient_1 = require("@etsoo/restclient");
44
+ Object.defineProperty(exports, "ApiAuthorizationScheme", { enumerable: true, get: function () { return restclient_1.ApiAuthorizationScheme; } });
45
+ Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return restclient_1.createClient; } });
41
46
  // result
42
47
  __exportStar(require("./result/ActionResult"), exports);
43
48
  __exportStar(require("./result/ActionResultError"), exports);
@@ -22,6 +22,10 @@ export interface IAppSettings extends IExternalSettings {
22
22
  * Detected culture
23
23
  */
24
24
  readonly detectedCulture: string;
25
+ /**
26
+ * Service id
27
+ */
28
+ readonly serviceId?: string;
25
29
  /**
26
30
  * Time zone
27
31
  */
@@ -262,8 +262,14 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
262
262
  get authorized(): boolean;
263
263
  private set authorized(value);
264
264
  private _isTryingLogin;
265
- private _lastCalled;
266
- private _refreshCountdownSeed;
265
+ /**
266
+ * Last called with token refresh
267
+ */
268
+ protected lastCalled: boolean;
269
+ /**
270
+ * Token refresh count down seed
271
+ */
272
+ protected refreshCountdownSeed: number;
267
273
  /**
268
274
  * Protected constructor
269
275
  * @param settings Settings
@@ -272,6 +278,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
272
278
  * @param name Application name
273
279
  */
274
280
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, name: string);
281
+ protected setApi(api: IApi): void;
275
282
  /**
276
283
  * Alert action result
277
284
  * @param result Action result
@@ -377,8 +384,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
377
384
  * Refresh countdown
378
385
  * @param seconds Seconds
379
386
  */
380
- private refreshCountdown;
381
- private refreshCountdownClear;
387
+ protected refreshCountdown(seconds: number): void;
388
+ protected refreshCountdownClear(): void;
382
389
  /**
383
390
  * Fresh countdown UI
384
391
  * @param callback Callback
@@ -21,40 +21,19 @@ export class CoreApp {
21
21
  this.headerTokenField = 'SmartERPRefreshToken';
22
22
  this._authorized = false;
23
23
  this._isTryingLogin = false;
24
- this._lastCalled = false;
25
- this._refreshCountdownSeed = 0;
26
- // onRequest, show loading or not, rewrite the property to override default action
27
- api.onRequest = (data) => {
28
- if (data.showLoading == null || data.showLoading) {
29
- notifier.showLoading();
30
- }
31
- };
32
- // onComplete, hide loading, rewrite the property to override default action
33
- api.onComplete = (data) => {
34
- if (data.showLoading == null || data.showLoading) {
35
- notifier.hideLoading();
36
- }
37
- this._lastCalled = true;
38
- };
39
- // Global API error handler
40
- api.onError = (error) => {
41
- // Error code
42
- const status = error.response
43
- ? api.transformResponse(error.response).status
44
- : undefined;
45
- if (status === 401) {
46
- // When status is equal to 401, unauthorized, try login
47
- this.tryLogin();
48
- }
49
- else {
50
- // Report the error
51
- notifier.alert(this.formatError(error));
52
- }
53
- };
24
+ /**
25
+ * Last called with token refresh
26
+ */
27
+ this.lastCalled = false;
28
+ /**
29
+ * Token refresh count down seed
30
+ */
31
+ this.refreshCountdownSeed = 0;
54
32
  this.settings = settings;
55
33
  this.api = api;
56
34
  this.notifier = notifier;
57
35
  this.name = name;
36
+ this.setApi(api);
58
37
  const { currentCulture, currentRegion } = settings;
59
38
  this.changeCulture(currentCulture);
60
39
  this.changeRegion(currentRegion);
@@ -94,6 +73,36 @@ export class CoreApp {
94
73
  set authorized(value) {
95
74
  this._authorized = value;
96
75
  }
76
+ setApi(api) {
77
+ // onRequest, show loading or not, rewrite the property to override default action
78
+ api.onRequest = (data) => {
79
+ if (data.showLoading == null || data.showLoading) {
80
+ this.notifier.showLoading();
81
+ }
82
+ };
83
+ // onComplete, hide loading, rewrite the property to override default action
84
+ api.onComplete = (data) => {
85
+ if (data.showLoading == null || data.showLoading) {
86
+ this.notifier.hideLoading();
87
+ }
88
+ this.lastCalled = true;
89
+ };
90
+ // Global API error handler
91
+ api.onError = (error) => {
92
+ // Error code
93
+ const status = error.response
94
+ ? api.transformResponse(error.response).status
95
+ : undefined;
96
+ if (status === 401) {
97
+ // When status is equal to 401, unauthorized, try login
98
+ this.tryLogin();
99
+ }
100
+ else {
101
+ // Report the error
102
+ this.notifier.alert(this.formatError(error));
103
+ }
104
+ };
105
+ }
97
106
  /**
98
107
  * Alert action result
99
108
  * @param result Action result
@@ -348,23 +357,23 @@ export class CoreApp {
348
357
  // Reset last call flag
349
358
  // Any success call will update it to true
350
359
  // So first time after login will be always silent
351
- this._lastCalled = false;
352
- this._refreshCountdownSeed = window.setTimeout(() => {
353
- if (this._lastCalled) {
360
+ this.lastCalled = false;
361
+ this.refreshCountdownSeed = window.setTimeout(() => {
362
+ if (this.lastCalled) {
354
363
  // Call refreshToken to update access token
355
364
  this.refreshToken();
356
365
  }
357
366
  else {
358
367
  // Popup countdown for user action
359
- this.freshCountdownUI(this.refreshToken);
368
+ this.freshCountdownUI();
360
369
  }
361
370
  }, 1000 * seconds);
362
371
  }
363
372
  refreshCountdownClear() {
364
373
  // Clear the current timeout seed
365
- if (this._refreshCountdownSeed > 0) {
366
- window.clearTimeout(this._refreshCountdownSeed);
367
- this._refreshCountdownSeed = 0;
374
+ if (this.refreshCountdownSeed > 0) {
375
+ window.clearTimeout(this.refreshCountdownSeed);
376
+ this.refreshCountdownSeed = 0;
368
377
  }
369
378
  }
370
379
  /**
@@ -14,6 +14,14 @@ export interface IExternalSettings {
14
14
  * Cloud web url
15
15
  */
16
16
  readonly webUrl: string;
17
+ /**
18
+ * Core system Url
19
+ */
20
+ readonly coreUrl?: string;
21
+ /**
22
+ * Core system API
23
+ */
24
+ readonly coreApi?: string;
17
25
  }
18
26
  /**
19
27
  * External settings host
@@ -19,6 +19,8 @@ export * from './dto/UpdateDto';
19
19
  export * from './i18n/enUS';
20
20
  export * from './i18n/zhCN';
21
21
  export * from './i18n/zhHK';
22
+ export { ApiAuthorizationScheme, createClient } from '@etsoo/restclient';
23
+ export type { IApi, IApiPayload } from '@etsoo/restclient';
22
24
  export * from './result/ActionResult';
23
25
  export * from './result/ActionResultError';
24
26
  export * from './result/IActionResult';
package/lib/mjs/index.js CHANGED
@@ -26,6 +26,8 @@ export * from './dto/UpdateDto';
26
26
  export * from './i18n/enUS';
27
27
  export * from './i18n/zhCN';
28
28
  export * from './i18n/zhHK';
29
+ // @etsoo/restclient
30
+ export { ApiAuthorizationScheme, createClient } from '@etsoo/restclient';
29
31
  // result
30
32
  export * from './result/ActionResult';
31
33
  export * from './result/ActionResultError';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.1.41",
3
+ "version": "1.1.45",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -59,15 +59,15 @@
59
59
  "devDependencies": {
60
60
  "@babel/cli": "^7.16.0",
61
61
  "@babel/core": "^7.16.0",
62
- "@babel/plugin-transform-runtime": "^7.16.0",
63
- "@babel/preset-env": "^7.16.0",
64
- "@babel/runtime-corejs3": "^7.16.0",
62
+ "@babel/plugin-transform-runtime": "^7.16.4",
63
+ "@babel/preset-env": "^7.16.4",
64
+ "@babel/runtime-corejs3": "^7.16.3",
65
65
  "@types/jest": "^27.0.2",
66
- "@typescript-eslint/eslint-plugin": "^5.3.1",
67
- "@typescript-eslint/parser": "^5.3.1",
66
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
67
+ "@typescript-eslint/parser": "^5.4.0",
68
68
  "eslint": "^8.2.0",
69
- "eslint-config-airbnb-base": "^14.2.1",
70
- "eslint-plugin-import": "^2.25.2",
69
+ "eslint-config-airbnb-base": "^15.0.0",
70
+ "eslint-plugin-import": "^2.25.3",
71
71
  "jest": "^27.3.1",
72
72
  "ts-jest": "^27.0.7",
73
73
  "typescript": "^4.4.4"
@@ -27,6 +27,11 @@ export interface IAppSettings extends IExternalSettings {
27
27
  */
28
28
  readonly detectedCulture: string;
29
29
 
30
+ /**
31
+ * Service id
32
+ */
33
+ readonly serviceId?: string;
34
+
30
35
  /**
31
36
  * Time zone
32
37
  */
@@ -362,8 +362,15 @@ export abstract class CoreApp<
362
362
 
363
363
  private _isTryingLogin = false;
364
364
 
365
- private _lastCalled = false;
366
- private _refreshCountdownSeed = 0;
365
+ /**
366
+ * Last called with token refresh
367
+ */
368
+ protected lastCalled = false;
369
+
370
+ /**
371
+ * Token refresh count down seed
372
+ */
373
+ protected refreshCountdownSeed = 0;
367
374
 
368
375
  /**
369
376
  * Protected constructor
@@ -378,19 +385,36 @@ export abstract class CoreApp<
378
385
  notifier: INotifier<N, C>,
379
386
  name: string
380
387
  ) {
388
+ this.settings = settings;
389
+ this.api = api;
390
+ this.notifier = notifier;
391
+ this.name = name;
392
+
393
+ this.setApi(api);
394
+
395
+ const { currentCulture, currentRegion } = settings;
396
+ this.changeCulture(currentCulture);
397
+
398
+ this.changeRegion(currentRegion);
399
+
400
+ // Setup callback
401
+ this.setup();
402
+ }
403
+
404
+ protected setApi(api: IApi) {
381
405
  // onRequest, show loading or not, rewrite the property to override default action
382
406
  api.onRequest = (data) => {
383
407
  if (data.showLoading == null || data.showLoading) {
384
- notifier.showLoading();
408
+ this.notifier.showLoading();
385
409
  }
386
410
  };
387
411
 
388
412
  // onComplete, hide loading, rewrite the property to override default action
389
413
  api.onComplete = (data) => {
390
414
  if (data.showLoading == null || data.showLoading) {
391
- notifier.hideLoading();
415
+ this.notifier.hideLoading();
392
416
  }
393
- this._lastCalled = true;
417
+ this.lastCalled = true;
394
418
  };
395
419
 
396
420
  // Global API error handler
@@ -405,22 +429,9 @@ export abstract class CoreApp<
405
429
  this.tryLogin();
406
430
  } else {
407
431
  // Report the error
408
- notifier.alert(this.formatError(error));
432
+ this.notifier.alert(this.formatError(error));
409
433
  }
410
434
  };
411
-
412
- this.settings = settings;
413
- this.api = api;
414
- this.notifier = notifier;
415
- this.name = name;
416
-
417
- const { currentCulture, currentRegion } = settings;
418
- this.changeCulture(currentCulture);
419
-
420
- this.changeRegion(currentRegion);
421
-
422
- // Setup callback
423
- this.setup();
424
435
  }
425
436
 
426
437
  /**
@@ -727,7 +738,7 @@ export abstract class CoreApp<
727
738
  * Refresh countdown
728
739
  * @param seconds Seconds
729
740
  */
730
- private refreshCountdown(seconds: number) {
741
+ protected refreshCountdown(seconds: number) {
731
742
  // Make sure is big than 60 seconds
732
743
  // Take action 60 seconds before expiry
733
744
  seconds -= 60;
@@ -739,24 +750,24 @@ export abstract class CoreApp<
739
750
  // Reset last call flag
740
751
  // Any success call will update it to true
741
752
  // So first time after login will be always silent
742
- this._lastCalled = false;
753
+ this.lastCalled = false;
743
754
 
744
- this._refreshCountdownSeed = window.setTimeout(() => {
745
- if (this._lastCalled) {
755
+ this.refreshCountdownSeed = window.setTimeout(() => {
756
+ if (this.lastCalled) {
746
757
  // Call refreshToken to update access token
747
758
  this.refreshToken();
748
759
  } else {
749
760
  // Popup countdown for user action
750
- this.freshCountdownUI(this.refreshToken);
761
+ this.freshCountdownUI();
751
762
  }
752
763
  }, 1000 * seconds);
753
764
  }
754
765
 
755
- private refreshCountdownClear() {
766
+ protected refreshCountdownClear() {
756
767
  // Clear the current timeout seed
757
- if (this._refreshCountdownSeed > 0) {
758
- window.clearTimeout(this._refreshCountdownSeed);
759
- this._refreshCountdownSeed = 0;
768
+ if (this.refreshCountdownSeed > 0) {
769
+ window.clearTimeout(this.refreshCountdownSeed);
770
+ this.refreshCountdownSeed = 0;
760
771
  }
761
772
  }
762
773
 
@@ -16,6 +16,16 @@ export interface IExternalSettings {
16
16
  * Cloud web url
17
17
  */
18
18
  readonly webUrl: string;
19
+
20
+ /**
21
+ * Core system Url
22
+ */
23
+ readonly coreUrl?: string;
24
+
25
+ /**
26
+ * Core system API
27
+ */
28
+ readonly coreApi?: string;
19
29
  }
20
30
 
21
31
  /**
package/src/index.ts CHANGED
@@ -33,6 +33,10 @@ export * from './i18n/enUS';
33
33
  export * from './i18n/zhCN';
34
34
  export * from './i18n/zhHK';
35
35
 
36
+ // @etsoo/restclient
37
+ export { ApiAuthorizationScheme, createClient } from '@etsoo/restclient';
38
+ export type { IApi, IApiPayload } from '@etsoo/restclient';
39
+
36
40
  // result
37
41
  export * from './result/ActionResult';
38
42
  export * from './result/ActionResultError';