@turinhub/tale-js-sdk 1.3.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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -0
  3. package/dist/acl/index.d.ts +295 -0
  4. package/dist/acl/index.js +653 -0
  5. package/dist/acl/types.d.ts +142 -0
  6. package/dist/acl/types.js +1 -0
  7. package/dist/auth/index.d.ts +166 -0
  8. package/dist/auth/index.js +522 -0
  9. package/dist/auth/types.d.ts +127 -0
  10. package/dist/auth/types.js +1 -0
  11. package/dist/cms/file.d.ts +398 -0
  12. package/dist/cms/file.js +800 -0
  13. package/dist/cms/folder.d.ts +128 -0
  14. package/dist/cms/folder.js +294 -0
  15. package/dist/cms/index.d.ts +3 -0
  16. package/dist/cms/index.js +6 -0
  17. package/dist/cms/types.d.ts +157 -0
  18. package/dist/cms/types.js +1 -0
  19. package/dist/common/types.d.ts +91 -0
  20. package/dist/common/types.js +2 -0
  21. package/dist/errors.d.ts +52 -0
  22. package/dist/errors.js +109 -0
  23. package/dist/index.d.ts +13 -0
  24. package/dist/index.js +12 -0
  25. package/dist/rbac/acl.d.ts +152 -0
  26. package/dist/rbac/acl.js +723 -0
  27. package/dist/rbac/index.d.ts +464 -0
  28. package/dist/rbac/index.js +1121 -0
  29. package/dist/rbac/rbac.d.ts +198 -0
  30. package/dist/rbac/rbac.js +984 -0
  31. package/dist/rbac/types.d.ts +125 -0
  32. package/dist/rbac/types.js +1 -0
  33. package/dist/rbac/user-group.d.ts +122 -0
  34. package/dist/rbac/user-group.js +570 -0
  35. package/dist/status.d.ts +40 -0
  36. package/dist/status.js +56 -0
  37. package/dist/task/index.d.ts +163 -0
  38. package/dist/task/index.js +495 -0
  39. package/dist/task/types.d.ts +116 -0
  40. package/dist/task/types.js +1 -0
  41. package/dist/task-type/index.d.ts +92 -0
  42. package/dist/task-type/index.js +256 -0
  43. package/dist/task-type/types.d.ts +54 -0
  44. package/dist/task-type/types.js +1 -0
  45. package/dist/token.d.ts +21 -0
  46. package/dist/token.js +114 -0
  47. package/dist/user/index.d.ts +267 -0
  48. package/dist/user/index.js +786 -0
  49. package/dist/user/types.d.ts +145 -0
  50. package/dist/user/types.js +1 -0
  51. package/dist/user-attribute/index.d.ts +186 -0
  52. package/dist/user-attribute/index.js +615 -0
  53. package/dist/user-attribute/types.d.ts +109 -0
  54. package/dist/user-attribute/types.js +1 -0
  55. package/dist/user-group/index.d.ts +231 -0
  56. package/dist/user-group/index.js +566 -0
  57. package/dist/user-group/types.d.ts +50 -0
  58. package/dist/user-group/types.js +1 -0
  59. package/package.json +50 -0
@@ -0,0 +1,615 @@
1
+ import { getAppToken } from "../token.js";
2
+ import { ApiError, ConfigurationError, NetworkError } from "../errors.js";
3
+ // ==================== Helper Functions ====================
4
+ /**
5
+ * Parses standard API response format: { code, msg, data }
6
+ */
7
+ function parseApiResponse(json, errorMessage, statusCode) {
8
+ if (typeof json !== "object" || json === null) {
9
+ throw new ApiError(`Invalid response: ${errorMessage} - not an object`, statusCode);
10
+ }
11
+ const response = json;
12
+ if (response.code !== 200) {
13
+ const errorMsg = typeof response.msg === "string" ? response.msg : errorMessage;
14
+ throw new ApiError(errorMsg, statusCode, response.code);
15
+ }
16
+ if (!response.data) {
17
+ throw new ApiError(`Invalid response: ${errorMessage} - missing data`, statusCode);
18
+ }
19
+ return response.data;
20
+ }
21
+ // ==================== Attribute Definition Management ====================
22
+ /**
23
+ * Creates a new user attribute definition.
24
+ *
25
+ * @param request - Attribute definition creation request
26
+ * @param options - Optional configuration
27
+ * @returns Promise resolving to created attribute definition
28
+ * @throws {ConfigurationError} When required environment variables are missing
29
+ * @throws {ApiError} When API request fails
30
+ * @throws {NetworkError} When network request fails
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const definition = await createUserAttributeDefinition({
35
+ * attribute_name: "department",
36
+ * description: "User's department",
37
+ * schema_definition: { type: "string" },
38
+ * is_enabled: true,
39
+ * });
40
+ * ```
41
+ */
42
+ export async function createUserAttributeDefinition(request, options) {
43
+ const token = options?.appToken ?? (await getAppToken(options));
44
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
45
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
46
+ if (!base) {
47
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
48
+ }
49
+ const url = new URL(String(base).replace(/\/+$/, "") + "/user-attribute/v1/definition");
50
+ let response;
51
+ try {
52
+ response = await globalThis.fetch(url.toString(), {
53
+ method: "POST",
54
+ headers: {
55
+ "Content-Type": "application/json",
56
+ "x-t-token": token,
57
+ },
58
+ body: JSON.stringify(request),
59
+ });
60
+ }
61
+ catch (error) {
62
+ throw new NetworkError(`Failed to create user attribute definition: ${error instanceof Error ? error.message : "Unknown error"}`);
63
+ }
64
+ const json = await response.json();
65
+ return parseApiResponse(json, "Failed to create user attribute definition", response.status);
66
+ }
67
+ /**
68
+ * Updates an existing user attribute definition.
69
+ *
70
+ * @param openId - Attribute definition open ID
71
+ * @param request - Attribute definition update request
72
+ * @param options - Optional configuration
73
+ * @returns Promise resolving to updated attribute definition
74
+ * @throws {ConfigurationError} When required environment variables are missing
75
+ * @throws {ApiError} When API request fails
76
+ * @throws {NetworkError} When network request fails
77
+ */
78
+ export async function updateUserAttributeDefinition(openId, request, options) {
79
+ const token = options?.appToken ?? (await getAppToken(options));
80
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
81
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
82
+ if (!base) {
83
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
84
+ }
85
+ const url = new URL(String(base).replace(/\/+$/, "") +
86
+ `/user-attribute/v1/definition/${encodeURIComponent(openId)}`);
87
+ let response;
88
+ try {
89
+ response = await globalThis.fetch(url.toString(), {
90
+ method: "PUT",
91
+ headers: {
92
+ "Content-Type": "application/json",
93
+ "x-t-token": token,
94
+ },
95
+ body: JSON.stringify(request),
96
+ });
97
+ }
98
+ catch (error) {
99
+ throw new NetworkError(`Failed to update user attribute definition: ${error instanceof Error ? error.message : "Unknown error"}`);
100
+ }
101
+ const json = await response.json();
102
+ return parseApiResponse(json, "Failed to update user attribute definition", response.status);
103
+ }
104
+ /**
105
+ * Deletes a user attribute definition.
106
+ *
107
+ * @param openId - Attribute definition open ID
108
+ * @param options - Optional configuration
109
+ * @throws {ConfigurationError} When required environment variables are missing
110
+ * @throws {ApiError} When API request fails
111
+ * @throws {NetworkError} When network request fails
112
+ */
113
+ export async function deleteUserAttributeDefinition(openId, options) {
114
+ const token = options?.appToken ?? (await getAppToken(options));
115
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
116
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
117
+ if (!base) {
118
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
119
+ }
120
+ const url = new URL(String(base).replace(/\/+$/, "") +
121
+ `/user-attribute/v1/definition/${encodeURIComponent(openId)}`);
122
+ let response;
123
+ try {
124
+ response = await globalThis.fetch(url.toString(), {
125
+ method: "DELETE",
126
+ headers: {
127
+ "Content-Type": "application/json",
128
+ "x-t-token": token,
129
+ },
130
+ });
131
+ }
132
+ catch (error) {
133
+ throw new NetworkError(`Failed to delete user attribute definition: ${error instanceof Error ? error.message : "Unknown error"}`);
134
+ }
135
+ const json = await response.json();
136
+ if (typeof json !== "object" || json === null) {
137
+ throw new ApiError("Invalid user attribute definition deletion response", response.status);
138
+ }
139
+ const apiResponse = json;
140
+ if (apiResponse.code !== 200) {
141
+ const errorMsg = typeof apiResponse.msg === "string"
142
+ ? apiResponse.msg
143
+ : "Failed to delete user attribute definition";
144
+ throw new ApiError(errorMsg, response.status, apiResponse.code);
145
+ }
146
+ }
147
+ /**
148
+ * Gets a user attribute definition by open ID.
149
+ *
150
+ * @param openId - Attribute definition open ID
151
+ * @param options - Optional configuration
152
+ * @returns Promise resolving to attribute definition
153
+ * @throws {ConfigurationError} When required environment variables are missing
154
+ * @throws {ApiError} When API request fails
155
+ * @throws {NetworkError} When network request fails
156
+ */
157
+ export async function getUserAttributeDefinition(openId, options) {
158
+ const token = options?.appToken ?? (await getAppToken(options));
159
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
160
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
161
+ if (!base) {
162
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
163
+ }
164
+ const url = new URL(String(base).replace(/\/+$/, "") +
165
+ `/user-attribute/v1/definition/${encodeURIComponent(openId)}`);
166
+ let response;
167
+ try {
168
+ response = await globalThis.fetch(url.toString(), {
169
+ method: "GET",
170
+ headers: {
171
+ "Content-Type": "application/json",
172
+ "x-t-token": token,
173
+ },
174
+ });
175
+ }
176
+ catch (error) {
177
+ throw new NetworkError(`Failed to get user attribute definition: ${error instanceof Error ? error.message : "Unknown error"}`);
178
+ }
179
+ const json = await response.json();
180
+ return parseApiResponse(json, "Failed to get user attribute definition", response.status);
181
+ }
182
+ /**
183
+ * Lists user attribute definitions with pagination and optional filtering.
184
+ *
185
+ * @param request - List request parameters
186
+ * @param options - Optional configuration
187
+ * @returns Promise resolving to paginated list of attribute definitions
188
+ * @throws {ConfigurationError} When required environment variables are missing
189
+ * @throws {ApiError} When API request fails
190
+ * @throws {NetworkError} When network request fails
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * const result = await listUserAttributeDefinitions({
195
+ * page: 0,
196
+ * size: 20,
197
+ * keyword: "department",
198
+ * });
199
+ * console.log(result.content); // Array of definitions
200
+ * console.log(result.total); // Total count
201
+ * ```
202
+ */
203
+ export async function listUserAttributeDefinitions(request) {
204
+ const token = request?.appToken ?? (await getAppToken(request));
205
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
206
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
207
+ if (!base) {
208
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
209
+ }
210
+ const url = new URL(String(base).replace(/\/+$/, "") + "/user-attribute/v1/definition");
211
+ const { appToken, baseUrl, ...requestParams } = request || {};
212
+ const queryParams = {
213
+ page: 0,
214
+ size: 20,
215
+ sort: "createdAt,desc",
216
+ ...requestParams,
217
+ };
218
+ Object.entries(queryParams).forEach(([key, value]) => {
219
+ if (value !== undefined) {
220
+ url.searchParams.append(key, String(value));
221
+ }
222
+ });
223
+ let response;
224
+ try {
225
+ response = await globalThis.fetch(url.toString(), {
226
+ method: "GET",
227
+ headers: {
228
+ "Content-Type": "application/json",
229
+ "x-t-token": token,
230
+ },
231
+ });
232
+ }
233
+ catch (error) {
234
+ throw new NetworkError(`Failed to list user attribute definitions: ${error instanceof Error ? error.message : "Unknown error"}`);
235
+ }
236
+ const json = await response.json();
237
+ return parseApiResponse(json, "Failed to list user attribute definitions", response.status);
238
+ }
239
+ /**
240
+ * Lists all enabled user attribute definitions.
241
+ *
242
+ * @param options - Optional configuration
243
+ * @returns Promise resolving to array of enabled attribute definitions
244
+ * @throws {ConfigurationError} When required environment variables are missing
245
+ * @throws {ApiError} When API request fails
246
+ * @throws {NetworkError} When network request fails
247
+ */
248
+ export async function listEnabledUserAttributeDefinitions(options) {
249
+ const token = options?.appToken ?? (await getAppToken(options));
250
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
251
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
252
+ if (!base) {
253
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
254
+ }
255
+ const url = new URL(String(base).replace(/\/+$/, "") + "/user-attribute/v1/definition/enabled");
256
+ let response;
257
+ try {
258
+ response = await globalThis.fetch(url.toString(), {
259
+ method: "GET",
260
+ headers: {
261
+ "Content-Type": "application/json",
262
+ "x-t-token": token,
263
+ },
264
+ });
265
+ }
266
+ catch (error) {
267
+ throw new NetworkError(`Failed to list enabled user attribute definitions: ${error instanceof Error ? error.message : "Unknown error"}`);
268
+ }
269
+ const json = await response.json();
270
+ const result = parseApiResponse(json, "Failed to list enabled user attribute definitions", response.status);
271
+ if (!Array.isArray(result)) {
272
+ throw new ApiError("Invalid enabled user attribute definitions response: not an array", response.status);
273
+ }
274
+ return result;
275
+ }
276
+ /**
277
+ * Toggles the enabled status of a user attribute definition.
278
+ *
279
+ * @param openId - Attribute definition open ID
280
+ * @param isEnabled - Whether the definition should be enabled
281
+ * @param options - Optional configuration
282
+ * @returns Promise resolving to updated attribute definition
283
+ * @throws {ConfigurationError} When required environment variables are missing
284
+ * @throws {ApiError} When API request fails
285
+ * @throws {NetworkError} When network request fails
286
+ */
287
+ export async function toggleUserAttributeDefinitionStatus(openId, isEnabled, options) {
288
+ const token = options?.appToken ?? (await getAppToken(options));
289
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
290
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
291
+ if (!base) {
292
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
293
+ }
294
+ const url = new URL(String(base).replace(/\/+$/, "") +
295
+ `/user-attribute/v1/definition/${encodeURIComponent(openId)}/status`);
296
+ url.searchParams.append("enabled", String(isEnabled));
297
+ let response;
298
+ try {
299
+ response = await globalThis.fetch(url.toString(), {
300
+ method: "PUT",
301
+ headers: {
302
+ "Content-Type": "application/json",
303
+ "x-t-token": token,
304
+ },
305
+ });
306
+ }
307
+ catch (error) {
308
+ throw new NetworkError(`Failed to toggle user attribute definition status: ${error instanceof Error ? error.message : "Unknown error"}`);
309
+ }
310
+ const json = await response.json();
311
+ return parseApiResponse(json, "Failed to toggle user attribute definition status", response.status);
312
+ }
313
+ // ==================== User Attribute Value Management ====================
314
+ /**
315
+ * Sets a user attribute value.
316
+ *
317
+ * @param userId - User ID
318
+ * @param attributeDefinitionId - Attribute definition ID
319
+ * @param request - Attribute value to set
320
+ * @param options - Optional configuration
321
+ * @returns Promise resolving to updated user attribute
322
+ * @throws {ConfigurationError} When required environment variables are missing
323
+ * @throws {ApiError} When API request fails
324
+ * @throws {NetworkError} When network request fails
325
+ */
326
+ export async function setUserAttribute(userId, attributeDefinitionId, request, options) {
327
+ const token = options?.appToken ?? (await getAppToken(options));
328
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
329
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
330
+ if (!base) {
331
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
332
+ }
333
+ const url = new URL(String(base).replace(/\/+$/, "") +
334
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
335
+ let response;
336
+ try {
337
+ response = await globalThis.fetch(url.toString(), {
338
+ method: "PUT",
339
+ headers: {
340
+ "Content-Type": "application/json",
341
+ "x-t-token": token,
342
+ },
343
+ body: JSON.stringify(request),
344
+ });
345
+ }
346
+ catch (error) {
347
+ throw new NetworkError(`Failed to set user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
348
+ }
349
+ const json = await response.json();
350
+ return parseApiResponse(json, "Failed to set user attribute", response.status);
351
+ }
352
+ /**
353
+ * Gets a user attribute value.
354
+ *
355
+ * @param userId - User ID
356
+ * @param attributeDefinitionId - Attribute definition ID
357
+ * @param options - Optional configuration
358
+ * @returns Promise resolving to user attribute
359
+ * @throws {ConfigurationError} When required environment variables are missing
360
+ * @throws {ApiError} When API request fails
361
+ * @throws {NetworkError} When network request fails
362
+ */
363
+ export async function getUserAttribute(userId, attributeDefinitionId, options) {
364
+ const token = options?.appToken ?? (await getAppToken(options));
365
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
366
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
367
+ if (!base) {
368
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
369
+ }
370
+ const url = new URL(String(base).replace(/\/+$/, "") +
371
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
372
+ let response;
373
+ try {
374
+ response = await globalThis.fetch(url.toString(), {
375
+ method: "GET",
376
+ headers: {
377
+ "Content-Type": "application/json",
378
+ "x-t-token": token,
379
+ },
380
+ });
381
+ }
382
+ catch (error) {
383
+ throw new NetworkError(`Failed to get user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
384
+ }
385
+ const json = await response.json();
386
+ return parseApiResponse(json, "Failed to get user attribute", response.status);
387
+ }
388
+ /**
389
+ * Deletes a user attribute value.
390
+ *
391
+ * @param userId - User ID
392
+ * @param attributeDefinitionId - Attribute definition ID
393
+ * @param options - Optional configuration
394
+ * @throws {ConfigurationError} When required environment variables are missing
395
+ * @throws {ApiError} When API request fails
396
+ * @throws {NetworkError} When network request fails
397
+ */
398
+ export async function deleteUserAttribute(userId, attributeDefinitionId, options) {
399
+ const token = options?.appToken ?? (await getAppToken(options));
400
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
401
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
402
+ if (!base) {
403
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
404
+ }
405
+ const url = new URL(String(base).replace(/\/+$/, "") +
406
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
407
+ let response;
408
+ try {
409
+ response = await globalThis.fetch(url.toString(), {
410
+ method: "DELETE",
411
+ headers: {
412
+ "Content-Type": "application/json",
413
+ "x-t-token": token,
414
+ },
415
+ });
416
+ }
417
+ catch (error) {
418
+ throw new NetworkError(`Failed to delete user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
419
+ }
420
+ const json = await response.json();
421
+ if (typeof json !== "object" || json === null) {
422
+ throw new ApiError("Invalid user attribute deletion response", response.status);
423
+ }
424
+ const apiResponse = json;
425
+ if (apiResponse.code !== 200) {
426
+ const errorMsg = typeof apiResponse.msg === "string"
427
+ ? apiResponse.msg
428
+ : "Failed to delete user attribute";
429
+ throw new ApiError(errorMsg, response.status, apiResponse.code);
430
+ }
431
+ }
432
+ /**
433
+ * Gets all attributes for a specific user.
434
+ *
435
+ * @param userId - User ID
436
+ * @param options - Optional configuration
437
+ * @returns Promise resolving to array of user attributes
438
+ * @throws {ConfigurationError} When required environment variables are missing
439
+ * @throws {ApiError} When API request fails
440
+ * @throws {NetworkError} When network request fails
441
+ */
442
+ export async function getUserAttributes(userId, options) {
443
+ const token = options?.appToken ?? (await getAppToken(options));
444
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
445
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
446
+ if (!base) {
447
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
448
+ }
449
+ const url = new URL(String(base).replace(/\/+$/, "") +
450
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute`);
451
+ let response;
452
+ try {
453
+ response = await globalThis.fetch(url.toString(), {
454
+ method: "GET",
455
+ headers: {
456
+ "Content-Type": "application/json",
457
+ "x-t-token": token,
458
+ },
459
+ });
460
+ }
461
+ catch (error) {
462
+ throw new NetworkError(`Failed to get user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
463
+ }
464
+ const json = await response.json();
465
+ const result = parseApiResponse(json, "Failed to get user attributes", response.status);
466
+ if (!Array.isArray(result)) {
467
+ throw new ApiError("Invalid user attributes response: not an array", response.status);
468
+ }
469
+ return result;
470
+ }
471
+ /**
472
+ * Lists user attributes with pagination.
473
+ *
474
+ * @param userId - User ID
475
+ * @param request - List request parameters
476
+ * @param options - Optional configuration
477
+ * @returns Promise resolving to paginated list of user attributes
478
+ * @throws {ConfigurationError} When required environment variables are missing
479
+ * @throws {ApiError} When API request fails
480
+ * @throws {NetworkError} When network request fails
481
+ */
482
+ export async function listUserAttributes(userId, request) {
483
+ const token = request?.appToken ?? (await getAppToken(request));
484
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
485
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
486
+ if (!base) {
487
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
488
+ }
489
+ const url = new URL(String(base).replace(/\/+$/, "") +
490
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/page`);
491
+ const { appToken, baseUrl, ...requestParams } = request || {};
492
+ const queryParams = {
493
+ page: 0,
494
+ size: 20,
495
+ sort: "createdAt,desc",
496
+ ...requestParams,
497
+ };
498
+ Object.entries(queryParams).forEach(([key, value]) => {
499
+ if (value !== undefined) {
500
+ url.searchParams.append(key, String(value));
501
+ }
502
+ });
503
+ let response;
504
+ try {
505
+ response = await globalThis.fetch(url.toString(), {
506
+ method: "GET",
507
+ headers: {
508
+ "Content-Type": "application/json",
509
+ "x-t-token": token,
510
+ },
511
+ });
512
+ }
513
+ catch (error) {
514
+ throw new NetworkError(`Failed to list user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
515
+ }
516
+ const json = await response.json();
517
+ return parseApiResponse(json, "Failed to list user attributes", response.status);
518
+ }
519
+ /**
520
+ * Lists all users who have a specific attribute.
521
+ *
522
+ * @param attributeDefinitionId - Attribute definition ID
523
+ * @param request - List request parameters
524
+ * @param options - Optional configuration
525
+ * @returns Promise resolving to paginated list of user attributes
526
+ * @throws {ConfigurationError} When required environment variables are missing
527
+ * @throws {ApiError} When API request fails
528
+ * @throws {NetworkError} When network request fails
529
+ */
530
+ export async function listAttributeUsers(attributeDefinitionId, request) {
531
+ const token = request?.appToken ?? (await getAppToken(request));
532
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
533
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
534
+ if (!base) {
535
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
536
+ }
537
+ const url = new URL(String(base).replace(/\/+$/, "") +
538
+ `/user-attribute/v1/definition/${encodeURIComponent(attributeDefinitionId)}/users`);
539
+ const { appToken, baseUrl, ...requestParams } = request || {};
540
+ const queryParams = {
541
+ page: 0,
542
+ size: 20,
543
+ ...requestParams,
544
+ };
545
+ Object.entries(queryParams).forEach(([key, value]) => {
546
+ if (value !== undefined) {
547
+ url.searchParams.append(key, String(value));
548
+ }
549
+ });
550
+ let response;
551
+ try {
552
+ response = await globalThis.fetch(url.toString(), {
553
+ method: "GET",
554
+ headers: {
555
+ "Content-Type": "application/json",
556
+ "x-t-token": token,
557
+ },
558
+ });
559
+ }
560
+ catch (error) {
561
+ throw new NetworkError(`Failed to list attribute users: ${error instanceof Error ? error.message : "Unknown error"}`);
562
+ }
563
+ const json = await response.json();
564
+ const data = parseApiResponse(json, "Failed to list attribute users", response.status);
565
+ if (!Array.isArray(data.content)) {
566
+ throw new ApiError("Invalid attribute users response: content is not an array", response.status);
567
+ }
568
+ return data;
569
+ }
570
+ /**
571
+ * Lists all user attributes grouped by user with pagination.
572
+ *
573
+ * @param request - List request parameters
574
+ * @param options - Optional configuration
575
+ * @returns Promise resolving to paginated list of grouped user attributes
576
+ * @throws {ConfigurationError} When required environment variables are missing
577
+ * @throws {ApiError} When API request fails
578
+ * @throws {NetworkError} When network request fails
579
+ */
580
+ export async function listAllUserAttributes(request) {
581
+ const token = request?.appToken ?? (await getAppToken(request));
582
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
583
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
584
+ if (!base) {
585
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
586
+ }
587
+ const url = new URL(String(base).replace(/\/+$/, "") + "/user-attribute/v1/user/attribute/page");
588
+ const { appToken, baseUrl, ...requestParams } = request || {};
589
+ const queryParams = {
590
+ page: 0,
591
+ size: 20,
592
+ sort: "registeredAt,desc",
593
+ ...requestParams,
594
+ };
595
+ Object.entries(queryParams).forEach(([key, value]) => {
596
+ if (value !== undefined) {
597
+ url.searchParams.append(key, String(value));
598
+ }
599
+ });
600
+ let response;
601
+ try {
602
+ response = await globalThis.fetch(url.toString(), {
603
+ method: "GET",
604
+ headers: {
605
+ "Content-Type": "application/json",
606
+ "x-t-token": token,
607
+ },
608
+ });
609
+ }
610
+ catch (error) {
611
+ throw new NetworkError(`Failed to list all user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
612
+ }
613
+ const json = await response.json();
614
+ return parseApiResponse(json, "Failed to list all user attributes", response.status);
615
+ }