@etsoo/appscript 1.5.44 → 1.5.46

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.
@@ -7,7 +7,7 @@ import { InitCallDto } from '../erp/dto/InitCallDto';
7
7
  import { InitCallResult, InitCallResultData } from '../result/InitCallResult';
8
8
  import { IUser } from '../state/User';
9
9
  import { IAppSettings } from './AppSettings';
10
- import { AppLoginParams, FormatResultCustomCallback, IApp, IAppFields, IDetectIPCallback, NavigateOptions, RefreshTokenProps, RefreshTokenResult } from './IApp';
10
+ import { AppLoginParams, FormatResultCustomCallback, IApp, IAppFields, IDetectIPCallback, NavigateOptions, RefreshTokenProps } from './IApp';
11
11
  import { UserRole } from './UserRole';
12
12
  import { ExternalEndpoint } from './ExternalSettings';
13
13
  import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
@@ -152,7 +152,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
152
152
  * Passphrase for encryption
153
153
  */
154
154
  protected passphrase: string;
155
- private cachedRefreshToken?;
156
155
  private apis;
157
156
  private tasks;
158
157
  /**
@@ -426,12 +425,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
426
425
  * @param givenName Given name
427
426
  */
428
427
  formatFullName(familyName: string | undefined | null, givenName: string | undefined | null): string;
429
- /**
430
- * Format refresh token result
431
- * @param result Refresh token result
432
- * @returns Message
433
- */
434
- protected formatRefreshTokenResult(result: RefreshTokenResult<IActionResult<U>>): string | undefined;
435
428
  private getFieldLabel;
436
429
  /**
437
430
  * Format result text
@@ -579,11 +572,11 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
579
572
  */
580
573
  abstract freshCountdownUI(callback?: () => PromiseLike<unknown>): void;
581
574
  /**
582
- * Refresh token
575
+ * Refresh token with result
583
576
  * @param props Props
584
577
  * @param callback Callback
585
578
  */
586
- refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | string) => boolean | void): Promise<void>;
579
+ refreshToken(props: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
587
580
  /**
588
581
  * Setup callback
589
582
  */
@@ -644,9 +637,8 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
644
637
  * User login
645
638
  * @param user User data
646
639
  * @param refreshToken Refresh token
647
- * @param keep Keep login or not
648
640
  */
649
- userLogin(user: U, refreshToken: string, keep?: boolean): void;
641
+ userLogin(user: U, refreshToken: string): void;
650
642
  /**
651
643
  * User logout
652
644
  * @param clearToken Clear refresh token or not
@@ -162,13 +162,14 @@ class CoreApp {
162
162
  throw new Error('No default region defined');
163
163
  }
164
164
  this.defaultRegion = region;
165
+ // Current system refresh token
165
166
  const refresh = async (api, token) => {
166
167
  if (this.lastCalled) {
167
168
  // Call refreshToken to update access token
168
- // No popups show
169
- await this.refreshToken({ showLoading: false }, (result) => {
170
- console.log(`CoreApp.${this.name}.ApiRefreshToken`, result);
171
- return false;
169
+ await this.refreshToken({ token, showLoading: false }, (result) => {
170
+ if (result === true)
171
+ return;
172
+ console.log(`CoreApp.${this.name}.RefreshToken`, result);
172
173
  });
173
174
  }
174
175
  else {
@@ -666,7 +667,6 @@ class CoreApp {
666
667
  }
667
668
  }
668
669
  else {
669
- this.cachedRefreshToken = undefined;
670
670
  this.updateApi(this.api.name, undefined, -1);
671
671
  }
672
672
  // Host notice
@@ -773,7 +773,6 @@ class CoreApp {
773
773
  * Clear cached token
774
774
  */
775
775
  clearCacheToken() {
776
- this.cachedRefreshToken = undefined;
777
776
  this.storage.setPersistedData(this.fields.headerToken, undefined);
778
777
  }
779
778
  /**
@@ -1024,28 +1023,6 @@ class CoreApp {
1024
1023
  }
1025
1024
  return wf;
1026
1025
  }
1027
- /**
1028
- * Format refresh token result
1029
- * @param result Refresh token result
1030
- * @returns Message
1031
- */
1032
- formatRefreshTokenResult(result) {
1033
- // Error message
1034
- if (typeof result === 'string')
1035
- return result;
1036
- // API error
1037
- if (result instanceof restclient_1.ApiDataError)
1038
- return this.formatError(result);
1039
- // Action result
1040
- const [token, r] = result;
1041
- // Success
1042
- if (r.ok)
1043
- return undefined;
1044
- // No token data
1045
- if (token == null)
1046
- return `${this.get('noData')} (token)`;
1047
- return ActionResultError_1.ActionResultError.format(r);
1048
- }
1049
1026
  getFieldLabel(field) {
1050
1027
  return this.get(field.formatInitial(false)) ?? field;
1051
1028
  }
@@ -1130,9 +1107,6 @@ class CoreApp {
1130
1107
  * @returns Cached token
1131
1108
  */
1132
1109
  getCacheToken() {
1133
- // Temp refresh token
1134
- if (this.cachedRefreshToken)
1135
- return this.cachedRefreshToken;
1136
1110
  return this.storage.getData(this.fields.headerToken);
1137
1111
  }
1138
1112
  /**
@@ -1378,11 +1352,72 @@ class CoreApp {
1378
1352
  this.notifier.hideLoading(true);
1379
1353
  }
1380
1354
  /**
1381
- * Refresh token
1355
+ * Refresh token with result
1382
1356
  * @param props Props
1383
1357
  * @param callback Callback
1384
1358
  */
1385
- async refreshToken(props, callback) { }
1359
+ async refreshToken(props, callback) {
1360
+ // Call refresh token API
1361
+ let data = await new AuthApi_1.AuthApi(this).refreshToken(props);
1362
+ let r;
1363
+ if (Array.isArray(data)) {
1364
+ const [token, result] = data;
1365
+ if (result.ok) {
1366
+ if (!token) {
1367
+ data = {
1368
+ ok: false,
1369
+ type: 'noData',
1370
+ field: 'token',
1371
+ title: this.get('noData')
1372
+ };
1373
+ }
1374
+ else if (result.data == null) {
1375
+ data = {
1376
+ ok: false,
1377
+ type: 'noData',
1378
+ field: 'user',
1379
+ title: this.get('noData')
1380
+ };
1381
+ }
1382
+ else {
1383
+ // User login
1384
+ this.userLogin(result.data, token);
1385
+ if (callback)
1386
+ callback(true);
1387
+ // Exit
1388
+ return;
1389
+ }
1390
+ }
1391
+ else if (this.checkDeviceResult(result)) {
1392
+ if (callback == null || callback(result) !== true) {
1393
+ this.initCall((ir) => {
1394
+ if (!ir)
1395
+ return;
1396
+ this.notifier.alert(this.get('environmentChanged') ??
1397
+ 'Environment changed', () => {
1398
+ // Callback, return true to prevent the default reload action
1399
+ if (callback == null || callback() !== true) {
1400
+ // Reload the page
1401
+ history.go(0);
1402
+ }
1403
+ });
1404
+ }, true);
1405
+ return;
1406
+ }
1407
+ }
1408
+ r = result;
1409
+ }
1410
+ else {
1411
+ r = data;
1412
+ }
1413
+ if (callback == null || callback(r) !== true) {
1414
+ const message = `${r.title} (${r.field})`;
1415
+ this.notifier.alert(message, () => {
1416
+ if (callback)
1417
+ callback(false);
1418
+ });
1419
+ }
1420
+ }
1386
1421
  /**
1387
1422
  * Setup callback
1388
1423
  */
@@ -1609,21 +1644,16 @@ class CoreApp {
1609
1644
  * User login
1610
1645
  * @param user User data
1611
1646
  * @param refreshToken Refresh token
1612
- * @param keep Keep login or not
1613
1647
  */
1614
- userLogin(user, refreshToken, keep) {
1648
+ userLogin(user, refreshToken) {
1649
+ // Hold the user data
1615
1650
  this.userData = user;
1616
1651
  // Cache the encrypted serverside device id
1617
1652
  if (user.deviceId) {
1618
1653
  this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
1619
1654
  }
1620
- if (keep) {
1621
- this.authorize(user.token, user.tokenScheme, refreshToken);
1622
- }
1623
- else {
1624
- this.cachedRefreshToken = this.encrypt(refreshToken);
1625
- this.authorize(user.token, user.tokenScheme, undefined);
1626
- }
1655
+ // Authorize
1656
+ this.authorize(user.token, user.tokenScheme, refreshToken);
1627
1657
  }
1628
1658
  /**
1629
1659
  * User logout
@@ -27,7 +27,7 @@ export interface NavigateOptions {
27
27
  * array means success, false means failed but no any message
28
28
  * other cases means failed with differnet message
29
29
  */
30
- export type RefreshTokenResult<R> = string | ApiDataError | [string | null, R];
30
+ export type RefreshTokenResult<R> = IActionResult | [string | null, R];
31
31
  /**
32
32
  * Format result custom type
33
33
  */
@@ -61,6 +61,10 @@ export type AppLoginParams = DataTypes.SimpleObject & {
61
61
  * Refresh token props
62
62
  */
63
63
  export interface RefreshTokenProps {
64
+ /**
65
+ * Refresh token
66
+ */
67
+ token: string;
64
68
  /**
65
69
  * API name
66
70
  */
@@ -515,11 +519,12 @@ export interface IApp {
515
519
  */
516
520
  pageExit(): void;
517
521
  /**
518
- * Refresh token
522
+ * Refresh token with result
519
523
  * @param props Props
520
524
  * @param callback Callback
525
+ * @param api API
521
526
  */
522
- refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | string) => boolean | void): Promise<void>;
527
+ refreshToken(props: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
523
528
  /**
524
529
  * Setup Api error handler
525
530
  * @param api Api
@@ -572,9 +577,8 @@ export interface IApp {
572
577
  * User login
573
578
  * @param user User data
574
579
  * @param refreshToken Refresh token
575
- * @param keep Keep login or not
576
580
  */
577
- userLogin(user: IUser, refreshToken: string, keep?: boolean): void;
581
+ userLogin(user: IUser, refreshToken: string): void;
578
582
  /**
579
583
  * User logout
580
584
  * @param clearToken Clear refresh token or not
@@ -10,6 +10,7 @@ import { GetLogInUrlRQ } from './rq/GetLogInUrlRQ';
10
10
  import { TokenRQ } from './rq/TokenRQ';
11
11
  import { ApiRefreshTokenDto } from './dto/ApiRefreshTokenDto';
12
12
  import { RefreshTokenProps, RefreshTokenResult } from '../app/IApp';
13
+ import { SwitchOrgRQ } from './rq/SwitchOrgRQ';
13
14
  /**
14
15
  * Authentication API
15
16
  */
@@ -56,11 +57,10 @@ export declare class AuthApi extends BaseApi {
56
57
  loginId(id: string, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
57
58
  /**
58
59
  * Refresh token
59
- * @param token Refresh token
60
60
  * @param props Props
61
61
  * @returns Result
62
62
  */
63
- refreshToken<R>(token: string, props?: RefreshTokenProps): Promise<RefreshTokenResult<R>>;
63
+ refreshToken<R>(props: RefreshTokenProps): Promise<RefreshTokenResult<R>>;
64
64
  /**
65
65
  * Reset password
66
66
  * @param rq Request data
@@ -75,4 +75,10 @@ export declare class AuthApi extends BaseApi {
75
75
  * @returns Result
76
76
  */
77
77
  signout(rq: SignoutRQ, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
78
+ /**
79
+ * Switch organization
80
+ * @param rq Request data
81
+ * @param payload Payload
82
+ */
83
+ switchOrg(rq: SwitchOrgRQ, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
78
84
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthApi = void 0;
4
4
  const BaseApi_1 = require("./BaseApi");
5
+ const shared_1 = require("@etsoo/shared");
5
6
  /**
6
7
  * Authentication API
7
8
  */
@@ -66,13 +67,12 @@ class AuthApi extends BaseApi_1.BaseApi {
66
67
  }
67
68
  /**
68
69
  * Refresh token
69
- * @param token Refresh token
70
70
  * @param props Props
71
71
  * @returns Result
72
72
  */
73
- async refreshToken(token, props) {
73
+ async refreshToken(props) {
74
74
  // Destruct
75
- const { api = 'Auth/RefreshToken', showLoading = false, tokenField = AuthApi.HeaderTokenField } = props ?? {};
75
+ const { api = 'Auth/RefreshToken', showLoading = false, token, tokenField = AuthApi.HeaderTokenField } = props ?? {};
76
76
  // Reqest data
77
77
  const rq = {
78
78
  deviceId: this.app.deviceId
@@ -90,7 +90,14 @@ class AuthApi extends BaseApi_1.BaseApi {
90
90
  // Call API
91
91
  const result = await this.api.put(api, rq, payload);
92
92
  if (result == null) {
93
- return this.api.lastError ?? this.app.get('unknownError');
93
+ return this.api.lastError
94
+ ? shared_1.ActionResult.create(this.api.lastError)
95
+ : {
96
+ ok: false,
97
+ type: 'unknownError',
98
+ field: 'result',
99
+ title: this.app.get('unknownError')
100
+ };
94
101
  }
95
102
  // Token
96
103
  const refreshToken = this.app.getResponseToken(payload.response, tokenField);
@@ -115,6 +122,14 @@ class AuthApi extends BaseApi_1.BaseApi {
115
122
  signout(rq, payload) {
116
123
  return this.api.put('Auth/Signout', rq, payload);
117
124
  }
125
+ /**
126
+ * Switch organization
127
+ * @param rq Request data
128
+ * @param payload Payload
129
+ */
130
+ switchOrg(rq, payload) {
131
+ return this.app.api.put('Auth/SwitchOrg', rq, payload);
132
+ }
118
133
  }
119
134
  exports.AuthApi = AuthApi;
120
135
  /**
@@ -57,13 +57,6 @@ export declare class OrgApi extends EntityApi {
57
57
  * @returns Result
58
58
  */
59
59
  sendActionMessage(rq: SendActionMessageRQ, payload?: IApiPayload<void>): Promise<void | undefined>;
60
- /**
61
- * Switch organization
62
- * @param id Organization id
63
- * @param serviceId Service id
64
- * @param payload Payload
65
- */
66
- switch(id: number, serviceId?: number, payload?: IApiPayload<boolean>): Promise<false | void>;
67
60
  /**
68
61
  * Update
69
62
  * @param data Modal data
@@ -72,22 +72,6 @@ class OrgApi extends EntityApi_1.EntityApi {
72
72
  payload ?? (payload = { showLoading: false });
73
73
  return this.api.post('System/SendActionMessage', { ...rq, appId }, payload);
74
74
  }
75
- /**
76
- * Switch organization
77
- * @param id Organization id
78
- * @param serviceId Service id
79
- * @param payload Payload
80
- */
81
- async switch(id, serviceId, payload) {
82
- const result = await this.app.api.put('Organization/Switch', {
83
- id,
84
- serviceId,
85
- deviceId: this.app.deviceId
86
- }, payload);
87
- if (result)
88
- return await this.app.refreshToken();
89
- return result;
90
- }
91
75
  /**
92
76
  * Update
93
77
  * @param data Modal data
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Switch organization request data
3
+ */
4
+ export type SwitchOrgRQ = {
5
+ /**
6
+ * Target organization id
7
+ */
8
+ organizationId: number;
9
+ /**
10
+ * From organization id
11
+ */
12
+ fromOrganizationId: number;
13
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -61,6 +61,7 @@ export * from './erp/rq/RegionsRQ';
61
61
  export * from './erp/rq/ResetPasswordRQ';
62
62
  export * from './erp/rq/SendActionMessageRQ';
63
63
  export * from './erp/rq/SignoutRQ';
64
+ export * from './erp/rq/SwitchOrgRQ';
64
65
  export * from './erp/rq/TiplistRQ';
65
66
  export * from './erp/rq/TokenRQ';
66
67
  export * from './erp/rq/UpdateStatusRQ';
@@ -75,7 +76,7 @@ export * from './i18n/CultureUtils';
75
76
  export * from './i18n/en';
76
77
  export * from './i18n/zhHans';
77
78
  export * from './i18n/zhHant';
78
- export { ApiAuthorizationScheme, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
79
+ export { ApiAuthorizationScheme, ApiDataError, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
79
80
  export type { IApi, IApiPayload } from '@etsoo/restclient';
80
81
  export * from './result/ActionResult';
81
82
  export * from './result/ActionResultError';
package/lib/cjs/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createClientAsync = exports.createClient = exports.ApiResponseType = exports.ApiMethod = exports.ApiAuthorizationScheme = void 0;
17
+ exports.createClientAsync = exports.createClient = exports.ApiResponseType = exports.ApiMethod = exports.ApiDataError = exports.ApiAuthorizationScheme = void 0;
18
18
  // address
19
19
  __exportStar(require("./address/AddressAutocomplete"), exports);
20
20
  __exportStar(require("./address/AddressCity"), exports);
@@ -86,6 +86,7 @@ __exportStar(require("./erp/rq/RegionsRQ"), exports);
86
86
  __exportStar(require("./erp/rq/ResetPasswordRQ"), exports);
87
87
  __exportStar(require("./erp/rq/SendActionMessageRQ"), exports);
88
88
  __exportStar(require("./erp/rq/SignoutRQ"), exports);
89
+ __exportStar(require("./erp/rq/SwitchOrgRQ"), exports);
89
90
  __exportStar(require("./erp/rq/TiplistRQ"), exports);
90
91
  __exportStar(require("./erp/rq/TokenRQ"), exports);
91
92
  __exportStar(require("./erp/rq/UpdateStatusRQ"), exports);
@@ -105,6 +106,7 @@ __exportStar(require("./i18n/zhHant"), exports);
105
106
  // @etsoo/restclient
106
107
  var restclient_1 = require("@etsoo/restclient");
107
108
  Object.defineProperty(exports, "ApiAuthorizationScheme", { enumerable: true, get: function () { return restclient_1.ApiAuthorizationScheme; } });
109
+ Object.defineProperty(exports, "ApiDataError", { enumerable: true, get: function () { return restclient_1.ApiDataError; } });
108
110
  Object.defineProperty(exports, "ApiMethod", { enumerable: true, get: function () { return restclient_1.ApiMethod; } });
109
111
  Object.defineProperty(exports, "ApiResponseType", { enumerable: true, get: function () { return restclient_1.ApiResponseType; } });
110
112
  Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return restclient_1.createClient; } });
@@ -7,7 +7,7 @@ import { InitCallDto } from '../erp/dto/InitCallDto';
7
7
  import { InitCallResult, InitCallResultData } from '../result/InitCallResult';
8
8
  import { IUser } from '../state/User';
9
9
  import { IAppSettings } from './AppSettings';
10
- import { AppLoginParams, FormatResultCustomCallback, IApp, IAppFields, IDetectIPCallback, NavigateOptions, RefreshTokenProps, RefreshTokenResult } from './IApp';
10
+ import { AppLoginParams, FormatResultCustomCallback, IApp, IAppFields, IDetectIPCallback, NavigateOptions, RefreshTokenProps } from './IApp';
11
11
  import { UserRole } from './UserRole';
12
12
  import { ExternalEndpoint } from './ExternalSettings';
13
13
  import { ApiRefreshTokenDto } from '../erp/dto/ApiRefreshTokenDto';
@@ -152,7 +152,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
152
152
  * Passphrase for encryption
153
153
  */
154
154
  protected passphrase: string;
155
- private cachedRefreshToken?;
156
155
  private apis;
157
156
  private tasks;
158
157
  /**
@@ -426,12 +425,6 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
426
425
  * @param givenName Given name
427
426
  */
428
427
  formatFullName(familyName: string | undefined | null, givenName: string | undefined | null): string;
429
- /**
430
- * Format refresh token result
431
- * @param result Refresh token result
432
- * @returns Message
433
- */
434
- protected formatRefreshTokenResult(result: RefreshTokenResult<IActionResult<U>>): string | undefined;
435
428
  private getFieldLabel;
436
429
  /**
437
430
  * Format result text
@@ -579,11 +572,11 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
579
572
  */
580
573
  abstract freshCountdownUI(callback?: () => PromiseLike<unknown>): void;
581
574
  /**
582
- * Refresh token
575
+ * Refresh token with result
583
576
  * @param props Props
584
577
  * @param callback Callback
585
578
  */
586
- refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | string) => boolean | void): Promise<void>;
579
+ refreshToken(props: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
587
580
  /**
588
581
  * Setup callback
589
582
  */
@@ -644,9 +637,8 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
644
637
  * User login
645
638
  * @param user User data
646
639
  * @param refreshToken Refresh token
647
- * @param keep Keep login or not
648
640
  */
649
- userLogin(user: U, refreshToken: string, keep?: boolean): void;
641
+ userLogin(user: U, refreshToken: string): void;
650
642
  /**
651
643
  * User logout
652
644
  * @param clearToken Clear refresh token or not
@@ -1,5 +1,5 @@
1
1
  import { NotificationAlign, NotificationMessageType } from '@etsoo/notificationbase';
2
- import { ApiDataError, createClient } from '@etsoo/restclient';
2
+ import { createClient } from '@etsoo/restclient';
3
3
  import { DataTypes, DateUtils, DomUtils, ExtendUtils, NumberUtils, Utils } from '@etsoo/shared';
4
4
  import { AddressRegion } from '../address/AddressRegion';
5
5
  import { BridgeUtils } from '../bridges/BridgeUtils';
@@ -159,13 +159,14 @@ export class CoreApp {
159
159
  throw new Error('No default region defined');
160
160
  }
161
161
  this.defaultRegion = region;
162
+ // Current system refresh token
162
163
  const refresh = async (api, token) => {
163
164
  if (this.lastCalled) {
164
165
  // Call refreshToken to update access token
165
- // No popups show
166
- await this.refreshToken({ showLoading: false }, (result) => {
167
- console.log(`CoreApp.${this.name}.ApiRefreshToken`, result);
168
- return false;
166
+ await this.refreshToken({ token, showLoading: false }, (result) => {
167
+ if (result === true)
168
+ return;
169
+ console.log(`CoreApp.${this.name}.RefreshToken`, result);
169
170
  });
170
171
  }
171
172
  else {
@@ -663,7 +664,6 @@ export class CoreApp {
663
664
  }
664
665
  }
665
666
  else {
666
- this.cachedRefreshToken = undefined;
667
667
  this.updateApi(this.api.name, undefined, -1);
668
668
  }
669
669
  // Host notice
@@ -770,7 +770,6 @@ export class CoreApp {
770
770
  * Clear cached token
771
771
  */
772
772
  clearCacheToken() {
773
- this.cachedRefreshToken = undefined;
774
773
  this.storage.setPersistedData(this.fields.headerToken, undefined);
775
774
  }
776
775
  /**
@@ -1021,28 +1020,6 @@ export class CoreApp {
1021
1020
  }
1022
1021
  return wf;
1023
1022
  }
1024
- /**
1025
- * Format refresh token result
1026
- * @param result Refresh token result
1027
- * @returns Message
1028
- */
1029
- formatRefreshTokenResult(result) {
1030
- // Error message
1031
- if (typeof result === 'string')
1032
- return result;
1033
- // API error
1034
- if (result instanceof ApiDataError)
1035
- return this.formatError(result);
1036
- // Action result
1037
- const [token, r] = result;
1038
- // Success
1039
- if (r.ok)
1040
- return undefined;
1041
- // No token data
1042
- if (token == null)
1043
- return `${this.get('noData')} (token)`;
1044
- return ActionResultError.format(r);
1045
- }
1046
1023
  getFieldLabel(field) {
1047
1024
  return this.get(field.formatInitial(false)) ?? field;
1048
1025
  }
@@ -1127,9 +1104,6 @@ export class CoreApp {
1127
1104
  * @returns Cached token
1128
1105
  */
1129
1106
  getCacheToken() {
1130
- // Temp refresh token
1131
- if (this.cachedRefreshToken)
1132
- return this.cachedRefreshToken;
1133
1107
  return this.storage.getData(this.fields.headerToken);
1134
1108
  }
1135
1109
  /**
@@ -1375,11 +1349,72 @@ export class CoreApp {
1375
1349
  this.notifier.hideLoading(true);
1376
1350
  }
1377
1351
  /**
1378
- * Refresh token
1352
+ * Refresh token with result
1379
1353
  * @param props Props
1380
1354
  * @param callback Callback
1381
1355
  */
1382
- async refreshToken(props, callback) { }
1356
+ async refreshToken(props, callback) {
1357
+ // Call refresh token API
1358
+ let data = await new AuthApi(this).refreshToken(props);
1359
+ let r;
1360
+ if (Array.isArray(data)) {
1361
+ const [token, result] = data;
1362
+ if (result.ok) {
1363
+ if (!token) {
1364
+ data = {
1365
+ ok: false,
1366
+ type: 'noData',
1367
+ field: 'token',
1368
+ title: this.get('noData')
1369
+ };
1370
+ }
1371
+ else if (result.data == null) {
1372
+ data = {
1373
+ ok: false,
1374
+ type: 'noData',
1375
+ field: 'user',
1376
+ title: this.get('noData')
1377
+ };
1378
+ }
1379
+ else {
1380
+ // User login
1381
+ this.userLogin(result.data, token);
1382
+ if (callback)
1383
+ callback(true);
1384
+ // Exit
1385
+ return;
1386
+ }
1387
+ }
1388
+ else if (this.checkDeviceResult(result)) {
1389
+ if (callback == null || callback(result) !== true) {
1390
+ this.initCall((ir) => {
1391
+ if (!ir)
1392
+ return;
1393
+ this.notifier.alert(this.get('environmentChanged') ??
1394
+ 'Environment changed', () => {
1395
+ // Callback, return true to prevent the default reload action
1396
+ if (callback == null || callback() !== true) {
1397
+ // Reload the page
1398
+ history.go(0);
1399
+ }
1400
+ });
1401
+ }, true);
1402
+ return;
1403
+ }
1404
+ }
1405
+ r = result;
1406
+ }
1407
+ else {
1408
+ r = data;
1409
+ }
1410
+ if (callback == null || callback(r) !== true) {
1411
+ const message = `${r.title} (${r.field})`;
1412
+ this.notifier.alert(message, () => {
1413
+ if (callback)
1414
+ callback(false);
1415
+ });
1416
+ }
1417
+ }
1383
1418
  /**
1384
1419
  * Setup callback
1385
1420
  */
@@ -1606,21 +1641,16 @@ export class CoreApp {
1606
1641
  * User login
1607
1642
  * @param user User data
1608
1643
  * @param refreshToken Refresh token
1609
- * @param keep Keep login or not
1610
1644
  */
1611
- userLogin(user, refreshToken, keep) {
1645
+ userLogin(user, refreshToken) {
1646
+ // Hold the user data
1612
1647
  this.userData = user;
1613
1648
  // Cache the encrypted serverside device id
1614
1649
  if (user.deviceId) {
1615
1650
  this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
1616
1651
  }
1617
- if (keep) {
1618
- this.authorize(user.token, user.tokenScheme, refreshToken);
1619
- }
1620
- else {
1621
- this.cachedRefreshToken = this.encrypt(refreshToken);
1622
- this.authorize(user.token, user.tokenScheme, undefined);
1623
- }
1652
+ // Authorize
1653
+ this.authorize(user.token, user.tokenScheme, refreshToken);
1624
1654
  }
1625
1655
  /**
1626
1656
  * User logout
@@ -27,7 +27,7 @@ export interface NavigateOptions {
27
27
  * array means success, false means failed but no any message
28
28
  * other cases means failed with differnet message
29
29
  */
30
- export type RefreshTokenResult<R> = string | ApiDataError | [string | null, R];
30
+ export type RefreshTokenResult<R> = IActionResult | [string | null, R];
31
31
  /**
32
32
  * Format result custom type
33
33
  */
@@ -61,6 +61,10 @@ export type AppLoginParams = DataTypes.SimpleObject & {
61
61
  * Refresh token props
62
62
  */
63
63
  export interface RefreshTokenProps {
64
+ /**
65
+ * Refresh token
66
+ */
67
+ token: string;
64
68
  /**
65
69
  * API name
66
70
  */
@@ -515,11 +519,12 @@ export interface IApp {
515
519
  */
516
520
  pageExit(): void;
517
521
  /**
518
- * Refresh token
522
+ * Refresh token with result
519
523
  * @param props Props
520
524
  * @param callback Callback
525
+ * @param api API
521
526
  */
522
- refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | string) => boolean | void): Promise<void>;
527
+ refreshToken(props: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
523
528
  /**
524
529
  * Setup Api error handler
525
530
  * @param api Api
@@ -572,9 +577,8 @@ export interface IApp {
572
577
  * User login
573
578
  * @param user User data
574
579
  * @param refreshToken Refresh token
575
- * @param keep Keep login or not
576
580
  */
577
- userLogin(user: IUser, refreshToken: string, keep?: boolean): void;
581
+ userLogin(user: IUser, refreshToken: string): void;
578
582
  /**
579
583
  * User logout
580
584
  * @param clearToken Clear refresh token or not
@@ -10,6 +10,7 @@ import { GetLogInUrlRQ } from './rq/GetLogInUrlRQ';
10
10
  import { TokenRQ } from './rq/TokenRQ';
11
11
  import { ApiRefreshTokenDto } from './dto/ApiRefreshTokenDto';
12
12
  import { RefreshTokenProps, RefreshTokenResult } from '../app/IApp';
13
+ import { SwitchOrgRQ } from './rq/SwitchOrgRQ';
13
14
  /**
14
15
  * Authentication API
15
16
  */
@@ -56,11 +57,10 @@ export declare class AuthApi extends BaseApi {
56
57
  loginId(id: string, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
57
58
  /**
58
59
  * Refresh token
59
- * @param token Refresh token
60
60
  * @param props Props
61
61
  * @returns Result
62
62
  */
63
- refreshToken<R>(token: string, props?: RefreshTokenProps): Promise<RefreshTokenResult<R>>;
63
+ refreshToken<R>(props: RefreshTokenProps): Promise<RefreshTokenResult<R>>;
64
64
  /**
65
65
  * Reset password
66
66
  * @param rq Request data
@@ -75,4 +75,10 @@ export declare class AuthApi extends BaseApi {
75
75
  * @returns Result
76
76
  */
77
77
  signout(rq: SignoutRQ, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
78
+ /**
79
+ * Switch organization
80
+ * @param rq Request data
81
+ * @param payload Payload
82
+ */
83
+ switchOrg(rq: SwitchOrgRQ, payload?: ResultPayload): Promise<IActionResult<{}> | undefined>;
78
84
  }
@@ -1,4 +1,5 @@
1
1
  import { BaseApi } from './BaseApi';
2
+ import { ActionResult } from '@etsoo/shared';
2
3
  /**
3
4
  * Authentication API
4
5
  */
@@ -63,13 +64,12 @@ export class AuthApi extends BaseApi {
63
64
  }
64
65
  /**
65
66
  * Refresh token
66
- * @param token Refresh token
67
67
  * @param props Props
68
68
  * @returns Result
69
69
  */
70
- async refreshToken(token, props) {
70
+ async refreshToken(props) {
71
71
  // Destruct
72
- const { api = 'Auth/RefreshToken', showLoading = false, tokenField = AuthApi.HeaderTokenField } = props ?? {};
72
+ const { api = 'Auth/RefreshToken', showLoading = false, token, tokenField = AuthApi.HeaderTokenField } = props ?? {};
73
73
  // Reqest data
74
74
  const rq = {
75
75
  deviceId: this.app.deviceId
@@ -87,7 +87,14 @@ export class AuthApi extends BaseApi {
87
87
  // Call API
88
88
  const result = await this.api.put(api, rq, payload);
89
89
  if (result == null) {
90
- return this.api.lastError ?? this.app.get('unknownError');
90
+ return this.api.lastError
91
+ ? ActionResult.create(this.api.lastError)
92
+ : {
93
+ ok: false,
94
+ type: 'unknownError',
95
+ field: 'result',
96
+ title: this.app.get('unknownError')
97
+ };
91
98
  }
92
99
  // Token
93
100
  const refreshToken = this.app.getResponseToken(payload.response, tokenField);
@@ -112,6 +119,14 @@ export class AuthApi extends BaseApi {
112
119
  signout(rq, payload) {
113
120
  return this.api.put('Auth/Signout', rq, payload);
114
121
  }
122
+ /**
123
+ * Switch organization
124
+ * @param rq Request data
125
+ * @param payload Payload
126
+ */
127
+ switchOrg(rq, payload) {
128
+ return this.app.api.put('Auth/SwitchOrg', rq, payload);
129
+ }
115
130
  }
116
131
  /**
117
132
  * Header token field name
@@ -57,13 +57,6 @@ export declare class OrgApi extends EntityApi {
57
57
  * @returns Result
58
58
  */
59
59
  sendActionMessage(rq: SendActionMessageRQ, payload?: IApiPayload<void>): Promise<void | undefined>;
60
- /**
61
- * Switch organization
62
- * @param id Organization id
63
- * @param serviceId Service id
64
- * @param payload Payload
65
- */
66
- switch(id: number, serviceId?: number, payload?: IApiPayload<boolean>): Promise<false | void>;
67
60
  /**
68
61
  * Update
69
62
  * @param data Modal data
@@ -69,22 +69,6 @@ export class OrgApi extends EntityApi {
69
69
  payload ?? (payload = { showLoading: false });
70
70
  return this.api.post('System/SendActionMessage', { ...rq, appId }, payload);
71
71
  }
72
- /**
73
- * Switch organization
74
- * @param id Organization id
75
- * @param serviceId Service id
76
- * @param payload Payload
77
- */
78
- async switch(id, serviceId, payload) {
79
- const result = await this.app.api.put('Organization/Switch', {
80
- id,
81
- serviceId,
82
- deviceId: this.app.deviceId
83
- }, payload);
84
- if (result)
85
- return await this.app.refreshToken();
86
- return result;
87
- }
88
72
  /**
89
73
  * Update
90
74
  * @param data Modal data
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Switch organization request data
3
+ */
4
+ export type SwitchOrgRQ = {
5
+ /**
6
+ * Target organization id
7
+ */
8
+ organizationId: number;
9
+ /**
10
+ * From organization id
11
+ */
12
+ fromOrganizationId: number;
13
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -61,6 +61,7 @@ export * from './erp/rq/RegionsRQ';
61
61
  export * from './erp/rq/ResetPasswordRQ';
62
62
  export * from './erp/rq/SendActionMessageRQ';
63
63
  export * from './erp/rq/SignoutRQ';
64
+ export * from './erp/rq/SwitchOrgRQ';
64
65
  export * from './erp/rq/TiplistRQ';
65
66
  export * from './erp/rq/TokenRQ';
66
67
  export * from './erp/rq/UpdateStatusRQ';
@@ -75,7 +76,7 @@ export * from './i18n/CultureUtils';
75
76
  export * from './i18n/en';
76
77
  export * from './i18n/zhHans';
77
78
  export * from './i18n/zhHant';
78
- export { ApiAuthorizationScheme, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
79
+ export { ApiAuthorizationScheme, ApiDataError, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
79
80
  export type { IApi, IApiPayload } from '@etsoo/restclient';
80
81
  export * from './result/ActionResult';
81
82
  export * from './result/ActionResultError';
package/lib/mjs/index.js CHANGED
@@ -69,6 +69,7 @@ export * from './erp/rq/RegionsRQ';
69
69
  export * from './erp/rq/ResetPasswordRQ';
70
70
  export * from './erp/rq/SendActionMessageRQ';
71
71
  export * from './erp/rq/SignoutRQ';
72
+ export * from './erp/rq/SwitchOrgRQ';
72
73
  export * from './erp/rq/TiplistRQ';
73
74
  export * from './erp/rq/TokenRQ';
74
75
  export * from './erp/rq/UpdateStatusRQ';
@@ -86,7 +87,7 @@ export * from './i18n/en';
86
87
  export * from './i18n/zhHans';
87
88
  export * from './i18n/zhHant';
88
89
  // @etsoo/restclient
89
- export { ApiAuthorizationScheme, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
90
+ export { ApiAuthorizationScheme, ApiDataError, ApiMethod, ApiResponseType, createClient, createClientAsync } from '@etsoo/restclient';
90
91
  // result
91
92
  export * from './result/ActionResult';
92
93
  export * from './result/ActionResultError';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.5.44",
3
+ "version": "1.5.46",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -39,8 +39,7 @@ import {
39
39
  IAppFields,
40
40
  IDetectIPCallback,
41
41
  NavigateOptions,
42
- RefreshTokenProps,
43
- RefreshTokenResult
42
+ RefreshTokenProps
44
43
  } from './IApp';
45
44
  import { UserRole } from './UserRole';
46
45
  import type CryptoJS from 'crypto-js';
@@ -283,8 +282,6 @@ export abstract class CoreApp<
283
282
  */
284
283
  protected passphrase: string = '';
285
284
 
286
- private cachedRefreshToken?: string;
287
-
288
285
  private apis: Record<string, ApiTaskData> = {};
289
286
 
290
287
  private tasks: [() => PromiseLike<void | false>, number, number][] = [];
@@ -329,14 +326,20 @@ export abstract class CoreApp<
329
326
  }
330
327
  this.defaultRegion = region;
331
328
 
329
+ // Current system refresh token
332
330
  const refresh: ApiRefreshTokenFunction = async (api, token) => {
333
331
  if (this.lastCalled) {
334
332
  // Call refreshToken to update access token
335
- // No popups show
336
- await this.refreshToken({ showLoading: false }, (result) => {
337
- console.log(`CoreApp.${this.name}.ApiRefreshToken`, result);
338
- return false;
339
- });
333
+ await this.refreshToken(
334
+ { token, showLoading: false },
335
+ (result) => {
336
+ if (result === true) return;
337
+ console.log(
338
+ `CoreApp.${this.name}.RefreshToken`,
339
+ result
340
+ );
341
+ }
342
+ );
340
343
  } else {
341
344
  // Popup countdown for user action
342
345
  this.freshCountdownUI();
@@ -1038,7 +1041,6 @@ export abstract class CoreApp<
1038
1041
  );
1039
1042
  }
1040
1043
  } else {
1041
- this.cachedRefreshToken = undefined;
1042
1044
  this.updateApi(this.api.name, undefined, -1);
1043
1045
  }
1044
1046
 
@@ -1170,7 +1172,6 @@ export abstract class CoreApp<
1170
1172
  * Clear cached token
1171
1173
  */
1172
1174
  clearCacheToken() {
1173
- this.cachedRefreshToken = undefined;
1174
1175
  this.storage.setPersistedData(this.fields.headerToken, undefined);
1175
1176
  }
1176
1177
 
@@ -1498,32 +1499,6 @@ export abstract class CoreApp<
1498
1499
  return wf;
1499
1500
  }
1500
1501
 
1501
- /**
1502
- * Format refresh token result
1503
- * @param result Refresh token result
1504
- * @returns Message
1505
- */
1506
- protected formatRefreshTokenResult(
1507
- result: RefreshTokenResult<IActionResult<U>>
1508
- ): string | undefined {
1509
- // Error message
1510
- if (typeof result === 'string') return result;
1511
-
1512
- // API error
1513
- if (result instanceof ApiDataError) return this.formatError(result);
1514
-
1515
- // Action result
1516
- const [token, r] = result;
1517
-
1518
- // Success
1519
- if (r.ok) return undefined;
1520
-
1521
- // No token data
1522
- if (token == null) return `${this.get('noData')} (token)`;
1523
-
1524
- return ActionResultError.format(r);
1525
- }
1526
-
1527
1502
  private getFieldLabel(field: string) {
1528
1503
  return this.get(field.formatInitial(false)) ?? field;
1529
1504
  }
@@ -1623,8 +1598,6 @@ export abstract class CoreApp<
1623
1598
  * @returns Cached token
1624
1599
  */
1625
1600
  getCacheToken(): string | undefined {
1626
- // Temp refresh token
1627
- if (this.cachedRefreshToken) return this.cachedRefreshToken;
1628
1601
  return this.storage.getData<string>(this.fields.headerToken);
1629
1602
  }
1630
1603
 
@@ -1910,14 +1883,78 @@ export abstract class CoreApp<
1910
1883
  abstract freshCountdownUI(callback?: () => PromiseLike<unknown>): void;
1911
1884
 
1912
1885
  /**
1913
- * Refresh token
1886
+ * Refresh token with result
1914
1887
  * @param props Props
1915
1888
  * @param callback Callback
1916
1889
  */
1917
1890
  async refreshToken(
1918
- props?: RefreshTokenProps,
1919
- callback?: (result?: boolean | string) => boolean | void
1920
- ) {}
1891
+ props: RefreshTokenProps,
1892
+ callback?: (result?: boolean | IActionResult) => boolean | void
1893
+ ) {
1894
+ // Call refresh token API
1895
+ let data = await new AuthApi(this).refreshToken<IActionResult<U>>(
1896
+ props
1897
+ );
1898
+
1899
+ let r: IActionResult;
1900
+ if (Array.isArray(data)) {
1901
+ const [token, result] = data;
1902
+ if (result.ok) {
1903
+ if (!token) {
1904
+ data = {
1905
+ ok: false,
1906
+ type: 'noData',
1907
+ field: 'token',
1908
+ title: this.get('noData')
1909
+ };
1910
+ } else if (result.data == null) {
1911
+ data = {
1912
+ ok: false,
1913
+ type: 'noData',
1914
+ field: 'user',
1915
+ title: this.get('noData')
1916
+ };
1917
+ } else {
1918
+ // User login
1919
+ this.userLogin(result.data, token);
1920
+
1921
+ if (callback) callback(true);
1922
+
1923
+ // Exit
1924
+ return;
1925
+ }
1926
+ } else if (this.checkDeviceResult(result)) {
1927
+ if (callback == null || callback(result) !== true) {
1928
+ this.initCall((ir) => {
1929
+ if (!ir) return;
1930
+ this.notifier.alert(
1931
+ this.get('environmentChanged') ??
1932
+ 'Environment changed',
1933
+ () => {
1934
+ // Callback, return true to prevent the default reload action
1935
+ if (callback == null || callback() !== true) {
1936
+ // Reload the page
1937
+ history.go(0);
1938
+ }
1939
+ }
1940
+ );
1941
+ }, true);
1942
+ return;
1943
+ }
1944
+ }
1945
+
1946
+ r = result;
1947
+ } else {
1948
+ r = data;
1949
+ }
1950
+
1951
+ if (callback == null || callback(r) !== true) {
1952
+ const message = `${r.title} (${r.field})`;
1953
+ this.notifier.alert(message, () => {
1954
+ if (callback) callback(false);
1955
+ });
1956
+ }
1957
+ }
1921
1958
 
1922
1959
  /**
1923
1960
  * Setup callback
@@ -2198,9 +2235,9 @@ export abstract class CoreApp<
2198
2235
  * User login
2199
2236
  * @param user User data
2200
2237
  * @param refreshToken Refresh token
2201
- * @param keep Keep login or not
2202
2238
  */
2203
- userLogin(user: U, refreshToken: string, keep?: boolean) {
2239
+ userLogin(user: U, refreshToken: string) {
2240
+ // Hold the user data
2204
2241
  this.userData = user;
2205
2242
 
2206
2243
  // Cache the encrypted serverside device id
@@ -2208,12 +2245,8 @@ export abstract class CoreApp<
2208
2245
  this.storage.setData(this.fields.serversideDeviceId, user.deviceId);
2209
2246
  }
2210
2247
 
2211
- if (keep) {
2212
- this.authorize(user.token, user.tokenScheme, refreshToken);
2213
- } else {
2214
- this.cachedRefreshToken = this.encrypt(refreshToken);
2215
- this.authorize(user.token, user.tokenScheme, undefined);
2216
- }
2248
+ // Authorize
2249
+ this.authorize(user.token, user.tokenScheme, refreshToken);
2217
2250
  }
2218
2251
 
2219
2252
  /**
package/src/app/IApp.ts CHANGED
@@ -45,7 +45,7 @@ export interface NavigateOptions {
45
45
  * array means success, false means failed but no any message
46
46
  * other cases means failed with differnet message
47
47
  */
48
- export type RefreshTokenResult<R> = string | ApiDataError | [string | null, R];
48
+ export type RefreshTokenResult<R> = IActionResult | [string | null, R];
49
49
 
50
50
  /**
51
51
  * Format result custom type
@@ -87,6 +87,11 @@ export type AppLoginParams = DataTypes.SimpleObject & {
87
87
  * Refresh token props
88
88
  */
89
89
  export interface RefreshTokenProps {
90
+ /**
91
+ * Refresh token
92
+ */
93
+ token: string;
94
+
90
95
  /**
91
96
  * API name
92
97
  */
@@ -695,13 +700,14 @@ export interface IApp {
695
700
  pageExit(): void;
696
701
 
697
702
  /**
698
- * Refresh token
703
+ * Refresh token with result
699
704
  * @param props Props
700
705
  * @param callback Callback
706
+ * @param api API
701
707
  */
702
708
  refreshToken(
703
- props?: RefreshTokenProps,
704
- callback?: (result?: boolean | string) => boolean | void
709
+ props: RefreshTokenProps,
710
+ callback?: (result?: boolean | IActionResult) => boolean | void
705
711
  ): Promise<void>;
706
712
 
707
713
  /**
@@ -771,9 +777,8 @@ export interface IApp {
771
777
  * User login
772
778
  * @param user User data
773
779
  * @param refreshToken Refresh token
774
- * @param keep Keep login or not
775
780
  */
776
- userLogin(user: IUser, refreshToken: string, keep?: boolean): void;
781
+ userLogin(user: IUser, refreshToken: string): void;
777
782
 
778
783
  /**
779
784
  * User logout
@@ -5,13 +5,14 @@ import { ResultPayload } from './dto/ResultPayload';
5
5
  import { LoginIdRQ } from './rq/LoginIdRQ';
6
6
  import { LoginRQ } from './rq/LoginRQ';
7
7
  import { ResetPasswordRQ } from './rq/ResetPasswordRQ';
8
- import { IActionResult } from '@etsoo/shared';
8
+ import { ActionResult, IActionResult } from '@etsoo/shared';
9
9
  import { SignoutRQ } from './rq/SignoutRQ';
10
10
  import { GetLogInUrlRQ } from './rq/GetLogInUrlRQ';
11
11
  import { TokenRQ } from './rq/TokenRQ';
12
12
  import { ApiRefreshTokenDto } from './dto/ApiRefreshTokenDto';
13
13
  import { RefreshTokenProps, RefreshTokenResult } from '../app/IApp';
14
14
  import { RefreshTokenRQ } from './rq/RefreshTokenRQ';
15
+ import { SwitchOrgRQ } from './rq/SwitchOrgRQ';
15
16
 
16
17
  /**
17
18
  * Authentication API
@@ -91,18 +92,17 @@ export class AuthApi extends BaseApi {
91
92
 
92
93
  /**
93
94
  * Refresh token
94
- * @param token Refresh token
95
95
  * @param props Props
96
96
  * @returns Result
97
97
  */
98
98
  async refreshToken<R>(
99
- token: string,
100
- props?: RefreshTokenProps
99
+ props: RefreshTokenProps
101
100
  ): Promise<RefreshTokenResult<R>> {
102
101
  // Destruct
103
102
  const {
104
103
  api = 'Auth/RefreshToken',
105
104
  showLoading = false,
105
+ token,
106
106
  tokenField = AuthApi.HeaderTokenField
107
107
  } = props ?? {};
108
108
 
@@ -125,7 +125,14 @@ export class AuthApi extends BaseApi {
125
125
  // Call API
126
126
  const result = await this.api.put(api, rq, payload);
127
127
  if (result == null) {
128
- return this.api.lastError ?? this.app.get('unknownError')!;
128
+ return this.api.lastError
129
+ ? ActionResult.create(this.api.lastError)
130
+ : {
131
+ ok: false,
132
+ type: 'unknownError',
133
+ field: 'result',
134
+ title: this.app.get('unknownError')
135
+ };
129
136
  }
130
137
 
131
138
  // Token
@@ -157,4 +164,13 @@ export class AuthApi extends BaseApi {
157
164
  signout(rq: SignoutRQ, payload?: ResultPayload) {
158
165
  return this.api.put('Auth/Signout', rq, payload);
159
166
  }
167
+
168
+ /**
169
+ * Switch organization
170
+ * @param rq Request data
171
+ * @param payload Payload
172
+ */
173
+ switchOrg(rq: SwitchOrgRQ, payload?: ResultPayload) {
174
+ return this.app.api.put('Auth/SwitchOrg', rq, payload);
175
+ }
160
176
  }
package/src/erp/OrgApi.ts CHANGED
@@ -111,30 +111,6 @@ export class OrgApi extends EntityApi {
111
111
  );
112
112
  }
113
113
 
114
- /**
115
- * Switch organization
116
- * @param id Organization id
117
- * @param serviceId Service id
118
- * @param payload Payload
119
- */
120
- async switch(
121
- id: number,
122
- serviceId?: number,
123
- payload?: IApiPayload<boolean>
124
- ) {
125
- const result = await this.app.api.put(
126
- 'Organization/Switch',
127
- {
128
- id,
129
- serviceId,
130
- deviceId: this.app.deviceId
131
- },
132
- payload
133
- );
134
- if (result) return await this.app.refreshToken();
135
- return result;
136
- }
137
-
138
114
  /**
139
115
  * Update
140
116
  * @param data Modal data
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Switch organization request data
3
+ */
4
+ export type SwitchOrgRQ = {
5
+ /**
6
+ * Target organization id
7
+ */
8
+ organizationId: number;
9
+
10
+ /**
11
+ * From organization id
12
+ */
13
+ fromOrganizationId: number;
14
+ };
package/src/index.ts CHANGED
@@ -76,6 +76,7 @@ export * from './erp/rq/RegionsRQ';
76
76
  export * from './erp/rq/ResetPasswordRQ';
77
77
  export * from './erp/rq/SendActionMessageRQ';
78
78
  export * from './erp/rq/SignoutRQ';
79
+ export * from './erp/rq/SwitchOrgRQ';
79
80
  export * from './erp/rq/TiplistRQ';
80
81
  export * from './erp/rq/TokenRQ';
81
82
  export * from './erp/rq/UpdateStatusRQ';
@@ -98,6 +99,7 @@ export * from './i18n/zhHant';
98
99
  // @etsoo/restclient
99
100
  export {
100
101
  ApiAuthorizationScheme,
102
+ ApiDataError,
101
103
  ApiMethod,
102
104
  ApiResponseType,
103
105
  createClient,