tale-js-sdk 0.1.2 → 0.1.3

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.
Files changed (48) hide show
  1. package/dist/acl/index.d.ts +170 -0
  2. package/dist/acl/index.js +747 -0
  3. package/dist/acl/types.d.ts +208 -0
  4. package/dist/acl/types.js +1 -0
  5. package/dist/auth/index.d.ts +2 -134
  6. package/dist/auth/index.js +120 -96
  7. package/dist/auth/types.d.ts +122 -0
  8. package/dist/auth/types.js +1 -0
  9. package/dist/common/types.d.ts +76 -0
  10. package/dist/common/types.js +3 -0
  11. package/dist/errors.js +18 -18
  12. package/dist/index.d.ts +2 -0
  13. package/dist/index.js +2 -0
  14. package/dist/rbac/acl.d.ts +152 -0
  15. package/dist/rbac/acl.js +723 -0
  16. package/dist/rbac/index.d.ts +2 -0
  17. package/dist/rbac/index.js +2 -0
  18. package/dist/rbac/rbac.d.ts +198 -0
  19. package/dist/rbac/rbac.js +984 -0
  20. package/dist/rbac/types.d.ts +356 -0
  21. package/dist/rbac/types.js +1 -0
  22. package/dist/rbac/user-group.d.ts +122 -0
  23. package/dist/rbac/user-group.js +570 -0
  24. package/dist/status.js +3 -3
  25. package/dist/token.d.ts +1 -1
  26. package/dist/token.js +5 -4
  27. package/dist/user/index.d.ts +2 -142
  28. package/dist/user/index.js +60 -59
  29. package/dist/user/types.d.ts +96 -0
  30. package/dist/user/types.js +1 -0
  31. package/dist/user-group/index.d.ts +230 -0
  32. package/dist/user-group/index.js +560 -0
  33. package/dist/user-group/types.d.ts +61 -0
  34. package/dist/user-group/types.js +1 -0
  35. package/package.json +13 -14
  36. package/dist/auth.d.ts +0 -271
  37. package/dist/auth.js +0 -461
  38. package/dist/client.d.ts +0 -20
  39. package/dist/client.js +0 -62
  40. package/dist/info.d.ts +0 -9
  41. package/dist/info.js +0 -18
  42. package/dist/package.json +0 -36
  43. package/dist/src/index.d.ts +0 -1
  44. package/dist/src/index.js +0 -1
  45. package/dist/src/info.d.ts +0 -6
  46. package/dist/src/info.js +0 -4
  47. package/dist/user.d.ts +0 -242
  48. package/dist/user.js +0 -331
@@ -0,0 +1,570 @@
1
+ import { getAppToken } from '../token.js';
2
+ import { ApiError, ConfigurationError, NetworkError } from '../errors.js';
3
+ /**
4
+ * Creates a new user group in the Tale application.
5
+ *
6
+ * @param groupData - User group data to create the new group
7
+ * @param options - Optional configuration for the request
8
+ * @returns Promise resolving to the created user group information
9
+ * @throws {ConfigurationError} When required environment variables are missing
10
+ * @throws {ApiError} When API request fails or returns invalid response
11
+ * @throws {NetworkError} When network request fails
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { createUserGroup } from '@tale/client';
16
+ *
17
+ * try {
18
+ * const result = await createUserGroup({
19
+ * name: 'Developers',
20
+ * description: 'Development team group',
21
+ * type: 'department',
22
+ * remark: 'All developers in the company'
23
+ * });
24
+ * console.log('User group created:', result.user_group.group_id);
25
+ * } catch (error) {
26
+ * console.error('Failed to create user group:', error.message);
27
+ * }
28
+ * ```
29
+ */
30
+ export async function createUserGroup(groupData, options) {
31
+ // Validate required fields
32
+ if (!groupData.name || groupData.name.trim() === '') {
33
+ throw new ApiError('name is required for user group creation', 400, '9400');
34
+ }
35
+ // Use provided app token or get one from token service
36
+ const token = options?.appToken ?? await getAppToken(options);
37
+ // Determine base URL
38
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
39
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
40
+ if (!base) {
41
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
42
+ }
43
+ const url = String(base).replace(/\/+$/, '') + '/user-group/v1';
44
+ let response;
45
+ try {
46
+ response = await globalThis.fetch(url, {
47
+ method: 'POST',
48
+ headers: {
49
+ 'Content-Type': 'application/json',
50
+ 'x-t-token': token,
51
+ },
52
+ body: JSON.stringify(groupData),
53
+ });
54
+ }
55
+ catch (error) {
56
+ throw new NetworkError(`Failed to create user group: ${error instanceof Error ? error.message : 'Unknown error'}`);
57
+ }
58
+ let json;
59
+ try {
60
+ const responseJson = await response.json();
61
+ json = responseJson;
62
+ }
63
+ catch (error) {
64
+ throw new ApiError(`Failed to parse user group creation response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
65
+ }
66
+ // Handle API errors
67
+ if (json.code !== 200) {
68
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User group creation failed';
69
+ throw new ApiError(errorMsg, response.status, json.code);
70
+ }
71
+ // Validate response structure
72
+ if (!json.data || !json.data.user_group) {
73
+ throw new ApiError('Invalid user group creation response: missing user group data', response.status);
74
+ }
75
+ return json.data;
76
+ }
77
+ /**
78
+ * Updates an existing user group by ID.
79
+ *
80
+ * @param groupId - User group ID to update
81
+ * @param updateData - User group information to update
82
+ * @param options - Optional configuration for the request
83
+ * @returns Promise resolving to the updated user group information
84
+ * @throws {ConfigurationError} When required environment variables are missing
85
+ * @throws {ApiError} When API request fails or returns invalid response
86
+ * @throws {NetworkError} When network request fails
87
+ */
88
+ export async function updateUserGroup(groupId, updateData, options) {
89
+ // Validate required fields
90
+ if (!groupId || groupId.trim() === '') {
91
+ throw new ApiError('group_id is required for user group update', 400, '9400');
92
+ }
93
+ // Use provided app token or get one from token service
94
+ const token = options?.appToken ?? await getAppToken(options);
95
+ // Determine base URL
96
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
97
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
98
+ if (!base) {
99
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
100
+ }
101
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}`;
102
+ let response;
103
+ try {
104
+ response = await globalThis.fetch(url, {
105
+ method: 'PUT',
106
+ headers: {
107
+ 'Content-Type': 'application/json',
108
+ 'x-t-token': token,
109
+ },
110
+ body: JSON.stringify(updateData),
111
+ });
112
+ }
113
+ catch (error) {
114
+ throw new NetworkError(`Failed to update user group: ${error instanceof Error ? error.message : 'Unknown error'}`);
115
+ }
116
+ let json;
117
+ try {
118
+ const responseJson = await response.json();
119
+ json = responseJson;
120
+ }
121
+ catch (error) {
122
+ throw new ApiError(`Failed to parse user group update response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
123
+ }
124
+ // Handle API errors
125
+ if (json.code !== 200) {
126
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User group update failed';
127
+ throw new ApiError(errorMsg, response.status, json.code);
128
+ }
129
+ // Validate response structure
130
+ if (!json.data || !json.data.user_group) {
131
+ throw new ApiError('Invalid user group update response: missing user group data', response.status);
132
+ }
133
+ return json.data;
134
+ }
135
+ /**
136
+ * Deletes a user group by ID.
137
+ *
138
+ * @param groupId - User group ID to delete
139
+ * @param options - Optional configuration for the request
140
+ * @returns Promise resolving to the deletion result
141
+ * @throws {ConfigurationError} When required environment variables are missing
142
+ * @throws {ApiError} When API request fails or returns invalid response
143
+ * @throws {NetworkError} When network request fails
144
+ */
145
+ export async function deleteUserGroup(groupId, options) {
146
+ // Validate required fields
147
+ if (!groupId || groupId.trim() === '') {
148
+ throw new ApiError('group_id is required for user group deletion', 400, '9400');
149
+ }
150
+ // Use provided app token or get one from token service
151
+ const token = options?.appToken ?? await getAppToken(options);
152
+ // Determine base URL
153
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
154
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
155
+ if (!base) {
156
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
157
+ }
158
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}`;
159
+ let response;
160
+ try {
161
+ response = await globalThis.fetch(url, {
162
+ method: 'DELETE',
163
+ headers: {
164
+ 'Content-Type': 'application/json',
165
+ 'x-t-token': token,
166
+ },
167
+ });
168
+ }
169
+ catch (error) {
170
+ throw new NetworkError(`Failed to delete user group: ${error instanceof Error ? error.message : 'Unknown error'}`);
171
+ }
172
+ let json;
173
+ try {
174
+ const responseJson = await response.json();
175
+ json = responseJson;
176
+ }
177
+ catch (error) {
178
+ throw new ApiError(`Failed to parse user group deletion response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
179
+ }
180
+ // Handle API errors
181
+ if (json.code !== 200) {
182
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User group deletion failed';
183
+ throw new ApiError(errorMsg, response.status, json.code);
184
+ }
185
+ // Validate response structure
186
+ if (!json.data || json.data.deleted !== true) {
187
+ throw new ApiError('Invalid user group deletion response: deletion not confirmed', response.status);
188
+ }
189
+ return json.data;
190
+ }
191
+ /**
192
+ * Retrieves user group information by ID.
193
+ *
194
+ * @param groupId - User group ID to query
195
+ * @param options - Optional configuration for the request
196
+ * @returns Promise resolving to the user group information
197
+ * @throws {ConfigurationError} When required environment variables are missing
198
+ * @throws {ApiError} When API request fails or returns invalid response
199
+ * @throws {NetworkError} When network request fails
200
+ */
201
+ export async function getUserGroupById(groupId, options) {
202
+ // Validate required fields
203
+ if (!groupId || groupId.trim() === '') {
204
+ throw new ApiError('group_id is required for user group query', 400, '9400');
205
+ }
206
+ // Use provided app token or get one from token service
207
+ const token = options?.appToken ?? await getAppToken(options);
208
+ // Determine base URL
209
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
210
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
211
+ if (!base) {
212
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
213
+ }
214
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}`;
215
+ let response;
216
+ try {
217
+ response = await globalThis.fetch(url, {
218
+ method: 'GET',
219
+ headers: {
220
+ 'Content-Type': 'application/json',
221
+ 'x-t-token': token,
222
+ },
223
+ });
224
+ }
225
+ catch (error) {
226
+ throw new NetworkError(`Failed to get user group: ${error instanceof Error ? error.message : 'Unknown error'}`);
227
+ }
228
+ let json;
229
+ try {
230
+ const responseJson = await response.json();
231
+ json = responseJson;
232
+ }
233
+ catch (error) {
234
+ throw new ApiError(`Failed to parse user group response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
235
+ }
236
+ // Handle API errors
237
+ if (json.code !== 200) {
238
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User group retrieval failed';
239
+ throw new ApiError(errorMsg, response.status, json.code);
240
+ }
241
+ // Validate response structure
242
+ if (!json.data || !json.data.user_group) {
243
+ throw new ApiError('Invalid user group response: missing user group data', response.status);
244
+ }
245
+ return json.data;
246
+ }
247
+ /**
248
+ * Lists user groups with pagination and filtering.
249
+ *
250
+ * @param options - Optional parameters for pagination, filtering, and configuration
251
+ * @returns Promise resolving to paginated user group list with metadata
252
+ * @throws {ConfigurationError} When required environment variables are missing
253
+ * @throws {ApiError} When API request fails or returns invalid response
254
+ * @throws {NetworkError} When network request fails
255
+ */
256
+ export async function listUserGroups(options) {
257
+ // Use provided app token or get one from token service
258
+ const token = options?.appToken ?? await getAppToken(options);
259
+ // Determine base URL
260
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
261
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
262
+ if (!base) {
263
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
264
+ }
265
+ // Build URL with query parameters
266
+ const url = new URL(String(base).replace(/\/+$/, '') + '/user-group/v1');
267
+ // Add query parameters with defaults
268
+ const queryParams = {
269
+ page: 0,
270
+ size: 20,
271
+ sort_by: 'created_at',
272
+ sort_direction: 'desc',
273
+ ...options
274
+ };
275
+ // Add parameters to URL
276
+ if (queryParams.page !== undefined) {
277
+ url.searchParams.append('page', String(queryParams.page));
278
+ }
279
+ if (queryParams.size !== undefined) {
280
+ url.searchParams.append('size', String(queryParams.size));
281
+ }
282
+ if (queryParams.keyword) {
283
+ url.searchParams.append('keyword', queryParams.keyword);
284
+ }
285
+ if (queryParams.sort_by) {
286
+ url.searchParams.append('sort_by', queryParams.sort_by);
287
+ }
288
+ if (queryParams.sort_direction) {
289
+ url.searchParams.append('sort_direction', queryParams.sort_direction);
290
+ }
291
+ let response;
292
+ try {
293
+ response = await globalThis.fetch(url.toString(), {
294
+ method: 'GET',
295
+ headers: {
296
+ 'Content-Type': 'application/json',
297
+ 'x-t-token': token,
298
+ },
299
+ });
300
+ }
301
+ catch (error) {
302
+ throw new NetworkError(`Failed to list user groups: ${error instanceof Error ? error.message : 'Unknown error'}`);
303
+ }
304
+ let json;
305
+ try {
306
+ const responseJson = await response.json();
307
+ json = responseJson;
308
+ }
309
+ catch (error) {
310
+ throw new ApiError(`Failed to parse user groups list response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
311
+ }
312
+ // Handle API errors
313
+ if (json.code !== 200) {
314
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User groups list retrieval failed';
315
+ throw new ApiError(errorMsg, response.status, json.code);
316
+ }
317
+ // Validate response structure
318
+ if (!json.data || !Array.isArray(json.data.content)) {
319
+ throw new ApiError('Invalid user groups list response: missing required data', response.status);
320
+ }
321
+ return json.data;
322
+ }
323
+ /**
324
+ * Gets the members of a user group.
325
+ *
326
+ * @param groupId - User group ID to query
327
+ * @param options - Optional parameters for pagination and configuration
328
+ * @returns Promise resolving to the user group members
329
+ * @throws {ConfigurationError} When required environment variables are missing
330
+ * @throws {ApiError} When API request fails or returns invalid response
331
+ * @throws {NetworkError} When network request fails
332
+ */
333
+ export async function getUserGroupMembers(groupId, options) {
334
+ // Validate required fields
335
+ if (!groupId || groupId.trim() === '') {
336
+ throw new ApiError('group_id is required for user group members query', 400, '9400');
337
+ }
338
+ // Use provided app token or get one from token service
339
+ const token = options?.appToken ?? await getAppToken(options);
340
+ // Determine base URL
341
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
342
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
343
+ if (!base) {
344
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
345
+ }
346
+ // Build URL with query parameters
347
+ const url = new URL(String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}/members`);
348
+ // Add query parameters with defaults
349
+ const queryParams = {
350
+ page: 0,
351
+ size: 20,
352
+ ...options
353
+ };
354
+ // Add parameters to URL
355
+ if (queryParams.page !== undefined) {
356
+ url.searchParams.append('page', String(queryParams.page));
357
+ }
358
+ if (queryParams.size !== undefined) {
359
+ url.searchParams.append('size', String(queryParams.size));
360
+ }
361
+ let response;
362
+ try {
363
+ response = await globalThis.fetch(url.toString(), {
364
+ method: 'GET',
365
+ headers: {
366
+ 'Content-Type': 'application/json',
367
+ 'x-t-token': token,
368
+ },
369
+ });
370
+ }
371
+ catch (error) {
372
+ throw new NetworkError(`Failed to get user group members: ${error instanceof Error ? error.message : 'Unknown error'}`);
373
+ }
374
+ let json;
375
+ try {
376
+ const responseJson = await response.json();
377
+ json = responseJson;
378
+ }
379
+ catch (error) {
380
+ throw new ApiError(`Failed to parse user group members response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
381
+ }
382
+ // Handle API errors
383
+ if (json.code !== 200) {
384
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User group members retrieval failed';
385
+ throw new ApiError(errorMsg, response.status, json.code);
386
+ }
387
+ // Validate response structure
388
+ if (!json.data || !json.data.members) {
389
+ throw new ApiError('Invalid user group members response: missing members data', response.status);
390
+ }
391
+ return json.data;
392
+ }
393
+ /**
394
+ * Adds members to a user group.
395
+ *
396
+ * @param groupId - User group ID to add members to
397
+ * @param memberData - Member data with user IDs to add
398
+ * @param options - Optional configuration for the request
399
+ * @returns Promise resolving to the added members result
400
+ * @throws {ConfigurationError} When required environment variables are missing
401
+ * @throws {ApiError} When API request fails or returns invalid response
402
+ * @throws {NetworkError} When network request fails
403
+ */
404
+ export async function addUserGroupMembers(groupId, memberData, options) {
405
+ // Validate required fields
406
+ if (!groupId || groupId.trim() === '') {
407
+ throw new ApiError('group_id is required for adding user group members', 400, '9400');
408
+ }
409
+ if (!memberData.user_ids || memberData.user_ids.length === 0) {
410
+ throw new ApiError('user_ids is required for adding user group members', 400, '9400');
411
+ }
412
+ // Use provided app token or get one from token service
413
+ const token = options?.appToken ?? await getAppToken(options);
414
+ // Determine base URL
415
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
416
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
417
+ if (!base) {
418
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
419
+ }
420
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}/members`;
421
+ let response;
422
+ try {
423
+ response = await globalThis.fetch(url, {
424
+ method: 'POST',
425
+ headers: {
426
+ 'Content-Type': 'application/json',
427
+ 'x-t-token': token,
428
+ },
429
+ body: JSON.stringify(memberData),
430
+ });
431
+ }
432
+ catch (error) {
433
+ throw new NetworkError(`Failed to add user group members: ${error instanceof Error ? error.message : 'Unknown error'}`);
434
+ }
435
+ let json;
436
+ try {
437
+ const responseJson = await response.json();
438
+ json = responseJson;
439
+ }
440
+ catch (error) {
441
+ throw new ApiError(`Failed to parse add user group members response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
442
+ }
443
+ // Handle API errors
444
+ if (json.code !== 200) {
445
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'Add user group members failed';
446
+ throw new ApiError(errorMsg, response.status, json.code);
447
+ }
448
+ // Validate response structure
449
+ if (!json.data || !json.data.members) {
450
+ throw new ApiError('Invalid add user group members response: missing members data', response.status);
451
+ }
452
+ return json.data;
453
+ }
454
+ /**
455
+ * Removes members from a user group.
456
+ *
457
+ * @param groupId - User group ID to remove members from
458
+ * @param memberData - Member data with user IDs to remove
459
+ * @param options - Optional configuration for the request
460
+ * @returns Promise resolving to the removal result
461
+ * @throws {ConfigurationError} When required environment variables are missing
462
+ * @throws {ApiError} When API request fails or returns invalid response
463
+ * @throws {NetworkError} When network request fails
464
+ */
465
+ export async function removeUserGroupMembers(groupId, memberData, options) {
466
+ // Validate required fields
467
+ if (!groupId || groupId.trim() === '') {
468
+ throw new ApiError('group_id is required for removing user group members', 400, '9400');
469
+ }
470
+ if (!memberData.user_ids || memberData.user_ids.length === 0) {
471
+ throw new ApiError('user_ids is required for removing user group members', 400, '9400');
472
+ }
473
+ // Use provided app token or get one from token service
474
+ const token = options?.appToken ?? await getAppToken(options);
475
+ // Determine base URL
476
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
477
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
478
+ if (!base) {
479
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
480
+ }
481
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/${encodeURIComponent(groupId)}/members`;
482
+ let response;
483
+ try {
484
+ response = await globalThis.fetch(url, {
485
+ method: 'DELETE',
486
+ headers: {
487
+ 'Content-Type': 'application/json',
488
+ 'x-t-token': token,
489
+ },
490
+ body: JSON.stringify(memberData),
491
+ });
492
+ }
493
+ catch (error) {
494
+ throw new NetworkError(`Failed to remove user group members: ${error instanceof Error ? error.message : 'Unknown error'}`);
495
+ }
496
+ let json;
497
+ try {
498
+ const responseJson = await response.json();
499
+ json = responseJson;
500
+ }
501
+ catch (error) {
502
+ throw new ApiError(`Failed to parse remove user group members response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
503
+ }
504
+ // Handle API errors
505
+ if (json.code !== 200) {
506
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'Remove user group members failed';
507
+ throw new ApiError(errorMsg, response.status, json.code);
508
+ }
509
+ // Validate response structure
510
+ if (!json.data || json.data.removed !== true) {
511
+ throw new ApiError('Invalid remove user group members response: removal not confirmed', response.status);
512
+ }
513
+ return json.data;
514
+ }
515
+ /**
516
+ * Gets the user groups that a user belongs to.
517
+ *
518
+ * @param userId - User ID to query
519
+ * @param options - Optional configuration for the request
520
+ * @returns Promise resolving to the user's groups
521
+ * @throws {ConfigurationError} When required environment variables are missing
522
+ * @throws {ApiError} When API request fails or returns invalid response
523
+ * @throws {NetworkError} When network request fails
524
+ */
525
+ export async function getUserGroups(userId, options) {
526
+ // Validate required fields
527
+ if (!userId || userId.trim() === '') {
528
+ throw new ApiError('user_id is required for user groups query', 400, '9400');
529
+ }
530
+ // Use provided app token or get one from token service
531
+ const token = options?.appToken ?? await getAppToken(options);
532
+ // Determine base URL
533
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
534
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
535
+ if (!base) {
536
+ throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
537
+ }
538
+ const url = String(base).replace(/\/+$/, '') + `/user-group/v1/by-user/${encodeURIComponent(userId)}`;
539
+ let response;
540
+ try {
541
+ response = await globalThis.fetch(url, {
542
+ method: 'GET',
543
+ headers: {
544
+ 'Content-Type': 'application/json',
545
+ 'x-t-token': token,
546
+ },
547
+ });
548
+ }
549
+ catch (error) {
550
+ throw new NetworkError(`Failed to get user groups: ${error instanceof Error ? error.message : 'Unknown error'}`);
551
+ }
552
+ let json;
553
+ try {
554
+ const responseJson = await response.json();
555
+ json = responseJson;
556
+ }
557
+ catch (error) {
558
+ throw new ApiError(`Failed to parse user groups response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
559
+ }
560
+ // Handle API errors
561
+ if (json.code !== 200) {
562
+ const errorMsg = typeof json.msg === 'string' ? json.msg : 'User groups retrieval failed';
563
+ throw new ApiError(errorMsg, response.status, json.code);
564
+ }
565
+ // Validate response structure
566
+ if (!json.data || !json.data.user_groups) {
567
+ throw new ApiError('Invalid user groups response: missing user groups data', response.status);
568
+ }
569
+ return json.data;
570
+ }
package/dist/status.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import pkg from "../package.json" with { type: "json" };
2
- import { getAppToken, ConfigurationError, ApiError, NetworkError } from "./token.js";
2
+ import { getAppToken, ConfigurationError, ApiError, NetworkError, } from "./token.js";
3
3
  export async function checkTaleStatus(baseUrl) {
4
4
  const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
5
5
  const base = baseUrl ?? env?.TALE_BASE_URL ?? undefined;
@@ -12,7 +12,7 @@ export async function checkTaleStatus(baseUrl) {
12
12
  response = await globalThis.fetch(url, { method: "GET" });
13
13
  }
14
14
  catch (error) {
15
- throw new NetworkError(`Failed to fetch Tale status: ${error instanceof Error ? error.message : 'Unknown error'}`);
15
+ throw new NetworkError(`Failed to fetch Tale status: ${error instanceof Error ? error.message : "Unknown error"}`);
16
16
  }
17
17
  let statusJson;
18
18
  try {
@@ -20,7 +20,7 @@ export async function checkTaleStatus(baseUrl) {
20
20
  statusJson = json;
21
21
  }
22
22
  catch (error) {
23
- throw new ApiError(`Failed to parse status response: ${error instanceof Error ? error.message : 'Invalid JSON'}`);
23
+ throw new ApiError(`Failed to parse status response: ${error instanceof Error ? error.message : "Invalid JSON"}`);
24
24
  }
25
25
  let appJson;
26
26
  try {
package/dist/token.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown } from './errors.js';
1
+ import { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown } from "./errors.js";
2
2
  export { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown, };
3
3
  export type AppTokenData = {
4
4
  type: string;
package/dist/token.js CHANGED
@@ -1,11 +1,11 @@
1
- import { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown, } from './errors.js';
1
+ import { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown, } from "./errors.js";
2
2
  export { TaleSdkError, ConfigurationError, ApiError, EnvironmentError, TokenError, NetworkError, ValidationError, isTaleSdkError, createErrorFromUnknown, };
3
3
  let cached;
4
4
  let expiryTimer;
5
5
  let inFlight;
6
6
  let inFlightKey;
7
7
  function getEnv() {
8
- return globalThis?.process?.env ?? import.meta?.env ?? undefined;
8
+ return (globalThis?.process?.env ?? import.meta?.env ?? undefined);
9
9
  }
10
10
  function clearCache() {
11
11
  cached = undefined;
@@ -63,7 +63,7 @@ async function fetchAppToken(options) {
63
63
  });
64
64
  }
65
65
  catch (error) {
66
- throw new ApiError(`Failed to fetch token: ${error instanceof Error ? error.message : 'Unknown error'}`);
66
+ throw new ApiError(`Failed to fetch token: ${error instanceof Error ? error.message : "Unknown error"}`);
67
67
  }
68
68
  const json = (await response.json());
69
69
  if (json?.code !== 200 || !json?.data?.token) {
@@ -81,7 +81,8 @@ function isTokenValid(info) {
81
81
  function isServerEnv() {
82
82
  const hasWindow = typeof globalThis.window !== "undefined" &&
83
83
  typeof globalThis.window.document !== "undefined";
84
- const hasNode = typeof globalThis.process !== "undefined" && !!globalThis.process.versions?.node;
84
+ const hasNode = typeof globalThis.process !== "undefined" &&
85
+ !!globalThis.process.versions?.node;
85
86
  return !hasWindow && hasNode;
86
87
  }
87
88
  export async function getAppToken(options) {