@etsoo/appscript 1.1.44 → 1.1.48

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,10 +22,6 @@ 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;
29
25
  /**
30
26
  * Time zone
31
27
  */
@@ -182,7 +182,7 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
182
182
  * @param refreshToken Refresh token
183
183
  * @param keep Keep in local storage or not
184
184
  */
185
- userLogin(user: IUserData, refreshToken?: string, keep?: boolean): void;
185
+ userLogin(user: IUserData, refreshToken: string, keep?: boolean): void;
186
186
  /**
187
187
  * User logout
188
188
  * @param clearToken Clear refresh token or not
@@ -278,6 +278,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
278
278
  * @param name Application name
279
279
  */
280
280
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, name: string);
281
+ protected setApi(api: IApi): void;
281
282
  /**
282
283
  * Alert action result
283
284
  * @param result Action result
@@ -414,7 +415,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
414
415
  * @param refreshToken Refresh token
415
416
  * @param keep Keep in local storage or not
416
417
  */
417
- userLogin(user: IUserData, refreshToken?: string, keep?: boolean): void;
418
+ userLogin(user: IUserData, refreshToken: string, keep?: boolean): void;
418
419
  /**
419
420
  * User logout
420
421
  * @param clearToken Clear refresh token or not
@@ -32,38 +32,11 @@ class CoreApp {
32
32
  * Token refresh count down seed
33
33
  */
34
34
  this.refreshCountdownSeed = 0;
35
- // onRequest, show loading or not, rewrite the property to override default action
36
- api.onRequest = (data) => {
37
- if (data.showLoading == null || data.showLoading) {
38
- notifier.showLoading();
39
- }
40
- };
41
- // onComplete, hide loading, rewrite the property to override default action
42
- api.onComplete = (data) => {
43
- if (data.showLoading == null || data.showLoading) {
44
- notifier.hideLoading();
45
- }
46
- this.lastCalled = true;
47
- };
48
- // Global API error handler
49
- api.onError = (error) => {
50
- // Error code
51
- const status = error.response
52
- ? api.transformResponse(error.response).status
53
- : undefined;
54
- if (status === 401) {
55
- // When status is equal to 401, unauthorized, try login
56
- this.tryLogin();
57
- }
58
- else {
59
- // Report the error
60
- notifier.alert(this.formatError(error));
61
- }
62
- };
63
35
  this.settings = settings;
64
36
  this.api = api;
65
37
  this.notifier = notifier;
66
38
  this.name = name;
39
+ this.setApi(api);
67
40
  const { currentCulture, currentRegion } = settings;
68
41
  this.changeCulture(currentCulture);
69
42
  this.changeRegion(currentRegion);
@@ -103,6 +76,36 @@ class CoreApp {
103
76
  set authorized(value) {
104
77
  this._authorized = value;
105
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
+ }
106
109
  /**
107
110
  * Alert action result
108
111
  * @param result Action result
@@ -24,12 +24,11 @@ export interface IExternalSettings {
24
24
  readonly coreApi?: string;
25
25
  }
26
26
  /**
27
- * External settings host
28
- * Usually passed by window global settings property
27
+ * External settings namespace
29
28
  */
30
- export interface IExternalSettingsHost {
29
+ export declare namespace ExternalSettings {
31
30
  /**
32
- * Configurable API settings
31
+ * Create instance
33
32
  */
34
- readonly settings: IExternalSettings;
33
+ function Create(): IExternalSettings | undefined;
35
34
  }
@@ -1,2 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExternalSettings = void 0;
4
+ /**
5
+ * External settings namespace
6
+ */
7
+ var ExternalSettings;
8
+ (function (ExternalSettings) {
9
+ /**
10
+ * Create instance
11
+ */
12
+ function Create() {
13
+ if ('settings' in globalThis) {
14
+ const settings = Reflect.get(globalThis, 'settings');
15
+ if (typeof settings === 'object') {
16
+ if (typeof window !== 'undefined') {
17
+ const hostname = window.location.hostname;
18
+ // replace {hostname}
19
+ for (const key in settings) {
20
+ const value = settings[key];
21
+ if (typeof value === 'string') {
22
+ settings[key] = value.replace('{hostname}', hostname);
23
+ }
24
+ }
25
+ }
26
+ return settings;
27
+ }
28
+ }
29
+ return undefined;
30
+ }
31
+ ExternalSettings.Create = Create;
32
+ })(ExternalSettings = exports.ExternalSettings || (exports.ExternalSettings = {}));
@@ -22,10 +22,6 @@ 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;
29
25
  /**
30
26
  * Time zone
31
27
  */
@@ -182,7 +182,7 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
182
182
  * @param refreshToken Refresh token
183
183
  * @param keep Keep in local storage or not
184
184
  */
185
- userLogin(user: IUserData, refreshToken?: string, keep?: boolean): void;
185
+ userLogin(user: IUserData, refreshToken: string, keep?: boolean): void;
186
186
  /**
187
187
  * User logout
188
188
  * @param clearToken Clear refresh token or not
@@ -278,6 +278,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
278
278
  * @param name Application name
279
279
  */
280
280
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, name: string);
281
+ protected setApi(api: IApi): void;
281
282
  /**
282
283
  * Alert action result
283
284
  * @param result Action result
@@ -414,7 +415,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
414
415
  * @param refreshToken Refresh token
415
416
  * @param keep Keep in local storage or not
416
417
  */
417
- userLogin(user: IUserData, refreshToken?: string, keep?: boolean): void;
418
+ userLogin(user: IUserData, refreshToken: string, keep?: boolean): void;
418
419
  /**
419
420
  * User logout
420
421
  * @param clearToken Clear refresh token or not
@@ -29,38 +29,11 @@ export class CoreApp {
29
29
  * Token refresh count down seed
30
30
  */
31
31
  this.refreshCountdownSeed = 0;
32
- // onRequest, show loading or not, rewrite the property to override default action
33
- api.onRequest = (data) => {
34
- if (data.showLoading == null || data.showLoading) {
35
- notifier.showLoading();
36
- }
37
- };
38
- // onComplete, hide loading, rewrite the property to override default action
39
- api.onComplete = (data) => {
40
- if (data.showLoading == null || data.showLoading) {
41
- notifier.hideLoading();
42
- }
43
- this.lastCalled = true;
44
- };
45
- // Global API error handler
46
- api.onError = (error) => {
47
- // Error code
48
- const status = error.response
49
- ? api.transformResponse(error.response).status
50
- : undefined;
51
- if (status === 401) {
52
- // When status is equal to 401, unauthorized, try login
53
- this.tryLogin();
54
- }
55
- else {
56
- // Report the error
57
- notifier.alert(this.formatError(error));
58
- }
59
- };
60
32
  this.settings = settings;
61
33
  this.api = api;
62
34
  this.notifier = notifier;
63
35
  this.name = name;
36
+ this.setApi(api);
64
37
  const { currentCulture, currentRegion } = settings;
65
38
  this.changeCulture(currentCulture);
66
39
  this.changeRegion(currentRegion);
@@ -100,6 +73,36 @@ export class CoreApp {
100
73
  set authorized(value) {
101
74
  this._authorized = value;
102
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
+ }
103
106
  /**
104
107
  * Alert action result
105
108
  * @param result Action result
@@ -24,12 +24,11 @@ export interface IExternalSettings {
24
24
  readonly coreApi?: string;
25
25
  }
26
26
  /**
27
- * External settings host
28
- * Usually passed by window global settings property
27
+ * External settings namespace
29
28
  */
30
- export interface IExternalSettingsHost {
29
+ export declare namespace ExternalSettings {
31
30
  /**
32
- * Configurable API settings
31
+ * Create instance
33
32
  */
34
- readonly settings: IExternalSettings;
33
+ function Create(): IExternalSettings | undefined;
35
34
  }
@@ -1 +1,29 @@
1
- export {};
1
+ /**
2
+ * External settings namespace
3
+ */
4
+ export var ExternalSettings;
5
+ (function (ExternalSettings) {
6
+ /**
7
+ * Create instance
8
+ */
9
+ function Create() {
10
+ if ('settings' in globalThis) {
11
+ const settings = Reflect.get(globalThis, 'settings');
12
+ if (typeof settings === 'object') {
13
+ if (typeof window !== 'undefined') {
14
+ const hostname = window.location.hostname;
15
+ // replace {hostname}
16
+ for (const key in settings) {
17
+ const value = settings[key];
18
+ if (typeof value === 'string') {
19
+ settings[key] = value.replace('{hostname}', hostname);
20
+ }
21
+ }
22
+ }
23
+ return settings;
24
+ }
25
+ }
26
+ return undefined;
27
+ }
28
+ ExternalSettings.Create = Create;
29
+ })(ExternalSettings || (ExternalSettings = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.1.44",
3
+ "version": "1.1.48",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,7 +54,7 @@
54
54
  "dependencies": {
55
55
  "@etsoo/notificationbase": "^1.0.93",
56
56
  "@etsoo/restclient": "^1.0.62",
57
- "@etsoo/shared": "^1.0.73"
57
+ "@etsoo/shared": "^1.0.75"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@babel/cli": "^7.16.0",
@@ -62,14 +62,14 @@
62
62
  "@babel/plugin-transform-runtime": "^7.16.4",
63
63
  "@babel/preset-env": "^7.16.4",
64
64
  "@babel/runtime-corejs3": "^7.16.3",
65
- "@types/jest": "^27.0.2",
65
+ "@types/jest": "^27.0.3",
66
66
  "@typescript-eslint/eslint-plugin": "^5.4.0",
67
67
  "@typescript-eslint/parser": "^5.4.0",
68
- "eslint": "^8.2.0",
68
+ "eslint": "^8.3.0",
69
69
  "eslint-config-airbnb-base": "^15.0.0",
70
70
  "eslint-plugin-import": "^2.25.3",
71
71
  "jest": "^27.3.1",
72
72
  "ts-jest": "^27.0.7",
73
- "typescript": "^4.4.4"
73
+ "typescript": "^4.5.2"
74
74
  }
75
75
  }
@@ -27,11 +27,6 @@ export interface IAppSettings extends IExternalSettings {
27
27
  */
28
28
  readonly detectedCulture: string;
29
29
 
30
- /**
31
- * Service id
32
- */
33
- readonly serviceId?: string;
34
-
35
30
  /**
36
31
  * Time zone
37
32
  */
@@ -244,7 +244,7 @@ export interface ICoreApp<
244
244
  * @param refreshToken Refresh token
245
245
  * @param keep Keep in local storage or not
246
246
  */
247
- userLogin(user: IUserData, refreshToken?: string, keep?: boolean): void;
247
+ userLogin(user: IUserData, refreshToken: string, keep?: boolean): void;
248
248
 
249
249
  /**
250
250
  * User logout
@@ -385,17 +385,34 @@ export abstract class CoreApp<
385
385
  notifier: INotifier<N, C>,
386
386
  name: string
387
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) {
388
405
  // onRequest, show loading or not, rewrite the property to override default action
389
406
  api.onRequest = (data) => {
390
407
  if (data.showLoading == null || data.showLoading) {
391
- notifier.showLoading();
408
+ this.notifier.showLoading();
392
409
  }
393
410
  };
394
411
 
395
412
  // onComplete, hide loading, rewrite the property to override default action
396
413
  api.onComplete = (data) => {
397
414
  if (data.showLoading == null || data.showLoading) {
398
- notifier.hideLoading();
415
+ this.notifier.hideLoading();
399
416
  }
400
417
  this.lastCalled = true;
401
418
  };
@@ -412,22 +429,9 @@ export abstract class CoreApp<
412
429
  this.tryLogin();
413
430
  } else {
414
431
  // Report the error
415
- notifier.alert(this.formatError(error));
432
+ this.notifier.alert(this.formatError(error));
416
433
  }
417
434
  };
418
-
419
- this.settings = settings;
420
- this.api = api;
421
- this.notifier = notifier;
422
- this.name = name;
423
-
424
- const { currentCulture, currentRegion } = settings;
425
- this.changeCulture(currentCulture);
426
-
427
- this.changeRegion(currentRegion);
428
-
429
- // Setup callback
430
- this.setup();
431
435
  }
432
436
 
433
437
  /**
@@ -825,7 +829,7 @@ export abstract class CoreApp<
825
829
  * @param refreshToken Refresh token
826
830
  * @param keep Keep in local storage or not
827
831
  */
828
- userLogin(user: IUserData, refreshToken?: string, keep: boolean = false) {
832
+ userLogin(user: IUserData, refreshToken: string, keep: boolean = false) {
829
833
  this.userData = user;
830
834
  this.authorize(user.token, refreshToken, keep);
831
835
  }
@@ -29,12 +29,33 @@ export interface IExternalSettings {
29
29
  }
30
30
 
31
31
  /**
32
- * External settings host
33
- * Usually passed by window global settings property
32
+ * External settings namespace
34
33
  */
35
- export interface IExternalSettingsHost {
34
+ export namespace ExternalSettings {
36
35
  /**
37
- * Configurable API settings
36
+ * Create instance
38
37
  */
39
- readonly settings: IExternalSettings;
38
+ export function Create(): IExternalSettings | undefined {
39
+ if ('settings' in globalThis) {
40
+ const settings = Reflect.get(globalThis, 'settings');
41
+ if (typeof settings === 'object') {
42
+ if (typeof window !== 'undefined') {
43
+ const hostname = window.location.hostname;
44
+ // replace {hostname}
45
+ for (const key in settings) {
46
+ const value = settings[key];
47
+ if (typeof value === 'string') {
48
+ settings[key] = value.replace(
49
+ '{hostname}',
50
+ hostname
51
+ );
52
+ }
53
+ }
54
+ }
55
+
56
+ return settings as IExternalSettings;
57
+ }
58
+ }
59
+ return undefined;
60
+ }
40
61
  }