@etsoo/appscript 1.2.1 → 1.2.6

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.
@@ -431,6 +431,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
431
431
  */
432
432
  protected passphrase: string;
433
433
  private cachedRefreshToken?;
434
+ /**
435
+ * Get persisted fields
436
+ */
437
+ protected get persistedFields(): string[];
434
438
  /**
435
439
  * Protected constructor
436
440
  * @param settings Settings
@@ -440,6 +444,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
440
444
  * @param name Application name
441
445
  */
442
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
+ private getDeviceId;
443
448
  /**
444
449
  * Restore settings from persisted source
445
450
  */
@@ -739,6 +744,10 @@ export declare namespace CoreApp {
739
744
  * Device id field name
740
745
  */
741
746
  const deviceIdField = "SmartERPDeviceId";
747
+ /**
748
+ * Devices field name
749
+ */
750
+ const devicesField = "SmartERPDevices";
742
751
  /**
743
752
  * Device passphrase field name
744
753
  */
@@ -44,10 +44,10 @@ class CoreApp {
44
44
  this.notifier = notifier;
45
45
  this.storage = storage;
46
46
  this.name = name;
47
- // Restore
48
- this.restore();
49
47
  // Device id
50
48
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
49
+ // Restore
50
+ this.restore();
51
51
  this.setApi(api);
52
52
  const { currentCulture, currentRegion } = settings;
53
53
  this.changeCulture(currentCulture);
@@ -112,33 +112,68 @@ class CoreApp {
112
112
  set authorized(value) {
113
113
  this._authorized = value;
114
114
  }
115
+ /**
116
+ * Get persisted fields
117
+ */
118
+ get persistedFields() {
119
+ return [
120
+ CoreApp.deviceIdField,
121
+ CoreApp.devicePassphraseField,
122
+ CoreApp.serversideDeviceIdField,
123
+ CoreApp.headerTokenField
124
+ ];
125
+ }
126
+ getDeviceId() {
127
+ return this.deviceId.substring(0, 15);
128
+ }
115
129
  /**
116
130
  * Restore settings from persisted source
117
131
  */
118
132
  restore() {
119
- const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
120
- if (passphraseEncrypted) {
121
- const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
122
- if (passphraseDecrypted != null) {
123
- this.passphrase = passphraseDecrypted;
124
- return false;
133
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
134
+ if (this.deviceId) {
135
+ // Devices
136
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
137
+ // Exists in the list?
138
+ const d = this.getDeviceId();
139
+ if (!devices.includes(d)) {
140
+ const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
141
+ if (passphraseEncrypted) {
142
+ const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
143
+ if (passphraseDecrypted != null) {
144
+ this.passphrase = passphraseDecrypted;
145
+ devices.push(d);
146
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
147
+ return false;
148
+ }
149
+ }
150
+ }
151
+ else {
152
+ // Remove passphrase
153
+ this.storage.setData(CoreApp.devicePassphraseField, undefined);
154
+ this.passphrase = '';
125
155
  }
126
156
  }
127
157
  // Restore
128
- this.storage.copy([
129
- CoreApp.deviceIdField,
130
- CoreApp.serversideDeviceIdField,
131
- CoreApp.headerTokenField
132
- ], true);
158
+ this.storage.copyFrom(this.persistedFields, true);
133
159
  return true;
134
160
  }
135
161
  /**
136
162
  * Persist settings to source when application exit
137
163
  */
138
164
  persist() {
139
- this.storage.setPersistedData(CoreApp.deviceIdField, this.deviceId);
140
- this.storage.setPersistedData(CoreApp.serversideDeviceIdField, this.storage.getData(CoreApp.serversideDeviceIdField));
141
- this.storage.setPersistedData(CoreApp.headerTokenField, this.storage.getData(CoreApp.headerTokenField));
165
+ // Devices
166
+ const devices = this.storage.getPersistedData(CoreApp.devicesField);
167
+ if (devices != null) {
168
+ const index = devices.indexOf(this.getDeviceId());
169
+ if (index !== -1) {
170
+ devices.splice(index, 1);
171
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
172
+ }
173
+ }
174
+ if (!this.authorized)
175
+ return;
176
+ this.storage.copyTo(this.persistedFields);
142
177
  }
143
178
  /**
144
179
  * Setup Api
@@ -256,6 +291,10 @@ class CoreApp {
256
291
  // Update device id and cache it
257
292
  this._deviceId = data.deviceId;
258
293
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
294
+ // Devices
295
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
296
+ devices.push(this.getDeviceId());
297
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
259
298
  // Current passphrase
260
299
  this.passphrase = passphrase;
261
300
  this.storage.setData(CoreApp.devicePassphraseField, this.encrypt(passphrase, this.name));
@@ -386,17 +425,15 @@ class CoreApp {
386
425
  * Clear cache data
387
426
  */
388
427
  clearCacheData() {
389
- this.storage.setData(CoreApp.serversideDeviceIdField, undefined);
390
- this.storage.setData(CoreApp.deviceIdField, undefined);
428
+ this.clearCacheToken();
391
429
  this.storage.setData(CoreApp.devicePassphraseField, undefined);
392
- this.storage.setData(CoreApp.headerTokenField, undefined);
393
430
  }
394
431
  /**
395
432
  * Clear cached token
396
433
  */
397
434
  clearCacheToken() {
398
435
  this.cachedRefreshToken = undefined;
399
- this.storage.setData(CoreApp.headerTokenField, undefined);
436
+ this.storage.setPersistedData(CoreApp.headerTokenField, undefined);
400
437
  }
401
438
  /**
402
439
  * Decrypt message
@@ -913,6 +950,10 @@ exports.CoreApp = CoreApp;
913
950
  * Device id field name
914
951
  */
915
952
  CoreApp.deviceIdField = 'SmartERPDeviceId';
953
+ /**
954
+ * Devices field name
955
+ */
956
+ CoreApp.devicesField = 'SmartERPDevices';
916
957
  /**
917
958
  * Device passphrase field name
918
959
  */
@@ -431,6 +431,10 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
431
431
  */
432
432
  protected passphrase: string;
433
433
  private cachedRefreshToken?;
434
+ /**
435
+ * Get persisted fields
436
+ */
437
+ protected get persistedFields(): string[];
434
438
  /**
435
439
  * Protected constructor
436
440
  * @param settings Settings
@@ -440,6 +444,7 @@ export declare abstract class CoreApp<S extends IAppSettings, N, C extends Notif
440
444
  * @param name Application name
441
445
  */
442
446
  protected constructor(settings: S, api: IApi, notifier: INotifier<N, C>, storage: IStorage, name: string);
447
+ private getDeviceId;
443
448
  /**
444
449
  * Restore settings from persisted source
445
450
  */
@@ -739,6 +744,10 @@ export declare namespace CoreApp {
739
744
  * Device id field name
740
745
  */
741
746
  const deviceIdField = "SmartERPDeviceId";
747
+ /**
748
+ * Devices field name
749
+ */
750
+ const devicesField = "SmartERPDevices";
742
751
  /**
743
752
  * Device passphrase field name
744
753
  */
@@ -41,10 +41,10 @@ export class CoreApp {
41
41
  this.notifier = notifier;
42
42
  this.storage = storage;
43
43
  this.name = name;
44
- // Restore
45
- this.restore();
46
44
  // Device id
47
45
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
46
+ // Restore
47
+ this.restore();
48
48
  this.setApi(api);
49
49
  const { currentCulture, currentRegion } = settings;
50
50
  this.changeCulture(currentCulture);
@@ -109,33 +109,68 @@ export class CoreApp {
109
109
  set authorized(value) {
110
110
  this._authorized = value;
111
111
  }
112
+ /**
113
+ * Get persisted fields
114
+ */
115
+ get persistedFields() {
116
+ return [
117
+ CoreApp.deviceIdField,
118
+ CoreApp.devicePassphraseField,
119
+ CoreApp.serversideDeviceIdField,
120
+ CoreApp.headerTokenField
121
+ ];
122
+ }
123
+ getDeviceId() {
124
+ return this.deviceId.substring(0, 15);
125
+ }
112
126
  /**
113
127
  * Restore settings from persisted source
114
128
  */
115
129
  restore() {
116
- const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
117
- if (passphraseEncrypted) {
118
- const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
119
- if (passphraseDecrypted != null) {
120
- this.passphrase = passphraseDecrypted;
121
- return false;
130
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
131
+ if (this.deviceId) {
132
+ // Devices
133
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
134
+ // Exists in the list?
135
+ const d = this.getDeviceId();
136
+ if (!devices.includes(d)) {
137
+ const passphraseEncrypted = this.storage.getData(CoreApp.devicePassphraseField);
138
+ if (passphraseEncrypted) {
139
+ const passphraseDecrypted = this.decrypt(passphraseEncrypted, this.name);
140
+ if (passphraseDecrypted != null) {
141
+ this.passphrase = passphraseDecrypted;
142
+ devices.push(d);
143
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
144
+ return false;
145
+ }
146
+ }
147
+ }
148
+ else {
149
+ // Remove passphrase
150
+ this.storage.setData(CoreApp.devicePassphraseField, undefined);
151
+ this.passphrase = '';
122
152
  }
123
153
  }
124
154
  // Restore
125
- this.storage.copy([
126
- CoreApp.deviceIdField,
127
- CoreApp.serversideDeviceIdField,
128
- CoreApp.headerTokenField
129
- ], true);
155
+ this.storage.copyFrom(this.persistedFields, true);
130
156
  return true;
131
157
  }
132
158
  /**
133
159
  * Persist settings to source when application exit
134
160
  */
135
161
  persist() {
136
- this.storage.setPersistedData(CoreApp.deviceIdField, this.deviceId);
137
- this.storage.setPersistedData(CoreApp.serversideDeviceIdField, this.storage.getData(CoreApp.serversideDeviceIdField));
138
- this.storage.setPersistedData(CoreApp.headerTokenField, this.storage.getData(CoreApp.headerTokenField));
162
+ // Devices
163
+ const devices = this.storage.getPersistedData(CoreApp.devicesField);
164
+ if (devices != null) {
165
+ const index = devices.indexOf(this.getDeviceId());
166
+ if (index !== -1) {
167
+ devices.splice(index, 1);
168
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
169
+ }
170
+ }
171
+ if (!this.authorized)
172
+ return;
173
+ this.storage.copyTo(this.persistedFields);
139
174
  }
140
175
  /**
141
176
  * Setup Api
@@ -253,6 +288,10 @@ export class CoreApp {
253
288
  // Update device id and cache it
254
289
  this._deviceId = data.deviceId;
255
290
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
291
+ // Devices
292
+ const devices = this.storage.getPersistedData(CoreApp.devicesField, []);
293
+ devices.push(this.getDeviceId());
294
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
256
295
  // Current passphrase
257
296
  this.passphrase = passphrase;
258
297
  this.storage.setData(CoreApp.devicePassphraseField, this.encrypt(passphrase, this.name));
@@ -383,17 +422,15 @@ export class CoreApp {
383
422
  * Clear cache data
384
423
  */
385
424
  clearCacheData() {
386
- this.storage.setData(CoreApp.serversideDeviceIdField, undefined);
387
- this.storage.setData(CoreApp.deviceIdField, undefined);
425
+ this.clearCacheToken();
388
426
  this.storage.setData(CoreApp.devicePassphraseField, undefined);
389
- this.storage.setData(CoreApp.headerTokenField, undefined);
390
427
  }
391
428
  /**
392
429
  * Clear cached token
393
430
  */
394
431
  clearCacheToken() {
395
432
  this.cachedRefreshToken = undefined;
396
- this.storage.setData(CoreApp.headerTokenField, undefined);
433
+ this.storage.setPersistedData(CoreApp.headerTokenField, undefined);
397
434
  }
398
435
  /**
399
436
  * Decrypt message
@@ -909,6 +946,10 @@ export class CoreApp {
909
946
  * Device id field name
910
947
  */
911
948
  CoreApp.deviceIdField = 'SmartERPDeviceId';
949
+ /**
950
+ * Devices field name
951
+ */
952
+ CoreApp.devicesField = 'SmartERPDevices';
912
953
  /**
913
954
  * Device passphrase field name
914
955
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.2.1",
3
+ "version": "1.2.6",
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.95",
56
56
  "@etsoo/restclient": "^1.0.63",
57
- "@etsoo/shared": "^1.0.93",
57
+ "@etsoo/shared": "^1.0.94",
58
58
  "@types/crypto-js": "^4.0.2",
59
59
  "crypto-js": "^4.1.1"
60
60
  },
@@ -600,6 +600,18 @@ export abstract class CoreApp<
600
600
 
601
601
  private cachedRefreshToken?: string;
602
602
 
603
+ /**
604
+ * Get persisted fields
605
+ */
606
+ protected get persistedFields() {
607
+ return [
608
+ CoreApp.deviceIdField,
609
+ CoreApp.devicePassphraseField,
610
+ CoreApp.serversideDeviceIdField,
611
+ CoreApp.headerTokenField
612
+ ];
613
+ }
614
+
603
615
  /**
604
616
  * Protected constructor
605
617
  * @param settings Settings
@@ -621,12 +633,12 @@ export abstract class CoreApp<
621
633
  this.storage = storage;
622
634
  this.name = name;
623
635
 
624
- // Restore
625
- this.restore();
626
-
627
636
  // Device id
628
637
  this._deviceId = storage.getData(CoreApp.deviceIdField, '');
629
638
 
639
+ // Restore
640
+ this.restore();
641
+
630
642
  this.setApi(api);
631
643
 
632
644
  const { currentCulture, currentRegion } = settings;
@@ -638,33 +650,54 @@ export abstract class CoreApp<
638
650
  this.setup();
639
651
  }
640
652
 
653
+ private getDeviceId() {
654
+ return this.deviceId.substring(0, 15);
655
+ }
656
+
641
657
  /**
642
658
  * Restore settings from persisted source
643
659
  */
644
660
  protected restore() {
645
- const passphraseEncrypted = this.storage.getData<string>(
646
- CoreApp.devicePassphraseField
647
- );
648
- if (passphraseEncrypted) {
649
- const passphraseDecrypted = this.decrypt(
650
- passphraseEncrypted,
651
- this.name
661
+ // Current device id, '' means new, or reload (not included) or duplicate (included)
662
+ if (this.deviceId) {
663
+ // Devices
664
+ const devices = this.storage.getPersistedData<string[]>(
665
+ CoreApp.devicesField,
666
+ []
652
667
  );
653
- if (passphraseDecrypted != null) {
654
- this.passphrase = passphraseDecrypted;
655
- return false;
668
+
669
+ // Exists in the list?
670
+ const d = this.getDeviceId();
671
+ if (!devices.includes(d)) {
672
+ const passphraseEncrypted = this.storage.getData<string>(
673
+ CoreApp.devicePassphraseField
674
+ );
675
+ if (passphraseEncrypted) {
676
+ const passphraseDecrypted = this.decrypt(
677
+ passphraseEncrypted,
678
+ this.name
679
+ );
680
+ if (passphraseDecrypted != null) {
681
+ this.passphrase = passphraseDecrypted;
682
+
683
+ devices.push(d);
684
+ this.storage.setPersistedData(
685
+ CoreApp.devicesField,
686
+ devices
687
+ );
688
+
689
+ return false;
690
+ }
691
+ }
692
+ } else {
693
+ // Remove passphrase
694
+ this.storage.setData(CoreApp.devicePassphraseField, undefined);
695
+ this.passphrase = '';
656
696
  }
657
697
  }
658
698
 
659
699
  // Restore
660
- this.storage.copy(
661
- [
662
- CoreApp.deviceIdField,
663
- CoreApp.serversideDeviceIdField,
664
- CoreApp.headerTokenField
665
- ],
666
- true
667
- );
700
+ this.storage.copyFrom(this.persistedFields, true);
668
701
 
669
702
  return true;
670
703
  }
@@ -673,17 +706,20 @@ export abstract class CoreApp<
673
706
  * Persist settings to source when application exit
674
707
  */
675
708
  persist() {
676
- this.storage.setPersistedData(CoreApp.deviceIdField, this.deviceId);
677
-
678
- this.storage.setPersistedData(
679
- CoreApp.serversideDeviceIdField,
680
- this.storage.getData<string>(CoreApp.serversideDeviceIdField)
709
+ // Devices
710
+ const devices = this.storage.getPersistedData<string[]>(
711
+ CoreApp.devicesField
681
712
  );
713
+ if (devices != null) {
714
+ const index = devices.indexOf(this.getDeviceId());
715
+ if (index !== -1) {
716
+ devices.splice(index, 1);
717
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
718
+ }
719
+ }
682
720
 
683
- this.storage.setPersistedData(
684
- CoreApp.headerTokenField,
685
- this.storage.getData<string>(CoreApp.headerTokenField)
686
- );
721
+ if (!this.authorized) return;
722
+ this.storage.copyTo(this.persistedFields);
687
723
  }
688
724
 
689
725
  /**
@@ -819,6 +855,14 @@ export abstract class CoreApp<
819
855
  this._deviceId = data.deviceId;
820
856
  this.storage.setData(CoreApp.deviceIdField, this._deviceId);
821
857
 
858
+ // Devices
859
+ const devices = this.storage.getPersistedData<string[]>(
860
+ CoreApp.devicesField,
861
+ []
862
+ );
863
+ devices.push(this.getDeviceId());
864
+ this.storage.setPersistedData(CoreApp.devicesField, devices);
865
+
822
866
  // Current passphrase
823
867
  this.passphrase = passphrase;
824
868
  this.storage.setData(
@@ -982,12 +1026,8 @@ export abstract class CoreApp<
982
1026
  * Clear cache data
983
1027
  */
984
1028
  clearCacheData() {
985
- this.storage.setData(CoreApp.serversideDeviceIdField, undefined);
986
-
987
- this.storage.setData(CoreApp.deviceIdField, undefined);
1029
+ this.clearCacheToken();
988
1030
  this.storage.setData(CoreApp.devicePassphraseField, undefined);
989
-
990
- this.storage.setData(CoreApp.headerTokenField, undefined);
991
1031
  }
992
1032
 
993
1033
  /**
@@ -995,7 +1035,7 @@ export abstract class CoreApp<
995
1035
  */
996
1036
  clearCacheToken() {
997
1037
  this.cachedRefreshToken = undefined;
998
- this.storage.setData(CoreApp.headerTokenField, undefined);
1038
+ this.storage.setPersistedData(CoreApp.headerTokenField, undefined);
999
1039
  }
1000
1040
 
1001
1041
  /**
@@ -1617,6 +1657,11 @@ export namespace CoreApp {
1617
1657
  */
1618
1658
  export const deviceIdField = 'SmartERPDeviceId';
1619
1659
 
1660
+ /**
1661
+ * Devices field name
1662
+ */
1663
+ export const devicesField = 'SmartERPDevices';
1664
+
1620
1665
  /**
1621
1666
  * Device passphrase field name
1622
1667
  */