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