@etsoo/appscript 1.6.9 → 1.6.11

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.
@@ -6,7 +6,7 @@ import { DataTypes, IActionResult } from "@etsoo/shared";
6
6
  import { RefreshTokenProps, RefreshTokenResult } from "../app/IApp";
7
7
  import { TokenInputRQ } from "./rq/TokenRQ";
8
8
  import { ApiRefreshTokenDto } from "./dto/ApiRefreshTokenDto";
9
- import { LoginRQ } from "./rq/LoginRQ";
9
+ import { LoginInputAuthResult, LoginInputRQ } from "./rq/LoginRQ";
10
10
  import { ResetPasswordInputRQ } from "./rq/ResetPasswordRQ";
11
11
  import { SignoutRQ } from "./rq/SignoutRQ";
12
12
  import { SwitchOrgRQ } from "./rq/SwitchOrgRQ";
@@ -71,7 +71,10 @@ export declare class AuthApi extends BaseApi {
71
71
  * @param tokenKey Refresh token key
72
72
  * @returns Result
73
73
  */
74
- protected loginBase<T extends IUser>(rq: LoginRQ, payload?: IApiPayload<IActionResult<T>>, tokenKey?: string): Promise<[IActionResult<T> | undefined, string | null]>;
74
+ login<T extends IUser, A extends AuthRequest | undefined>(rq: LoginInputRQ, auth: A, payload?: IApiPayload<IActionResult<A extends undefined ? T : LoginInputAuthResult>>, tokenKey?: string): Promise<[
75
+ IActionResult<A extends undefined ? T : LoginInputAuthResult> | undefined,
76
+ string | null
77
+ ]>;
75
78
  /**
76
79
  * Login id check
77
80
  * @param id Check id
@@ -88,12 +88,19 @@ class AuthApi extends BaseApi_1.BaseApi {
88
88
  * @param tokenKey Refresh token key
89
89
  * @returns Result
90
90
  */
91
- async loginBase(rq, payload, tokenKey) {
91
+ async login(rq, auth, payload, tokenKey) {
92
92
  // Default values
93
93
  payload ?? (payload = {});
94
94
  tokenKey ?? (tokenKey = AuthApi.HeaderTokenField);
95
+ const data = {
96
+ ...rq,
97
+ auth,
98
+ deviceId: this.app.deviceId,
99
+ region: this.app.region,
100
+ timeZone: this.app.getTimeZone()
101
+ };
95
102
  // Call the API
96
- const result = await this.api.post("Auth/Login", rq, payload);
103
+ const result = await this.api.post("Auth/Login", data, payload);
97
104
  // Get the refresh token
98
105
  const refreshToken = result?.ok
99
106
  ? this.app.getResponseToken(payload.response, tokenKey)
@@ -1,15 +1,20 @@
1
+ /**
2
+ * Login id input request data
3
+ */
4
+ export type LoginIdInputRQ = {
5
+ /**
6
+ * Username, Email or mobile
7
+ */
8
+ id: string;
9
+ };
1
10
  /**
2
11
  * Login id request data
3
12
  */
4
- export type LoginIdRQ = {
13
+ export type LoginIdRQ = LoginIdInputRQ & {
5
14
  /**
6
15
  * Device id
7
16
  */
8
17
  deviceId: string;
9
- /**
10
- * Username, Email or mobile
11
- */
12
- id: string;
13
18
  /**
14
19
  * Country or region
15
20
  */
@@ -1,9 +1,9 @@
1
1
  import { AuthRequest } from "./AuthRequest";
2
- import { LoginIdRQ } from "./LoginIdRQ";
2
+ import { LoginIdInputRQ, LoginIdRQ } from "./LoginIdRQ";
3
3
  /**
4
- * Login request data
4
+ * Login input request data
5
5
  */
6
- export type LoginRQ = LoginIdRQ & {
6
+ export type LoginInputRQ = LoginIdInputRQ & {
7
7
  /**
8
8
  * Password
9
9
  */
@@ -12,12 +12,22 @@ export type LoginRQ = LoginIdRQ & {
12
12
  * Organization
13
13
  */
14
14
  org?: number;
15
+ };
16
+ /**
17
+ * Login input auth result
18
+ */
19
+ export type LoginInputAuthResult = {
15
20
  /**
16
- * Authorization request data
21
+ * Redirect URI
17
22
  */
18
- auth?: AuthRequest;
23
+ uri: string;
19
24
  };
20
25
  /**
21
- * Login input request data
26
+ * Login request data
22
27
  */
23
- export type LoginInputRQ = Pick<LoginRQ, "id" | "pwd" | "org" | "auth">;
28
+ export type LoginRQ = LoginIdRQ & LoginInputRQ & {
29
+ /**
30
+ * Authorization request data
31
+ */
32
+ auth?: AuthRequest;
33
+ };
@@ -676,6 +676,11 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
676
676
  * @returns Result
677
677
  */
678
678
  protected apiRefreshToken(api: IApi, rq: ApiRefreshTokenRQ): Promise<[string, number] | undefined>;
679
+ /**
680
+ * Save refresh token to cache
681
+ * @param token Refresh token
682
+ */
683
+ saveCacheToken(token: string | undefined): void;
679
684
  /**
680
685
  * Setup tasks
681
686
  */
@@ -738,9 +738,7 @@ class CoreApp {
738
738
  this.api.authorize(schema, token);
739
739
  // Overwrite the current value
740
740
  if (refreshToken !== "") {
741
- if (refreshToken != null)
742
- refreshToken = this.encrypt(refreshToken);
743
- this.storage.setData(this.fields.headerToken, refreshToken);
741
+ this.saveCacheToken(refreshToken);
744
742
  }
745
743
  // Reset tryLogin state
746
744
  this._isTryingLogin = false;
@@ -1657,6 +1655,15 @@ class CoreApp {
1657
1655
  // Return the new refresh token and access token expiration seconds
1658
1656
  return [data.refreshToken, data.expiresIn];
1659
1657
  }
1658
+ /**
1659
+ * Save refresh token to cache
1660
+ * @param token Refresh token
1661
+ */
1662
+ saveCacheToken(token) {
1663
+ if (token != null)
1664
+ token = this.encrypt(token);
1665
+ this.storage.setData(this.fields.headerToken, token);
1666
+ }
1660
1667
  /**
1661
1668
  * Setup tasks
1662
1669
  */
@@ -575,6 +575,11 @@ export interface IApp {
575
575
  * @param callback Callback
576
576
  */
577
577
  refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
578
+ /**
579
+ * Save refresh token to cache
580
+ * @param token Refresh token
581
+ */
582
+ saveCacheToken(token: string | undefined): void;
578
583
  /**
579
584
  * Setup Api error handler
580
585
  * @param api Api
@@ -6,7 +6,7 @@ import { DataTypes, IActionResult } from "@etsoo/shared";
6
6
  import { RefreshTokenProps, RefreshTokenResult } from "../app/IApp";
7
7
  import { TokenInputRQ } from "./rq/TokenRQ";
8
8
  import { ApiRefreshTokenDto } from "./dto/ApiRefreshTokenDto";
9
- import { LoginRQ } from "./rq/LoginRQ";
9
+ import { LoginInputAuthResult, LoginInputRQ } from "./rq/LoginRQ";
10
10
  import { ResetPasswordInputRQ } from "./rq/ResetPasswordRQ";
11
11
  import { SignoutRQ } from "./rq/SignoutRQ";
12
12
  import { SwitchOrgRQ } from "./rq/SwitchOrgRQ";
@@ -71,7 +71,10 @@ export declare class AuthApi extends BaseApi {
71
71
  * @param tokenKey Refresh token key
72
72
  * @returns Result
73
73
  */
74
- protected loginBase<T extends IUser>(rq: LoginRQ, payload?: IApiPayload<IActionResult<T>>, tokenKey?: string): Promise<[IActionResult<T> | undefined, string | null]>;
74
+ login<T extends IUser, A extends AuthRequest | undefined>(rq: LoginInputRQ, auth: A, payload?: IApiPayload<IActionResult<A extends undefined ? T : LoginInputAuthResult>>, tokenKey?: string): Promise<[
75
+ IActionResult<A extends undefined ? T : LoginInputAuthResult> | undefined,
76
+ string | null
77
+ ]>;
75
78
  /**
76
79
  * Login id check
77
80
  * @param id Check id
@@ -85,12 +85,19 @@ export class AuthApi extends BaseApi {
85
85
  * @param tokenKey Refresh token key
86
86
  * @returns Result
87
87
  */
88
- async loginBase(rq, payload, tokenKey) {
88
+ async login(rq, auth, payload, tokenKey) {
89
89
  // Default values
90
90
  payload ?? (payload = {});
91
91
  tokenKey ?? (tokenKey = AuthApi.HeaderTokenField);
92
+ const data = {
93
+ ...rq,
94
+ auth,
95
+ deviceId: this.app.deviceId,
96
+ region: this.app.region,
97
+ timeZone: this.app.getTimeZone()
98
+ };
92
99
  // Call the API
93
- const result = await this.api.post("Auth/Login", rq, payload);
100
+ const result = await this.api.post("Auth/Login", data, payload);
94
101
  // Get the refresh token
95
102
  const refreshToken = result?.ok
96
103
  ? this.app.getResponseToken(payload.response, tokenKey)
@@ -1,15 +1,20 @@
1
+ /**
2
+ * Login id input request data
3
+ */
4
+ export type LoginIdInputRQ = {
5
+ /**
6
+ * Username, Email or mobile
7
+ */
8
+ id: string;
9
+ };
1
10
  /**
2
11
  * Login id request data
3
12
  */
4
- export type LoginIdRQ = {
13
+ export type LoginIdRQ = LoginIdInputRQ & {
5
14
  /**
6
15
  * Device id
7
16
  */
8
17
  deviceId: string;
9
- /**
10
- * Username, Email or mobile
11
- */
12
- id: string;
13
18
  /**
14
19
  * Country or region
15
20
  */
@@ -1,9 +1,9 @@
1
1
  import { AuthRequest } from "./AuthRequest";
2
- import { LoginIdRQ } from "./LoginIdRQ";
2
+ import { LoginIdInputRQ, LoginIdRQ } from "./LoginIdRQ";
3
3
  /**
4
- * Login request data
4
+ * Login input request data
5
5
  */
6
- export type LoginRQ = LoginIdRQ & {
6
+ export type LoginInputRQ = LoginIdInputRQ & {
7
7
  /**
8
8
  * Password
9
9
  */
@@ -12,12 +12,22 @@ export type LoginRQ = LoginIdRQ & {
12
12
  * Organization
13
13
  */
14
14
  org?: number;
15
+ };
16
+ /**
17
+ * Login input auth result
18
+ */
19
+ export type LoginInputAuthResult = {
15
20
  /**
16
- * Authorization request data
21
+ * Redirect URI
17
22
  */
18
- auth?: AuthRequest;
23
+ uri: string;
19
24
  };
20
25
  /**
21
- * Login input request data
26
+ * Login request data
22
27
  */
23
- export type LoginInputRQ = Pick<LoginRQ, "id" | "pwd" | "org" | "auth">;
28
+ export type LoginRQ = LoginIdRQ & LoginInputRQ & {
29
+ /**
30
+ * Authorization request data
31
+ */
32
+ auth?: AuthRequest;
33
+ };
@@ -676,6 +676,11 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
676
676
  * @returns Result
677
677
  */
678
678
  protected apiRefreshToken(api: IApi, rq: ApiRefreshTokenRQ): Promise<[string, number] | undefined>;
679
+ /**
680
+ * Save refresh token to cache
681
+ * @param token Refresh token
682
+ */
683
+ saveCacheToken(token: string | undefined): void;
679
684
  /**
680
685
  * Setup tasks
681
686
  */
@@ -735,9 +735,7 @@ export class CoreApp {
735
735
  this.api.authorize(schema, token);
736
736
  // Overwrite the current value
737
737
  if (refreshToken !== "") {
738
- if (refreshToken != null)
739
- refreshToken = this.encrypt(refreshToken);
740
- this.storage.setData(this.fields.headerToken, refreshToken);
738
+ this.saveCacheToken(refreshToken);
741
739
  }
742
740
  // Reset tryLogin state
743
741
  this._isTryingLogin = false;
@@ -1654,6 +1652,15 @@ export class CoreApp {
1654
1652
  // Return the new refresh token and access token expiration seconds
1655
1653
  return [data.refreshToken, data.expiresIn];
1656
1654
  }
1655
+ /**
1656
+ * Save refresh token to cache
1657
+ * @param token Refresh token
1658
+ */
1659
+ saveCacheToken(token) {
1660
+ if (token != null)
1661
+ token = this.encrypt(token);
1662
+ this.storage.setData(this.fields.headerToken, token);
1663
+ }
1657
1664
  /**
1658
1665
  * Setup tasks
1659
1666
  */
@@ -575,6 +575,11 @@ export interface IApp {
575
575
  * @param callback Callback
576
576
  */
577
577
  refreshToken(props?: RefreshTokenProps, callback?: (result?: boolean | IActionResult) => boolean | void): Promise<void>;
578
+ /**
579
+ * Save refresh token to cache
580
+ * @param token Refresh token
581
+ */
582
+ saveCacheToken(token: string | undefined): void;
578
583
  /**
579
584
  * Setup Api error handler
580
585
  * @param api Api
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.6.9",
3
+ "version": "1.6.11",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -7,7 +7,7 @@ import { RefreshTokenProps, RefreshTokenResult } from "../app/IApp";
7
7
  import { TokenInputRQ, TokenRQ } from "./rq/TokenRQ";
8
8
  import { ApiRefreshTokenDto } from "./dto/ApiRefreshTokenDto";
9
9
  import { GetLogInUrlRQ } from "./rq/GetLogInUrlRQ";
10
- import { LoginRQ } from "./rq/LoginRQ";
10
+ import { LoginInputAuthResult, LoginInputRQ, LoginRQ } from "./rq/LoginRQ";
11
11
  import { LoginIdRQ } from "./rq/LoginIdRQ";
12
12
  import { RefreshTokenRQ } from "./rq/RefreshTokenRQ";
13
13
  import { ResetPasswordInputRQ, ResetPasswordRQ } from "./rq/ResetPasswordRQ";
@@ -125,17 +125,33 @@ export class AuthApi extends BaseApi {
125
125
  * @param tokenKey Refresh token key
126
126
  * @returns Result
127
127
  */
128
- protected async loginBase<T extends IUser>(
129
- rq: LoginRQ,
130
- payload?: IApiPayload<IActionResult<T>>,
128
+ async login<T extends IUser, A extends AuthRequest | undefined>(
129
+ rq: LoginInputRQ,
130
+ auth: A,
131
+ payload?: IApiPayload<
132
+ IActionResult<A extends undefined ? T : LoginInputAuthResult>
133
+ >,
131
134
  tokenKey?: string
132
- ): Promise<[IActionResult<T> | undefined, string | null]> {
135
+ ): Promise<
136
+ [
137
+ IActionResult<A extends undefined ? T : LoginInputAuthResult> | undefined,
138
+ string | null
139
+ ]
140
+ > {
133
141
  // Default values
134
142
  payload ??= {};
135
143
  tokenKey ??= AuthApi.HeaderTokenField;
136
144
 
145
+ const data: LoginRQ = {
146
+ ...rq,
147
+ auth,
148
+ deviceId: this.app.deviceId,
149
+ region: this.app.region,
150
+ timeZone: this.app.getTimeZone()
151
+ };
152
+
137
153
  // Call the API
138
- const result = await this.api.post("Auth/Login", rq, payload);
154
+ const result = await this.api.post("Auth/Login", data, payload);
139
155
 
140
156
  // Get the refresh token
141
157
  const refreshToken = result?.ok
@@ -1,16 +1,21 @@
1
1
  /**
2
- * Login id request data
2
+ * Login id input request data
3
3
  */
4
- export type LoginIdRQ = {
4
+ export type LoginIdInputRQ = {
5
5
  /**
6
- * Device id
6
+ * Username, Email or mobile
7
7
  */
8
- deviceId: string;
8
+ id: string;
9
+ };
9
10
 
11
+ /**
12
+ * Login id request data
13
+ */
14
+ export type LoginIdRQ = LoginIdInputRQ & {
10
15
  /**
11
- * Username, Email or mobile
16
+ * Device id
12
17
  */
13
- id: string;
18
+ deviceId: string;
14
19
 
15
20
  /**
16
21
  * Country or region
@@ -1,10 +1,10 @@
1
1
  import { AuthRequest } from "./AuthRequest";
2
- import { LoginIdRQ } from "./LoginIdRQ";
2
+ import { LoginIdInputRQ, LoginIdRQ } from "./LoginIdRQ";
3
3
 
4
4
  /**
5
- * Login request data
5
+ * Login input request data
6
6
  */
7
- export type LoginRQ = LoginIdRQ & {
7
+ export type LoginInputRQ = LoginIdInputRQ & {
8
8
  /**
9
9
  * Password
10
10
  */
@@ -14,14 +14,25 @@ export type LoginRQ = LoginIdRQ & {
14
14
  * Organization
15
15
  */
16
16
  org?: number;
17
+ };
17
18
 
19
+ /**
20
+ * Login input auth result
21
+ */
22
+ export type LoginInputAuthResult = {
18
23
  /**
19
- * Authorization request data
24
+ * Redirect URI
20
25
  */
21
- auth?: AuthRequest;
26
+ uri: string;
22
27
  };
23
28
 
24
29
  /**
25
- * Login input request data
30
+ * Login request data
26
31
  */
27
- export type LoginInputRQ = Pick<LoginRQ, "id" | "pwd" | "org" | "auth">;
32
+ export type LoginRQ = LoginIdRQ &
33
+ LoginInputRQ & {
34
+ /**
35
+ * Authorization request data
36
+ */
37
+ auth?: AuthRequest;
38
+ };
@@ -1120,8 +1120,7 @@ export abstract class CoreApp<
1120
1120
 
1121
1121
  // Overwrite the current value
1122
1122
  if (refreshToken !== "") {
1123
- if (refreshToken != null) refreshToken = this.encrypt(refreshToken);
1124
- this.storage.setData(this.fields.headerToken, refreshToken);
1123
+ this.saveCacheToken(refreshToken);
1125
1124
  }
1126
1125
 
1127
1126
  // Reset tryLogin state
@@ -2222,6 +2221,15 @@ export abstract class CoreApp<
2222
2221
  return [data.refreshToken, data.expiresIn];
2223
2222
  }
2224
2223
 
2224
+ /**
2225
+ * Save refresh token to cache
2226
+ * @param token Refresh token
2227
+ */
2228
+ saveCacheToken(token: string | undefined) {
2229
+ if (token != null) token = this.encrypt(token);
2230
+ this.storage.setData(this.fields.headerToken, token);
2231
+ }
2232
+
2225
2233
  /**
2226
2234
  * Setup tasks
2227
2235
  */
package/src/app/IApp.ts CHANGED
@@ -765,6 +765,12 @@ export interface IApp {
765
765
  callback?: (result?: boolean | IActionResult) => boolean | void
766
766
  ): Promise<void>;
767
767
 
768
+ /**
769
+ * Save refresh token to cache
770
+ * @param token Refresh token
771
+ */
772
+ saveCacheToken(token: string | undefined): void;
773
+
768
774
  /**
769
775
  * Setup Api error handler
770
776
  * @param api Api