@etsoo/react 1.3.68 → 1.3.73

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.
@@ -1,4 +1,4 @@
1
- import { IApi, InitCallDto, InitCallResult, RefreshTokenProps } from '@etsoo/appscript';
1
+ import { IApi, RefreshTokenProps } from '@etsoo/appscript';
2
2
  import { IServiceAppSettings } from './IServiceAppSettings';
3
3
  import { IServicePageData } from './IServicePage';
4
4
  import { IServiceUser } from './IServiceUser';
@@ -13,29 +13,25 @@ import { RefreshTokenRQ } from './RefreshTokenRQ';
13
13
  */
14
14
  export declare class ServiceApp<P extends IServicePageData = IServicePageData, U extends IServiceUser = IServiceUser, S extends IServiceAppSettings = IServiceAppSettings> extends ReactApp<S, U, P> {
15
15
  /**
16
- * Core system API
16
+ * Service API
17
17
  */
18
- readonly coreApi: IApi;
18
+ readonly serviceApi: IApi;
19
+ private _serviceUser?;
19
20
  /**
20
- * Core system user
21
+ * Service user
21
22
  */
22
- coreUser?: ISmartERPUser;
23
+ get serviceUser(): IServiceUser | undefined;
24
+ protected set serviceUser(value: IServiceUser | undefined);
23
25
  /**
24
- * Service device id
26
+ * Service passphrase
25
27
  */
26
- protected serviceDeviceId: string;
28
+ protected servicePassphrase: string;
27
29
  /**
28
30
  * Constructor
29
31
  * @param settings Settings
30
32
  * @param name Application name
31
33
  */
32
34
  constructor(settings: S, name: string);
33
- /**
34
- * Api init call, connect to the core Api
35
- * @param data Data
36
- * @returns Result
37
- */
38
- protected apiInitCall(data: InitCallDto): Promise<InitCallResult | undefined>;
39
35
  /**
40
36
  * Go to the login page
41
37
  */
@@ -67,9 +63,9 @@ export declare class ServiceApp<P extends IServicePageData = IServicePageData, U
67
63
  tryLogin<D extends {} = {}>(data?: D): Promise<boolean>;
68
64
  /**
69
65
  * User login extended
70
- * @param user New user
66
+ * @param user Core system user
71
67
  * @param refreshToken Refresh token
72
- * @param coreUser Core system user
68
+ * @param serviceUser Service user
73
69
  */
74
- userLoginEx(user: IServiceUser, refreshToken: string, coreUser: ISmartERPUser): void;
70
+ userLoginEx(user: ISmartERPUser, refreshToken: string, serviceUser: IServiceUser): void;
75
71
  }
@@ -17,32 +17,33 @@ export class ServiceApp extends ReactApp {
17
17
  constructor(settings, name) {
18
18
  super(settings, name);
19
19
  /**
20
- * Service device id
20
+ * Service passphrase
21
21
  */
22
- this.serviceDeviceId = '';
22
+ this.servicePassphrase = '';
23
23
  // Check
24
- if (settings.serviceId == null || settings.coreApi == null) {
24
+ if (settings.serviceId == null || settings.serviceEndpoint == null) {
25
25
  throw new Error('No service settings');
26
26
  }
27
- // Core API
27
+ // Service API
28
28
  const api = createClient();
29
- api.baseUrl = settings.coreApi;
29
+ api.baseUrl = settings.serviceEndpoint;
30
30
  this.setApi(api);
31
- this.coreApi = api;
31
+ this.serviceApi = api;
32
32
  }
33
33
  /**
34
- * Api init call, connect to the core Api
35
- * @param data Data
36
- * @returns Result
34
+ * Service user
37
35
  */
38
- async apiInitCall(data) {
39
- return await this.coreApi.put(this.initCallApi, data);
36
+ get serviceUser() {
37
+ return this._serviceUser;
38
+ }
39
+ set serviceUser(value) {
40
+ this._serviceUser = value;
40
41
  }
41
42
  /**
42
43
  * Go to the login page
43
44
  */
44
45
  toLoginPage() {
45
- const coreUrl = this.settings.coreUrl;
46
+ const coreUrl = this.settings.webUrl;
46
47
  window.location.href =
47
48
  coreUrl +
48
49
  '?serviceId=' +
@@ -96,7 +97,7 @@ export class ServiceApp extends ReactApp {
96
97
  // User data
97
98
  const userData = result.data;
98
99
  // Use core system access token to service api to exchange service access token
99
- const serviceResult = await this.api.put('Auth/ExchangeToken', {
100
+ const serviceResult = await this.serviceApi.put('Auth/ExchangeToken', {
100
101
  token: this.encryptEnhanced(userData.token, this.settings.serviceId.toString())
101
102
  }, {
102
103
  showLoading,
@@ -120,14 +121,14 @@ export class ServiceApp extends ReactApp {
120
121
  return false;
121
122
  }
122
123
  // Login
123
- this.userLoginEx(serviceResult.data, refreshToken, userData);
124
+ this.userLoginEx(userData, refreshToken, serviceResult.data);
124
125
  // Success callback
125
126
  if (failCallback)
126
127
  failCallback(true);
127
128
  return true;
128
129
  };
129
130
  // Call API
130
- const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
131
+ const result = await this.api.put('Auth/RefreshToken', rq, payload);
131
132
  if (result == null)
132
133
  return false;
133
134
  if (!result.ok) {
@@ -143,7 +144,7 @@ export class ServiceApp extends ReactApp {
143
144
  // Set password for the action
144
145
  rq.pwd = this.encrypt(this.hash(pwd));
145
146
  // Submit again
146
- const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
147
+ const result = await this.api.put('Auth/RefreshToken', rq, payload);
147
148
  if (result == null)
148
149
  return;
149
150
  if (result.ok) {
@@ -183,7 +184,7 @@ export class ServiceApp extends ReactApp {
183
184
  * @returns Pure text
184
185
  */
185
186
  serviceDecrypt(messageEncrypted, passphrase) {
186
- return this.decrypt(messageEncrypted, passphrase !== null && passphrase !== void 0 ? passphrase : this.serviceDeviceId);
187
+ return this.decrypt(messageEncrypted, passphrase !== null && passphrase !== void 0 ? passphrase : this.servicePassphrase);
187
188
  }
188
189
  /**
189
190
  * Service encrypt message
@@ -193,7 +194,7 @@ export class ServiceApp extends ReactApp {
193
194
  * @returns Result
194
195
  */
195
196
  serviceEncrypt(message, passphrase, iterations) {
196
- return this.encrypt(message, passphrase !== null && passphrase !== void 0 ? passphrase : this.serviceDeviceId, iterations);
197
+ return this.encrypt(message, passphrase !== null && passphrase !== void 0 ? passphrase : this.servicePassphrase, iterations);
197
198
  }
198
199
  /**
199
200
  * Try login
@@ -224,17 +225,20 @@ export class ServiceApp extends ReactApp {
224
225
  }
225
226
  /**
226
227
  * User login extended
227
- * @param user New user
228
+ * @param user Core system user
228
229
  * @param refreshToken Refresh token
229
- * @param coreUser Core system user
230
+ * @param serviceUser Service user
230
231
  */
231
- userLoginEx(user, refreshToken, coreUser) {
232
+ userLoginEx(user, refreshToken, serviceUser) {
233
+ var _a;
232
234
  // Service user login
233
- this.serviceDeviceId = user.serviceDeviceId;
234
- super.userLogin(user, refreshToken, false);
235
- // Core system user
236
- this.coreUser = coreUser;
237
- // Core system API token
238
- this.coreApi.authorize(this.settings.authScheme, coreUser.token);
235
+ this.servicePassphrase =
236
+ (_a = this.decrypt(serviceUser.serviceDeviceId, this.settings.serviceId.toString())) !== null && _a !== void 0 ? _a : '';
237
+ // Keep = true, means service could hold the refresh token for long access
238
+ super.userLogin(user, refreshToken, true);
239
+ // Service user
240
+ this.serviceUser = serviceUser;
241
+ // Service API token
242
+ this.serviceApi.authorize(this.settings.authScheme, serviceUser.token);
239
243
  }
240
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.68",
3
+ "version": "1.3.73",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,11 +50,11 @@
50
50
  "@emotion/react": "^11.7.1",
51
51
  "@emotion/style": "^0.8.0",
52
52
  "@emotion/styled": "^11.6.0",
53
- "@etsoo/appscript": "^1.1.83",
54
- "@etsoo/notificationbase": "^1.0.94",
55
- "@etsoo/shared": "^1.0.78",
56
- "@mui/icons-material": "^5.2.4",
57
- "@mui/material": "^5.2.4",
53
+ "@etsoo/appscript": "^1.1.86",
54
+ "@etsoo/notificationbase": "^1.0.95",
55
+ "@etsoo/shared": "^1.0.82",
56
+ "@mui/icons-material": "^5.2.5",
57
+ "@mui/material": "^5.2.5",
58
58
  "@reach/router": "^1.3.4",
59
59
  "@types/pica": "^5.1.3",
60
60
  "@types/pulltorefreshjs": "^0.1.5",
@@ -81,8 +81,8 @@
81
81
  "@babel/runtime-corejs3": "^7.16.5",
82
82
  "@types/jest": "^27.0.3",
83
83
  "@types/react-test-renderer": "^17.0.1",
84
- "@typescript-eslint/eslint-plugin": "^5.7.0",
85
- "@typescript-eslint/parser": "^5.7.0",
84
+ "@typescript-eslint/eslint-plugin": "^5.8.0",
85
+ "@typescript-eslint/parser": "^5.8.0",
86
86
  "eslint": "^8.5.0",
87
87
  "eslint-config-airbnb-base": "^15.0.0",
88
88
  "eslint-plugin-import": "^2.25.3",
@@ -2,8 +2,6 @@ import {
2
2
  createClient,
3
3
  IApi,
4
4
  IApiPayload,
5
- InitCallDto,
6
- InitCallResult,
7
5
  RefreshTokenProps,
8
6
  RefreshTokenResult
9
7
  } from '@etsoo/appscript';
@@ -28,19 +26,25 @@ export class ServiceApp<
28
26
  S extends IServiceAppSettings = IServiceAppSettings
29
27
  > extends ReactApp<S, U, P> {
30
28
  /**
31
- * Core system API
29
+ * Service API
32
30
  */
33
- readonly coreApi: IApi;
31
+ readonly serviceApi: IApi;
34
32
 
33
+ private _serviceUser?: IServiceUser;
35
34
  /**
36
- * Core system user
35
+ * Service user
37
36
  */
38
- coreUser?: ISmartERPUser;
37
+ get serviceUser() {
38
+ return this._serviceUser;
39
+ }
40
+ protected set serviceUser(value: IServiceUser | undefined) {
41
+ this._serviceUser = value;
42
+ }
39
43
 
40
44
  /**
41
- * Service device id
45
+ * Service passphrase
42
46
  */
43
- protected serviceDeviceId: string = '';
47
+ protected servicePassphrase: string = '';
44
48
 
45
49
  /**
46
50
  * Constructor
@@ -51,32 +55,23 @@ export class ServiceApp<
51
55
  super(settings, name);
52
56
 
53
57
  // Check
54
- if (settings.serviceId == null || settings.coreApi == null) {
58
+ if (settings.serviceId == null || settings.serviceEndpoint == null) {
55
59
  throw new Error('No service settings');
56
60
  }
57
61
 
58
- // Core API
62
+ // Service API
59
63
  const api = createClient();
60
- api.baseUrl = settings.coreApi;
64
+ api.baseUrl = settings.serviceEndpoint;
61
65
 
62
66
  this.setApi(api);
63
- this.coreApi = api;
64
- }
65
-
66
- /**
67
- * Api init call, connect to the core Api
68
- * @param data Data
69
- * @returns Result
70
- */
71
- protected override async apiInitCall(data: InitCallDto) {
72
- return await this.coreApi.put<InitCallResult>(this.initCallApi, data);
67
+ this.serviceApi = api;
73
68
  }
74
69
 
75
70
  /**
76
71
  * Go to the login page
77
72
  */
78
73
  override toLoginPage() {
79
- const coreUrl = this.settings.coreUrl;
74
+ const coreUrl = this.settings.webUrl;
80
75
  window.location.href =
81
76
  coreUrl +
82
77
  '?serviceId=' +
@@ -145,7 +140,7 @@ export class ServiceApp<
145
140
  const userData = result.data;
146
141
 
147
142
  // Use core system access token to service api to exchange service access token
148
- const serviceResult = await this.api.put<ServiceLoginResult>(
143
+ const serviceResult = await this.serviceApi.put<ServiceLoginResult>(
149
144
  'Auth/ExchangeToken',
150
145
  {
151
146
  token: this.encryptEnhanced(
@@ -177,7 +172,7 @@ export class ServiceApp<
177
172
  }
178
173
 
179
174
  // Login
180
- this.userLoginEx(serviceResult.data, refreshToken, userData);
175
+ this.userLoginEx(userData, refreshToken, serviceResult.data);
181
176
 
182
177
  // Success callback
183
178
  if (failCallback) failCallback(true);
@@ -186,7 +181,7 @@ export class ServiceApp<
186
181
  };
187
182
 
188
183
  // Call API
189
- const result = await this.coreApi.put<SmartERPLoginResult>(
184
+ const result = await this.api.put<SmartERPLoginResult>(
190
185
  'Auth/RefreshToken',
191
186
  rq,
192
187
  payload
@@ -210,12 +205,11 @@ export class ServiceApp<
210
205
  rq.pwd = this.encrypt(this.hash(pwd));
211
206
 
212
207
  // Submit again
213
- const result =
214
- await this.coreApi.put<SmartERPLoginResult>(
215
- 'Auth/RefreshToken',
216
- rq,
217
- payload
218
- );
208
+ const result = await this.api.put<SmartERPLoginResult>(
209
+ 'Auth/RefreshToken',
210
+ rq,
211
+ payload
212
+ );
219
213
 
220
214
  if (result == null) return;
221
215
 
@@ -271,7 +265,7 @@ export class ServiceApp<
271
265
  serviceDecrypt(messageEncrypted: string, passphrase?: string) {
272
266
  return this.decrypt(
273
267
  messageEncrypted,
274
- passphrase ?? this.serviceDeviceId
268
+ passphrase ?? this.servicePassphrase
275
269
  );
276
270
  }
277
271
 
@@ -285,7 +279,7 @@ export class ServiceApp<
285
279
  serviceEncrypt(message: string, passphrase?: string, iterations?: number) {
286
280
  return this.encrypt(
287
281
  message,
288
- passphrase ?? this.serviceDeviceId,
282
+ passphrase ?? this.servicePassphrase,
289
283
  iterations
290
284
  );
291
285
  }
@@ -321,23 +315,29 @@ export class ServiceApp<
321
315
 
322
316
  /**
323
317
  * User login extended
324
- * @param user New user
318
+ * @param user Core system user
325
319
  * @param refreshToken Refresh token
326
- * @param coreUser Core system user
320
+ * @param serviceUser Service user
327
321
  */
328
322
  userLoginEx(
329
- user: IServiceUser,
323
+ user: ISmartERPUser,
330
324
  refreshToken: string,
331
- coreUser: ISmartERPUser
325
+ serviceUser: IServiceUser
332
326
  ): void {
333
327
  // Service user login
334
- this.serviceDeviceId = user.serviceDeviceId;
335
- super.userLogin(user, refreshToken, false);
328
+ this.servicePassphrase =
329
+ this.decrypt(
330
+ serviceUser.serviceDeviceId,
331
+ this.settings.serviceId.toString()
332
+ ) ?? '';
333
+
334
+ // Keep = true, means service could hold the refresh token for long access
335
+ super.userLogin(user, refreshToken, true);
336
336
 
337
- // Core system user
338
- this.coreUser = coreUser;
337
+ // Service user
338
+ this.serviceUser = serviceUser;
339
339
 
340
- // Core system API token
341
- this.coreApi.authorize(this.settings.authScheme, coreUser.token);
340
+ // Service API token
341
+ this.serviceApi.authorize(this.settings.authScheme, serviceUser.token);
342
342
  }
343
343
  }