@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.
- package/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/acl/index.d.ts +295 -0
- package/dist/acl/index.js +653 -0
- package/dist/acl/types.d.ts +142 -0
- package/dist/acl/types.js +1 -0
- package/dist/auth/index.d.ts +166 -0
- package/dist/auth/index.js +522 -0
- package/dist/auth/types.d.ts +127 -0
- package/dist/auth/types.js +1 -0
- package/dist/cms/file.d.ts +398 -0
- package/dist/cms/file.js +800 -0
- package/dist/cms/folder.d.ts +128 -0
- package/dist/cms/folder.js +294 -0
- package/dist/cms/index.d.ts +3 -0
- package/dist/cms/index.js +6 -0
- package/dist/cms/types.d.ts +157 -0
- package/dist/cms/types.js +1 -0
- package/dist/common/types.d.ts +91 -0
- package/dist/common/types.js +2 -0
- package/dist/errors.d.ts +52 -0
- package/dist/errors.js +109 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/rbac/acl.d.ts +152 -0
- package/dist/rbac/acl.js +723 -0
- package/dist/rbac/index.d.ts +464 -0
- package/dist/rbac/index.js +1121 -0
- package/dist/rbac/rbac.d.ts +198 -0
- package/dist/rbac/rbac.js +984 -0
- package/dist/rbac/types.d.ts +125 -0
- package/dist/rbac/types.js +1 -0
- package/dist/rbac/user-group.d.ts +122 -0
- package/dist/rbac/user-group.js +570 -0
- package/dist/status.d.ts +40 -0
- package/dist/status.js +56 -0
- package/dist/task/index.d.ts +163 -0
- package/dist/task/index.js +495 -0
- package/dist/task/types.d.ts +116 -0
- package/dist/task/types.js +1 -0
- package/dist/task-type/index.d.ts +92 -0
- package/dist/task-type/index.js +256 -0
- package/dist/task-type/types.d.ts +54 -0
- package/dist/task-type/types.js +1 -0
- package/dist/token.d.ts +21 -0
- package/dist/token.js +114 -0
- package/dist/user/index.d.ts +267 -0
- package/dist/user/index.js +786 -0
- package/dist/user/types.d.ts +145 -0
- package/dist/user/types.js +1 -0
- package/dist/user-attribute/index.d.ts +186 -0
- package/dist/user-attribute/index.js +615 -0
- package/dist/user-attribute/types.d.ts +109 -0
- package/dist/user-attribute/types.js +1 -0
- package/dist/user-group/index.d.ts +231 -0
- package/dist/user-group/index.js +566 -0
- package/dist/user-group/types.d.ts +50 -0
- package/dist/user-group/types.js +1 -0
- package/package.json +50 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { UserTask, CreateUserTaskRequest, UpdateUserTaskRequest, BatchCreateUserTasksRequest, BatchQueryUserTasksRequest, BatchCreateUserTasksResponse, ListUserTasksRequest, UpdateTaskStatusRequest, TaskStatistics, TaskStatusResponse, UserTaskOptions } from "./types.js";
|
|
2
|
+
import type { PageResponse } from "../common/types.js";
|
|
3
|
+
export type { UserTask, CreateUserTaskRequest, UpdateUserTaskRequest, BatchCreateUserTasksRequest, BatchQueryUserTasksRequest, BatchCreateUserTasksResponse, ListUserTasksRequest, UpdateTaskStatusRequest, TaskStatistics, TaskStatusResponse, UserTaskOptions, };
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new user task.
|
|
6
|
+
*
|
|
7
|
+
* @param request - Task creation request
|
|
8
|
+
* @param options - Optional configuration
|
|
9
|
+
* @returns Promise resolving to created task
|
|
10
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
11
|
+
* @throws {ApiError} When API request fails
|
|
12
|
+
* @throws {NetworkError} When network request fails
|
|
13
|
+
*/
|
|
14
|
+
export declare function createUserTask(request: CreateUserTaskRequest, options?: UserTaskOptions): Promise<UserTask>;
|
|
15
|
+
/**
|
|
16
|
+
* Batch creates user tasks.
|
|
17
|
+
*
|
|
18
|
+
* @param request - Batch creation request
|
|
19
|
+
* @param options - Optional configuration
|
|
20
|
+
* @returns Promise resolving to batch creation response
|
|
21
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
22
|
+
* @throws {ApiError} When API request fails
|
|
23
|
+
* @throws {NetworkError} When network request fails
|
|
24
|
+
*/
|
|
25
|
+
export declare function batchCreateUserTasks(request: BatchCreateUserTasksRequest, options?: UserTaskOptions): Promise<BatchCreateUserTasksResponse>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets a user task by ID.
|
|
28
|
+
*
|
|
29
|
+
* @param taskId - Task ID
|
|
30
|
+
* @param options - Optional configuration (include_attachments, include_sub_tasks)
|
|
31
|
+
* @returns Promise resolving to task
|
|
32
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
33
|
+
* @throws {ApiError} When API request fails
|
|
34
|
+
* @throws {NetworkError} When network request fails
|
|
35
|
+
*/
|
|
36
|
+
export declare function getUserTask(taskId: string, options?: UserTaskOptions & {
|
|
37
|
+
include_attachments?: boolean;
|
|
38
|
+
include_sub_tasks?: boolean;
|
|
39
|
+
}): Promise<UserTask>;
|
|
40
|
+
/**
|
|
41
|
+
* Batch gets user tasks by IDs.
|
|
42
|
+
*
|
|
43
|
+
* @param request - Batch query request
|
|
44
|
+
* @param options - Optional configuration
|
|
45
|
+
* @returns Promise resolving to array of tasks
|
|
46
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
47
|
+
* @throws {ApiError} When API request fails
|
|
48
|
+
* @throws {NetworkError} When network request fails
|
|
49
|
+
*/
|
|
50
|
+
export declare function batchGetUserTasks(request: BatchQueryUserTasksRequest, options?: UserTaskOptions): Promise<UserTask[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Lists user tasks with pagination and optional filtering.
|
|
53
|
+
*
|
|
54
|
+
* @param request - List request parameters
|
|
55
|
+
* @param options - Optional configuration
|
|
56
|
+
* @returns Promise resolving to paginated list of tasks
|
|
57
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
58
|
+
* @throws {ApiError} When API request fails
|
|
59
|
+
* @throws {NetworkError} When network request fails
|
|
60
|
+
*/
|
|
61
|
+
export declare function listUserTasks(request?: ListUserTasksRequest & UserTaskOptions): Promise<PageResponse<UserTask>>;
|
|
62
|
+
/**
|
|
63
|
+
* Updates a user task.
|
|
64
|
+
*
|
|
65
|
+
* @param taskId - Task ID
|
|
66
|
+
* @param request - Update request
|
|
67
|
+
* @param options - Optional configuration
|
|
68
|
+
* @returns Promise resolving to updated task
|
|
69
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
70
|
+
* @throws {ApiError} When API request fails
|
|
71
|
+
* @throws {NetworkError} When network request fails
|
|
72
|
+
*/
|
|
73
|
+
export declare function updateUserTask(taskId: string, request: UpdateUserTaskRequest, options?: UserTaskOptions): Promise<UserTask>;
|
|
74
|
+
/**
|
|
75
|
+
* Deletes a user task.
|
|
76
|
+
*
|
|
77
|
+
* @param taskId - Task ID
|
|
78
|
+
* @param options - Optional configuration
|
|
79
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
80
|
+
* @throws {ApiError} When API request fails
|
|
81
|
+
* @throws {NetworkError} When network request fails
|
|
82
|
+
*/
|
|
83
|
+
export declare function deleteUserTask(taskId: string, options?: UserTaskOptions): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Gets task input data.
|
|
86
|
+
*
|
|
87
|
+
* @param taskId - Task ID
|
|
88
|
+
* @param options - Optional configuration
|
|
89
|
+
* @returns Promise resolving to task input data
|
|
90
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
91
|
+
* @throws {ApiError} When API request fails
|
|
92
|
+
* @throws {NetworkError} When network request fails
|
|
93
|
+
*/
|
|
94
|
+
export declare function getTaskInput(taskId: string, options?: UserTaskOptions): Promise<Record<string, unknown>>;
|
|
95
|
+
/**
|
|
96
|
+
* Gets task output data.
|
|
97
|
+
*
|
|
98
|
+
* @param taskId - Task ID
|
|
99
|
+
* @param options - Optional configuration
|
|
100
|
+
* @returns Promise resolving to task output data
|
|
101
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
102
|
+
* @throws {ApiError} When API request fails
|
|
103
|
+
* @throws {NetworkError} When network request fails
|
|
104
|
+
*/
|
|
105
|
+
export declare function getTaskOutput(taskId: string, options?: UserTaskOptions): Promise<Record<string, unknown>>;
|
|
106
|
+
/**
|
|
107
|
+
* Updates task input data.
|
|
108
|
+
*
|
|
109
|
+
* @param taskId - Task ID
|
|
110
|
+
* @param taskInput - New task input data
|
|
111
|
+
* @param options - Optional configuration
|
|
112
|
+
* @returns Promise resolving to updated task
|
|
113
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
114
|
+
* @throws {ApiError} When API request fails
|
|
115
|
+
* @throws {NetworkError} When network request fails
|
|
116
|
+
*/
|
|
117
|
+
export declare function updateTaskInput(taskId: string, taskInput: Record<string, unknown>, options?: UserTaskOptions): Promise<UserTask>;
|
|
118
|
+
/**
|
|
119
|
+
* Updates task output data.
|
|
120
|
+
*
|
|
121
|
+
* @param taskId - Task ID
|
|
122
|
+
* @param taskOutput - New task output data
|
|
123
|
+
* @param options - Optional configuration
|
|
124
|
+
* @returns Promise resolving to updated task
|
|
125
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
126
|
+
* @throws {ApiError} When API request fails
|
|
127
|
+
* @throws {NetworkError} When network request fails
|
|
128
|
+
*/
|
|
129
|
+
export declare function updateTaskOutput(taskId: string, taskOutput: Record<string, unknown>, options?: UserTaskOptions): Promise<UserTask>;
|
|
130
|
+
/**
|
|
131
|
+
* Gets task status.
|
|
132
|
+
*
|
|
133
|
+
* @param taskId - Task ID
|
|
134
|
+
* @param options - Optional configuration
|
|
135
|
+
* @returns Promise resolving to task status
|
|
136
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
137
|
+
* @throws {ApiError} When API request fails
|
|
138
|
+
* @throws {NetworkError} When network request fails
|
|
139
|
+
*/
|
|
140
|
+
export declare function getTaskStatus(taskId: string, options?: UserTaskOptions): Promise<string>;
|
|
141
|
+
/**
|
|
142
|
+
* Updates task status.
|
|
143
|
+
*
|
|
144
|
+
* @param taskId - Task ID
|
|
145
|
+
* @param request - Status update request
|
|
146
|
+
* @param options - Optional configuration
|
|
147
|
+
* @returns Promise resolving to updated task
|
|
148
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
149
|
+
* @throws {ApiError} When API request fails
|
|
150
|
+
* @throws {NetworkError} When network request fails
|
|
151
|
+
*/
|
|
152
|
+
export declare function updateTaskStatus(taskId: string, request: UpdateTaskStatusRequest, options?: UserTaskOptions): Promise<UserTask>;
|
|
153
|
+
/**
|
|
154
|
+
* Gets task statistics.
|
|
155
|
+
*
|
|
156
|
+
* @param userId - Optional user ID filter
|
|
157
|
+
* @param options - Optional configuration
|
|
158
|
+
* @returns Promise resolving to task statistics
|
|
159
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
160
|
+
* @throws {ApiError} When API request fails
|
|
161
|
+
* @throws {NetworkError} When network request fails
|
|
162
|
+
*/
|
|
163
|
+
export declare function getTaskStatistics(userId?: string, options?: UserTaskOptions): Promise<TaskStatistics>;
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { getAppToken } from "../token.js";
|
|
2
|
+
import { ApiError, ConfigurationError, NetworkError } from "../errors.js";
|
|
3
|
+
// ==================== Helper Functions ====================
|
|
4
|
+
function parseApiResponse(json, errorMessage, statusCode) {
|
|
5
|
+
if (typeof json !== "object" || json === null) {
|
|
6
|
+
throw new ApiError(`Invalid response: ${errorMessage} - not an object`, statusCode);
|
|
7
|
+
}
|
|
8
|
+
const response = json;
|
|
9
|
+
if (response.code !== 200) {
|
|
10
|
+
const errorMsg = typeof response.msg === "string" ? response.msg : errorMessage;
|
|
11
|
+
throw new ApiError(errorMsg, statusCode, response.code);
|
|
12
|
+
}
|
|
13
|
+
if (!response.data) {
|
|
14
|
+
throw new ApiError(`Invalid response: ${errorMessage} - missing data`, statusCode);
|
|
15
|
+
}
|
|
16
|
+
return response.data;
|
|
17
|
+
}
|
|
18
|
+
function getBaseUrl(options) {
|
|
19
|
+
const env = globalThis?.process?.env ?? import.meta?.env ?? undefined;
|
|
20
|
+
const base = options?.baseUrl ?? env?.TALE_BASE_URL ?? undefined;
|
|
21
|
+
if (!base) {
|
|
22
|
+
throw new ConfigurationError("Missing required environment variable: TALE_BASE_URL");
|
|
23
|
+
}
|
|
24
|
+
return String(base).replace(/\/+$/, "");
|
|
25
|
+
}
|
|
26
|
+
// ==================== UserTask CRUD Operations ====================
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new user task.
|
|
29
|
+
*
|
|
30
|
+
* @param request - Task creation request
|
|
31
|
+
* @param options - Optional configuration
|
|
32
|
+
* @returns Promise resolving to created task
|
|
33
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
34
|
+
* @throws {ApiError} When API request fails
|
|
35
|
+
* @throws {NetworkError} When network request fails
|
|
36
|
+
*/
|
|
37
|
+
export async function createUserTask(request, options) {
|
|
38
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
39
|
+
const base = getBaseUrl(options);
|
|
40
|
+
const url = new URL(base + "/user-task/v1/tasks");
|
|
41
|
+
let response;
|
|
42
|
+
try {
|
|
43
|
+
response = await globalThis.fetch(url.toString(), {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
"x-t-token": token,
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify(request),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new NetworkError(`Failed to create user task: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
54
|
+
}
|
|
55
|
+
const json = await response.json();
|
|
56
|
+
return parseApiResponse(json, "Failed to create user task", response.status);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Batch creates user tasks.
|
|
60
|
+
*
|
|
61
|
+
* @param request - Batch creation request
|
|
62
|
+
* @param options - Optional configuration
|
|
63
|
+
* @returns Promise resolving to batch creation response
|
|
64
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
65
|
+
* @throws {ApiError} When API request fails
|
|
66
|
+
* @throws {NetworkError} When network request fails
|
|
67
|
+
*/
|
|
68
|
+
export async function batchCreateUserTasks(request, options) {
|
|
69
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
70
|
+
const base = getBaseUrl(options);
|
|
71
|
+
const url = new URL(base + "/user-task/v2/tasks/batch");
|
|
72
|
+
let response;
|
|
73
|
+
try {
|
|
74
|
+
response = await globalThis.fetch(url.toString(), {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: {
|
|
77
|
+
"Content-Type": "application/json",
|
|
78
|
+
"x-t-token": token,
|
|
79
|
+
},
|
|
80
|
+
body: JSON.stringify(request),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
throw new NetworkError(`Failed to batch create user tasks: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
85
|
+
}
|
|
86
|
+
const json = await response.json();
|
|
87
|
+
return parseApiResponse(json, "Failed to batch create user tasks", response.status);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Gets a user task by ID.
|
|
91
|
+
*
|
|
92
|
+
* @param taskId - Task ID
|
|
93
|
+
* @param options - Optional configuration (include_attachments, include_sub_tasks)
|
|
94
|
+
* @returns Promise resolving to task
|
|
95
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
96
|
+
* @throws {ApiError} When API request fails
|
|
97
|
+
* @throws {NetworkError} When network request fails
|
|
98
|
+
*/
|
|
99
|
+
export async function getUserTask(taskId, options) {
|
|
100
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
101
|
+
const base = getBaseUrl(options);
|
|
102
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}`);
|
|
103
|
+
const { appToken, baseUrl, ...queryParams } = options || {};
|
|
104
|
+
if (queryParams.include_attachments !== undefined) {
|
|
105
|
+
url.searchParams.append("include_attachments", String(queryParams.include_attachments));
|
|
106
|
+
}
|
|
107
|
+
if (queryParams.include_sub_tasks !== undefined) {
|
|
108
|
+
url.searchParams.append("include_sub_tasks", String(queryParams.include_sub_tasks));
|
|
109
|
+
}
|
|
110
|
+
let response;
|
|
111
|
+
try {
|
|
112
|
+
response = await globalThis.fetch(url.toString(), {
|
|
113
|
+
method: "GET",
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"x-t-token": token,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
throw new NetworkError(`Failed to get user task: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
122
|
+
}
|
|
123
|
+
const json = await response.json();
|
|
124
|
+
return parseApiResponse(json, "Failed to get user task", response.status);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Batch gets user tasks by IDs.
|
|
128
|
+
*
|
|
129
|
+
* @param request - Batch query request
|
|
130
|
+
* @param options - Optional configuration
|
|
131
|
+
* @returns Promise resolving to array of tasks
|
|
132
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
133
|
+
* @throws {ApiError} When API request fails
|
|
134
|
+
* @throws {NetworkError} When network request fails
|
|
135
|
+
*/
|
|
136
|
+
export async function batchGetUserTasks(request, options) {
|
|
137
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
138
|
+
const base = getBaseUrl(options);
|
|
139
|
+
const url = new URL(base + "/user-task/v1/tasks/batch");
|
|
140
|
+
let response;
|
|
141
|
+
try {
|
|
142
|
+
response = await globalThis.fetch(url.toString(), {
|
|
143
|
+
method: "POST",
|
|
144
|
+
headers: {
|
|
145
|
+
"Content-Type": "application/json",
|
|
146
|
+
"x-t-token": token,
|
|
147
|
+
},
|
|
148
|
+
body: JSON.stringify(request),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
throw new NetworkError(`Failed to batch get user tasks: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
153
|
+
}
|
|
154
|
+
const json = await response.json();
|
|
155
|
+
const result = parseApiResponse(json, "Failed to batch get user tasks", response.status);
|
|
156
|
+
if (!Array.isArray(result)) {
|
|
157
|
+
throw new ApiError("Invalid batch get user tasks response: not an array", response.status);
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Lists user tasks with pagination and optional filtering.
|
|
163
|
+
*
|
|
164
|
+
* @param request - List request parameters
|
|
165
|
+
* @param options - Optional configuration
|
|
166
|
+
* @returns Promise resolving to paginated list of tasks
|
|
167
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
168
|
+
* @throws {ApiError} When API request fails
|
|
169
|
+
* @throws {NetworkError} When network request fails
|
|
170
|
+
*/
|
|
171
|
+
export async function listUserTasks(request) {
|
|
172
|
+
const token = request?.appToken ?? (await getAppToken(request));
|
|
173
|
+
const base = getBaseUrl(request);
|
|
174
|
+
const url = new URL(base + "/user-task/v2/tasks");
|
|
175
|
+
const { appToken, baseUrl, ...requestParams } = request || {};
|
|
176
|
+
const queryParams = {
|
|
177
|
+
page: 0,
|
|
178
|
+
size: 20,
|
|
179
|
+
sort: "createdAt,desc",
|
|
180
|
+
...requestParams,
|
|
181
|
+
};
|
|
182
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
183
|
+
if (value !== undefined) {
|
|
184
|
+
url.searchParams.append(key, String(value));
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
let response;
|
|
188
|
+
try {
|
|
189
|
+
response = await globalThis.fetch(url.toString(), {
|
|
190
|
+
method: "GET",
|
|
191
|
+
headers: {
|
|
192
|
+
"Content-Type": "application/json",
|
|
193
|
+
"x-t-token": token,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
throw new NetworkError(`Failed to list user tasks: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
199
|
+
}
|
|
200
|
+
const json = await response.json();
|
|
201
|
+
return parseApiResponse(json, "Failed to list user tasks", response.status);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Updates a user task.
|
|
205
|
+
*
|
|
206
|
+
* @param taskId - Task ID
|
|
207
|
+
* @param request - Update request
|
|
208
|
+
* @param options - Optional configuration
|
|
209
|
+
* @returns Promise resolving to updated task
|
|
210
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
211
|
+
* @throws {ApiError} When API request fails
|
|
212
|
+
* @throws {NetworkError} When network request fails
|
|
213
|
+
*/
|
|
214
|
+
export async function updateUserTask(taskId, request, options) {
|
|
215
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
216
|
+
const base = getBaseUrl(options);
|
|
217
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}`);
|
|
218
|
+
let response;
|
|
219
|
+
try {
|
|
220
|
+
response = await globalThis.fetch(url.toString(), {
|
|
221
|
+
method: "PUT",
|
|
222
|
+
headers: {
|
|
223
|
+
"Content-Type": "application/json",
|
|
224
|
+
"x-t-token": token,
|
|
225
|
+
},
|
|
226
|
+
body: JSON.stringify(request),
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
throw new NetworkError(`Failed to update user task: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
231
|
+
}
|
|
232
|
+
const json = await response.json();
|
|
233
|
+
return parseApiResponse(json, "Failed to update user task", response.status);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Deletes a user task.
|
|
237
|
+
*
|
|
238
|
+
* @param taskId - Task ID
|
|
239
|
+
* @param options - Optional configuration
|
|
240
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
241
|
+
* @throws {ApiError} When API request fails
|
|
242
|
+
* @throws {NetworkError} When network request fails
|
|
243
|
+
*/
|
|
244
|
+
export async function deleteUserTask(taskId, options) {
|
|
245
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
246
|
+
const base = getBaseUrl(options);
|
|
247
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}`);
|
|
248
|
+
let response;
|
|
249
|
+
try {
|
|
250
|
+
response = await globalThis.fetch(url.toString(), {
|
|
251
|
+
method: "DELETE",
|
|
252
|
+
headers: {
|
|
253
|
+
"Content-Type": "application/json",
|
|
254
|
+
"x-t-token": token,
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
throw new NetworkError(`Failed to delete user task: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
260
|
+
}
|
|
261
|
+
const json = await response.json();
|
|
262
|
+
if (typeof json !== "object" || json === null) {
|
|
263
|
+
throw new ApiError("Invalid user task deletion response", response.status);
|
|
264
|
+
}
|
|
265
|
+
const apiResponse = json;
|
|
266
|
+
if (apiResponse.code !== 200) {
|
|
267
|
+
const errorMsg = typeof apiResponse.msg === "string"
|
|
268
|
+
? apiResponse.msg
|
|
269
|
+
: "Failed to delete user task";
|
|
270
|
+
throw new ApiError(errorMsg, response.status, apiResponse.code);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// ==================== Task Input/Output Operations ====================
|
|
274
|
+
/**
|
|
275
|
+
* Gets task input data.
|
|
276
|
+
*
|
|
277
|
+
* @param taskId - Task ID
|
|
278
|
+
* @param options - Optional configuration
|
|
279
|
+
* @returns Promise resolving to task input data
|
|
280
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
281
|
+
* @throws {ApiError} When API request fails
|
|
282
|
+
* @throws {NetworkError} When network request fails
|
|
283
|
+
*/
|
|
284
|
+
export async function getTaskInput(taskId, options) {
|
|
285
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
286
|
+
const base = getBaseUrl(options);
|
|
287
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/input`);
|
|
288
|
+
let response;
|
|
289
|
+
try {
|
|
290
|
+
response = await globalThis.fetch(url.toString(), {
|
|
291
|
+
method: "GET",
|
|
292
|
+
headers: {
|
|
293
|
+
"Content-Type": "application/json",
|
|
294
|
+
"x-t-token": token,
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
throw new NetworkError(`Failed to get task input: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
300
|
+
}
|
|
301
|
+
const json = await response.json();
|
|
302
|
+
return parseApiResponse(json, "Failed to get task input", response.status);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Gets task output data.
|
|
306
|
+
*
|
|
307
|
+
* @param taskId - Task ID
|
|
308
|
+
* @param options - Optional configuration
|
|
309
|
+
* @returns Promise resolving to task output data
|
|
310
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
311
|
+
* @throws {ApiError} When API request fails
|
|
312
|
+
* @throws {NetworkError} When network request fails
|
|
313
|
+
*/
|
|
314
|
+
export async function getTaskOutput(taskId, options) {
|
|
315
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
316
|
+
const base = getBaseUrl(options);
|
|
317
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/output`);
|
|
318
|
+
let response;
|
|
319
|
+
try {
|
|
320
|
+
response = await globalThis.fetch(url.toString(), {
|
|
321
|
+
method: "GET",
|
|
322
|
+
headers: {
|
|
323
|
+
"Content-Type": "application/json",
|
|
324
|
+
"x-t-token": token,
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
throw new NetworkError(`Failed to get task output: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
330
|
+
}
|
|
331
|
+
const json = await response.json();
|
|
332
|
+
return parseApiResponse(json, "Failed to get task output", response.status);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Updates task input data.
|
|
336
|
+
*
|
|
337
|
+
* @param taskId - Task ID
|
|
338
|
+
* @param taskInput - New task input data
|
|
339
|
+
* @param options - Optional configuration
|
|
340
|
+
* @returns Promise resolving to updated task
|
|
341
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
342
|
+
* @throws {ApiError} When API request fails
|
|
343
|
+
* @throws {NetworkError} When network request fails
|
|
344
|
+
*/
|
|
345
|
+
export async function updateTaskInput(taskId, taskInput, options) {
|
|
346
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
347
|
+
const base = getBaseUrl(options);
|
|
348
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/input`);
|
|
349
|
+
let response;
|
|
350
|
+
try {
|
|
351
|
+
response = await globalThis.fetch(url.toString(), {
|
|
352
|
+
method: "PUT",
|
|
353
|
+
headers: {
|
|
354
|
+
"Content-Type": "application/json",
|
|
355
|
+
"x-t-token": token,
|
|
356
|
+
},
|
|
357
|
+
body: JSON.stringify(taskInput),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
throw new NetworkError(`Failed to update task input: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
362
|
+
}
|
|
363
|
+
const json = await response.json();
|
|
364
|
+
return parseApiResponse(json, "Failed to update task input", response.status);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Updates task output data.
|
|
368
|
+
*
|
|
369
|
+
* @param taskId - Task ID
|
|
370
|
+
* @param taskOutput - New task output data
|
|
371
|
+
* @param options - Optional configuration
|
|
372
|
+
* @returns Promise resolving to updated task
|
|
373
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
374
|
+
* @throws {ApiError} When API request fails
|
|
375
|
+
* @throws {NetworkError} When network request fails
|
|
376
|
+
*/
|
|
377
|
+
export async function updateTaskOutput(taskId, taskOutput, options) {
|
|
378
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
379
|
+
const base = getBaseUrl(options);
|
|
380
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/output`);
|
|
381
|
+
let response;
|
|
382
|
+
try {
|
|
383
|
+
response = await globalThis.fetch(url.toString(), {
|
|
384
|
+
method: "PUT",
|
|
385
|
+
headers: {
|
|
386
|
+
"Content-Type": "application/json",
|
|
387
|
+
"x-t-token": token,
|
|
388
|
+
},
|
|
389
|
+
body: JSON.stringify(taskOutput),
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
catch (error) {
|
|
393
|
+
throw new NetworkError(`Failed to update task output: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
394
|
+
}
|
|
395
|
+
const json = await response.json();
|
|
396
|
+
return parseApiResponse(json, "Failed to update task output", response.status);
|
|
397
|
+
}
|
|
398
|
+
// ==================== Task Status Operations ====================
|
|
399
|
+
/**
|
|
400
|
+
* Gets task status.
|
|
401
|
+
*
|
|
402
|
+
* @param taskId - Task ID
|
|
403
|
+
* @param options - Optional configuration
|
|
404
|
+
* @returns Promise resolving to task status
|
|
405
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
406
|
+
* @throws {ApiError} When API request fails
|
|
407
|
+
* @throws {NetworkError} When network request fails
|
|
408
|
+
*/
|
|
409
|
+
export async function getTaskStatus(taskId, options) {
|
|
410
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
411
|
+
const base = getBaseUrl(options);
|
|
412
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/status`);
|
|
413
|
+
let response;
|
|
414
|
+
try {
|
|
415
|
+
response = await globalThis.fetch(url.toString(), {
|
|
416
|
+
method: "GET",
|
|
417
|
+
headers: {
|
|
418
|
+
"Content-Type": "application/json",
|
|
419
|
+
"x-t-token": token,
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
throw new NetworkError(`Failed to get task status: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
425
|
+
}
|
|
426
|
+
const json = await response.json();
|
|
427
|
+
const result = parseApiResponse(json, "Failed to get task status", response.status);
|
|
428
|
+
return result.task_status;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Updates task status.
|
|
432
|
+
*
|
|
433
|
+
* @param taskId - Task ID
|
|
434
|
+
* @param request - Status update request
|
|
435
|
+
* @param options - Optional configuration
|
|
436
|
+
* @returns Promise resolving to updated task
|
|
437
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
438
|
+
* @throws {ApiError} When API request fails
|
|
439
|
+
* @throws {NetworkError} When network request fails
|
|
440
|
+
*/
|
|
441
|
+
export async function updateTaskStatus(taskId, request, options) {
|
|
442
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
443
|
+
const base = getBaseUrl(options);
|
|
444
|
+
const url = new URL(base + `/user-task/v1/tasks/${encodeURIComponent(taskId)}/status`);
|
|
445
|
+
let response;
|
|
446
|
+
try {
|
|
447
|
+
response = await globalThis.fetch(url.toString(), {
|
|
448
|
+
method: "PUT",
|
|
449
|
+
headers: {
|
|
450
|
+
"Content-Type": "application/json",
|
|
451
|
+
"x-t-token": token,
|
|
452
|
+
},
|
|
453
|
+
body: JSON.stringify(request),
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
catch (error) {
|
|
457
|
+
throw new NetworkError(`Failed to update task status: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
458
|
+
}
|
|
459
|
+
const json = await response.json();
|
|
460
|
+
return parseApiResponse(json, "Failed to update task status", response.status);
|
|
461
|
+
}
|
|
462
|
+
// ==================== Task Statistics ====================
|
|
463
|
+
/**
|
|
464
|
+
* Gets task statistics.
|
|
465
|
+
*
|
|
466
|
+
* @param userId - Optional user ID filter
|
|
467
|
+
* @param options - Optional configuration
|
|
468
|
+
* @returns Promise resolving to task statistics
|
|
469
|
+
* @throws {ConfigurationError} When required environment variables are missing
|
|
470
|
+
* @throws {ApiError} When API request fails
|
|
471
|
+
* @throws {NetworkError} When network request fails
|
|
472
|
+
*/
|
|
473
|
+
export async function getTaskStatistics(userId, options) {
|
|
474
|
+
const token = options?.appToken ?? (await getAppToken(options));
|
|
475
|
+
const base = getBaseUrl(options);
|
|
476
|
+
const url = new URL(base + "/user-task/v1/tasks/statistics");
|
|
477
|
+
if (userId !== undefined) {
|
|
478
|
+
url.searchParams.append("user_id", userId);
|
|
479
|
+
}
|
|
480
|
+
let response;
|
|
481
|
+
try {
|
|
482
|
+
response = await globalThis.fetch(url.toString(), {
|
|
483
|
+
method: "GET",
|
|
484
|
+
headers: {
|
|
485
|
+
"Content-Type": "application/json",
|
|
486
|
+
"x-t-token": token,
|
|
487
|
+
},
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
throw new NetworkError(`Failed to get task statistics: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
492
|
+
}
|
|
493
|
+
const json = await response.json();
|
|
494
|
+
return parseApiResponse(json, "Failed to get task statistics", response.status);
|
|
495
|
+
}
|