tale-js-sdk 0.1.4 → 1.1.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.
@@ -0,0 +1,628 @@
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.totalElements); // 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
+ ...requestParams,
216
+ };
217
+ Object.entries(queryParams).forEach(([key, value]) => {
218
+ if (value !== undefined) {
219
+ url.searchParams.append(key, String(value));
220
+ }
221
+ });
222
+ let response;
223
+ try {
224
+ response = await globalThis.fetch(url.toString(), {
225
+ method: "GET",
226
+ headers: {
227
+ "Content-Type": "application/json",
228
+ "x-t-token": token,
229
+ },
230
+ });
231
+ }
232
+ catch (error) {
233
+ throw new NetworkError(`Failed to list user attribute definitions: ${error instanceof Error ? error.message : "Unknown error"}`);
234
+ }
235
+ const json = await response.json();
236
+ const data = parseApiResponse(json, "Failed to list user attribute definitions", response.status);
237
+ if (!Array.isArray(data.content)) {
238
+ throw new ApiError("Invalid user attribute definitions response: content is not an array", response.status);
239
+ }
240
+ return data;
241
+ }
242
+ /**
243
+ * Lists all enabled user attribute definitions.
244
+ *
245
+ * @param options - Optional configuration
246
+ * @returns Promise resolving to array of enabled attribute definitions
247
+ * @throws {ConfigurationError} When required environment variables are missing
248
+ * @throws {ApiError} When API request fails
249
+ * @throws {NetworkError} When network request fails
250
+ */
251
+ export async function listEnabledUserAttributeDefinitions(options) {
252
+ const token = options?.appToken ?? (await getAppToken(options));
253
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
254
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
255
+ if (!base) {
256
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
257
+ }
258
+ const url = new URL(String(base).replace(/\/+$/, "") +
259
+ "/user-attribute/v1/definition/enabled");
260
+ let response;
261
+ try {
262
+ response = await globalThis.fetch(url.toString(), {
263
+ method: "GET",
264
+ headers: {
265
+ "Content-Type": "application/json",
266
+ "x-t-token": token,
267
+ },
268
+ });
269
+ }
270
+ catch (error) {
271
+ throw new NetworkError(`Failed to list enabled user attribute definitions: ${error instanceof Error ? error.message : "Unknown error"}`);
272
+ }
273
+ const json = await response.json();
274
+ const result = parseApiResponse(json, "Failed to list enabled user attribute definitions", response.status);
275
+ if (!Array.isArray(result)) {
276
+ throw new ApiError("Invalid enabled user attribute definitions response: not an array", response.status);
277
+ }
278
+ return result;
279
+ }
280
+ /**
281
+ * Toggles the enabled status of a user attribute definition.
282
+ *
283
+ * @param openId - Attribute definition open ID
284
+ * @param isEnabled - Whether the definition should be enabled
285
+ * @param options - Optional configuration
286
+ * @returns Promise resolving to updated attribute definition
287
+ * @throws {ConfigurationError} When required environment variables are missing
288
+ * @throws {ApiError} When API request fails
289
+ * @throws {NetworkError} When network request fails
290
+ */
291
+ export async function toggleUserAttributeDefinitionStatus(openId, isEnabled, options) {
292
+ const token = options?.appToken ?? (await getAppToken(options));
293
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
294
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
295
+ if (!base) {
296
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
297
+ }
298
+ const url = new URL(String(base).replace(/\/+$/, "") +
299
+ `/user-attribute/v1/definition/${encodeURIComponent(openId)}/status`);
300
+ url.searchParams.append("enabled", String(isEnabled));
301
+ let response;
302
+ try {
303
+ response = await globalThis.fetch(url.toString(), {
304
+ method: "PUT",
305
+ headers: {
306
+ "Content-Type": "application/json",
307
+ "x-t-token": token,
308
+ },
309
+ });
310
+ }
311
+ catch (error) {
312
+ throw new NetworkError(`Failed to toggle user attribute definition status: ${error instanceof Error ? error.message : "Unknown error"}`);
313
+ }
314
+ const json = await response.json();
315
+ return parseApiResponse(json, "Failed to toggle user attribute definition status", response.status);
316
+ }
317
+ // ==================== User Attribute Value Management ====================
318
+ /**
319
+ * Sets a user attribute value.
320
+ *
321
+ * @param userId - User ID
322
+ * @param attributeDefinitionId - Attribute definition ID
323
+ * @param request - Attribute value to set
324
+ * @param options - Optional configuration
325
+ * @returns Promise resolving to updated user attribute
326
+ * @throws {ConfigurationError} When required environment variables are missing
327
+ * @throws {ApiError} When API request fails
328
+ * @throws {NetworkError} When network request fails
329
+ */
330
+ export async function setUserAttribute(userId, attributeDefinitionId, request, options) {
331
+ const token = options?.appToken ?? (await getAppToken(options));
332
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
333
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
334
+ if (!base) {
335
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
336
+ }
337
+ const url = new URL(String(base).replace(/\/+$/, "") +
338
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
339
+ let response;
340
+ try {
341
+ response = await globalThis.fetch(url.toString(), {
342
+ method: "PUT",
343
+ headers: {
344
+ "Content-Type": "application/json",
345
+ "x-t-token": token,
346
+ },
347
+ body: JSON.stringify(request),
348
+ });
349
+ }
350
+ catch (error) {
351
+ throw new NetworkError(`Failed to set user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
352
+ }
353
+ const json = await response.json();
354
+ return parseApiResponse(json, "Failed to set user attribute", response.status);
355
+ }
356
+ /**
357
+ * Gets a user attribute value.
358
+ *
359
+ * @param userId - User ID
360
+ * @param attributeDefinitionId - Attribute definition ID
361
+ * @param options - Optional configuration
362
+ * @returns Promise resolving to user attribute
363
+ * @throws {ConfigurationError} When required environment variables are missing
364
+ * @throws {ApiError} When API request fails
365
+ * @throws {NetworkError} When network request fails
366
+ */
367
+ export async function getUserAttribute(userId, attributeDefinitionId, options) {
368
+ const token = options?.appToken ?? (await getAppToken(options));
369
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
370
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
371
+ if (!base) {
372
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
373
+ }
374
+ const url = new URL(String(base).replace(/\/+$/, "") +
375
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
376
+ let response;
377
+ try {
378
+ response = await globalThis.fetch(url.toString(), {
379
+ method: "GET",
380
+ headers: {
381
+ "Content-Type": "application/json",
382
+ "x-t-token": token,
383
+ },
384
+ });
385
+ }
386
+ catch (error) {
387
+ throw new NetworkError(`Failed to get user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
388
+ }
389
+ const json = await response.json();
390
+ return parseApiResponse(json, "Failed to get user attribute", response.status);
391
+ }
392
+ /**
393
+ * Deletes a user attribute value.
394
+ *
395
+ * @param userId - User ID
396
+ * @param attributeDefinitionId - Attribute definition ID
397
+ * @param options - Optional configuration
398
+ * @throws {ConfigurationError} When required environment variables are missing
399
+ * @throws {ApiError} When API request fails
400
+ * @throws {NetworkError} When network request fails
401
+ */
402
+ export async function deleteUserAttribute(userId, attributeDefinitionId, options) {
403
+ const token = options?.appToken ?? (await getAppToken(options));
404
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
405
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
406
+ if (!base) {
407
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
408
+ }
409
+ const url = new URL(String(base).replace(/\/+$/, "") +
410
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/${encodeURIComponent(attributeDefinitionId)}`);
411
+ let response;
412
+ try {
413
+ response = await globalThis.fetch(url.toString(), {
414
+ method: "DELETE",
415
+ headers: {
416
+ "Content-Type": "application/json",
417
+ "x-t-token": token,
418
+ },
419
+ });
420
+ }
421
+ catch (error) {
422
+ throw new NetworkError(`Failed to delete user attribute: ${error instanceof Error ? error.message : "Unknown error"}`);
423
+ }
424
+ const json = await response.json();
425
+ if (typeof json !== "object" || json === null) {
426
+ throw new ApiError("Invalid user attribute deletion response", response.status);
427
+ }
428
+ const apiResponse = json;
429
+ if (apiResponse.code !== 200) {
430
+ const errorMsg = typeof apiResponse.msg === "string"
431
+ ? apiResponse.msg
432
+ : "Failed to delete user attribute";
433
+ throw new ApiError(errorMsg, response.status, apiResponse.code);
434
+ }
435
+ }
436
+ /**
437
+ * Gets all attributes for a specific user.
438
+ *
439
+ * @param userId - User ID
440
+ * @param options - Optional configuration
441
+ * @returns Promise resolving to array of user attributes
442
+ * @throws {ConfigurationError} When required environment variables are missing
443
+ * @throws {ApiError} When API request fails
444
+ * @throws {NetworkError} When network request fails
445
+ */
446
+ export async function getUserAttributes(userId, options) {
447
+ const token = options?.appToken ?? (await getAppToken(options));
448
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
449
+ const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
450
+ if (!base) {
451
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
452
+ }
453
+ const url = new URL(String(base).replace(/\/+$/, "") +
454
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute`);
455
+ let response;
456
+ try {
457
+ response = await globalThis.fetch(url.toString(), {
458
+ method: "GET",
459
+ headers: {
460
+ "Content-Type": "application/json",
461
+ "x-t-token": token,
462
+ },
463
+ });
464
+ }
465
+ catch (error) {
466
+ throw new NetworkError(`Failed to get user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
467
+ }
468
+ const json = await response.json();
469
+ const result = parseApiResponse(json, "Failed to get user attributes", response.status);
470
+ if (!Array.isArray(result)) {
471
+ throw new ApiError("Invalid user attributes response: not an array", response.status);
472
+ }
473
+ return result;
474
+ }
475
+ /**
476
+ * Lists user attributes with pagination.
477
+ *
478
+ * @param userId - User ID
479
+ * @param request - List request parameters
480
+ * @param options - Optional configuration
481
+ * @returns Promise resolving to paginated list of user attributes
482
+ * @throws {ConfigurationError} When required environment variables are missing
483
+ * @throws {ApiError} When API request fails
484
+ * @throws {NetworkError} When network request fails
485
+ */
486
+ export async function listUserAttributes(userId, request) {
487
+ const token = request?.appToken ?? (await getAppToken(request));
488
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
489
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
490
+ if (!base) {
491
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
492
+ }
493
+ const url = new URL(String(base).replace(/\/+$/, "") +
494
+ `/user-attribute/v1/user/${encodeURIComponent(userId)}/attribute/page`);
495
+ const { appToken, baseUrl, ...requestParams } = request || {};
496
+ const queryParams = {
497
+ page: 0,
498
+ size: 20,
499
+ ...requestParams,
500
+ };
501
+ Object.entries(queryParams).forEach(([key, value]) => {
502
+ if (value !== undefined) {
503
+ url.searchParams.append(key, String(value));
504
+ }
505
+ });
506
+ let response;
507
+ try {
508
+ response = await globalThis.fetch(url.toString(), {
509
+ method: "GET",
510
+ headers: {
511
+ "Content-Type": "application/json",
512
+ "x-t-token": token,
513
+ },
514
+ });
515
+ }
516
+ catch (error) {
517
+ throw new NetworkError(`Failed to list user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
518
+ }
519
+ const json = await response.json();
520
+ const data = parseApiResponse(json, "Failed to list user attributes", response.status);
521
+ if (!Array.isArray(data.content)) {
522
+ throw new ApiError("Invalid user attributes response: content is not an array", response.status);
523
+ }
524
+ return data;
525
+ }
526
+ /**
527
+ * Lists all users who have a specific attribute.
528
+ *
529
+ * @param attributeDefinitionId - Attribute definition ID
530
+ * @param request - List request parameters
531
+ * @param options - Optional configuration
532
+ * @returns Promise resolving to paginated list of user attributes
533
+ * @throws {ConfigurationError} When required environment variables are missing
534
+ * @throws {ApiError} When API request fails
535
+ * @throws {NetworkError} When network request fails
536
+ */
537
+ export async function listAttributeUsers(attributeDefinitionId, request) {
538
+ const token = request?.appToken ?? (await getAppToken(request));
539
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
540
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
541
+ if (!base) {
542
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
543
+ }
544
+ const url = new URL(String(base).replace(/\/+$/, "") +
545
+ `/user-attribute/v1/definition/${encodeURIComponent(attributeDefinitionId)}/users`);
546
+ const { appToken, baseUrl, ...requestParams } = request || {};
547
+ const queryParams = {
548
+ page: 0,
549
+ size: 20,
550
+ ...requestParams,
551
+ };
552
+ Object.entries(queryParams).forEach(([key, value]) => {
553
+ if (value !== undefined) {
554
+ url.searchParams.append(key, String(value));
555
+ }
556
+ });
557
+ let response;
558
+ try {
559
+ response = await globalThis.fetch(url.toString(), {
560
+ method: "GET",
561
+ headers: {
562
+ "Content-Type": "application/json",
563
+ "x-t-token": token,
564
+ },
565
+ });
566
+ }
567
+ catch (error) {
568
+ throw new NetworkError(`Failed to list attribute users: ${error instanceof Error ? error.message : "Unknown error"}`);
569
+ }
570
+ const json = await response.json();
571
+ const data = parseApiResponse(json, "Failed to list attribute users", response.status);
572
+ if (!Array.isArray(data.content)) {
573
+ throw new ApiError("Invalid attribute users response: content is not an array", response.status);
574
+ }
575
+ return data;
576
+ }
577
+ /**
578
+ * Lists all user attributes grouped by user with pagination.
579
+ *
580
+ * @param request - List request parameters
581
+ * @param options - Optional configuration
582
+ * @returns Promise resolving to paginated list of grouped user attributes
583
+ * @throws {ConfigurationError} When required environment variables are missing
584
+ * @throws {ApiError} When API request fails
585
+ * @throws {NetworkError} When network request fails
586
+ */
587
+ export async function listAllUserAttributes(request) {
588
+ const token = request?.appToken ?? (await getAppToken(request));
589
+ const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
590
+ const base = request?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
591
+ if (!base) {
592
+ throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
593
+ }
594
+ const url = new URL(String(base).replace(/\/+$/, "") +
595
+ "/user-attribute/v1/user/attribute/page");
596
+ const { appToken, baseUrl, ...requestParams } = request || {};
597
+ const queryParams = {
598
+ page: 0,
599
+ size: 20,
600
+ sort_by: "createdAt",
601
+ sort_direction: "desc",
602
+ ...requestParams,
603
+ };
604
+ Object.entries(queryParams).forEach(([key, value]) => {
605
+ if (value !== undefined) {
606
+ url.searchParams.append(key, String(value));
607
+ }
608
+ });
609
+ let response;
610
+ try {
611
+ response = await globalThis.fetch(url.toString(), {
612
+ method: "GET",
613
+ headers: {
614
+ "Content-Type": "application/json",
615
+ "x-t-token": token,
616
+ },
617
+ });
618
+ }
619
+ catch (error) {
620
+ throw new NetworkError(`Failed to list all user attributes: ${error instanceof Error ? error.message : "Unknown error"}`);
621
+ }
622
+ const json = await response.json();
623
+ const data = parseApiResponse(json, "Failed to list all user attributes", response.status);
624
+ if (!Array.isArray(data.content)) {
625
+ throw new ApiError("Invalid all user attributes response: content is not an array", response.status);
626
+ }
627
+ return data;
628
+ }