@turinhub/tale-js-sdk 1.3.0 → 2.0.0
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.
- package/README.md +6 -6
- package/dist/acl/index.d.ts +62 -49
- package/dist/acl/index.js +262 -67
- package/dist/acl/types.d.ts +63 -98
- package/dist/attachment/index.d.ts +17 -0
- package/dist/attachment/index.js +247 -0
- package/dist/attachment/types.d.ts +82 -0
- package/dist/attachment/types.js +1 -0
- package/dist/attachment-type/index.d.ts +15 -0
- package/dist/attachment-type/index.js +203 -0
- package/dist/attachment-type/types.d.ts +60 -0
- package/dist/attachment-type/types.js +1 -0
- package/dist/auth/index.d.ts +21 -21
- package/dist/auth/index.js +66 -66
- package/dist/auth/types.d.ts +51 -51
- package/dist/cms/file.d.ts +88 -70
- package/dist/cms/file.js +228 -77
- package/dist/cms/folder.d.ts +9 -9
- package/dist/cms/folder.js +18 -18
- package/dist/cms/types.d.ts +58 -38
- package/dist/common/types.d.ts +47 -63
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2 -0
- package/dist/rbac/index.d.ts +37 -42
- package/dist/rbac/index.js +96 -98
- package/dist/rbac/types.d.ts +38 -40
- package/dist/status.d.ts +11 -11
- package/dist/status.js +30 -3
- package/dist/task/index.d.ts +15 -147
- package/dist/task/index.js +170 -161
- package/dist/task/types.d.ts +57 -81
- package/dist/task-type/index.d.ts +7 -7
- package/dist/task-type/index.js +12 -12
- package/dist/task-type/types.d.ts +18 -34
- package/dist/token.d.ts +3 -3
- package/dist/token.js +4 -4
- package/dist/user/index.d.ts +28 -29
- package/dist/user/index.js +69 -74
- package/dist/user/types.d.ts +32 -33
- package/dist/user-attribute/index.d.ts +4 -7
- package/dist/user-attribute/index.js +19 -22
- package/dist/user-attribute/types.d.ts +29 -29
- package/dist/user-group/index.d.ts +4 -223
- package/dist/user-group/index.js +61 -479
- package/dist/user-group/types.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { getAppToken } from "../token.js";
|
|
2
|
+
import { ApiError, ConfigurationError, NetworkError } from "../errors.js";
|
|
3
|
+
function parseApiResponse(json, errorMessage, statusCode) {
|
|
4
|
+
if (typeof json !== "object" || json === null) {
|
|
5
|
+
throw new ApiError(`Invalid response: ${errorMessage} - not an object`, statusCode);
|
|
6
|
+
}
|
|
7
|
+
const response = json;
|
|
8
|
+
if (response.code !== 200) {
|
|
9
|
+
const errorMsg = typeof response.msg === "string" ? response.msg : errorMessage;
|
|
10
|
+
throw new ApiError(errorMsg, statusCode, response.code);
|
|
11
|
+
}
|
|
12
|
+
if (!response.data) {
|
|
13
|
+
throw new ApiError(`Invalid response: ${errorMessage} - missing data`, statusCode);
|
|
14
|
+
}
|
|
15
|
+
return response.data;
|
|
16
|
+
}
|
|
17
|
+
function assertRequiredString(value, fieldName) {
|
|
18
|
+
if (!value || value.trim() === "") {
|
|
19
|
+
throw new ApiError(`${fieldName} is required`, 400, "9400");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function getBaseUrl(options) {
|
|
23
|
+
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
24
|
+
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
25
|
+
if (!base) {
|
|
26
|
+
throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
|
|
27
|
+
}
|
|
28
|
+
return String(base).replace(/\/+$/, "");
|
|
29
|
+
}
|
|
30
|
+
/** List attachment types */
|
|
31
|
+
export async function listAttachmentTypes(request) {
|
|
32
|
+
const token = request?.appToken ?? (await getAppToken(request));
|
|
33
|
+
const base = getBaseUrl(request);
|
|
34
|
+
const url = new URL(base + "/attachment-type/v2/types");
|
|
35
|
+
const { appToken, baseUrl, ...requestParams } = request || {};
|
|
36
|
+
const queryParams = {
|
|
37
|
+
page: 0,
|
|
38
|
+
size: 20,
|
|
39
|
+
sort: "createdAt,desc",
|
|
40
|
+
...requestParams,
|
|
41
|
+
};
|
|
42
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
43
|
+
if (value !== undefined) {
|
|
44
|
+
url.searchParams.append(key, String(value));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
let response;
|
|
48
|
+
try {
|
|
49
|
+
response = await globalThis.fetch(url.toString(), {
|
|
50
|
+
method: "GET",
|
|
51
|
+
headers: {
|
|
52
|
+
"Content-Type": "application/json",
|
|
53
|
+
"x-t-token": token,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
throw new NetworkError(`Failed to list attachment types: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
59
|
+
}
|
|
60
|
+
const json = await response.json();
|
|
61
|
+
return parseApiResponse(json, "Failed to list attachment types", response.status);
|
|
62
|
+
}
|
|
63
|
+
/** List attachment types by reference */
|
|
64
|
+
export async function listAttachmentTypesByRef(request) {
|
|
65
|
+
const refTypeId = request?.refTypeId?.trim();
|
|
66
|
+
const refType = request?.refType?.trim();
|
|
67
|
+
if (!refTypeId && !refType) {
|
|
68
|
+
throw new ApiError("At least one of refTypeId or refType is required", 400, "9400");
|
|
69
|
+
}
|
|
70
|
+
const token = request.appToken ?? (await getAppToken(request));
|
|
71
|
+
const base = getBaseUrl(request);
|
|
72
|
+
const url = new URL(base + "/attachment-type/v2/types/by-ref");
|
|
73
|
+
const { appToken, baseUrl, refTypeId: _refTypeId, refType: _refType, ...pagination } = request;
|
|
74
|
+
if (refTypeId) {
|
|
75
|
+
url.searchParams.append("refTypeId", refTypeId);
|
|
76
|
+
}
|
|
77
|
+
if (refType) {
|
|
78
|
+
url.searchParams.append("refType", refType);
|
|
79
|
+
}
|
|
80
|
+
const queryParams = {
|
|
81
|
+
page: 0,
|
|
82
|
+
size: 20,
|
|
83
|
+
sort: "createdAt,desc",
|
|
84
|
+
...pagination,
|
|
85
|
+
};
|
|
86
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
87
|
+
if (value !== undefined) {
|
|
88
|
+
url.searchParams.append(key, String(value));
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
let response;
|
|
92
|
+
try {
|
|
93
|
+
response = await globalThis.fetch(url.toString(), {
|
|
94
|
+
method: "GET",
|
|
95
|
+
headers: {
|
|
96
|
+
"Content-Type": "application/json",
|
|
97
|
+
"x-t-token": token,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
throw new NetworkError(`Failed to list attachment types by ref: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
103
|
+
}
|
|
104
|
+
const json = await response.json();
|
|
105
|
+
return parseApiResponse(json, "Failed to list attachment types by ref", response.status);
|
|
106
|
+
}
|
|
107
|
+
/** Get attachment type by ID */
|
|
108
|
+
export async function getAttachmentType(typeId, options) {
|
|
109
|
+
assertRequiredString(typeId, "typeId");
|
|
110
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
111
|
+
const base = getBaseUrl(options);
|
|
112
|
+
const url = new URL(base + `/attachment-type/v2/types/${encodeURIComponent(typeId)}`);
|
|
113
|
+
let response;
|
|
114
|
+
try {
|
|
115
|
+
response = await globalThis.fetch(url.toString(), {
|
|
116
|
+
method: "GET",
|
|
117
|
+
headers: {
|
|
118
|
+
"Content-Type": "application/json",
|
|
119
|
+
"x-t-token": token,
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
throw new NetworkError(`Failed to get attachment type: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
125
|
+
}
|
|
126
|
+
const json = await response.json();
|
|
127
|
+
return parseApiResponse(json, "Failed to get attachment type", response.status);
|
|
128
|
+
}
|
|
129
|
+
/** Create attachment type */
|
|
130
|
+
export async function createAttachmentType(request, options) {
|
|
131
|
+
assertRequiredString(request?.refType, "refType");
|
|
132
|
+
assertRequiredString(request?.typeCode, "typeCode");
|
|
133
|
+
assertRequiredString(request?.typeName, "typeName");
|
|
134
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
135
|
+
const base = getBaseUrl(options);
|
|
136
|
+
const url = new URL(base + "/attachment-type/v2/types");
|
|
137
|
+
let response;
|
|
138
|
+
try {
|
|
139
|
+
response = await globalThis.fetch(url.toString(), {
|
|
140
|
+
method: "POST",
|
|
141
|
+
headers: {
|
|
142
|
+
"Content-Type": "application/json",
|
|
143
|
+
"x-t-token": token,
|
|
144
|
+
},
|
|
145
|
+
body: JSON.stringify(request),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw new NetworkError(`Failed to create attachment type: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
150
|
+
}
|
|
151
|
+
const json = await response.json();
|
|
152
|
+
return parseApiResponse(json, "Failed to create attachment type", response.status);
|
|
153
|
+
}
|
|
154
|
+
/** Update attachment type */
|
|
155
|
+
export async function updateAttachmentType(typeId, request, options) {
|
|
156
|
+
assertRequiredString(typeId, "typeId");
|
|
157
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
158
|
+
const base = getBaseUrl(options);
|
|
159
|
+
const url = new URL(base + `/attachment-type/v2/types/${encodeURIComponent(typeId)}`);
|
|
160
|
+
let response;
|
|
161
|
+
try {
|
|
162
|
+
response = await globalThis.fetch(url.toString(), {
|
|
163
|
+
method: "PUT",
|
|
164
|
+
headers: {
|
|
165
|
+
"Content-Type": "application/json",
|
|
166
|
+
"x-t-token": token,
|
|
167
|
+
},
|
|
168
|
+
body: JSON.stringify(request),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
throw new NetworkError(`Failed to update attachment type: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
173
|
+
}
|
|
174
|
+
const json = await response.json();
|
|
175
|
+
return parseApiResponse(json, "Failed to update attachment type", response.status);
|
|
176
|
+
}
|
|
177
|
+
/** Delete attachment type */
|
|
178
|
+
export async function deleteAttachmentType(typeId, options) {
|
|
179
|
+
assertRequiredString(typeId, "typeId");
|
|
180
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
181
|
+
const base = getBaseUrl(options);
|
|
182
|
+
const url = new URL(base + `/attachment-type/v2/types/${encodeURIComponent(typeId)}`);
|
|
183
|
+
let response;
|
|
184
|
+
try {
|
|
185
|
+
response = await globalThis.fetch(url.toString(), {
|
|
186
|
+
method: "DELETE",
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": "application/json",
|
|
189
|
+
"x-t-token": token,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
throw new NetworkError(`Failed to delete attachment type: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
195
|
+
}
|
|
196
|
+
const json = (await response.json());
|
|
197
|
+
if (json.code !== 200) {
|
|
198
|
+
const errorMsg = typeof json.msg === "string"
|
|
199
|
+
? json.msg
|
|
200
|
+
: "Failed to delete attachment type";
|
|
201
|
+
throw new ApiError(errorMsg, response.status, json.code);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { CommonOptions } from "../common/types.js";
|
|
2
|
+
export type { AttachmentRefType } from "../attachment/types.js";
|
|
3
|
+
type AttachmentRefType = import("../attachment/types.js").AttachmentRefType;
|
|
4
|
+
export interface AttachmentType {
|
|
5
|
+
typeId: string;
|
|
6
|
+
appId: string;
|
|
7
|
+
refType: string;
|
|
8
|
+
refTypeId?: string | null;
|
|
9
|
+
typeCode: string;
|
|
10
|
+
typeName: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
allowedExtensions: string[];
|
|
13
|
+
allowedMimeTypes: string[];
|
|
14
|
+
maxFileSize: number;
|
|
15
|
+
isUnique: boolean;
|
|
16
|
+
isEnabled: boolean;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
updatedAt: string;
|
|
19
|
+
remark?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface CreateAttachmentTypeRequest {
|
|
22
|
+
refType: AttachmentRefType;
|
|
23
|
+
refTypeId?: string;
|
|
24
|
+
typeCode: string;
|
|
25
|
+
typeName: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
allowedExtensions?: string[];
|
|
28
|
+
allowedMimeTypes?: string[];
|
|
29
|
+
maxFileSize?: number;
|
|
30
|
+
isUnique?: boolean;
|
|
31
|
+
isEnabled?: boolean;
|
|
32
|
+
remark?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface UpdateAttachmentTypeRequest {
|
|
35
|
+
refType?: AttachmentRefType;
|
|
36
|
+
refTypeId?: string;
|
|
37
|
+
typeCode?: string;
|
|
38
|
+
typeName?: string;
|
|
39
|
+
description?: string;
|
|
40
|
+
allowedExtensions?: string[];
|
|
41
|
+
allowedMimeTypes?: string[];
|
|
42
|
+
maxFileSize?: number;
|
|
43
|
+
isUnique?: boolean;
|
|
44
|
+
isEnabled?: boolean;
|
|
45
|
+
remark?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface ListAttachmentTypesRequest {
|
|
48
|
+
page?: number;
|
|
49
|
+
size?: number;
|
|
50
|
+
sort?: string;
|
|
51
|
+
}
|
|
52
|
+
export interface ListAttachmentTypesByRefRequest {
|
|
53
|
+
refTypeId?: string;
|
|
54
|
+
refType?: AttachmentRefType;
|
|
55
|
+
page?: number;
|
|
56
|
+
size?: number;
|
|
57
|
+
sort?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface AttachmentTypeOptions extends CommonOptions {
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -35,11 +35,11 @@ export type { LoginRequest, LoginOptions, LoginWithSmsOptions, VerifySmsOptions,
|
|
|
35
35
|
* username: 'johndoe',
|
|
36
36
|
* password: 'secure_password_123'
|
|
37
37
|
* }, {
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
38
|
+
* includeRbac: true, // Include roles and privileges (SDK default: false)
|
|
39
|
+
* includeUserGroups: true, // Include user groups (SDK default: false)
|
|
40
|
+
* includeLoginMethods: true, // Include login methods (SDK default: false)
|
|
41
|
+
* includeAttributes: true, // Include user attributes (SDK default: false)
|
|
42
|
+
* includeAcl: true // Include ACL records (SDK default: false)
|
|
43
43
|
* });
|
|
44
44
|
* ```
|
|
45
45
|
*
|
|
@@ -49,11 +49,11 @@ export type { LoginRequest, LoginOptions, LoginWithSmsOptions, VerifySmsOptions,
|
|
|
49
49
|
* @note By default, all include parameters are set to false for better performance.
|
|
50
50
|
* Explicitly set them to true if you need the additional data.
|
|
51
51
|
*
|
|
52
|
-
* @param options.
|
|
53
|
-
* @param options.
|
|
54
|
-
* @param options.
|
|
55
|
-
* @param options.
|
|
56
|
-
* @param options.
|
|
52
|
+
* @param options.includeRbac Include roles and privileges; default false
|
|
53
|
+
* @param options.includeLoginMethods Include user login methods; default false
|
|
54
|
+
* @param options.includeUserGroups Include user groups; default false
|
|
55
|
+
* @param options.includeAttributes Include user attributes; default false
|
|
56
|
+
* @param options.includeAcl Include ACL records; default false
|
|
57
57
|
*/
|
|
58
58
|
export declare function login(credentials: LoginRequest, options?: LoginOptions): Promise<LoginResponse>;
|
|
59
59
|
/**
|
|
@@ -98,7 +98,7 @@ export declare function validateToken(token: string, options?: {
|
|
|
98
98
|
*
|
|
99
99
|
* try {
|
|
100
100
|
* const result = await loginWithSms('+8613800138000');
|
|
101
|
-
* console.log('SMS sent:', result.
|
|
101
|
+
* console.log('SMS sent:', result.smsId);
|
|
102
102
|
* console.log('Type:', result.type); // 'login' or 'register'
|
|
103
103
|
* } catch (error) {
|
|
104
104
|
* console.error('SMS sending failed:', error.message);
|
|
@@ -109,7 +109,7 @@ export declare function loginWithSms(phone: string, options?: LoginWithSmsOption
|
|
|
109
109
|
/**
|
|
110
110
|
* Verifies SMS code and authenticates user (login or register).
|
|
111
111
|
*
|
|
112
|
-
* @param request - SMS verification request with
|
|
112
|
+
* @param request - SMS verification request with smsId, code, and optional user data
|
|
113
113
|
* @param options - Optional configuration for the request
|
|
114
114
|
* @returns Promise resolving to login response with user info and token
|
|
115
115
|
* @throws {ConfigurationError} When required environment variables are missing
|
|
@@ -122,11 +122,11 @@ export declare function loginWithSms(phone: string, options?: LoginWithSmsOption
|
|
|
122
122
|
*
|
|
123
123
|
* try {
|
|
124
124
|
* const result = await verifySmsCode({
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
125
|
+
* smsId: 'uuid-here',
|
|
126
|
+
* smsType: 'login',
|
|
127
|
+
* verificationCode: '123456'
|
|
128
128
|
* });
|
|
129
|
-
* console.log('Login successful:', result.user.
|
|
129
|
+
* console.log('Login successful:', result.user.userId);
|
|
130
130
|
* console.log('User token:', result.token.token);
|
|
131
131
|
* } catch (error) {
|
|
132
132
|
* console.error('SMS verification failed:', error.message);
|
|
@@ -150,13 +150,13 @@ export declare function verifySmsCode(request: VerifySmsRequest, options?: Verif
|
|
|
150
150
|
*
|
|
151
151
|
* try {
|
|
152
152
|
* const result = await registerWithSms({
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
153
|
+
* smsId: 'uuid-here',
|
|
154
|
+
* smsType: 'register',
|
|
155
|
+
* verificationCode: '123456',
|
|
156
156
|
* username: 'newuser',
|
|
157
|
-
*
|
|
157
|
+
* passwordEncrypted: 'encrypted_password'
|
|
158
158
|
* });
|
|
159
|
-
* console.log('Registration successful:', result.user.
|
|
159
|
+
* console.log('Registration successful:', result.user.userId);
|
|
160
160
|
* console.log('User token:', result.token.token);
|
|
161
161
|
* } catch (error) {
|
|
162
162
|
* console.error('Registration failed:', error.message);
|
package/dist/auth/index.js
CHANGED
|
@@ -35,11 +35,11 @@ import { ApiError, ConfigurationError, NetworkError } from "../errors.js";
|
|
|
35
35
|
* username: 'johndoe',
|
|
36
36
|
* password: 'secure_password_123'
|
|
37
37
|
* }, {
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
38
|
+
* includeRbac: true, // Include roles and privileges (SDK default: false)
|
|
39
|
+
* includeUserGroups: true, // Include user groups (SDK default: false)
|
|
40
|
+
* includeLoginMethods: true, // Include login methods (SDK default: false)
|
|
41
|
+
* includeAttributes: true, // Include user attributes (SDK default: false)
|
|
42
|
+
* includeAcl: true // Include ACL records (SDK default: false)
|
|
43
43
|
* });
|
|
44
44
|
* ```
|
|
45
45
|
*
|
|
@@ -49,11 +49,11 @@ import { ApiError, ConfigurationError, NetworkError } from "../errors.js";
|
|
|
49
49
|
* @note By default, all include parameters are set to false for better performance.
|
|
50
50
|
* Explicitly set them to true if you need the additional data.
|
|
51
51
|
*
|
|
52
|
-
* @param options.
|
|
53
|
-
* @param options.
|
|
54
|
-
* @param options.
|
|
55
|
-
* @param options.
|
|
56
|
-
* @param options.
|
|
52
|
+
* @param options.includeRbac Include roles and privileges; default false
|
|
53
|
+
* @param options.includeLoginMethods Include user login methods; default false
|
|
54
|
+
* @param options.includeUserGroups Include user groups; default false
|
|
55
|
+
* @param options.includeAttributes Include user attributes; default false
|
|
56
|
+
* @param options.includeAcl Include ACL records; default false
|
|
57
57
|
*/
|
|
58
58
|
export async function login(credentials, options) {
|
|
59
59
|
// Validate required fields
|
|
@@ -75,20 +75,20 @@ export async function login(credentials, options) {
|
|
|
75
75
|
throw new ConfigurationError("Missing required environment variable: TALE_APP_KEY");
|
|
76
76
|
}
|
|
77
77
|
// Build URL with query parameters
|
|
78
|
-
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/
|
|
78
|
+
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/v2/login/username-password");
|
|
79
79
|
// Add include parameters as query params (SDK defaults to false)
|
|
80
|
-
url.searchParams.set("
|
|
81
|
-
url.searchParams.set("
|
|
82
|
-
url.searchParams.set("
|
|
83
|
-
url.searchParams.set("
|
|
84
|
-
url.searchParams.set("
|
|
80
|
+
url.searchParams.set("includeRbac", String(options?.includeRbac ?? false));
|
|
81
|
+
url.searchParams.set("includeLoginMethods", String(options?.includeLoginMethods ?? false));
|
|
82
|
+
url.searchParams.set("includeUserGroups", String(options?.includeUserGroups ?? false));
|
|
83
|
+
url.searchParams.set("includeAttributes", String(options?.includeAttributes ?? false));
|
|
84
|
+
url.searchParams.set("includeAcl", String(options?.includeAcl ?? false));
|
|
85
85
|
// Build request body
|
|
86
86
|
const requestBody = {
|
|
87
87
|
username: credentials.username.trim(),
|
|
88
88
|
password: credentials.password,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
appKey: appKey,
|
|
90
|
+
deviceName: options?.deviceName,
|
|
91
|
+
deviceFingerprint: options?.deviceFingerprint,
|
|
92
92
|
scope: options?.scope || "",
|
|
93
93
|
};
|
|
94
94
|
let response;
|
|
@@ -125,7 +125,7 @@ export async function login(credentials, options) {
|
|
|
125
125
|
throw new ApiError("Account is frozen or access forbidden", response.status, "9403");
|
|
126
126
|
}
|
|
127
127
|
if (response.status === 401) {
|
|
128
|
-
throw new ApiError("Invalid
|
|
128
|
+
throw new ApiError("Invalid appKey or authentication failed", response.status, "9401");
|
|
129
129
|
}
|
|
130
130
|
if (!response.ok) {
|
|
131
131
|
const errorMsg = typeof json === "object" &&
|
|
@@ -147,10 +147,10 @@ export async function login(credentials, options) {
|
|
|
147
147
|
app: responseData.app,
|
|
148
148
|
user: responseData.user,
|
|
149
149
|
token: responseData.token,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
userRoles: responseData.userRoles || [],
|
|
151
|
+
userPrivileges: responseData.userPrivileges || [],
|
|
152
|
+
userGroups: responseData.userGroups || [],
|
|
153
|
+
userLoginMethods: responseData.userLoginMethods || [],
|
|
154
154
|
};
|
|
155
155
|
return loginResponse;
|
|
156
156
|
}
|
|
@@ -187,7 +187,7 @@ export async function validateToken(token, options) {
|
|
|
187
187
|
if (!base) {
|
|
188
188
|
throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
|
|
189
189
|
}
|
|
190
|
-
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/
|
|
190
|
+
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/v2/assert-token");
|
|
191
191
|
if (options?.scope) {
|
|
192
192
|
url.searchParams.append("scope", options.scope);
|
|
193
193
|
}
|
|
@@ -223,7 +223,7 @@ export async function validateToken(token, options) {
|
|
|
223
223
|
*
|
|
224
224
|
* try {
|
|
225
225
|
* const result = await loginWithSms('+8613800138000');
|
|
226
|
-
* console.log('SMS sent:', result.
|
|
226
|
+
* console.log('SMS sent:', result.smsId);
|
|
227
227
|
* console.log('Type:', result.type); // 'login' or 'register'
|
|
228
228
|
* } catch (error) {
|
|
229
229
|
* console.error('SMS sending failed:', error.message);
|
|
@@ -243,7 +243,7 @@ export async function loginWithSms(phone, options) {
|
|
|
243
243
|
}
|
|
244
244
|
// Use provided app token or get one from token service
|
|
245
245
|
const authToken = options?.appToken ?? (await getAppToken());
|
|
246
|
-
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/
|
|
246
|
+
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/v2/sms/login-or-register");
|
|
247
247
|
url.searchParams.append("phone", phone.trim());
|
|
248
248
|
let response;
|
|
249
249
|
try {
|
|
@@ -289,21 +289,21 @@ export async function loginWithSms(phone, options) {
|
|
|
289
289
|
// Handle response data structure - check if data is wrapped in a 'data' field
|
|
290
290
|
const responseData = json.data || json;
|
|
291
291
|
// Validate response structure
|
|
292
|
-
if (!responseData || !responseData.
|
|
292
|
+
if (!responseData || !responseData.smsId || !responseData.type) {
|
|
293
293
|
throw new ApiError("Invalid SMS response: missing required data", response.status);
|
|
294
294
|
}
|
|
295
295
|
return {
|
|
296
|
-
|
|
296
|
+
appKey: responseData.appKey,
|
|
297
297
|
phone: responseData.phone,
|
|
298
298
|
type: responseData.type,
|
|
299
|
-
|
|
300
|
-
|
|
299
|
+
smsId: responseData.smsId,
|
|
300
|
+
expiredAt: responseData.expiredAt,
|
|
301
301
|
};
|
|
302
302
|
}
|
|
303
303
|
/**
|
|
304
304
|
* Verifies SMS code and authenticates user (login or register).
|
|
305
305
|
*
|
|
306
|
-
* @param request - SMS verification request with
|
|
306
|
+
* @param request - SMS verification request with smsId, code, and optional user data
|
|
307
307
|
* @param options - Optional configuration for the request
|
|
308
308
|
* @returns Promise resolving to login response with user info and token
|
|
309
309
|
* @throws {ConfigurationError} When required environment variables are missing
|
|
@@ -316,11 +316,11 @@ export async function loginWithSms(phone, options) {
|
|
|
316
316
|
*
|
|
317
317
|
* try {
|
|
318
318
|
* const result = await verifySmsCode({
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
319
|
+
* smsId: 'uuid-here',
|
|
320
|
+
* smsType: 'login',
|
|
321
|
+
* verificationCode: '123456'
|
|
322
322
|
* });
|
|
323
|
-
* console.log('Login successful:', result.user.
|
|
323
|
+
* console.log('Login successful:', result.user.userId);
|
|
324
324
|
* console.log('User token:', result.token.token);
|
|
325
325
|
* } catch (error) {
|
|
326
326
|
* console.error('SMS verification failed:', error.message);
|
|
@@ -329,14 +329,14 @@ export async function loginWithSms(phone, options) {
|
|
|
329
329
|
*/
|
|
330
330
|
export async function verifySmsCode(request, options) {
|
|
331
331
|
// Validate required fields
|
|
332
|
-
if (!request.
|
|
333
|
-
throw new ApiError("
|
|
332
|
+
if (!request.smsId || request.smsId.trim() === "") {
|
|
333
|
+
throw new ApiError("smsId is required", 400, "9400");
|
|
334
334
|
}
|
|
335
|
-
if (!request.
|
|
336
|
-
throw new ApiError('
|
|
335
|
+
if (!request.smsType || !["login", "register"].includes(request.smsType)) {
|
|
336
|
+
throw new ApiError('smsType must be "login" or "register"', 400, "9400");
|
|
337
337
|
}
|
|
338
|
-
if (!request.
|
|
339
|
-
throw new ApiError("
|
|
338
|
+
if (!request.verificationCode || request.verificationCode.trim() === "") {
|
|
339
|
+
throw new ApiError("verificationCode is required", 400, "9400");
|
|
340
340
|
}
|
|
341
341
|
// Determine base URL
|
|
342
342
|
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
@@ -346,10 +346,10 @@ export async function verifySmsCode(request, options) {
|
|
|
346
346
|
}
|
|
347
347
|
// Use provided app token or get one from token service
|
|
348
348
|
const authToken = options?.appToken ?? (await getAppToken(options));
|
|
349
|
-
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/
|
|
350
|
-
url.searchParams.append("
|
|
351
|
-
url.searchParams.append("
|
|
352
|
-
url.searchParams.append("
|
|
349
|
+
const url = new URL(String(base).replace(/\/+$/, "") + "/auth/v2/sms/verify");
|
|
350
|
+
url.searchParams.append("smsId", request.smsId.trim());
|
|
351
|
+
url.searchParams.append("smsType", request.smsType);
|
|
352
|
+
url.searchParams.append("verificationCode", request.verificationCode.trim());
|
|
353
353
|
let response;
|
|
354
354
|
try {
|
|
355
355
|
response = await globalThis.fetch(url.toString(), {
|
|
@@ -402,10 +402,10 @@ export async function verifySmsCode(request, options) {
|
|
|
402
402
|
app: responseData.app,
|
|
403
403
|
user: responseData.user,
|
|
404
404
|
token: responseData.token,
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
405
|
+
userRoles: responseData.userRoles || [],
|
|
406
|
+
userPrivileges: responseData.userPrivileges || [],
|
|
407
|
+
userGroups: responseData.userGroups || [],
|
|
408
|
+
userLoginMethods: responseData.userLoginMethods || [],
|
|
409
409
|
};
|
|
410
410
|
return smsLoginResponse;
|
|
411
411
|
}
|
|
@@ -425,13 +425,13 @@ export async function verifySmsCode(request, options) {
|
|
|
425
425
|
*
|
|
426
426
|
* try {
|
|
427
427
|
* const result = await registerWithSms({
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
428
|
+
* smsId: 'uuid-here',
|
|
429
|
+
* smsType: 'register',
|
|
430
|
+
* verificationCode: '123456',
|
|
431
431
|
* username: 'newuser',
|
|
432
|
-
*
|
|
432
|
+
* passwordEncrypted: 'encrypted_password'
|
|
433
433
|
* });
|
|
434
|
-
* console.log('Registration successful:', result.user.
|
|
434
|
+
* console.log('Registration successful:', result.user.userId);
|
|
435
435
|
* console.log('User token:', result.token.token);
|
|
436
436
|
* } catch (error) {
|
|
437
437
|
* console.error('Registration failed:', error.message);
|
|
@@ -440,8 +440,8 @@ export async function verifySmsCode(request, options) {
|
|
|
440
440
|
*/
|
|
441
441
|
export async function registerWithSms(request, options) {
|
|
442
442
|
// Validate required fields for registration
|
|
443
|
-
if (request.
|
|
444
|
-
throw new ApiError('
|
|
443
|
+
if (request.smsType !== "register") {
|
|
444
|
+
throw new ApiError('smsType must be "register" for registration', 400, "9400");
|
|
445
445
|
}
|
|
446
446
|
// Determine base URL
|
|
447
447
|
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
@@ -451,14 +451,14 @@ export async function registerWithSms(request, options) {
|
|
|
451
451
|
}
|
|
452
452
|
// Use provided app token or get one from token service
|
|
453
453
|
const authToken = options?.appToken ?? (await getAppToken(options));
|
|
454
|
-
const url = String(base).replace(/\/+$/, "") + "/auth/
|
|
454
|
+
const url = String(base).replace(/\/+$/, "") + "/auth/v2/sms/register/verify";
|
|
455
455
|
// Build request body
|
|
456
456
|
const requestBody = {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
457
|
+
smsId: request.smsId.trim(),
|
|
458
|
+
smsType: request.smsType,
|
|
459
|
+
verificationCode: request.verificationCode.trim(),
|
|
460
460
|
username: request.username?.trim(),
|
|
461
|
-
|
|
461
|
+
passwordEncrypted: request.passwordEncrypted,
|
|
462
462
|
};
|
|
463
463
|
let response;
|
|
464
464
|
try {
|
|
@@ -513,10 +513,10 @@ export async function registerWithSms(request, options) {
|
|
|
513
513
|
app: responseData.app,
|
|
514
514
|
user: responseData.user,
|
|
515
515
|
token: responseData.token,
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
516
|
+
userRoles: responseData.userRoles || [],
|
|
517
|
+
userPrivileges: responseData.userPrivileges || [],
|
|
518
|
+
userGroups: responseData.userGroups || [],
|
|
519
|
+
userLoginMethods: responseData.userLoginMethods || [],
|
|
520
520
|
};
|
|
521
521
|
return registrationResponse;
|
|
522
522
|
}
|