@etsoo/appscript 1.2.45 → 1.2.49

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.
@@ -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
- CoreApp.deviceIdField,
636
- this.addIdentifier(CoreApp.devicePassphraseField),
637
- CoreApp.serversideDeviceIdField,
638
- 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,8 +686,14 @@ export abstract class CoreApp<
660
686
  this.storage = storage;
661
687
  this.name = name;
662
688
 
689
+ // Fields, attach with the name identifier
690
+ this.fields = appFields.reduce(
691
+ (a, v) => ({ ...a, [v]: 'smarterp-' + v + '-' + name }),
692
+ {} as any
693
+ );
694
+
663
695
  // Device id
664
- this._deviceId = storage.getData(CoreApp.deviceIdField, '');
696
+ this._deviceId = storage.getData(this.fields.deviceId, '');
665
697
 
666
698
  // Restore
667
699
  this.restore();
@@ -684,22 +716,31 @@ export abstract class CoreApp<
684
716
  private resetKeys() {
685
717
  this.storage.clear(
686
718
  [
687
- this.addIdentifier(CoreApp.devicePassphraseField),
688
- CoreApp.headerTokenField,
689
- CoreApp.serversideDeviceIdField
719
+ this.fields.devicePassphrase,
720
+ this.fields.headerToken,
721
+ this.fields.serversideDeviceId
690
722
  ],
691
723
  false
692
724
  );
693
725
  this.passphrase = '';
694
726
  }
695
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
+
696
737
  /**
697
738
  * Restore settings from persisted source
698
739
  */
699
740
  protected restore() {
700
741
  // Devices
701
742
  const devices = this.storage.getPersistedData<string[]>(
702
- CoreApp.devicesField,
743
+ this.fields.devices,
703
744
  []
704
745
  );
705
746
 
@@ -708,7 +749,7 @@ export abstract class CoreApp<
708
749
  this.storage.copyFrom(this.persistedFields, false);
709
750
 
710
751
  // Reset device id
711
- this._deviceId = this.storage.getData(CoreApp.deviceIdField, '');
752
+ this._deviceId = this.storage.getData(this.fields.deviceId, '');
712
753
 
713
754
  // Totally new, no data restored
714
755
  if (this._deviceId === '') return false;
@@ -725,7 +766,7 @@ export abstract class CoreApp<
725
766
 
726
767
  // this.name to identifier different app's secret
727
768
  const passphraseEncrypted = this.storage.getData<string>(
728
- this.addIdentifier(CoreApp.devicePassphraseField)
769
+ this.fields.devicePassphrase
729
770
  );
730
771
  if (passphraseEncrypted) {
731
772
  const passphraseDecrypted = this.decrypt(
@@ -735,7 +776,7 @@ export abstract class CoreApp<
735
776
  if (passphraseDecrypted != null) {
736
777
  // Add the device to the list
737
778
  devices.push(d);
738
- this.storage.setPersistedData(CoreApp.devicesField, devices);
779
+ this.storage.setPersistedData(this.fields.devices, devices);
739
780
 
740
781
  this.passphrase = passphraseDecrypted;
741
782
 
@@ -755,14 +796,14 @@ export abstract class CoreApp<
755
796
  persist() {
756
797
  // Devices
757
798
  const devices = this.storage.getPersistedData<string[]>(
758
- CoreApp.devicesField
799
+ this.fields.devices
759
800
  );
760
801
  if (devices != null) {
761
802
  const index = devices.indexOf(this.getDeviceId());
762
803
  if (index !== -1) {
763
804
  // Remove current device from the list
764
805
  devices.splice(index, 1);
765
- this.storage.setPersistedData(CoreApp.devicesField, devices);
806
+ this.storage.setPersistedData(this.fields.devices, devices);
766
807
  }
767
808
  }
768
809
 
@@ -770,15 +811,6 @@ export abstract class CoreApp<
770
811
  this.storage.copyTo(this.persistedFields);
771
812
  }
772
813
 
773
- /**
774
- * Add app name as identifier
775
- * @param field Field
776
- * @returns Result
777
- */
778
- protected addIdentifier(field: string) {
779
- return field + '-' + this.name;
780
- }
781
-
782
814
  /**
783
815
  * Setup Api
784
816
  * @param api Api
@@ -839,7 +871,7 @@ export abstract class CoreApp<
839
871
 
840
872
  // Serverside encrypted device id
841
873
  const identifier = this.storage.getData<string>(
842
- CoreApp.serversideDeviceIdField
874
+ this.fields.serversideDeviceId
843
875
  );
844
876
 
845
877
  // Timestamp
@@ -884,7 +916,7 @@ export abstract class CoreApp<
884
916
  if (callback) callback(false);
885
917
 
886
918
  // Clear device id
887
- this.storage.setData(CoreApp.deviceIdField, undefined);
919
+ this.storage.setData(this.fields.deviceId, undefined);
888
920
 
889
921
  return;
890
922
  }
@@ -910,20 +942,20 @@ export abstract class CoreApp<
910
942
 
911
943
  // Update device id and cache it
912
944
  this._deviceId = data.deviceId;
913
- this.storage.setData(CoreApp.deviceIdField, this._deviceId);
945
+ this.storage.setData(this.fields.deviceId, this._deviceId);
914
946
 
915
947
  // Devices
916
948
  const devices = this.storage.getPersistedData<string[]>(
917
- CoreApp.devicesField,
949
+ this.fields.devices,
918
950
  []
919
951
  );
920
952
  devices.push(this.getDeviceId());
921
- this.storage.setPersistedData(CoreApp.devicesField, devices);
953
+ this.storage.setPersistedData(this.fields.devices, devices);
922
954
 
923
955
  // Current passphrase
924
956
  this.passphrase = passphrase;
925
957
  this.storage.setData(
926
- this.addIdentifier(CoreApp.devicePassphraseField),
958
+ this.fields.devicePassphrase,
927
959
  this.encrypt(passphrase, this.name)
928
960
  );
929
961
 
@@ -969,7 +1001,7 @@ export abstract class CoreApp<
969
1001
  * @returns Fields
970
1002
  */
971
1003
  protected initCallEncryptedUpdateFields(): string[] {
972
- return [CoreApp.headerTokenField];
1004
+ return [this.fields.headerToken];
973
1005
  }
974
1006
 
975
1007
  /**
@@ -997,7 +1029,7 @@ export abstract class CoreApp<
997
1029
  // Cover the current value
998
1030
  if (refreshToken !== '') {
999
1031
  if (refreshToken != null) refreshToken = this.encrypt(refreshToken);
1000
- this.storage.setData(CoreApp.headerTokenField, refreshToken);
1032
+ this.storage.setData(this.fields.headerToken, refreshToken);
1001
1033
  }
1002
1034
 
1003
1035
  // Reset tryLogin state
@@ -1085,10 +1117,7 @@ export abstract class CoreApp<
1085
1117
  */
1086
1118
  clearCacheData() {
1087
1119
  this.clearCacheToken();
1088
- this.storage.setData(
1089
- this.addIdentifier(CoreApp.devicePassphraseField),
1090
- undefined
1091
- );
1120
+ this.storage.setData(this.fields.devicePassphrase, undefined);
1092
1121
  }
1093
1122
 
1094
1123
  /**
@@ -1096,7 +1125,7 @@ export abstract class CoreApp<
1096
1125
  */
1097
1126
  clearCacheToken() {
1098
1127
  this.cachedRefreshToken = undefined;
1099
- this.storage.setPersistedData(CoreApp.headerTokenField, undefined);
1128
+ this.storage.setPersistedData(this.fields.headerToken, undefined);
1100
1129
  }
1101
1130
 
1102
1131
  /**
@@ -1408,7 +1437,7 @@ export abstract class CoreApp<
1408
1437
  getCacheToken(): string | undefined {
1409
1438
  // Temp refresh token
1410
1439
  if (this.cachedRefreshToken) return this.cachedRefreshToken;
1411
- return this.storage.getData<string>(CoreApp.headerTokenField);
1440
+ return this.storage.getData<string>(this.fields.headerToken);
1412
1441
  }
1413
1442
 
1414
1443
  /**
@@ -1442,7 +1471,7 @@ export abstract class CoreApp<
1442
1471
  const response = this.api.transformResponse(rawResponse);
1443
1472
  return this.api.getHeaderValue(
1444
1473
  response.headers,
1445
- CoreApp.headerTokenField
1474
+ 'SmartERPRefreshToken'
1446
1475
  );
1447
1476
  }
1448
1477
 
@@ -1677,8 +1706,9 @@ export abstract class CoreApp<
1677
1706
  * Try login, returning false means is loading
1678
1707
  * UI get involved while refreshToken not intended
1679
1708
  * @param data Additional request data
1709
+ * @param showLoading Show loading bar or not during call
1680
1710
  */
1681
- async tryLogin<D extends {} = {}>(_data?: D) {
1711
+ async tryLogin<D extends {} = {}>(_data?: D, _showLoading?: boolean) {
1682
1712
  if (this._isTryingLogin) return false;
1683
1713
  this._isTryingLogin = true;
1684
1714
  return true;
@@ -1694,7 +1724,7 @@ export abstract class CoreApp<
1694
1724
  this.userData = user;
1695
1725
 
1696
1726
  // Cache the encrypted serverside device id
1697
- this.storage.setData(CoreApp.serversideDeviceIdField, user.deviceId);
1727
+ this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
1698
1728
 
1699
1729
  if (keep) {
1700
1730
  this.authorize(user.token, refreshToken);
@@ -1741,30 +1771,3 @@ export abstract class CoreApp<
1741
1771
  );
1742
1772
  }
1743
1773
  }
1744
-
1745
- export namespace CoreApp {
1746
- /**
1747
- * Response token header field name
1748
- */
1749
- export const headerTokenField = 'SmartERPRefreshToken';
1750
-
1751
- /**
1752
- * Serverside device id encrypted field name
1753
- */
1754
- export const serversideDeviceIdField = 'SmartERPServersideDeviceId';
1755
-
1756
- /**
1757
- * Device id field name
1758
- */
1759
- export const deviceIdField = 'SmartERPDeviceId';
1760
-
1761
- /**
1762
- * Devices field name
1763
- */
1764
- export const devicesField = 'SmartERPDevices';
1765
-
1766
- /**
1767
- * Device passphrase field name
1768
- */
1769
- export const devicePassphraseField = 'SmartERPDevicePassphrase';
1770
- }
@@ -56,6 +56,26 @@ export namespace BusinessUtils {
56
56
  });
57
57
  }
58
58
 
59
+ /**
60
+ * Get 12-month items
61
+ * @param monthLabels Month labels
62
+ * @param startMonth Start month, 0 as Jan.
63
+ * @returns 12 months
64
+ */
65
+ export function getMonths(monthLabels: string[], startMonth: number = 0) {
66
+ const months: DataTypes.IdLabelItem[] = [];
67
+
68
+ for (let i = 0; i < 12; i++) {
69
+ if (startMonth >= 12) startMonth = 0;
70
+
71
+ months.push({ id: startMonth, label: monthLabels[startMonth] });
72
+
73
+ startMonth++;
74
+ }
75
+
76
+ return months;
77
+ }
78
+
59
79
  /**
60
80
  * Get product unit's label
61
81
  * Please define the label in culture with key 'unitPC' for ProductUnit.PC like that
@@ -50,6 +50,20 @@
50
50
  "message": "Message",
51
51
  "mobile": "Mobile number",
52
52
  "mobilePhones": "Mobile numbers",
53
+ "months": [
54
+ "Jan.",
55
+ "Feb.",
56
+ "Mar.",
57
+ "Apr.",
58
+ "May.",
59
+ "Jun.",
60
+ "Jul.",
61
+ "Aug.",
62
+ "Sep.",
63
+ "Oct.",
64
+ "Nov.",
65
+ "Dec."
66
+ ],
53
67
  "more": "More",
54
68
  "moreTag": "{0} more",
55
69
  "name": "Name",
@@ -67,6 +81,7 @@
67
81
  "pageNotFound": "Page Not Found",
68
82
  "prompt": "Input",
69
83
  "pullToRefresh": "Pull down to refresh",
84
+ "quarters": ["Q1", "Q2", "Q3", "Q4"],
70
85
  "record": "Record",
71
86
  "refresh": "Refresh",
72
87
  "refreshing": "Refreshing",
@@ -50,6 +50,20 @@
50
50
  "message": "留言",
51
51
  "mobile": "手机号码",
52
52
  "mobilePhones": "手机号码",
53
+ "months": [
54
+ "1月",
55
+ "2月",
56
+ "3月",
57
+ "4月",
58
+ "5月",
59
+ "6月",
60
+ "7月",
61
+ "8月",
62
+ "9月",
63
+ "10月",
64
+ "11月",
65
+ "12月"
66
+ ],
53
67
  "more": "更多",
54
68
  "moreTag": "({0}+)",
55
69
  "name": "姓名",
@@ -67,6 +81,7 @@
67
81
  "pageNotFound": "找不到页面",
68
82
  "prompt": "输入",
69
83
  "pullToRefresh": "下拉刷新",
84
+ "quarters": ["一季度", "二季度", "三季度", "四季度"],
70
85
  "record": "记录",
71
86
  "refresh": "刷新",
72
87
  "refreshing": "正在刷新",
@@ -50,6 +50,20 @@
50
50
  "message": "留言",
51
51
  "mobilePhone": "手機號碼",
52
52
  "mobilePhones": "手機號碼",
53
+ "months": [
54
+ "1月",
55
+ "2月",
56
+ "3月",
57
+ "4月",
58
+ "5月",
59
+ "6月",
60
+ "7月",
61
+ "8月",
62
+ "9月",
63
+ "10月",
64
+ "11月",
65
+ "12月"
66
+ ],
53
67
  "more": "更多",
54
68
  "moreTag": "({0}+)",
55
69
  "name": "姓名",
@@ -67,6 +81,7 @@
67
81
  "pageNotFound": "找不到頁面",
68
82
  "prompt": "輸入",
69
83
  "pullToRefresh": "下拉刷新",
84
+ "quarters": ["一季度", "二季度", "三季度", "四季度"],
70
85
  "record": "記錄",
71
86
  "refresh": "刷新",
72
87
  "refreshing": "正在刷新",