tale-js-sdk 0.1.2 → 0.1.4
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/dist/acl/index.d.ts +170 -0
- package/dist/acl/index.js +747 -0
- package/dist/acl/types.d.ts +208 -0
- package/dist/acl/types.js +1 -0
- package/dist/auth/index.d.ts +2 -134
- package/dist/auth/index.js +120 -96
- package/dist/auth/types.d.ts +122 -0
- package/dist/auth/types.js +1 -0
- package/dist/common/types.d.ts +82 -0
- package/dist/common/types.js +2 -0
- package/dist/errors.js +18 -18
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/rbac/acl.d.ts +152 -0
- package/dist/rbac/acl.js +723 -0
- package/dist/rbac/index.d.ts +2 -0
- package/dist/rbac/index.js +2 -0
- package/dist/rbac/rbac.d.ts +198 -0
- package/dist/rbac/rbac.js +984 -0
- package/dist/rbac/types.d.ts +356 -0
- package/dist/rbac/types.js +1 -0
- package/dist/rbac/user-group.d.ts +122 -0
- package/dist/rbac/user-group.js +570 -0
- package/dist/status.js +3 -3
- package/dist/token.d.ts +1 -1
- package/dist/token.js +5 -4
- package/dist/user/index.d.ts +165 -142
- package/dist/user/index.js +511 -59
- package/dist/user/types.d.ts +149 -0
- package/dist/user/types.js +1 -0
- package/dist/user-group/index.d.ts +230 -0
- package/dist/user-group/index.js +560 -0
- package/dist/user-group/types.d.ts +64 -0
- package/dist/user-group/types.js +1 -0
- package/package.json +4 -3
- package/dist/auth.d.ts +0 -271
- package/dist/auth.js +0 -461
- package/dist/client.d.ts +0 -20
- package/dist/client.js +0 -62
- package/dist/info.d.ts +0 -9
- package/dist/info.js +0 -18
- package/dist/package.json +0 -36
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/info.d.ts +0 -6
- package/dist/src/info.js +0 -4
- package/dist/user.d.ts +0 -242
- package/dist/user.js +0 -331
package/dist/auth.js
DELETED
|
@@ -1,461 +0,0 @@
|
|
|
1
|
-
import { ApiError, ConfigurationError, NetworkError } from './errors.js';
|
|
2
|
-
/**
|
|
3
|
-
* Authenticates a user with username and password.
|
|
4
|
-
*
|
|
5
|
-
* @param credentials - User login credentials
|
|
6
|
-
* @param options - Optional configuration for the login request
|
|
7
|
-
* @returns Promise resolving to login response with user info and token
|
|
8
|
-
* @throws {ConfigurationError} When required environment variables are missing
|
|
9
|
-
* @throws {ApiError} When authentication fails or returns invalid response
|
|
10
|
-
* @throws {NetworkError} When network request fails
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* import { login } from '@tale/client';
|
|
15
|
-
*
|
|
16
|
-
* try {
|
|
17
|
-
* const result = await login({
|
|
18
|
-
* username: 'johndoe',
|
|
19
|
-
* password: 'secure_password_123'
|
|
20
|
-
* });
|
|
21
|
-
* console.log('Login successful:', result.user.username);
|
|
22
|
-
* console.log('User token:', result.token.token);
|
|
23
|
-
* } catch (error) {
|
|
24
|
-
* console.error('Login failed:', error.message);
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export async function login(credentials, options) {
|
|
29
|
-
// Validate required fields
|
|
30
|
-
if (!credentials.username || credentials.username.trim() === '') {
|
|
31
|
-
throw new ApiError('username is required', 400, '9400');
|
|
32
|
-
}
|
|
33
|
-
if (!credentials.password || credentials.password.trim() === '') {
|
|
34
|
-
throw new ApiError('password is required', 400, '9400');
|
|
35
|
-
}
|
|
36
|
-
// Determine base URL
|
|
37
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
38
|
-
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
39
|
-
if (!base) {
|
|
40
|
-
throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
|
|
41
|
-
}
|
|
42
|
-
// Determine app key
|
|
43
|
-
const appKey = credentials.app_key ?? options?.appKey ?? env?.TALE_APP_KEY ?? undefined;
|
|
44
|
-
if (!appKey) {
|
|
45
|
-
throw new ConfigurationError('Missing required app_key or TALE_APP_KEY');
|
|
46
|
-
}
|
|
47
|
-
const url = String(base).replace(/\/+$/, '') + '/auth/v1/login/username-password';
|
|
48
|
-
// Build request body
|
|
49
|
-
const requestBody = {
|
|
50
|
-
username: credentials.username.trim(),
|
|
51
|
-
password: credentials.password,
|
|
52
|
-
app_key: appKey,
|
|
53
|
-
device_name: options?.device_name,
|
|
54
|
-
device_fingerprint: options?.device_fingerprint,
|
|
55
|
-
scope: options?.scope || ''
|
|
56
|
-
};
|
|
57
|
-
let response;
|
|
58
|
-
try {
|
|
59
|
-
response = await globalThis.fetch(url, {
|
|
60
|
-
method: 'POST',
|
|
61
|
-
headers: {
|
|
62
|
-
'Content-Type': 'application/json',
|
|
63
|
-
},
|
|
64
|
-
body: JSON.stringify(requestBody),
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
throw new NetworkError(`Failed to authenticate user: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
69
|
-
}
|
|
70
|
-
let json;
|
|
71
|
-
try {
|
|
72
|
-
const responseJson = await response.json();
|
|
73
|
-
json = responseJson;
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
throw new ApiError(`Failed to parse login response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
|
|
77
|
-
}
|
|
78
|
-
// Handle authentication errors based on response status
|
|
79
|
-
if (response.status === 400) {
|
|
80
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
81
|
-
String(json.message) : 'Authentication failed';
|
|
82
|
-
throw new ApiError(errorMsg, response.status, '9400');
|
|
83
|
-
}
|
|
84
|
-
if (response.status === 403) {
|
|
85
|
-
throw new ApiError('Account is frozen or access forbidden', response.status, '9403');
|
|
86
|
-
}
|
|
87
|
-
if (response.status === 401) {
|
|
88
|
-
throw new ApiError('Invalid app_key or authentication failed', response.status, '9401');
|
|
89
|
-
}
|
|
90
|
-
if (!response.ok) {
|
|
91
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
92
|
-
String(json.message) : 'Authentication failed';
|
|
93
|
-
throw new ApiError(errorMsg, response.status);
|
|
94
|
-
}
|
|
95
|
-
// Validate response structure
|
|
96
|
-
if (!json || !json.user || !json.token) {
|
|
97
|
-
throw new ApiError('Invalid login response: missing required data', response.status);
|
|
98
|
-
}
|
|
99
|
-
// Build standard response structure
|
|
100
|
-
const loginResponse = {
|
|
101
|
-
app: json.app,
|
|
102
|
-
user: json.user,
|
|
103
|
-
token: json.token,
|
|
104
|
-
user_roles: json.user_roles || [],
|
|
105
|
-
user_privileges: json.user_privileges || [],
|
|
106
|
-
user_groups: json.user_groups || [],
|
|
107
|
-
user_login_methods: json.user_login_methods || []
|
|
108
|
-
};
|
|
109
|
-
return loginResponse;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Validates a user token.
|
|
113
|
-
*
|
|
114
|
-
* @param token - User token to validate
|
|
115
|
-
* @param options - Optional configuration for validation
|
|
116
|
-
* @returns Promise resolving to validation result
|
|
117
|
-
* @throws {ConfigurationError} When required environment variables are missing
|
|
118
|
-
* @throws {ApiError} When validation fails
|
|
119
|
-
* @throws {NetworkError} When network request fails
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```typescript
|
|
123
|
-
* import { validateToken } from '@tale/client';
|
|
124
|
-
*
|
|
125
|
-
* try {
|
|
126
|
-
* const result = await validateToken('tu_user_token_here');
|
|
127
|
-
* console.log('Token is valid');
|
|
128
|
-
* } catch (error) {
|
|
129
|
-
* console.error('Token validation failed:', error.message);
|
|
130
|
-
* }
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
export async function validateToken(token, options) {
|
|
134
|
-
// Validate required fields
|
|
135
|
-
if (!token || token.trim() === '') {
|
|
136
|
-
throw new ApiError('token is required', 400, '9400');
|
|
137
|
-
}
|
|
138
|
-
// Determine base URL
|
|
139
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
140
|
-
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
141
|
-
if (!base) {
|
|
142
|
-
throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
|
|
143
|
-
}
|
|
144
|
-
const url = new URL(String(base).replace(/\/+$/, '') + '/auth/v1/assert_token');
|
|
145
|
-
url.searchParams.append('token', token.trim());
|
|
146
|
-
if (options?.scope) {
|
|
147
|
-
url.searchParams.append('scope', options.scope);
|
|
148
|
-
}
|
|
149
|
-
let response;
|
|
150
|
-
try {
|
|
151
|
-
response = await globalThis.fetch(url.toString(), {
|
|
152
|
-
method: 'GET',
|
|
153
|
-
headers: {
|
|
154
|
-
'Content-Type': 'application/json',
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
throw new NetworkError(`Failed to validate token: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
160
|
-
}
|
|
161
|
-
// Token is valid if we get a 200 response
|
|
162
|
-
return response.ok;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Sends SMS verification code for login or registration.
|
|
166
|
-
*
|
|
167
|
-
* @param phone - Phone number for sending SMS
|
|
168
|
-
* @param options - Optional configuration for the request
|
|
169
|
-
* @returns Promise resolving to SMS sending response
|
|
170
|
-
* @throws {ConfigurationError} When required environment variables are missing
|
|
171
|
-
* @throws {ApiError} When SMS sending fails or returns invalid response
|
|
172
|
-
* @throws {NetworkError} When network request fails
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```typescript
|
|
176
|
-
* import { sendSmsVerification } from '@tale/client';
|
|
177
|
-
*
|
|
178
|
-
* try {
|
|
179
|
-
* const result = await sendSmsVerification('+8613800138000');
|
|
180
|
-
* console.log('SMS sent:', result.sms_id);
|
|
181
|
-
* console.log('Type:', result.type); // 'login' or 'register'
|
|
182
|
-
* } catch (error) {
|
|
183
|
-
* console.error('SMS sending failed:', error.message);
|
|
184
|
-
* }
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
export async function sendSmsVerification(phone, options) {
|
|
188
|
-
// Validate required fields
|
|
189
|
-
if (!phone || phone.trim() === '') {
|
|
190
|
-
throw new ApiError('phone number is required', 400, '9400');
|
|
191
|
-
}
|
|
192
|
-
// Determine base URL
|
|
193
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
194
|
-
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
195
|
-
if (!base) {
|
|
196
|
-
throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
|
|
197
|
-
}
|
|
198
|
-
// Determine app key
|
|
199
|
-
const appKey = options?.appKey ?? env?.TALE_APP_KEY ?? undefined;
|
|
200
|
-
if (!appKey) {
|
|
201
|
-
throw new ConfigurationError('Missing required app_key or TALE_APP_KEY');
|
|
202
|
-
}
|
|
203
|
-
const url = new URL(String(base).replace(/\/+$/, '') + '/auth/v1/sms/login_or_register');
|
|
204
|
-
url.searchParams.append('phone', phone.trim());
|
|
205
|
-
let response;
|
|
206
|
-
try {
|
|
207
|
-
response = await globalThis.fetch(url.toString(), {
|
|
208
|
-
method: 'GET',
|
|
209
|
-
headers: {
|
|
210
|
-
'Authorization': `Bearer ${appKey}`,
|
|
211
|
-
'Content-Type': 'application/json',
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
catch (error) {
|
|
216
|
-
throw new NetworkError(`Failed to send SMS verification: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
217
|
-
}
|
|
218
|
-
let json;
|
|
219
|
-
try {
|
|
220
|
-
const responseJson = await response.json();
|
|
221
|
-
json = responseJson;
|
|
222
|
-
}
|
|
223
|
-
catch (error) {
|
|
224
|
-
throw new ApiError(`Failed to parse SMS response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
|
|
225
|
-
}
|
|
226
|
-
// Handle API errors
|
|
227
|
-
if (response.status === 400) {
|
|
228
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
229
|
-
String(json.message) : 'Invalid phone number format';
|
|
230
|
-
throw new ApiError(errorMsg, response.status, '9400');
|
|
231
|
-
}
|
|
232
|
-
if (response.status === 429) {
|
|
233
|
-
throw new ApiError('SMS sending rate limit exceeded', response.status, '9705');
|
|
234
|
-
}
|
|
235
|
-
if (!response.ok) {
|
|
236
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
237
|
-
String(json.message) : 'SMS sending failed';
|
|
238
|
-
throw new ApiError(errorMsg, response.status);
|
|
239
|
-
}
|
|
240
|
-
// Validate response structure
|
|
241
|
-
if (!json || !json.sms_id || !json.type) {
|
|
242
|
-
throw new ApiError('Invalid SMS response: missing required data', response.status);
|
|
243
|
-
}
|
|
244
|
-
return {
|
|
245
|
-
app_key: json.app_key,
|
|
246
|
-
phone: json.phone,
|
|
247
|
-
type: json.type,
|
|
248
|
-
sms_id: json.sms_id,
|
|
249
|
-
expired_at: json.expired_at
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Verifies SMS code and authenticates user (login or register).
|
|
254
|
-
*
|
|
255
|
-
* @param request - SMS verification request with sms_id, code, and optional user data
|
|
256
|
-
* @param options - Optional configuration for the request
|
|
257
|
-
* @returns Promise resolving to login response with user info and token
|
|
258
|
-
* @throws {ConfigurationError} When required environment variables are missing
|
|
259
|
-
* @throws {ApiError} When verification fails or returns invalid response
|
|
260
|
-
* @throws {NetworkError} When network request fails
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* ```typescript
|
|
264
|
-
* import { verifySmsCode } from '@tale/client';
|
|
265
|
-
*
|
|
266
|
-
* try {
|
|
267
|
-
* const result = await verifySmsCode({
|
|
268
|
-
* sms_id: 'uuid-here',
|
|
269
|
-
* sms_type: 'login',
|
|
270
|
-
* verification_code: '123456'
|
|
271
|
-
* });
|
|
272
|
-
* console.log('Login successful:', result.user.user_id);
|
|
273
|
-
* console.log('User token:', result.token.token);
|
|
274
|
-
* } catch (error) {
|
|
275
|
-
* console.error('SMS verification failed:', error.message);
|
|
276
|
-
* }
|
|
277
|
-
* ```
|
|
278
|
-
*/
|
|
279
|
-
export async function verifySmsCode(request, options) {
|
|
280
|
-
// Validate required fields
|
|
281
|
-
if (!request.sms_id || request.sms_id.trim() === '') {
|
|
282
|
-
throw new ApiError('sms_id is required', 400, '9400');
|
|
283
|
-
}
|
|
284
|
-
if (!request.sms_type || !['login', 'register'].includes(request.sms_type)) {
|
|
285
|
-
throw new ApiError('sms_type must be "login" or "register"', 400, '9400');
|
|
286
|
-
}
|
|
287
|
-
if (!request.verification_code || request.verification_code.trim() === '') {
|
|
288
|
-
throw new ApiError('verification_code is required', 400, '9400');
|
|
289
|
-
}
|
|
290
|
-
// Determine base URL
|
|
291
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
292
|
-
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
293
|
-
if (!base) {
|
|
294
|
-
throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
|
|
295
|
-
}
|
|
296
|
-
// Determine app key
|
|
297
|
-
const appKey = options?.appKey ?? env?.TALE_APP_KEY ?? undefined;
|
|
298
|
-
if (!appKey) {
|
|
299
|
-
throw new ConfigurationError('Missing required app_key or TALE_APP_KEY');
|
|
300
|
-
}
|
|
301
|
-
const url = new URL(String(base).replace(/\/+$/, '') + '/auth/v1/sms/verify');
|
|
302
|
-
url.searchParams.append('sms_id', request.sms_id.trim());
|
|
303
|
-
url.searchParams.append('sms_type', request.sms_type);
|
|
304
|
-
url.searchParams.append('verification_code', request.verification_code.trim());
|
|
305
|
-
let response;
|
|
306
|
-
try {
|
|
307
|
-
response = await globalThis.fetch(url.toString(), {
|
|
308
|
-
method: 'GET',
|
|
309
|
-
headers: {
|
|
310
|
-
'Authorization': `Bearer ${appKey}`,
|
|
311
|
-
'Content-Type': 'application/json',
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
catch (error) {
|
|
316
|
-
throw new NetworkError(`Failed to verify SMS code: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
317
|
-
}
|
|
318
|
-
let json;
|
|
319
|
-
try {
|
|
320
|
-
const responseJson = await response.json();
|
|
321
|
-
json = responseJson;
|
|
322
|
-
}
|
|
323
|
-
catch (error) {
|
|
324
|
-
throw new ApiError(`Failed to parse SMS verification response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
|
|
325
|
-
}
|
|
326
|
-
// Handle verification errors
|
|
327
|
-
if (response.status === 400) {
|
|
328
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
329
|
-
String(json.message) : 'SMS verification failed';
|
|
330
|
-
throw new ApiError(errorMsg, response.status, '9739');
|
|
331
|
-
}
|
|
332
|
-
if (response.status === 403) {
|
|
333
|
-
throw new ApiError('Account is frozen or access forbidden', response.status, '9403');
|
|
334
|
-
}
|
|
335
|
-
if (!response.ok) {
|
|
336
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
337
|
-
String(json.message) : 'SMS verification failed';
|
|
338
|
-
throw new ApiError(errorMsg, response.status);
|
|
339
|
-
}
|
|
340
|
-
// Validate response structure
|
|
341
|
-
if (!json || !json.user || !json.token) {
|
|
342
|
-
throw new ApiError('Invalid SMS verification response: missing required data', response.status);
|
|
343
|
-
}
|
|
344
|
-
// Build standard response structure
|
|
345
|
-
const smsLoginResponse = {
|
|
346
|
-
app: json.app,
|
|
347
|
-
user: json.user,
|
|
348
|
-
token: json.token,
|
|
349
|
-
user_roles: json.user_roles || [],
|
|
350
|
-
user_privileges: json.user_privileges || [],
|
|
351
|
-
user_groups: json.user_groups || [],
|
|
352
|
-
user_login_methods: json.user_login_methods || []
|
|
353
|
-
};
|
|
354
|
-
return smsLoginResponse;
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Registers a new user with SMS verification and optional additional information.
|
|
358
|
-
*
|
|
359
|
-
* @param request - Registration request with SMS verification and optional user data
|
|
360
|
-
* @param options - Optional configuration for the request
|
|
361
|
-
* @returns Promise resolving to registration response with user info and token
|
|
362
|
-
* @throws {ConfigurationError} When required environment variables are missing
|
|
363
|
-
* @throws {ApiError} When registration fails or returns invalid response
|
|
364
|
-
* @throws {NetworkError} When network request fails
|
|
365
|
-
*
|
|
366
|
-
* @example
|
|
367
|
-
* ```typescript
|
|
368
|
-
* import { registerWithSms } from '@tale/client';
|
|
369
|
-
*
|
|
370
|
-
* try {
|
|
371
|
-
* const result = await registerWithSms({
|
|
372
|
-
* sms_id: 'uuid-here',
|
|
373
|
-
* sms_type: 'register',
|
|
374
|
-
* verification_code: '123456',
|
|
375
|
-
* username: 'newuser',
|
|
376
|
-
* password_encrypted: 'encrypted_password'
|
|
377
|
-
* });
|
|
378
|
-
* console.log('Registration successful:', result.user.user_id);
|
|
379
|
-
* console.log('User token:', result.token.token);
|
|
380
|
-
* } catch (error) {
|
|
381
|
-
* console.error('Registration failed:', error.message);
|
|
382
|
-
* }
|
|
383
|
-
* ```
|
|
384
|
-
*/
|
|
385
|
-
export async function registerWithSms(request, options) {
|
|
386
|
-
// Validate required fields for registration
|
|
387
|
-
if (request.sms_type !== 'register') {
|
|
388
|
-
throw new ApiError('sms_type must be "register" for registration', 400, '9400');
|
|
389
|
-
}
|
|
390
|
-
// Determine base URL
|
|
391
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
392
|
-
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
393
|
-
if (!base) {
|
|
394
|
-
throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
|
|
395
|
-
}
|
|
396
|
-
// Determine app key
|
|
397
|
-
const appKey = options?.appKey ?? env?.TALE_APP_KEY ?? undefined;
|
|
398
|
-
if (!appKey) {
|
|
399
|
-
throw new ConfigurationError('Missing required app_key or TALE_APP_KEY');
|
|
400
|
-
}
|
|
401
|
-
const url = String(base).replace(/\/+$/, '') + '/auth/v1/sms/register/verify';
|
|
402
|
-
// Build request body
|
|
403
|
-
const requestBody = {
|
|
404
|
-
sms_id: request.sms_id.trim(),
|
|
405
|
-
sms_type: request.sms_type,
|
|
406
|
-
verification_code: request.verification_code.trim(),
|
|
407
|
-
username: request.username?.trim(),
|
|
408
|
-
password_encrypted: request.password_encrypted
|
|
409
|
-
};
|
|
410
|
-
let response;
|
|
411
|
-
try {
|
|
412
|
-
response = await globalThis.fetch(url, {
|
|
413
|
-
method: 'POST',
|
|
414
|
-
headers: {
|
|
415
|
-
'Authorization': `Bearer ${appKey}`,
|
|
416
|
-
'Content-Type': 'application/json',
|
|
417
|
-
},
|
|
418
|
-
body: JSON.stringify(requestBody),
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
catch (error) {
|
|
422
|
-
throw new NetworkError(`Failed to register with SMS: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
423
|
-
}
|
|
424
|
-
let json;
|
|
425
|
-
try {
|
|
426
|
-
const responseJson = await response.json();
|
|
427
|
-
json = responseJson;
|
|
428
|
-
}
|
|
429
|
-
catch (error) {
|
|
430
|
-
throw new ApiError(`Failed to parse registration response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
|
|
431
|
-
}
|
|
432
|
-
// Handle registration errors
|
|
433
|
-
if (response.status === 400) {
|
|
434
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
435
|
-
String(json.message) : 'SMS registration failed';
|
|
436
|
-
throw new ApiError(errorMsg, response.status, '9739');
|
|
437
|
-
}
|
|
438
|
-
if (response.status === 403) {
|
|
439
|
-
throw new ApiError('Account is frozen or access forbidden', response.status, '9403');
|
|
440
|
-
}
|
|
441
|
-
if (!response.ok) {
|
|
442
|
-
const errorMsg = typeof json === 'object' && json !== null && 'message' in json ?
|
|
443
|
-
String(json.message) : 'SMS registration failed';
|
|
444
|
-
throw new ApiError(errorMsg, response.status);
|
|
445
|
-
}
|
|
446
|
-
// Validate response structure
|
|
447
|
-
if (!json || !json.user || !json.token) {
|
|
448
|
-
throw new ApiError('Invalid registration response: missing required data', response.status);
|
|
449
|
-
}
|
|
450
|
-
// Build standard response structure
|
|
451
|
-
const registrationResponse = {
|
|
452
|
-
app: json.app,
|
|
453
|
-
user: json.user,
|
|
454
|
-
token: json.token,
|
|
455
|
-
user_roles: json.user_roles || [],
|
|
456
|
-
user_privileges: json.user_privileges || [],
|
|
457
|
-
user_groups: json.user_groups || [],
|
|
458
|
-
user_login_methods: json.user_login_methods || []
|
|
459
|
-
};
|
|
460
|
-
return registrationResponse;
|
|
461
|
-
}
|
package/dist/client.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export type TaleClientOptions = {
|
|
2
|
-
baseUrl?: string;
|
|
3
|
-
appKey?: string;
|
|
4
|
-
appSecret?: string;
|
|
5
|
-
};
|
|
6
|
-
export declare class TaleClient {
|
|
7
|
-
baseUrl: string;
|
|
8
|
-
appKey: string;
|
|
9
|
-
appSecret: string;
|
|
10
|
-
constructor(opts?: TaleClientOptions);
|
|
11
|
-
request<T>(method: string, path: string, body?: unknown): Promise<T>;
|
|
12
|
-
get<T>(path: string): Promise<T>;
|
|
13
|
-
post<T>(path: string, data: unknown): Promise<T>;
|
|
14
|
-
put<T>(path: string, data: unknown): Promise<T>;
|
|
15
|
-
delete<T>(path: string): Promise<T>;
|
|
16
|
-
health(): Promise<{
|
|
17
|
-
status: string;
|
|
18
|
-
}>;
|
|
19
|
-
}
|
|
20
|
-
export declare function createClient(opts?: TaleClientOptions): TaleClient;
|
package/dist/client.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
function readEnv() {
|
|
2
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
3
|
-
return {
|
|
4
|
-
baseUrl: env?.TALE_BASE_URL ?? undefined,
|
|
5
|
-
appKey: env?.TALE_APP_KEY ?? undefined,
|
|
6
|
-
appSecret: env?.TALE_APP_SECRET ?? undefined,
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
export class TaleClient {
|
|
10
|
-
constructor(opts = {}) {
|
|
11
|
-
const env = readEnv();
|
|
12
|
-
const baseUrl = opts.baseUrl ?? env.baseUrl;
|
|
13
|
-
const appKey = opts.appKey ?? env.appKey;
|
|
14
|
-
const appSecret = opts.appSecret ?? env.appSecret;
|
|
15
|
-
if (!baseUrl || !appKey || !appSecret) {
|
|
16
|
-
throw new Error("Missing required environment variables: TALE_BASE_URL, TALE_APP_KEY, TALE_APP_SECRET");
|
|
17
|
-
}
|
|
18
|
-
this.baseUrl = baseUrl;
|
|
19
|
-
this.appKey = appKey;
|
|
20
|
-
this.appSecret = appSecret;
|
|
21
|
-
}
|
|
22
|
-
async request(method, path, body) {
|
|
23
|
-
const url = new URL(path, this.baseUrl).toString();
|
|
24
|
-
const headers = {
|
|
25
|
-
"X-Tale-App-Key": this.appKey,
|
|
26
|
-
"X-Tale-App-Secret": this.appSecret,
|
|
27
|
-
};
|
|
28
|
-
const init = { method, headers };
|
|
29
|
-
if (body !== undefined) {
|
|
30
|
-
headers["Content-Type"] = "application/json";
|
|
31
|
-
init.body = JSON.stringify(body);
|
|
32
|
-
}
|
|
33
|
-
const res = await fetch(url, init);
|
|
34
|
-
if (!res.ok) {
|
|
35
|
-
const text = await res.text();
|
|
36
|
-
throw new Error(`HTTP ${res.status} ${res.statusText}: ${text}`);
|
|
37
|
-
}
|
|
38
|
-
const ct = res.headers.get("content-type") || "";
|
|
39
|
-
if (ct.includes("application/json")) {
|
|
40
|
-
return (await res.json());
|
|
41
|
-
}
|
|
42
|
-
return (await res.text());
|
|
43
|
-
}
|
|
44
|
-
get(path) {
|
|
45
|
-
return this.request("GET", path);
|
|
46
|
-
}
|
|
47
|
-
post(path, data) {
|
|
48
|
-
return this.request("POST", path, data);
|
|
49
|
-
}
|
|
50
|
-
put(path, data) {
|
|
51
|
-
return this.request("PUT", path, data);
|
|
52
|
-
}
|
|
53
|
-
delete(path) {
|
|
54
|
-
return this.request("DELETE", path);
|
|
55
|
-
}
|
|
56
|
-
health() {
|
|
57
|
-
return this.get("/health");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
export function createClient(opts = {}) {
|
|
61
|
-
return new TaleClient(opts);
|
|
62
|
-
}
|
package/dist/info.d.ts
DELETED
package/dist/info.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import pkg from "../package.json" with { type: "json" };
|
|
2
|
-
export function getSdkStatus() {
|
|
3
|
-
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
4
|
-
const taleBaseUrl = env?.TALE_BASE_URL ?? undefined;
|
|
5
|
-
const taleAppKey = env?.TALE_APP_KEY ?? undefined;
|
|
6
|
-
const taleAppSecret = env?.TALE_APP_SECRET ?? undefined;
|
|
7
|
-
if (!taleBaseUrl || !taleAppKey || !taleAppSecret) {
|
|
8
|
-
throw new Error("Missing required environment variables: TALE_BASE_URL, TALE_APP_KEY, TALE_APP_SECRET");
|
|
9
|
-
}
|
|
10
|
-
return {
|
|
11
|
-
name: pkg.name,
|
|
12
|
-
version: pkg.version,
|
|
13
|
-
description: pkg.description,
|
|
14
|
-
taleBaseUrl,
|
|
15
|
-
taleAppKey,
|
|
16
|
-
taleAppSecret,
|
|
17
|
-
};
|
|
18
|
-
}
|
package/dist/package.json
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@tale/client",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"description": "Official TypeScript SDK for Tale backend services",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"tale",
|
|
7
|
-
"sdk",
|
|
8
|
-
"client",
|
|
9
|
-
"api",
|
|
10
|
-
"backend",
|
|
11
|
-
"typescript"
|
|
12
|
-
],
|
|
13
|
-
"homepage": "https://github.com/turinhub/tale-js-sdk#readme",
|
|
14
|
-
"bugs": {
|
|
15
|
-
"url": "https://github.com/turinhub/tale-js-sdk/issues"
|
|
16
|
-
},
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/turinhub/tale-js-sdk.git"
|
|
20
|
-
},
|
|
21
|
-
"license": "MIT",
|
|
22
|
-
"author": "Turinhub <dev@turinhub.com>",
|
|
23
|
-
"type": "module",
|
|
24
|
-
"main": "dist/index.js",
|
|
25
|
-
"scripts": {
|
|
26
|
-
"test": "jest",
|
|
27
|
-
"build": "tsc -p tsconfig.json",
|
|
28
|
-
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@types/jest": "^30.0.0",
|
|
32
|
-
"jest": "^30.2.0",
|
|
33
|
-
"typescript": "^5.9.3"
|
|
34
|
-
},
|
|
35
|
-
"types": "dist/index.d.ts"
|
|
36
|
-
}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './info.js';
|
package/dist/src/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './info.js';
|
package/dist/src/info.d.ts
DELETED
package/dist/src/info.js
DELETED