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
package/dist/user.d.ts DELETED
@@ -1,242 +0,0 @@
1
- export interface CreateUserRequest {
2
- username: string;
3
- password_encrypted?: string;
4
- nickname?: string;
5
- phone?: string;
6
- email?: string;
7
- avatar_url?: string;
8
- role_ids?: string[];
9
- }
10
- export interface AppInfo {
11
- app_id: string;
12
- org_id: string;
13
- app_key: string;
14
- app_name: string;
15
- }
16
- export interface User {
17
- user_id: string;
18
- nick_name: string;
19
- avatar_url: string;
20
- username: string;
21
- email: string;
22
- phone: string;
23
- registered_at: string;
24
- remark: string | null;
25
- is_frozen: boolean;
26
- latest_login_time?: string;
27
- }
28
- export interface Role {
29
- role_id: string;
30
- role_name: string;
31
- description?: string;
32
- }
33
- export interface Privilege {
34
- privilege_id: string;
35
- privilege_name: string;
36
- description?: string;
37
- }
38
- export interface UserGroup {
39
- group_id: string;
40
- group_name: string;
41
- description?: string;
42
- }
43
- export interface UserLoginMethod {
44
- method: string;
45
- enabled: boolean;
46
- }
47
- export interface CreateUserResponse {
48
- app: AppInfo;
49
- user: User;
50
- user_roles: Role[];
51
- user_privileges: Privilege[];
52
- user_groups: UserGroup[];
53
- user_login_methods: UserLoginMethod[];
54
- }
55
- export interface CreateUserJson {
56
- data: CreateUserResponse;
57
- code: number;
58
- msg: string;
59
- }
60
- export interface CreateUserOptions {
61
- baseUrl?: string;
62
- appKey?: string;
63
- appSecret?: string;
64
- }
65
- export interface GetUserRequest {
66
- user_id?: string;
67
- include_rbac?: boolean;
68
- include_login_methods?: boolean;
69
- include_user_groups?: boolean;
70
- include_acl?: boolean;
71
- }
72
- export interface DeleteUserResponse {
73
- deleted: boolean;
74
- user_id: string;
75
- }
76
- export interface DeleteUserJson {
77
- data: DeleteUserResponse;
78
- code: number;
79
- msg: string;
80
- }
81
- export interface CommonOptions {
82
- baseUrl?: string;
83
- appKey?: string;
84
- appSecret?: string;
85
- }
86
- export interface ListUsersRequest {
87
- page?: number;
88
- size?: number;
89
- sort_by?: string;
90
- sort_direction?: 'asc' | 'desc';
91
- user_groups?: string[];
92
- user_roles?: string[];
93
- user_ids?: string[];
94
- keyword?: string;
95
- include_attributes?: boolean;
96
- }
97
- export interface UserAttribute {
98
- attribute_id: string;
99
- attribute_name: string;
100
- attribute_value: string;
101
- }
102
- export interface UserListItem {
103
- app: AppInfo;
104
- user: User;
105
- user_roles: Role[];
106
- user_privileges: Privilege[];
107
- user_groups: UserGroup[];
108
- user_attributes?: UserAttribute[];
109
- }
110
- export interface Sort {
111
- empty: boolean;
112
- sorted: boolean;
113
- unsorted: boolean;
114
- }
115
- export interface Pageable {
116
- sort: Sort;
117
- offset: number;
118
- pageNumber: number;
119
- pageSize: number;
120
- paged: boolean;
121
- unpaged: boolean;
122
- }
123
- export interface ListUsersResponse {
124
- content: UserListItem[];
125
- pageable: Pageable;
126
- last: boolean;
127
- totalPages: number;
128
- totalElements: number;
129
- size: number;
130
- number: number;
131
- sort: Sort;
132
- first: boolean;
133
- numberOfElements: number;
134
- empty: boolean;
135
- }
136
- export interface ListUsersJson {
137
- data: ListUsersResponse;
138
- code: number;
139
- msg: string;
140
- }
141
- /**
142
- * Creates a new user in the Tale application.
143
- *
144
- * @param userData - User data to create the new user
145
- * @param options - Optional configuration for the request
146
- * @returns Promise resolving to the created user information
147
- * @throws {ConfigurationError} When required environment variables are missing
148
- * @throws {ApiError} When API request fails or returns invalid response
149
- * @throws {NetworkError} When network request fails
150
- *
151
- * @example
152
- * ```typescript
153
- * import { createUser } from '@tale/client';
154
- *
155
- * try {
156
- * const result = await createUser({
157
- * username: 'john_doe',
158
- * nickname: 'John Doe',
159
- * email: 'john@example.com'
160
- * });
161
- * console.log('User created:', result.user.user_id);
162
- * } catch (error) {
163
- * console.error('Failed to create user:', error.message);
164
- * }
165
- * ```
166
- */
167
- export declare function createUser(userData: CreateUserRequest, options?: CreateUserOptions): Promise<CreateUserResponse>;
168
- /**
169
- * Retrieves user information by ID or open ID.
170
- *
171
- * @param userId - Optional user ID (open_id) to query. If not provided, returns current user info
172
- * @param options - Optional parameters for inclusion flags and configuration
173
- * @returns Promise resolving to the user information
174
- * @throws {ConfigurationError} When required environment variables are missing
175
- * @throws {ApiError} When API request fails or returns invalid response
176
- * @throws {NetworkError} When network request fails
177
- *
178
- * @example
179
- * ```typescript
180
- * import { getUserById } from '@tale/client';
181
- *
182
- * try {
183
- * const result = await getUserById('user_open_id_here');
184
- * console.log('User info:', result.user.username);
185
- * } catch (error) {
186
- * console.error('Failed to get user:', error.message);
187
- * }
188
- * ```
189
- */
190
- export declare function getUserById(userId?: string, options?: GetUserRequest & CommonOptions): Promise<CreateUserResponse>;
191
- /**
192
- * Deletes a user by ID or open ID.
193
- *
194
- * @param userId - User ID (open_id) to delete
195
- * @param options - Optional configuration for the request
196
- * @returns Promise resolving to the deletion result
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
- * @example
202
- * ```typescript
203
- * import { deleteUser } from '@tale/client';
204
- *
205
- * try {
206
- * const result = await deleteUser('user_open_id_here');
207
- * console.log('User deleted:', result.deleted);
208
- * } catch (error) {
209
- * console.error('Failed to delete user:', error.message);
210
- * }
211
- * ```
212
- */
213
- export declare function deleteUser(userId: string, options?: CommonOptions): Promise<DeleteUserResponse>;
214
- /**
215
- * Lists users with pagination, filtering, and search capabilities.
216
- *
217
- * @param options - Optional parameters for pagination, filtering, and configuration
218
- * @returns Promise resolving to paginated user list with metadata
219
- * @throws {ConfigurationError} When required environment variables are missing
220
- * @throws {ApiError} When API request fails or returns invalid response
221
- * @throws {NetworkError} When network request fails
222
- *
223
- * @example
224
- * ```typescript
225
- * import { listUsers } from '@tale/client';
226
- *
227
- * try {
228
- * const result = await listUsers({
229
- * page: 0,
230
- * size: 20,
231
- * keyword: 'john',
232
- * sort_by: 'username',
233
- * sort_direction: 'asc'
234
- * });
235
- * console.log(`Found ${result.totalElements} users`);
236
- * console.log('First user:', result.content[0].user.username);
237
- * } catch (error) {
238
- * console.error('Failed to list users:', error.message);
239
- * }
240
- * ```
241
- */
242
- export declare function listUsers(options?: ListUsersRequest & CommonOptions): Promise<ListUsersResponse>;
package/dist/user.js DELETED
@@ -1,331 +0,0 @@
1
- import { getAppToken } from './token.js';
2
- import { ApiError, ConfigurationError, NetworkError } from './errors.js';
3
- /**
4
- * Creates a new user in the Tale application.
5
- *
6
- * @param userData - User data to create the new user
7
- * @param options - Optional configuration for the request
8
- * @returns Promise resolving to the created user 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 { createUser } from '@tale/client';
16
- *
17
- * try {
18
- * const result = await createUser({
19
- * username: 'john_doe',
20
- * nickname: 'John Doe',
21
- * email: 'john@example.com'
22
- * });
23
- * console.log('User created:', result.user.user_id);
24
- * } catch (error) {
25
- * console.error('Failed to create user:', error.message);
26
- * }
27
- * ```
28
- */
29
- export async function createUser(userData, options) {
30
- // Validate required fields
31
- if (!userData.username || userData.username.trim() === '') {
32
- throw new ApiError('username is required', 400, '9400');
33
- }
34
- // Get app token for authentication
35
- const token = await getAppToken(options);
36
- // Determine base URL
37
- const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
38
- const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
39
- if (!base) {
40
- throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
41
- }
42
- const url = String(base).replace(/\/+$/, '') + '/account/v1/user';
43
- let response;
44
- try {
45
- response = await globalThis.fetch(url, {
46
- method: 'POST',
47
- headers: {
48
- 'Content-Type': 'application/json',
49
- 'x-t-token': token,
50
- },
51
- body: JSON.stringify(userData),
52
- });
53
- }
54
- catch (error) {
55
- throw new NetworkError(`Failed to create user: ${error instanceof Error ? error.message : 'Unknown error'}`);
56
- }
57
- let json;
58
- try {
59
- const responseJson = await response.json();
60
- json = responseJson;
61
- }
62
- catch (error) {
63
- throw new ApiError(`Failed to parse user creation response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
64
- }
65
- // Handle API errors
66
- if (json.code !== 200) {
67
- const errorMsg = typeof json.msg === 'string' ? json.msg : 'User creation failed';
68
- throw new ApiError(errorMsg, response.status, json.code);
69
- }
70
- // Validate response structure
71
- if (!json.data || !json.data.user || !json.data.app) {
72
- throw new ApiError('Invalid user creation response: missing required data', response.status);
73
- }
74
- return json.data;
75
- }
76
- /**
77
- * Retrieves user information by ID or open ID.
78
- *
79
- * @param userId - Optional user ID (open_id) to query. If not provided, returns current user info
80
- * @param options - Optional parameters for inclusion flags and configuration
81
- * @returns Promise resolving to the user information
82
- * @throws {ConfigurationError} When required environment variables are missing
83
- * @throws {ApiError} When API request fails or returns invalid response
84
- * @throws {NetworkError} When network request fails
85
- *
86
- * @example
87
- * ```typescript
88
- * import { getUserById } from '@tale/client';
89
- *
90
- * try {
91
- * const result = await getUserById('user_open_id_here');
92
- * console.log('User info:', result.user.username);
93
- * } catch (error) {
94
- * console.error('Failed to get user:', error.message);
95
- * }
96
- * ```
97
- */
98
- export async function getUserById(userId, options) {
99
- // Get app token for authentication
100
- const token = await getAppToken(options);
101
- // Determine base URL
102
- const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
103
- const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
104
- if (!base) {
105
- throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
106
- }
107
- // Build URL with query parameters
108
- const url = new URL(String(base).replace(/\/+$/, '') + '/account/v1/user');
109
- // Add query parameters
110
- const queryParams = {
111
- user_id: userId,
112
- include_rbac: true,
113
- include_login_methods: true,
114
- include_user_groups: true,
115
- include_acl: false,
116
- ...options
117
- };
118
- Object.entries(queryParams).forEach(([key, value]) => {
119
- if (value !== undefined) {
120
- url.searchParams.append(key, String(value));
121
- }
122
- });
123
- let response;
124
- try {
125
- response = await globalThis.fetch(url.toString(), {
126
- method: 'GET',
127
- headers: {
128
- 'Content-Type': 'application/json',
129
- 'x-t-token': token,
130
- },
131
- });
132
- }
133
- catch (error) {
134
- throw new NetworkError(`Failed to get user: ${error instanceof Error ? error.message : 'Unknown error'}`);
135
- }
136
- let json;
137
- try {
138
- const responseJson = await response.json();
139
- json = responseJson;
140
- }
141
- catch (error) {
142
- throw new ApiError(`Failed to parse user response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
143
- }
144
- // Handle API errors
145
- if (json.code !== 200) {
146
- const errorMsg = typeof json.msg === 'string' ? json.msg : 'User retrieval failed';
147
- throw new ApiError(errorMsg, response.status, json.code);
148
- }
149
- // Validate response structure
150
- if (!json.data || !json.data.user || !json.data.app) {
151
- throw new ApiError('Invalid user response: missing required data', response.status);
152
- }
153
- return json.data;
154
- }
155
- /**
156
- * Deletes a user by ID or open ID.
157
- *
158
- * @param userId - User ID (open_id) to delete
159
- * @param options - Optional configuration for the request
160
- * @returns Promise resolving to the deletion result
161
- * @throws {ConfigurationError} When required environment variables are missing
162
- * @throws {ApiError} When API request fails or returns invalid response
163
- * @throws {NetworkError} When network request fails
164
- *
165
- * @example
166
- * ```typescript
167
- * import { deleteUser } from '@tale/client';
168
- *
169
- * try {
170
- * const result = await deleteUser('user_open_id_here');
171
- * console.log('User deleted:', result.deleted);
172
- * } catch (error) {
173
- * console.error('Failed to delete user:', error.message);
174
- * }
175
- * ```
176
- */
177
- export async function deleteUser(userId, options) {
178
- // Validate required fields
179
- if (!userId || userId.trim() === '') {
180
- throw new ApiError('user_id is required for deletion', 400, '9400');
181
- }
182
- // Get app token for authentication
183
- const token = await getAppToken(options);
184
- // Determine base URL
185
- const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
186
- const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
187
- if (!base) {
188
- throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
189
- }
190
- const url = String(base).replace(/\/+$/, '') + `/account/v1/users/${encodeURIComponent(userId)}`;
191
- let response;
192
- try {
193
- response = await globalThis.fetch(url, {
194
- method: 'DELETE',
195
- headers: {
196
- 'Content-Type': 'application/json',
197
- 'x-t-token': token,
198
- },
199
- });
200
- }
201
- catch (error) {
202
- throw new NetworkError(`Failed to delete user: ${error instanceof Error ? error.message : 'Unknown error'}`);
203
- }
204
- let json;
205
- try {
206
- const responseJson = await response.json();
207
- json = responseJson;
208
- }
209
- catch (error) {
210
- throw new ApiError(`Failed to parse user deletion response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
211
- }
212
- // Handle API errors
213
- if (json.code !== 200) {
214
- const errorMsg = typeof json.msg === 'string' ? json.msg : 'User deletion failed';
215
- throw new ApiError(errorMsg, response.status, json.code);
216
- }
217
- // Validate response structure
218
- if (!json.data || json.data.deleted !== true) {
219
- throw new ApiError('Invalid user deletion response: deletion not confirmed', response.status);
220
- }
221
- return json.data;
222
- }
223
- /**
224
- * Lists users with pagination, filtering, and search capabilities.
225
- *
226
- * @param options - Optional parameters for pagination, filtering, and configuration
227
- * @returns Promise resolving to paginated user list with metadata
228
- * @throws {ConfigurationError} When required environment variables are missing
229
- * @throws {ApiError} When API request fails or returns invalid response
230
- * @throws {NetworkError} When network request fails
231
- *
232
- * @example
233
- * ```typescript
234
- * import { listUsers } from '@tale/client';
235
- *
236
- * try {
237
- * const result = await listUsers({
238
- * page: 0,
239
- * size: 20,
240
- * keyword: 'john',
241
- * sort_by: 'username',
242
- * sort_direction: 'asc'
243
- * });
244
- * console.log(`Found ${result.totalElements} users`);
245
- * console.log('First user:', result.content[0].user.username);
246
- * } catch (error) {
247
- * console.error('Failed to list users:', error.message);
248
- * }
249
- * ```
250
- */
251
- export async function listUsers(options) {
252
- // Get app token for authentication
253
- const token = await getAppToken(options);
254
- // Determine base URL
255
- const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
256
- const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
257
- if (!base) {
258
- throw new ConfigurationError('Missing required environment variable: TALE_BASE_URL');
259
- }
260
- // Build URL with query parameters
261
- const url = new URL(String(base).replace(/\/+$/, '') + '/account/v1/users');
262
- // Add query parameters with defaults
263
- const queryParams = {
264
- page: 0,
265
- size: 20,
266
- sort_by: 'createdAt',
267
- sort_direction: 'desc',
268
- include_attributes: false,
269
- ...options
270
- };
271
- // Handle array parameters (need to be comma-separated)
272
- if (queryParams.user_groups && queryParams.user_groups.length > 0) {
273
- url.searchParams.append('user_groups', queryParams.user_groups.join(','));
274
- }
275
- if (queryParams.user_roles && queryParams.user_roles.length > 0) {
276
- url.searchParams.append('user_roles', queryParams.user_roles.join(','));
277
- }
278
- if (queryParams.user_ids && queryParams.user_ids.length > 0) {
279
- url.searchParams.append('user_ids', queryParams.user_ids.join(','));
280
- }
281
- // Add other parameters
282
- if (queryParams.page !== undefined) {
283
- url.searchParams.append('page', String(queryParams.page));
284
- }
285
- if (queryParams.size !== undefined) {
286
- url.searchParams.append('size', String(queryParams.size));
287
- }
288
- if (queryParams.sort_by) {
289
- url.searchParams.append('sort_by', queryParams.sort_by);
290
- }
291
- if (queryParams.sort_direction) {
292
- url.searchParams.append('sort_direction', queryParams.sort_direction);
293
- }
294
- if (queryParams.keyword) {
295
- url.searchParams.append('keyword', queryParams.keyword);
296
- }
297
- if (queryParams.include_attributes !== undefined) {
298
- url.searchParams.append('include_attributes', String(queryParams.include_attributes));
299
- }
300
- let response;
301
- try {
302
- response = await globalThis.fetch(url.toString(), {
303
- method: 'GET',
304
- headers: {
305
- 'Content-Type': 'application/json',
306
- 'x-t-token': token,
307
- },
308
- });
309
- }
310
- catch (error) {
311
- throw new NetworkError(`Failed to list users: ${error instanceof Error ? error.message : 'Unknown error'}`);
312
- }
313
- let json;
314
- try {
315
- const responseJson = await response.json();
316
- json = responseJson;
317
- }
318
- catch (error) {
319
- throw new ApiError(`Failed to parse users list response: ${error instanceof Error ? error.message : 'Invalid JSON'}`, response.status);
320
- }
321
- // Handle API errors
322
- if (json.code !== 200) {
323
- const errorMsg = typeof json.msg === 'string' ? json.msg : 'Users list retrieval failed';
324
- throw new ApiError(errorMsg, response.status, json.code);
325
- }
326
- // Validate response structure
327
- if (!json.data || !Array.isArray(json.data.content)) {
328
- throw new ApiError('Invalid users list response: missing required data', response.status);
329
- }
330
- return json.data;
331
- }