@etsoo/appscript 1.2.47 → 1.2.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.
@@ -43,6 +43,16 @@ export interface RefreshTokenProps<D extends {}> {
43
43
  */
44
44
  showLoading?: boolean;
45
45
  }
46
+ /**
47
+ * App fields
48
+ */
49
+ declare const appFields: readonly ["headerToken", "serversideDeviceId", "deviceId", "devices", "devicePassphrase"];
50
+ /**
51
+ * Basic type template
52
+ */
53
+ export declare type IAppFields = {
54
+ [key in typeof appFields[number]]: string;
55
+ };
46
56
  /**
47
57
  * Core application interface
48
58
  */
@@ -51,6 +61,10 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
51
61
  * Settings
52
62
  */
53
63
  readonly settings: S;
64
+ /**
65
+ * Fields
66
+ */
67
+ readonly fields: IAppFields;
54
68
  /**
55
69
  * API
56
70
  */
@@ -371,6 +385,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
371
385
  * Settings
372
386
  */
373
387
  readonly settings: S;
388
+ /**
389
+ * Fields
390
+ */
391
+ readonly fields: IAppFields;
374
392
  /**
375
393
  * API
376
394
  */
@@ -467,6 +485,12 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
467
485
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
468
486
  private getDeviceId;
469
487
  private resetKeys;
488
+ /**
489
+ * Add app name as identifier
490
+ * @param field Field
491
+ * @returns Result
492
+ */
493
+ protected addIdentifier(field: string): string;
470
494
  /**
471
495
  * Restore settings from persisted source
472
496
  */
@@ -475,12 +499,6 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
475
499
  * Persist settings to source when application exit
476
500
  */
477
501
  persist(): void;
478
- /**
479
- * Add app name as identifier
480
- * @param field Field
481
- * @returns Result
482
- */
483
- protected addIdentifier(field: string): string;
484
502
  /**
485
503
  * Setup Api
486
504
  * @param api Api
@@ -779,25 +797,4 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
779
797
  */
780
798
  warning(message: NotificationContent<N>, align?: NotificationAlign): void;
781
799
  }
782
- export declare namespace CoreApp {
783
- /**
784
- * Response token header field name
785
- */
786
- const headerTokenField = "SmartERPRefreshToken";
787
- /**
788
- * Serverside device id encrypted field name
789
- */
790
- const serversideDeviceIdField = "SmartERPServersideDeviceId";
791
- /**
792
- * Device id field name
793
- */
794
- const deviceIdField = "SmartERPDeviceId";
795
- /**
796
- * Devices field name
797
- */
798
- const devicesField = "SmartERPDevices";
799
- /**
800
- * Device passphrase field name
801
- */
802
- const devicePassphraseField = "SmartERPDevicePassphrase";
803
- }
800
+ export {};
@@ -9,6 +9,16 @@ const AddressRegion_1 = require("../address/AddressRegion");
9
9
  const AddressUtils_1 = require("../address/AddressUtils");
10
10
  const BusinessUtils_1 = require("../business/BusinessUtils");
11
11
  const ActionResultError_1 = require("../result/ActionResultError");
12
+ /**
13
+ * App fields
14
+ */
15
+ const appFields = [
16
+ 'headerToken',
17
+ 'serversideDeviceId',
18
+ 'deviceId',
19
+ 'devices',
20
+ 'devicePassphrase'
21
+ ];
12
22
  /**
13
23
  * Core application
14
24
  */
@@ -45,8 +55,10 @@ class CoreApp {
45
55
  this.notifier = notifier;
46
56
  this.storage = storage;
47
57
  this.name = name;
58
+ // Fields, attach with the name identifier
59
+ this.fields = appFields.reduce((a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }), {});
48
60
  // Device id
49
- this._deviceId = storage.getData(this.addIdentifier(CoreApp.deviceIdField), '');
61
+ this._deviceId = storage.getData(this.fields.deviceId, '');
50
62
  // Restore
51
63
  this.restore();
52
64
  this.setApi(api);
@@ -118,10 +130,10 @@ class CoreApp {
118
130
  */
119
131
  get persistedFields() {
120
132
  return [
121
- this.addIdentifier(CoreApp.deviceIdField),
122
- this.addIdentifier(CoreApp.devicePassphraseField),
123
- this.addIdentifier(CoreApp.serversideDeviceIdField),
124
- this.addIdentifier(CoreApp.headerTokenField)
133
+ this.fields.deviceId,
134
+ this.fields.devicePassphrase,
135
+ this.fields.serversideDeviceId,
136
+ this.fields.headerToken
125
137
  ];
126
138
  }
127
139
  getDeviceId() {
@@ -129,23 +141,31 @@ class CoreApp {
129
141
  }
130
142
  resetKeys() {
131
143
  this.storage.clear([
132
- this.addIdentifier(CoreApp.devicePassphraseField),
133
- this.addIdentifier(CoreApp.headerTokenField),
134
- this.addIdentifier(CoreApp.serversideDeviceIdField)
144
+ this.fields.devicePassphrase,
145
+ this.fields.headerToken,
146
+ this.fields.serversideDeviceId
135
147
  ], false);
136
148
  this.passphrase = '';
137
149
  }
150
+ /**
151
+ * Add app name as identifier
152
+ * @param field Field
153
+ * @returns Result
154
+ */
155
+ addIdentifier(field) {
156
+ return field + '-' + this.name;
157
+ }
138
158
  /**
139
159
  * Restore settings from persisted source
140
160
  */
141
161
  restore() {
142
162
  // Devices
143
- const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
163
+ const devices = this.storage.getPersistedData(this.fields.devices, []);
144
164
  if (this.deviceId === '') {
145
165
  // First vist, restore and keep the source
146
166
  this.storage.copyFrom(this.persistedFields, false);
147
167
  // Reset device id
148
- this._deviceId = this.storage.getData(this.addIdentifier(CoreApp.deviceIdField), '');
168
+ this._deviceId = this.storage.getData(this.fields.deviceId, '');
149
169
  // Totally new, no data restored
150
170
  if (this._deviceId === '')
151
171
  return false;
@@ -159,13 +179,13 @@ class CoreApp {
159
179
  return false;
160
180
  }
161
181
  // this.name to identifier different app's secret
162
- const passphraseEncrypted = this.storage.getData(this.addIdentifier(CoreApp.devicePassphraseField));
182
+ const passphraseEncrypted = this.storage.getData(this.fields.devicePassphrase);
163
183
  if (passphraseEncrypted) {
164
184
  const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
165
185
  if (passphraseDecrypted != null) {
166
186
  // Add the device to the list
167
187
  devices.push(d);
168
- this.storage.setPersistedData(CoreApp.devicesField, devices);
188
+ this.storage.setPersistedData(this.fields.devices, devices);
169
189
  this.passphrase = passphraseDecrypted;
170
190
  return true;
171
191
  }
@@ -179,27 +199,19 @@ class CoreApp {
179
199
  */
180
200
  persist() {
181
201
  // Devices
182
- const devices = this.storage.getPersistedData(CoreApp.devicesField);
202
+ const devices = this.storage.getPersistedData(this.fields.devices);
183
203
  if (devices != null) {
184
204
  const index = devices.indexOf(this.getDeviceId());
185
205
  if (index !== -1) {
186
206
  // Remove current device from the list
187
207
  devices.splice(index, 1);
188
- this.storage.setPersistedData(CoreApp.devicesField, devices);
208
+ this.storage.setPersistedData(this.fields.devices, devices);
189
209
  }
190
210
  }
191
211
  if (!this.authorized)
192
212
  return;
193
213
  this.storage.copyTo(this.persistedFields);
194
214
  }
195
- /**
196
- * Add app name as identifier
197
- * @param field Field
198
- * @returns Result
199
- */
200
- addIdentifier(field) {
201
- return field + '-' + this.name;
202
- }
203
215
  /**
204
216
  * Setup Api
205
217
  * @param api Api
@@ -256,7 +268,7 @@ class CoreApp {
256
268
  return;
257
269
  }
258
270
  // Serverside encrypted device id
259
- const identifier = this.storage.getData(this.addIdentifier(CoreApp.serversideDeviceIdField));
271
+ const identifier = this.storage.getData(this.fields.serversideDeviceId);
260
272
  // Timestamp
261
273
  const timestamp = new Date().getTime();
262
274
  // Request data
@@ -292,7 +304,7 @@ class CoreApp {
292
304
  if (callback)
293
305
  callback(false);
294
306
  // Clear device id
295
- this.storage.setData(this.addIdentifier(CoreApp.deviceIdField), undefined);
307
+ this.storage.setData(this.fields.deviceId, undefined);
296
308
  return;
297
309
  }
298
310
  this.initCallUpdate(result.data, data.timestamp);
@@ -315,14 +327,14 @@ class CoreApp {
315
327
  return;
316
328
  // Update device id and cache it
317
329
  this._deviceId = data.deviceId;
318
- this.storage.setData(this.addIdentifier(CoreApp.deviceIdField), this._deviceId);
330
+ this.storage.setData(this.fields.deviceId, this._deviceId);
319
331
  // Devices
320
- const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
332
+ const devices = this.storage.getPersistedData(this.fields.devices, []);
321
333
  devices.push(this.getDeviceId());
322
- this.storage.setPersistedData(CoreApp.devicesField, devices);
334
+ this.storage.setPersistedData(this.fields.devices, devices);
323
335
  // Current passphrase
324
336
  this.passphrase = passphrase;
325
- this.storage.setData(this.addIdentifier(CoreApp.devicePassphraseField), this.encrypt(passphrase, this.name));
337
+ this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
326
338
  // Previous passphrase
327
339
  if (data.previousPassphrase) {
328
340
  const prev = this.decrypt(data.previousPassphrase, timestamp.toString());
@@ -354,7 +366,7 @@ class CoreApp {
354
366
  * @returns Fields
355
367
  */
356
368
  initCallEncryptedUpdateFields() {
357
- return [this.addIdentifier(CoreApp.headerTokenField)];
369
+ return [this.fields.headerToken];
358
370
  }
359
371
  /**
360
372
  * Alert action result
@@ -379,7 +391,7 @@ class CoreApp {
379
391
  if (refreshToken !== '') {
380
392
  if (refreshToken != null)
381
393
  refreshToken = this.encrypt(refreshToken);
382
- this.storage.setData(this.addIdentifier(CoreApp.headerTokenField), refreshToken);
394
+ this.storage.setData(this.fields.headerToken, refreshToken);
383
395
  }
384
396
  // Reset tryLogin state
385
397
  this._isTryingLogin = false;
@@ -452,14 +464,14 @@ class CoreApp {
452
464
  */
453
465
  clearCacheData() {
454
466
  this.clearCacheToken();
455
- this.storage.setData(this.addIdentifier(CoreApp.devicePassphraseField), undefined);
467
+ this.storage.setData(this.fields.devicePassphrase, undefined);
456
468
  }
457
469
  /**
458
470
  * Clear cached token
459
471
  */
460
472
  clearCacheToken() {
461
473
  this.cachedRefreshToken = undefined;
462
- this.storage.setPersistedData(this.addIdentifier(CoreApp.headerTokenField), undefined);
474
+ this.storage.setPersistedData(this.fields.headerToken, undefined);
463
475
  }
464
476
  /**
465
477
  * Decrypt message
@@ -714,7 +726,7 @@ class CoreApp {
714
726
  // Temp refresh token
715
727
  if (this.cachedRefreshToken)
716
728
  return this.cachedRefreshToken;
717
- return this.storage.getData(this.addIdentifier(CoreApp.headerTokenField));
729
+ return this.storage.getData(this.fields.headerToken);
718
730
  }
719
731
  /**
720
732
  * Get all regions
@@ -741,7 +753,7 @@ class CoreApp {
741
753
  */
742
754
  getResponseToken(rawResponse) {
743
755
  const response = this.api.transformResponse(rawResponse);
744
- return this.api.getHeaderValue(response.headers, CoreApp.headerTokenField);
756
+ return this.api.getHeaderValue(response.headers, 'SmartERPRefreshToken');
745
757
  }
746
758
  /**
747
759
  * Get time zone
@@ -960,7 +972,7 @@ class CoreApp {
960
972
  userLogin(user, refreshToken, keep) {
961
973
  this.userData = user;
962
974
  // Cache the encrypted serverside device id
963
- this.storage.setData(this.addIdentifier(CoreApp.serversideDeviceIdField), user.deviceId);
975
+ this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
964
976
  if (keep) {
965
977
  this.authorize(user.token, refreshToken);
966
978
  }
@@ -998,25 +1010,3 @@ class CoreApp {
998
1010
  }
999
1011
  }
1000
1012
  exports.CoreApp = CoreApp;
1001
- (function (CoreApp) {
1002
- /**
1003
- * Response token header field name
1004
- */
1005
- CoreApp.headerTokenField = 'SmartERPRefreshToken';
1006
- /**
1007
- * Serverside device id encrypted field name
1008
- */
1009
- CoreApp.serversideDeviceIdField = 'SmartERPServersideDeviceId';
1010
- /**
1011
- * Device id field name
1012
- */
1013
- CoreApp.deviceIdField = 'SmartERPDeviceId';
1014
- /**
1015
- * Devices field name
1016
- */
1017
- CoreApp.devicesField = 'SmartERPDevices';
1018
- /**
1019
- * Device passphrase field name
1020
- */
1021
- CoreApp.devicePassphraseField = 'SmartERPDevicePassphrase';
1022
- })(CoreApp = exports.CoreApp || (exports.CoreApp = {}));
@@ -43,6 +43,16 @@ export interface RefreshTokenProps<D extends {}> {
43
43
  */
44
44
  showLoading?: boolean;
45
45
  }
46
+ /**
47
+ * App fields
48
+ */
49
+ declare const appFields: readonly ["headerToken", "serversideDeviceId", "deviceId", "devices", "devicePassphrase"];
50
+ /**
51
+ * Basic type template
52
+ */
53
+ export declare type IAppFields = {
54
+ [key in typeof appFields[number]]: string;
55
+ };
46
56
  /**
47
57
  * Core application interface
48
58
  */
@@ -51,6 +61,10 @@ export interface ICoreApp<S extends IAppSettings, N, C extends NotificationCallP
51
61
  * Settings
52
62
  */
53
63
  readonly settings: S;
64
+ /**
65
+ * Fields
66
+ */
67
+ readonly fields: IAppFields;
54
68
  /**
55
69
  * API
56
70
  */
@@ -371,6 +385,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
371
385
  * Settings
372
386
  */
373
387
  readonly settings: S;
388
+ /**
389
+ * Fields
390
+ */
391
+ readonly fields: IAppFields;
374
392
  /**
375
393
  * API
376
394
  */
@@ -467,6 +485,12 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
467
485
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
468
486
  private getDeviceId;
469
487
  private resetKeys;
488
+ /**
489
+ * Add app name as identifier
490
+ * @param field Field
491
+ * @returns Result
492
+ */
493
+ protected addIdentifier(field: string): string;
470
494
  /**
471
495
  * Restore settings from persisted source
472
496
  */
@@ -475,12 +499,6 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
475
499
  * Persist settings to source when application exit
476
500
  */
477
501
  persist(): void;
478
- /**
479
- * Add app name as identifier
480
- * @param field Field
481
- * @returns Result
482
- */
483
- protected addIdentifier(field: string): string;
484
502
  /**
485
503
  * Setup Api
486
504
  * @param api Api
@@ -779,25 +797,4 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
779
797
  */
780
798
  warning(message: NotificationContent<N>, align?: NotificationAlign): void;
781
799
  }
782
- export declare namespace CoreApp {
783
- /**
784
- * Response token header field name
785
- */
786
- const headerTokenField = "SmartERPRefreshToken";
787
- /**
788
- * Serverside device id encrypted field name
789
- */
790
- const serversideDeviceIdField = "SmartERPServersideDeviceId";
791
- /**
792
- * Device id field name
793
- */
794
- const deviceIdField = "SmartERPDeviceId";
795
- /**
796
- * Devices field name
797
- */
798
- const devicesField = "SmartERPDevices";
799
- /**
800
- * Device passphrase field name
801
- */
802
- const devicePassphraseField = "SmartERPDevicePassphrase";
803
- }
800
+ export {};
@@ -6,6 +6,16 @@ import { AddressRegion } from '../address/AddressRegion';
6
6
  import { AddressUtils } from '../address/AddressUtils';
7
7
  import { BusinessUtils } from '../business/BusinessUtils';
8
8
  import { ActionResultError } from '../result/ActionResultError';
9
+ /**
10
+ * App fields
11
+ */
12
+ const appFields = [
13
+ 'headerToken',
14
+ 'serversideDeviceId',
15
+ 'deviceId',
16
+ 'devices',
17
+ 'devicePassphrase'
18
+ ];
9
19
  /**
10
20
  * Core application
11
21
  */
@@ -42,8 +52,10 @@ export class CoreApp {
42
52
  this.notifier = notifier;
43
53
  this.storage = storage;
44
54
  this.name = name;
55
+ // Fields, attach with the name identifier
56
+ this.fields = appFields.reduce((a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }), {});
45
57
  // Device id
46
- this._deviceId = storage.getData(this.addIdentifier(CoreApp.deviceIdField), '');
58
+ this._deviceId = storage.getData(this.fields.deviceId, '');
47
59
  // Restore
48
60
  this.restore();
49
61
  this.setApi(api);
@@ -115,10 +127,10 @@ export class CoreApp {
115
127
  */
116
128
  get persistedFields() {
117
129
  return [
118
- this.addIdentifier(CoreApp.deviceIdField),
119
- this.addIdentifier(CoreApp.devicePassphraseField),
120
- this.addIdentifier(CoreApp.serversideDeviceIdField),
121
- this.addIdentifier(CoreApp.headerTokenField)
130
+ this.fields.deviceId,
131
+ this.fields.devicePassphrase,
132
+ this.fields.serversideDeviceId,
133
+ this.fields.headerToken
122
134
  ];
123
135
  }
124
136
  getDeviceId() {
@@ -126,23 +138,31 @@ export class CoreApp {
126
138
  }
127
139
  resetKeys() {
128
140
  this.storage.clear([
129
- this.addIdentifier(CoreApp.devicePassphraseField),
130
- this.addIdentifier(CoreApp.headerTokenField),
131
- this.addIdentifier(CoreApp.serversideDeviceIdField)
141
+ this.fields.devicePassphrase,
142
+ this.fields.headerToken,
143
+ this.fields.serversideDeviceId
132
144
  ], false);
133
145
  this.passphrase = '';
134
146
  }
147
+ /**
148
+ * Add app name as identifier
149
+ * @param field Field
150
+ * @returns Result
151
+ */
152
+ addIdentifier(field) {
153
+ return field + '-' + this.name;
154
+ }
135
155
  /**
136
156
  * Restore settings from persisted source
137
157
  */
138
158
  restore() {
139
159
  // Devices
140
- const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
160
+ const devices = this.storage.getPersistedData(this.fields.devices, []);
141
161
  if (this.deviceId === '') {
142
162
  // First vist, restore and keep the source
143
163
  this.storage.copyFrom(this.persistedFields, false);
144
164
  // Reset device id
145
- this._deviceId = this.storage.getData(this.addIdentifier(CoreApp.deviceIdField), '');
165
+ this._deviceId = this.storage.getData(this.fields.deviceId, '');
146
166
  // Totally new, no data restored
147
167
  if (this._deviceId === '')
148
168
  return false;
@@ -156,13 +176,13 @@ export class CoreApp {
156
176
  return false;
157
177
  }
158
178
  // this.name to identifier different app's secret
159
- const passphraseEncrypted = this.storage.getData(this.addIdentifier(CoreApp.devicePassphraseField));
179
+ const passphraseEncrypted = this.storage.getData(this.fields.devicePassphrase);
160
180
  if (passphraseEncrypted) {
161
181
  const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
162
182
  if (passphraseDecrypted != null) {
163
183
  // Add the device to the list
164
184
  devices.push(d);
165
- this.storage.setPersistedData(CoreApp.devicesField, devices);
185
+ this.storage.setPersistedData(this.fields.devices, devices);
166
186
  this.passphrase = passphraseDecrypted;
167
187
  return true;
168
188
  }
@@ -176,27 +196,19 @@ export class CoreApp {
176
196
  */
177
197
  persist() {
178
198
  // Devices
179
- const devices = this.storage.getPersistedData(CoreApp.devicesField);
199
+ const devices = this.storage.getPersistedData(this.fields.devices);
180
200
  if (devices != null) {
181
201
  const index = devices.indexOf(this.getDeviceId());
182
202
  if (index !== -1) {
183
203
  // Remove current device from the list
184
204
  devices.splice(index, 1);
185
- this.storage.setPersistedData(CoreApp.devicesField, devices);
205
+ this.storage.setPersistedData(this.fields.devices, devices);
186
206
  }
187
207
  }
188
208
  if (!this.authorized)
189
209
  return;
190
210
  this.storage.copyTo(this.persistedFields);
191
211
  }
192
- /**
193
- * Add app name as identifier
194
- * @param field Field
195
- * @returns Result
196
- */
197
- addIdentifier(field) {
198
- return field + '-' + this.name;
199
- }
200
212
  /**
201
213
  * Setup Api
202
214
  * @param api Api
@@ -253,7 +265,7 @@ export class CoreApp {
253
265
  return;
254
266
  }
255
267
  // Serverside encrypted device id
256
- const identifier = this.storage.getData(this.addIdentifier(CoreApp.serversideDeviceIdField));
268
+ const identifier = this.storage.getData(this.fields.serversideDeviceId);
257
269
  // Timestamp
258
270
  const timestamp = new Date().getTime();
259
271
  // Request data
@@ -289,7 +301,7 @@ export class CoreApp {
289
301
  if (callback)
290
302
  callback(false);
291
303
  // Clear device id
292
- this.storage.setData(this.addIdentifier(CoreApp.deviceIdField), undefined);
304
+ this.storage.setData(this.fields.deviceId, undefined);
293
305
  return;
294
306
  }
295
307
  this.initCallUpdate(result.data, data.timestamp);
@@ -312,14 +324,14 @@ export class CoreApp {
312
324
  return;
313
325
  // Update device id and cache it
314
326
  this._deviceId = data.deviceId;
315
- this.storage.setData(this.addIdentifier(CoreApp.deviceIdField), this._deviceId);
327
+ this.storage.setData(this.fields.deviceId, this._deviceId);
316
328
  // Devices
317
- const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
329
+ const devices = this.storage.getPersistedData(this.fields.devices, []);
318
330
  devices.push(this.getDeviceId());
319
- this.storage.setPersistedData(CoreApp.devicesField, devices);
331
+ this.storage.setPersistedData(this.fields.devices, devices);
320
332
  // Current passphrase
321
333
  this.passphrase = passphrase;
322
- this.storage.setData(this.addIdentifier(CoreApp.devicePassphraseField), this.encrypt(passphrase, this.name));
334
+ this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
323
335
  // Previous passphrase
324
336
  if (data.previousPassphrase) {
325
337
  const prev = this.decrypt(data.previousPassphrase, timestamp.toString());
@@ -351,7 +363,7 @@ export class CoreApp {
351
363
  * @returns Fields
352
364
  */
353
365
  initCallEncryptedUpdateFields() {
354
- return [this.addIdentifier(CoreApp.headerTokenField)];
366
+ return [this.fields.headerToken];
355
367
  }
356
368
  /**
357
369
  * Alert action result
@@ -376,7 +388,7 @@ export class CoreApp {
376
388
  if (refreshToken !== '') {
377
389
  if (refreshToken != null)
378
390
  refreshToken = this.encrypt(refreshToken);
379
- this.storage.setData(this.addIdentifier(CoreApp.headerTokenField), refreshToken);
391
+ this.storage.setData(this.fields.headerToken, refreshToken);
380
392
  }
381
393
  // Reset tryLogin state
382
394
  this._isTryingLogin = false;
@@ -449,14 +461,14 @@ export class CoreApp {
449
461
  */
450
462
  clearCacheData() {
451
463
  this.clearCacheToken();
452
- this.storage.setData(this.addIdentifier(CoreApp.devicePassphraseField), undefined);
464
+ this.storage.setData(this.fields.devicePassphrase, undefined);
453
465
  }
454
466
  /**
455
467
  * Clear cached token
456
468
  */
457
469
  clearCacheToken() {
458
470
  this.cachedRefreshToken = undefined;
459
- this.storage.setPersistedData(this.addIdentifier(CoreApp.headerTokenField), undefined);
471
+ this.storage.setPersistedData(this.fields.headerToken, undefined);
460
472
  }
461
473
  /**
462
474
  * Decrypt message
@@ -711,7 +723,7 @@ export class CoreApp {
711
723
  // Temp refresh token
712
724
  if (this.cachedRefreshToken)
713
725
  return this.cachedRefreshToken;
714
- return this.storage.getData(this.addIdentifier(CoreApp.headerTokenField));
726
+ return this.storage.getData(this.fields.headerToken);
715
727
  }
716
728
  /**
717
729
  * Get all regions
@@ -738,7 +750,7 @@ export class CoreApp {
738
750
  */
739
751
  getResponseToken(rawResponse) {
740
752
  const response = this.api.transformResponse(rawResponse);
741
- return this.api.getHeaderValue(response.headers, CoreApp.headerTokenField);
753
+ return this.api.getHeaderValue(response.headers, 'SmartERPRefreshToken');
742
754
  }
743
755
  /**
744
756
  * Get time zone
@@ -957,7 +969,7 @@ export class CoreApp {
957
969
  userLogin(user, refreshToken, keep) {
958
970
  this.userData = user;
959
971
  // Cache the encrypted serverside device id
960
- this.storage.setData(this.addIdentifier(CoreApp.serversideDeviceIdField), user.deviceId);
972
+ this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
961
973
  if (keep) {
962
974
  this.authorize(user.token, refreshToken);
963
975
  }
@@ -994,25 +1006,3 @@ export class CoreApp {
994
1006
  });
995
1007
  }
996
1008
  }
997
- (function (CoreApp) {
998
- /**
999
- * Response token header field name
1000
- */
1001
- CoreApp.headerTokenField = 'SmartERPRefreshToken';
1002
- /**
1003
- * Serverside device id encrypted field name
1004
- */
1005
- CoreApp.serversideDeviceIdField = 'SmartERPServersideDeviceId';
1006
- /**
1007
- * Device id field name
1008
- */
1009
- CoreApp.deviceIdField = 'SmartERPDeviceId';
1010
- /**
1011
- * Devices field name
1012
- */
1013
- CoreApp.devicesField = 'SmartERPDevices';
1014
- /**
1015
- * Device passphrase field name
1016
- */
1017
- CoreApp.devicePassphraseField = 'SmartERPDevicePassphrase';
1018
- })(CoreApp || (CoreApp = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.47",
3
+ "version": "1.2.48",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -83,6 +83,22 @@ export interface RefreshTokenProps<D extends {}> {
83
83
  showLoading?: boolean;
84
84
  }
85
85
 
86
+ /**
87
+ * App fields
88
+ */
89
+ const appFields = [
90
+ 'headerToken',
91
+ 'serversideDeviceId',
92
+ 'deviceId',
93
+ 'devices',
94
+ 'devicePassphrase'
95
+ ] as const;
96
+
97
+ /**
98
+ * Basic type template
99
+ */
100
+ export type IAppFields = { [key in typeof appFields[number]]: string };
101
+
86
102
  /**
87
103
  * Core application interface
88
104
  */
@@ -96,6 +112,11 @@ export interface ICoreApp<
96
112
  */
97
113
  readonly settings: S;
98
114
 
115
+ /**
116
+ * Fields
117
+ */
118
+ readonly fields: IAppFields;
119
+
99
120
  /**
100
121
  * API
101
122
  */
@@ -502,6 +523,11 @@ export abstract class CoreApp<
502
523
  */
503
524
  readonly settings: S;
504
525
 
526
+ /**
527
+ * Fields
528
+ */
529
+ readonly fields: IAppFields;
530
+
505
531
  /**
506
532
  * API
507
533
  */
@@ -632,10 +658,10 @@ export abstract class CoreApp<
632
658
  */
633
659
  protected get persistedFields() {
634
660
  return [
635
- this.addIdentifier(CoreApp.deviceIdField),
636
- this.addIdentifier(CoreApp.devicePassphraseField),
637
- this.addIdentifier(CoreApp.serversideDeviceIdField),
638
- this.addIdentifier(CoreApp.headerTokenField)
661
+ this.fields.deviceId,
662
+ this.fields.devicePassphrase,
663
+ this.fields.serversideDeviceId,
664
+ this.fields.headerToken
639
665
  ];
640
666
  }
641
667
 
@@ -660,12 +686,15 @@ export abstract class CoreApp<
660
686
  this.storage = storage;
661
687
  this.name = name;
662
688
 
663
- // Device id
664
- this._deviceId = storage.getData(
665
- this.addIdentifier(CoreApp.deviceIdField),
666
- ''
689
+ // Fields, attach with the name identifier
690
+ this.fields = appFields.reduce(
691
+ (a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }),
692
+ {} as any
667
693
  );
668
694
 
695
+ // Device id
696
+ this._deviceId = storage.getData(this.fields.deviceId, '');
697
+
669
698
  // Restore
670
699
  this.restore();
671
700
 
@@ -687,22 +716,31 @@ export abstract class CoreApp<
687
716
  private resetKeys() {
688
717
  this.storage.clear(
689
718
  [
690
- this.addIdentifier(CoreApp.devicePassphraseField),
691
- this.addIdentifier(CoreApp.headerTokenField),
692
- this.addIdentifier(CoreApp.serversideDeviceIdField)
719
+ this.fields.devicePassphrase,
720
+ this.fields.headerToken,
721
+ this.fields.serversideDeviceId
693
722
  ],
694
723
  false
695
724
  );
696
725
  this.passphrase = '';
697
726
  }
698
727
 
728
+ /**
729
+ * Add app name as identifier
730
+ * @param field Field
731
+ * @returns Result
732
+ */
733
+ protected addIdentifier(field: string) {
734
+ return field + '-' + this.name;
735
+ }
736
+
699
737
  /**
700
738
  * Restore settings from persisted source
701
739
  */
702
740
  protected restore() {
703
741
  // Devices
704
742
  const devices = this.storage.getPersistedData<string[]>(
705
- CoreApp.devicesField,
743
+ this.fields.devices,
706
744
  []
707
745
  );
708
746
 
@@ -711,10 +749,7 @@ export abstract class CoreApp<
711
749
  this.storage.copyFrom(this.persistedFields, false);
712
750
 
713
751
  // Reset device id
714
- this._deviceId = this.storage.getData(
715
- this.addIdentifier(CoreApp.deviceIdField),
716
- ''
717
- );
752
+ this._deviceId = this.storage.getData(this.fields.deviceId, '');
718
753
 
719
754
  // Totally new, no data restored
720
755
  if (this._deviceId === '') return false;
@@ -731,7 +766,7 @@ export abstract class CoreApp<
731
766
 
732
767
  // this.name to identifier different app's secret
733
768
  const passphraseEncrypted = this.storage.getData<string>(
734
- this.addIdentifier(CoreApp.devicePassphraseField)
769
+ this.fields.devicePassphrase
735
770
  );
736
771
  if (passphraseEncrypted) {
737
772
  const passphraseDecrypted = this.decrypt(
@@ -741,7 +776,7 @@ export abstract class CoreApp<
741
776
  if (passphraseDecrypted != null) {
742
777
  // Add the device to the list
743
778
  devices.push(d);
744
- this.storage.setPersistedData(CoreApp.devicesField, devices);
779
+ this.storage.setPersistedData(this.fields.devices, devices);
745
780
 
746
781
  this.passphrase = passphraseDecrypted;
747
782
 
@@ -761,14 +796,14 @@ export abstract class CoreApp<
761
796
  persist() {
762
797
  // Devices
763
798
  const devices = this.storage.getPersistedData<string[]>(
764
- CoreApp.devicesField
799
+ this.fields.devices
765
800
  );
766
801
  if (devices != null) {
767
802
  const index = devices.indexOf(this.getDeviceId());
768
803
  if (index !== -1) {
769
804
  // Remove current device from the list
770
805
  devices.splice(index, 1);
771
- this.storage.setPersistedData(CoreApp.devicesField, devices);
806
+ this.storage.setPersistedData(this.fields.devices, devices);
772
807
  }
773
808
  }
774
809
 
@@ -776,15 +811,6 @@ export abstract class CoreApp<
776
811
  this.storage.copyTo(this.persistedFields);
777
812
  }
778
813
 
779
- /**
780
- * Add app name as identifier
781
- * @param field Field
782
- * @returns Result
783
- */
784
- protected addIdentifier(field: string) {
785
- return field + '-' + this.name;
786
- }
787
-
788
814
  /**
789
815
  * Setup Api
790
816
  * @param api Api
@@ -845,7 +871,7 @@ export abstract class CoreApp<
845
871
 
846
872
  // Serverside encrypted device id
847
873
  const identifier = this.storage.getData<string>(
848
- this.addIdentifier(CoreApp.serversideDeviceIdField)
874
+ this.fields.serversideDeviceId
849
875
  );
850
876
 
851
877
  // Timestamp
@@ -890,10 +916,7 @@ export abstract class CoreApp<
890
916
  if (callback) callback(false);
891
917
 
892
918
  // Clear device id
893
- this.storage.setData(
894
- this.addIdentifier(CoreApp.deviceIdField),
895
- undefined
896
- );
919
+ this.storage.setData(this.fields.deviceId, undefined);
897
920
 
898
921
  return;
899
922
  }
@@ -919,23 +942,20 @@ export abstract class CoreApp<
919
942
 
920
943
  // Update device id and cache it
921
944
  this._deviceId = data.deviceId;
922
- this.storage.setData(
923
- this.addIdentifier(CoreApp.deviceIdField),
924
- this._deviceId
925
- );
945
+ this.storage.setData(this.fields.deviceId, this._deviceId);
926
946
 
927
947
  // Devices
928
948
  const devices = this.storage.getPersistedData<string[]>(
929
- CoreApp.devicesField,
949
+ this.fields.devices,
930
950
  []
931
951
  );
932
952
  devices.push(this.getDeviceId());
933
- this.storage.setPersistedData(CoreApp.devicesField, devices);
953
+ this.storage.setPersistedData(this.fields.devices, devices);
934
954
 
935
955
  // Current passphrase
936
956
  this.passphrase = passphrase;
937
957
  this.storage.setData(
938
- this.addIdentifier(CoreApp.devicePassphraseField),
958
+ this.fields.devicePassphrase,
939
959
  this.encrypt(passphrase, this.name)
940
960
  );
941
961
 
@@ -981,7 +1001,7 @@ export abstract class CoreApp<
981
1001
  * @returns Fields
982
1002
  */
983
1003
  protected initCallEncryptedUpdateFields(): string[] {
984
- return [this.addIdentifier(CoreApp.headerTokenField)];
1004
+ return [this.fields.headerToken];
985
1005
  }
986
1006
 
987
1007
  /**
@@ -1009,10 +1029,7 @@ export abstract class CoreApp<
1009
1029
  // Cover the current value
1010
1030
  if (refreshToken !== '') {
1011
1031
  if (refreshToken != null) refreshToken = this.encrypt(refreshToken);
1012
- this.storage.setData(
1013
- this.addIdentifier(CoreApp.headerTokenField),
1014
- refreshToken
1015
- );
1032
+ this.storage.setData(this.fields.headerToken, refreshToken);
1016
1033
  }
1017
1034
 
1018
1035
  // Reset tryLogin state
@@ -1100,10 +1117,7 @@ export abstract class CoreApp<
1100
1117
  */
1101
1118
  clearCacheData() {
1102
1119
  this.clearCacheToken();
1103
- this.storage.setData(
1104
- this.addIdentifier(CoreApp.devicePassphraseField),
1105
- undefined
1106
- );
1120
+ this.storage.setData(this.fields.devicePassphrase, undefined);
1107
1121
  }
1108
1122
 
1109
1123
  /**
@@ -1111,10 +1125,7 @@ export abstract class CoreApp<
1111
1125
  */
1112
1126
  clearCacheToken() {
1113
1127
  this.cachedRefreshToken = undefined;
1114
- this.storage.setPersistedData(
1115
- this.addIdentifier(CoreApp.headerTokenField),
1116
- undefined
1117
- );
1128
+ this.storage.setPersistedData(this.fields.headerToken, undefined);
1118
1129
  }
1119
1130
 
1120
1131
  /**
@@ -1426,9 +1437,7 @@ export abstract class CoreApp<
1426
1437
  getCacheToken(): string | undefined {
1427
1438
  // Temp refresh token
1428
1439
  if (this.cachedRefreshToken) return this.cachedRefreshToken;
1429
- return this.storage.getData<string>(
1430
- this.addIdentifier(CoreApp.headerTokenField)
1431
- );
1440
+ return this.storage.getData<string>(this.fields.headerToken);
1432
1441
  }
1433
1442
 
1434
1443
  /**
@@ -1462,7 +1471,7 @@ export abstract class CoreApp<
1462
1471
  const response = this.api.transformResponse(rawResponse);
1463
1472
  return this.api.getHeaderValue(
1464
1473
  response.headers,
1465
- CoreApp.headerTokenField
1474
+ 'SmartERPRefreshToken'
1466
1475
  );
1467
1476
  }
1468
1477
 
@@ -1714,10 +1723,7 @@ export abstract class CoreApp<
1714
1723
  this.userData = user;
1715
1724
 
1716
1725
  // Cache the encrypted serverside device id
1717
- this.storage.setData(
1718
- this.addIdentifier(CoreApp.serversideDeviceIdField),
1719
- user.deviceId
1720
- );
1726
+ this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
1721
1727
 
1722
1728
  if (keep) {
1723
1729
  this.authorize(user.token, refreshToken);
@@ -1764,30 +1770,3 @@ export abstract class CoreApp<
1764
1770
  );
1765
1771
  }
1766
1772
  }
1767
-
1768
- export namespace CoreApp {
1769
- /**
1770
- * Response token header field name
1771
- */
1772
- export const headerTokenField = 'SmartERPRefreshToken';
1773
-
1774
- /**
1775
- * Serverside device id encrypted field name
1776
- */
1777
- export const serversideDeviceIdField = 'SmartERPServersideDeviceId';
1778
-
1779
- /**
1780
- * Device id field name
1781
- */
1782
- export const deviceIdField = 'SmartERPDeviceId';
1783
-
1784
- /**
1785
- * Devices field name
1786
- */
1787
- export const devicesField = 'SmartERPDevices';
1788
-
1789
- /**
1790
- * Device passphrase field name
1791
- */
1792
- export const devicePassphraseField = 'SmartERPDevicePassphrase';
1793
- }