@etsoo/appscript 1.1.9 → 1.1.13

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.
@@ -147,6 +147,20 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
147
147
  * Callback where exit a page
148
148
  */
149
149
  pageExit(): void;
150
+ /**
151
+ * Refresh countdown
152
+ * @param seconds Seconds
153
+ */
154
+ refreshCountdown(seconds: number): void;
155
+ /**
156
+ * Fresh countdown UI
157
+ * @param callback Callback
158
+ */
159
+ freshCountdownUI(callback: () => void): void;
160
+ /**
161
+ * Refresh token
162
+ */
163
+ refreshToken(): Promise<boolean>;
150
164
  /**
151
165
  * Transform URL
152
166
  * @param url URL
@@ -228,6 +242,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
228
242
  get authorized(): boolean;
229
243
  private set authorized(value);
230
244
  private _isTryingLogin;
245
+ private _lastCalled;
246
+ private _refreshCountdownSeed;
231
247
  /**
232
248
  * Protected constructor
233
249
  * @param settings Settings
@@ -330,6 +346,20 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
330
346
  * Callback where exit a page
331
347
  */
332
348
  pageExit(): void;
349
+ /**
350
+ * Refresh countdown
351
+ * @param seconds Seconds
352
+ */
353
+ refreshCountdown(seconds: number): void;
354
+ /**
355
+ * Fresh countdown UI
356
+ * @param callback Callback
357
+ */
358
+ abstract freshCountdownUI(callback: () => void): void;
359
+ /**
360
+ * Refresh token
361
+ */
362
+ refreshToken(): Promise<boolean>;
333
363
  /**
334
364
  * Setup callback
335
365
  */
@@ -21,6 +21,8 @@ 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;
24
26
  // onRequest, show loading or not, rewrite the property to override default action
25
27
  api.onRequest = (data) => {
26
28
  if (data.showLoading == null || data.showLoading) {
@@ -32,6 +34,7 @@ class CoreApp {
32
34
  if (data.showLoading == null || data.showLoading) {
33
35
  notifier.hideLoading();
34
36
  }
37
+ this._lastCalled = true;
35
38
  };
36
39
  // Global API error handler
37
40
  api.onError = (error) => {
@@ -305,6 +308,40 @@ class CoreApp {
305
308
  * Callback where exit a page
306
309
  */
307
310
  pageExit() { }
311
+ /**
312
+ * Refresh countdown
313
+ * @param seconds Seconds
314
+ */
315
+ refreshCountdown(seconds) {
316
+ // Make sure is big than 60 seconds
317
+ // Take action 60 seconds before expiry
318
+ seconds -= 60;
319
+ if (seconds <= 0)
320
+ return;
321
+ // Clear the current timeout seed
322
+ if (this._refreshCountdownSeed > 0) {
323
+ window.clearTimeout(this._refreshCountdownSeed);
324
+ }
325
+ // Reset last call flag
326
+ // Any success call will update it to true
327
+ this._lastCalled = false;
328
+ this._refreshCountdownSeed = window.setTimeout(() => {
329
+ if (this._lastCalled) {
330
+ // Call refreshToken to update access token
331
+ this.refreshToken();
332
+ }
333
+ else {
334
+ // Popup countdown for user action
335
+ this.freshCountdownUI(() => this.refreshToken());
336
+ }
337
+ }, 1000 * seconds);
338
+ }
339
+ /**
340
+ * Refresh token
341
+ */
342
+ async refreshToken() {
343
+ return true;
344
+ }
308
345
  /**
309
346
  * Setup callback
310
347
  */
@@ -147,6 +147,20 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
147
147
  * Callback where exit a page
148
148
  */
149
149
  pageExit(): void;
150
+ /**
151
+ * Refresh countdown
152
+ * @param seconds Seconds
153
+ */
154
+ refreshCountdown(seconds: number): void;
155
+ /**
156
+ * Fresh countdown UI
157
+ * @param callback Callback
158
+ */
159
+ freshCountdownUI(callback: () => void): void;
160
+ /**
161
+ * Refresh token
162
+ */
163
+ refreshToken(): Promise<boolean>;
150
164
  /**
151
165
  * Transform URL
152
166
  * @param url URL
@@ -228,6 +242,8 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
228
242
  get authorized(): boolean;
229
243
  private set authorized(value);
230
244
  private _isTryingLogin;
245
+ private _lastCalled;
246
+ private _refreshCountdownSeed;
231
247
  /**
232
248
  * Protected constructor
233
249
  * @param settings Settings
@@ -330,6 +346,20 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
330
346
  * Callback where exit a page
331
347
  */
332
348
  pageExit(): void;
349
+ /**
350
+ * Refresh countdown
351
+ * @param seconds Seconds
352
+ */
353
+ refreshCountdown(seconds: number): void;
354
+ /**
355
+ * Fresh countdown UI
356
+ * @param callback Callback
357
+ */
358
+ abstract freshCountdownUI(callback: () => void): void;
359
+ /**
360
+ * Refresh token
361
+ */
362
+ refreshToken(): Promise<boolean>;
333
363
  /**
334
364
  * Setup callback
335
365
  */
@@ -18,6 +18,8 @@ export class CoreApp {
18
18
  this.headerTokenField = 'SmartERPRefreshToken';
19
19
  this._authorized = false;
20
20
  this._isTryingLogin = false;
21
+ this._lastCalled = false;
22
+ this._refreshCountdownSeed = 0;
21
23
  // onRequest, show loading or not, rewrite the property to override default action
22
24
  api.onRequest = (data) => {
23
25
  if (data.showLoading == null || data.showLoading) {
@@ -29,6 +31,7 @@ export class CoreApp {
29
31
  if (data.showLoading == null || data.showLoading) {
30
32
  notifier.hideLoading();
31
33
  }
34
+ this._lastCalled = true;
32
35
  };
33
36
  // Global API error handler
34
37
  api.onError = (error) => {
@@ -302,6 +305,40 @@ export class CoreApp {
302
305
  * Callback where exit a page
303
306
  */
304
307
  pageExit() { }
308
+ /**
309
+ * Refresh countdown
310
+ * @param seconds Seconds
311
+ */
312
+ refreshCountdown(seconds) {
313
+ // Make sure is big than 60 seconds
314
+ // Take action 60 seconds before expiry
315
+ seconds -= 60;
316
+ if (seconds <= 0)
317
+ return;
318
+ // Clear the current timeout seed
319
+ if (this._refreshCountdownSeed > 0) {
320
+ window.clearTimeout(this._refreshCountdownSeed);
321
+ }
322
+ // Reset last call flag
323
+ // Any success call will update it to true
324
+ this._lastCalled = false;
325
+ this._refreshCountdownSeed = window.setTimeout(() => {
326
+ if (this._lastCalled) {
327
+ // Call refreshToken to update access token
328
+ this.refreshToken();
329
+ }
330
+ else {
331
+ // Popup countdown for user action
332
+ this.freshCountdownUI(() => this.refreshToken());
333
+ }
334
+ }, 1000 * seconds);
335
+ }
336
+ /**
337
+ * Refresh token
338
+ */
339
+ async refreshToken() {
340
+ return true;
341
+ }
305
342
  /**
306
343
  * Setup callback
307
344
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.1.9",
3
+ "version": "1.1.13",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -47,9 +47,9 @@
47
47
  },
48
48
  "homepage": "https://github.com/ETSOO/NotificationBase#readme",
49
49
  "dependencies": {
50
- "@etsoo/notificationbase": "^1.0.84",
51
- "@etsoo/restclient": "^1.0.54",
52
- "@etsoo/shared": "^1.0.51"
50
+ "@etsoo/notificationbase": "^1.0.86",
51
+ "@etsoo/restclient": "^1.0.55",
52
+ "@etsoo/shared": "^1.0.53"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@babel/cli": "^7.15.7",
@@ -58,13 +58,13 @@
58
58
  "@babel/preset-env": "^7.15.8",
59
59
  "@babel/runtime-corejs3": "^7.15.4",
60
60
  "@types/jest": "^27.0.2",
61
- "@typescript-eslint/eslint-plugin": "^5.0.0",
62
- "@typescript-eslint/parser": "^5.0.0",
61
+ "@typescript-eslint/eslint-plugin": "^5.1.0",
62
+ "@typescript-eslint/parser": "^5.1.0",
63
63
  "eslint": "^8.0.1",
64
64
  "eslint-config-airbnb-base": "^14.2.1",
65
65
  "eslint-plugin-import": "^2.25.2",
66
- "jest": "^27.2.5",
67
- "ts-jest": "^27.0.6",
66
+ "jest": "^27.3.0",
67
+ "ts-jest": "^27.0.7",
68
68
  "typescript": "^4.4.4"
69
69
  }
70
70
  }
@@ -195,6 +195,23 @@ export interface ICoreApp<
195
195
  */
196
196
  pageExit(): void;
197
197
 
198
+ /**
199
+ * Refresh countdown
200
+ * @param seconds Seconds
201
+ */
202
+ refreshCountdown(seconds: number): void;
203
+
204
+ /**
205
+ * Fresh countdown UI
206
+ * @param callback Callback
207
+ */
208
+ freshCountdownUI(callback: () => void): void;
209
+
210
+ /**
211
+ * Refresh token
212
+ */
213
+ refreshToken(): Promise<boolean>;
214
+
198
215
  /**
199
216
  * Transform URL
200
217
  * @param url URL
@@ -313,6 +330,9 @@ export abstract class CoreApp<
313
330
 
314
331
  private _isTryingLogin = false;
315
332
 
333
+ private _lastCalled = false;
334
+ private _refreshCountdownSeed = 0;
335
+
316
336
  /**
317
337
  * Protected constructor
318
338
  * @param settings Settings
@@ -332,6 +352,7 @@ export abstract class CoreApp<
332
352
  if (data.showLoading == null || data.showLoading) {
333
353
  notifier.hideLoading();
334
354
  }
355
+ this._lastCalled = true;
335
356
  };
336
357
 
337
358
  // Global API error handler
@@ -634,6 +655,49 @@ export abstract class CoreApp<
634
655
  */
635
656
  pageExit() {}
636
657
 
658
+ /**
659
+ * Refresh countdown
660
+ * @param seconds Seconds
661
+ */
662
+ refreshCountdown(seconds: number) {
663
+ // Make sure is big than 60 seconds
664
+ // Take action 60 seconds before expiry
665
+ seconds -= 60;
666
+ if (seconds <= 0) return;
667
+
668
+ // Clear the current timeout seed
669
+ if (this._refreshCountdownSeed > 0) {
670
+ window.clearTimeout(this._refreshCountdownSeed);
671
+ }
672
+
673
+ // Reset last call flag
674
+ // Any success call will update it to true
675
+ this._lastCalled = false;
676
+
677
+ this._refreshCountdownSeed = window.setTimeout(() => {
678
+ if (this._lastCalled) {
679
+ // Call refreshToken to update access token
680
+ this.refreshToken();
681
+ } else {
682
+ // Popup countdown for user action
683
+ this.freshCountdownUI(() => this.refreshToken());
684
+ }
685
+ }, 1000 * seconds);
686
+ }
687
+
688
+ /**
689
+ * Fresh countdown UI
690
+ * @param callback Callback
691
+ */
692
+ abstract freshCountdownUI(callback: () => void): void;
693
+
694
+ /**
695
+ * Refresh token
696
+ */
697
+ async refreshToken(): Promise<boolean> {
698
+ return true;
699
+ }
700
+
637
701
  /**
638
702
  * Setup callback
639
703
  */