@elaraai/e3-api-client 0.0.2-beta.4 → 0.0.2-beta.41
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 +2 -2
- package/dist/src/datasets.d.ts +73 -4
- package/dist/src/datasets.d.ts.map +1 -1
- package/dist/src/datasets.js +197 -19
- package/dist/src/datasets.js.map +1 -1
- package/dist/src/executions.d.ts +74 -11
- package/dist/src/executions.d.ts.map +1 -1
- package/dist/src/executions.js +184 -36
- package/dist/src/executions.js.map +1 -1
- package/dist/src/http.d.ts +60 -12
- package/dist/src/http.d.ts.map +1 -1
- package/dist/src/http.js +135 -34
- package/dist/src/http.js.map +1 -1
- package/dist/src/index.d.ts +8 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +10 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/packages.d.ts +12 -27
- package/dist/src/packages.d.ts.map +1 -1
- package/dist/src/packages.js +139 -39
- package/dist/src/packages.js.map +1 -1
- package/dist/src/platform.d.ts +553 -1343
- package/dist/src/platform.d.ts.map +1 -1
- package/dist/src/platform.js +123 -915
- package/dist/src/platform.js.map +1 -1
- package/dist/src/repos.d.ts +16 -0
- package/dist/src/repos.d.ts.map +1 -0
- package/dist/src/repos.js +19 -0
- package/dist/src/repos.js.map +1 -0
- package/dist/src/repository.d.ts +67 -5
- package/dist/src/repository.d.ts.map +1 -1
- package/dist/src/repository.js +94 -10
- package/dist/src/repository.js.map +1 -1
- package/dist/src/tasks.d.ts +25 -3
- package/dist/src/tasks.d.ts.map +1 -1
- package/dist/src/tasks.js +29 -8
- package/dist/src/tasks.js.map +1 -1
- package/dist/src/types.d.ts +1360 -716
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +235 -2
- package/dist/src/types.js.map +1 -1
- package/dist/src/workspaces.d.ts +36 -7
- package/dist/src/workspaces.d.ts.map +1 -1
- package/dist/src/workspaces.js +43 -22
- package/dist/src/workspaces.js.map +1 -1
- package/package.json +4 -4
package/dist/src/executions.js
CHANGED
|
@@ -2,76 +2,224 @@
|
|
|
2
2
|
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
3
|
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
4
|
*/
|
|
5
|
-
import { NullType, none, some } from '@elaraai/east';
|
|
6
|
-
import { LogChunkType, DataflowRequestType, DataflowGraphType,
|
|
7
|
-
import { get, post,
|
|
5
|
+
import { NullType, none, some, variant } from '@elaraai/east';
|
|
6
|
+
import { LogChunkType, DataflowRequestType, DataflowGraphType, DataflowExecutionStateType, } from './types.js';
|
|
7
|
+
import { get, post, ApiError } from './http.js';
|
|
8
8
|
/**
|
|
9
9
|
* Start dataflow execution on a workspace (non-blocking).
|
|
10
10
|
*
|
|
11
11
|
* Returns immediately after spawning execution in background.
|
|
12
|
-
* Use
|
|
12
|
+
* Use dataflowExecutePoll() to poll for progress.
|
|
13
13
|
*
|
|
14
14
|
* @param url - Base URL of the e3 API server
|
|
15
|
+
* @param repo - Repository name
|
|
15
16
|
* @param workspace - Workspace name
|
|
16
|
-
* @param
|
|
17
|
+
* @param dataflowOptions - Execution options
|
|
18
|
+
* @param options - Request options including auth token
|
|
19
|
+
* @throws {ApiError} On application-level errors
|
|
20
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
17
21
|
*/
|
|
18
|
-
export async function
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
export async function dataflowExecuteLaunch(url, repo, workspace, dataflowOptions = {}, options) {
|
|
23
|
+
const maxRetries = 5;
|
|
24
|
+
const baseDelay = 200;
|
|
25
|
+
for (let attempt = 0;; attempt++) {
|
|
26
|
+
try {
|
|
27
|
+
await post(url, `/repos/${encodeURIComponent(repo)}/workspaces/${encodeURIComponent(workspace)}/dataflow`, {
|
|
28
|
+
concurrency: dataflowOptions.concurrency != null ? some(BigInt(dataflowOptions.concurrency)) : none,
|
|
29
|
+
force: dataflowOptions.force ?? false,
|
|
30
|
+
filter: dataflowOptions.filter != null ? some(dataflowOptions.filter) : none,
|
|
31
|
+
}, DataflowRequestType, NullType, options);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
if (err instanceof ApiError && err.code === 'workspace_locked' && attempt < maxRetries) {
|
|
36
|
+
const delay = baseDelay * Math.pow(2, attempt) * (0.5 + Math.random() * 0.5);
|
|
37
|
+
await new Promise(r => setTimeout(r, delay));
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
25
43
|
}
|
|
26
44
|
/**
|
|
27
|
-
*
|
|
45
|
+
* Build DataflowResult from DataflowExecutionState.
|
|
28
46
|
*
|
|
29
|
-
*
|
|
47
|
+
* Converts events into task execution results.
|
|
48
|
+
*/
|
|
49
|
+
function buildDataflowResult(state) {
|
|
50
|
+
const tasks = [];
|
|
51
|
+
// Process events to build task results
|
|
52
|
+
// Events are: start, complete, cached, failed, error, input_unavailable
|
|
53
|
+
for (const event of state.events) {
|
|
54
|
+
switch (event.type) {
|
|
55
|
+
case 'complete':
|
|
56
|
+
tasks.push({
|
|
57
|
+
name: event.value.task,
|
|
58
|
+
cached: false,
|
|
59
|
+
state: variant('success', null),
|
|
60
|
+
duration: event.value.duration,
|
|
61
|
+
});
|
|
62
|
+
break;
|
|
63
|
+
case 'cached':
|
|
64
|
+
tasks.push({
|
|
65
|
+
name: event.value.task,
|
|
66
|
+
cached: true,
|
|
67
|
+
state: variant('success', null),
|
|
68
|
+
duration: 0,
|
|
69
|
+
});
|
|
70
|
+
break;
|
|
71
|
+
case 'failed':
|
|
72
|
+
tasks.push({
|
|
73
|
+
name: event.value.task,
|
|
74
|
+
cached: false,
|
|
75
|
+
state: variant('failed', { exitCode: event.value.exitCode }),
|
|
76
|
+
duration: event.value.duration,
|
|
77
|
+
});
|
|
78
|
+
break;
|
|
79
|
+
case 'error':
|
|
80
|
+
tasks.push({
|
|
81
|
+
name: event.value.task,
|
|
82
|
+
cached: false,
|
|
83
|
+
state: variant('error', { message: event.value.message }),
|
|
84
|
+
duration: 0,
|
|
85
|
+
});
|
|
86
|
+
break;
|
|
87
|
+
case 'input_unavailable':
|
|
88
|
+
tasks.push({
|
|
89
|
+
name: event.value.task,
|
|
90
|
+
cached: false,
|
|
91
|
+
state: variant('skipped', null),
|
|
92
|
+
duration: 0,
|
|
93
|
+
});
|
|
94
|
+
break;
|
|
95
|
+
// 'start' events don't create task results - they're tracked separately
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Get summary from state or calculate from tasks
|
|
99
|
+
const summary = state.summary.type === 'some' ? state.summary.value : {
|
|
100
|
+
executed: BigInt(tasks.filter(t => !t.cached && t.state.type === 'success').length),
|
|
101
|
+
cached: BigInt(tasks.filter(t => t.cached).length),
|
|
102
|
+
failed: BigInt(tasks.filter(t => t.state.type === 'failed' || t.state.type === 'error').length),
|
|
103
|
+
skipped: BigInt(tasks.filter(t => t.state.type === 'skipped').length),
|
|
104
|
+
duration: 0,
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
success: state.status.type === 'completed',
|
|
108
|
+
executed: summary.executed,
|
|
109
|
+
cached: summary.cached,
|
|
110
|
+
failed: summary.failed,
|
|
111
|
+
skipped: summary.skipped,
|
|
112
|
+
tasks,
|
|
113
|
+
duration: summary.duration,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Execute dataflow on a workspace with client-side polling.
|
|
118
|
+
*
|
|
119
|
+
* Starts execution, polls until complete, and returns the result.
|
|
30
120
|
*
|
|
31
121
|
* @param url - Base URL of the e3 API server
|
|
122
|
+
* @param repo - Repository name
|
|
32
123
|
* @param workspace - Workspace name
|
|
33
|
-
* @param
|
|
124
|
+
* @param dataflowOptions - Execution options
|
|
125
|
+
* @param options - Request options including auth token
|
|
126
|
+
* @param pollOptions - Polling configuration
|
|
34
127
|
* @returns Dataflow execution result
|
|
35
128
|
*/
|
|
36
|
-
export async function dataflowExecute(url, workspace, options = {}) {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
129
|
+
export async function dataflowExecute(url, repo, workspace, dataflowOptions = {}, options, pollOptions = {}) {
|
|
130
|
+
const { pollInterval = 500, timeout = 300000 } = pollOptions;
|
|
131
|
+
// Start execution
|
|
132
|
+
await dataflowExecuteLaunch(url, repo, workspace, dataflowOptions, options);
|
|
133
|
+
// Poll until complete
|
|
134
|
+
const startTime = Date.now();
|
|
135
|
+
while (Date.now() - startTime < timeout) {
|
|
136
|
+
const state = await dataflowExecutePoll(url, repo, workspace, {}, options);
|
|
137
|
+
if (state.status.type === 'completed' || state.status.type === 'failed' || state.status.type === 'aborted') {
|
|
138
|
+
return buildDataflowResult(state);
|
|
139
|
+
}
|
|
140
|
+
await new Promise(r => setTimeout(r, pollInterval));
|
|
141
|
+
}
|
|
142
|
+
throw new Error('Dataflow execution timed out');
|
|
43
143
|
}
|
|
144
|
+
// Backward compatibility alias
|
|
145
|
+
export { dataflowExecuteLaunch as dataflowStart };
|
|
44
146
|
/**
|
|
45
147
|
* Get the dependency graph for a workspace.
|
|
46
148
|
*
|
|
47
149
|
* @param url - Base URL of the e3 API server
|
|
150
|
+
* @param repo - Repository name
|
|
48
151
|
* @param workspace - Workspace name
|
|
152
|
+
* @param options - Request options including auth token
|
|
49
153
|
* @returns Dataflow graph with tasks and dependencies
|
|
154
|
+
* @throws {ApiError} On application-level errors
|
|
155
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
50
156
|
*/
|
|
51
|
-
export async function dataflowGraph(url, workspace) {
|
|
52
|
-
|
|
53
|
-
return unwrap(response);
|
|
157
|
+
export async function dataflowGraph(url, repo, workspace, options) {
|
|
158
|
+
return get(url, `/repos/${encodeURIComponent(repo)}/workspaces/${encodeURIComponent(workspace)}/dataflow/graph`, DataflowGraphType, options);
|
|
54
159
|
}
|
|
55
160
|
/**
|
|
56
161
|
* Read task logs from a workspace.
|
|
57
162
|
*
|
|
58
163
|
* @param url - Base URL of the e3 API server
|
|
164
|
+
* @param repo - Repository name
|
|
59
165
|
* @param workspace - Workspace name
|
|
60
166
|
* @param task - Task name
|
|
61
|
-
* @param
|
|
167
|
+
* @param logOptions - Log reading options
|
|
168
|
+
* @param options - Request options including auth token
|
|
62
169
|
* @returns Log chunk with data and metadata
|
|
170
|
+
* @throws {ApiError} On application-level errors
|
|
171
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
172
|
+
*/
|
|
173
|
+
export async function taskLogs(url, repo, workspace, task, logOptions = {}, options) {
|
|
174
|
+
const params = new URLSearchParams();
|
|
175
|
+
if (logOptions.stream)
|
|
176
|
+
params.set('stream', logOptions.stream);
|
|
177
|
+
if (logOptions.offset != null)
|
|
178
|
+
params.set('offset', String(logOptions.offset));
|
|
179
|
+
if (logOptions.limit != null)
|
|
180
|
+
params.set('limit', String(logOptions.limit));
|
|
181
|
+
const query = params.toString();
|
|
182
|
+
const path = `/repos/${encodeURIComponent(repo)}/workspaces/${encodeURIComponent(workspace)}/dataflow/logs/${encodeURIComponent(task)}${query ? `?${query}` : ''}`;
|
|
183
|
+
return get(url, path, LogChunkType, options);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Get dataflow execution state (for polling).
|
|
187
|
+
*
|
|
188
|
+
* Returns the current execution state including events for progress tracking.
|
|
189
|
+
* Use offset/limit for pagination of events.
|
|
190
|
+
*
|
|
191
|
+
* @param url - Base URL of the e3 API server
|
|
192
|
+
* @param repo - Repository name
|
|
193
|
+
* @param workspace - Workspace name
|
|
194
|
+
* @param stateOptions - Pagination options for events
|
|
195
|
+
* @param options - Request options including auth token
|
|
196
|
+
* @returns Execution state with events and summary
|
|
197
|
+
* @throws {ApiError} On application-level errors
|
|
198
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
63
199
|
*/
|
|
64
|
-
export async function
|
|
200
|
+
export async function dataflowExecutePoll(url, repo, workspace, stateOptions = {}, options) {
|
|
65
201
|
const params = new URLSearchParams();
|
|
66
|
-
if (
|
|
67
|
-
params.set('
|
|
68
|
-
if (
|
|
69
|
-
params.set('
|
|
70
|
-
if (options.limit != null)
|
|
71
|
-
params.set('limit', String(options.limit));
|
|
202
|
+
if (stateOptions.offset != null)
|
|
203
|
+
params.set('offset', String(stateOptions.offset));
|
|
204
|
+
if (stateOptions.limit != null)
|
|
205
|
+
params.set('limit', String(stateOptions.limit));
|
|
72
206
|
const query = params.toString();
|
|
73
|
-
const path = `/
|
|
74
|
-
|
|
75
|
-
|
|
207
|
+
const path = `/repos/${encodeURIComponent(repo)}/workspaces/${encodeURIComponent(workspace)}/dataflow/execution${query ? `?${query}` : ''}`;
|
|
208
|
+
return get(url, path, DataflowExecutionStateType, options);
|
|
209
|
+
}
|
|
210
|
+
// Backward compatibility alias
|
|
211
|
+
export { dataflowExecutePoll as dataflowExecution };
|
|
212
|
+
/**
|
|
213
|
+
* Cancel a running dataflow execution.
|
|
214
|
+
*
|
|
215
|
+
* @param url - Base URL of the API server
|
|
216
|
+
* @param repo - Repository name
|
|
217
|
+
* @param workspace - Workspace name
|
|
218
|
+
* @param options - Request options (token, etc.)
|
|
219
|
+
* @throws {ApiError} If cancellation fails or no execution is running
|
|
220
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
221
|
+
*/
|
|
222
|
+
export async function dataflowCancel(url, repo, workspace, options) {
|
|
223
|
+
await post(url, `/repos/${encodeURIComponent(repo)}/workspaces/${encodeURIComponent(workspace)}/dataflow/cancel`, null, NullType, NullType, options);
|
|
76
224
|
}
|
|
77
225
|
//# sourceMappingURL=executions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executions.js","sourceRoot":"","sources":["../../src/executions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;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EAAE,CAAC;AAEpD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,GAAW,EACX,IAAY,EACZ,SAAiB,EACjB,OAAuB;IAEvB,MAAM,IAAI,CACR,GAAG,EACH,UAAU,kBAAkB,CAAC,IAAI,CAAC,eAAe,kBAAkB,CAAC,SAAS,CAAC,kBAAkB,EAChG,IAAI,EACJ,QAAQ,EACR,QAAQ,EACR,OAAO,CACR,CAAC;AACJ,CAAC"}
|
package/dist/src/http.d.ts
CHANGED
|
@@ -4,6 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { EastType, ValueTypeOf } from '@elaraai/east';
|
|
6
6
|
import { ErrorType } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* API response wrapper - success or typed error.
|
|
9
|
+
*
|
|
10
|
+
* This type mirrors the BEAST2 wire format (ResponseType from types.ts) used for
|
|
11
|
+
* HTTP 200 responses. The server sends either { type: 'success', value } or
|
|
12
|
+
* { type: 'error', value } as BEAST2-encoded payloads.
|
|
13
|
+
*
|
|
14
|
+
* Note: Client functions now throw ApiError instead of returning this type directly.
|
|
15
|
+
* This type is still used internally for decoding responses and is exported for
|
|
16
|
+
* cases where callers need to work with the wire format directly.
|
|
17
|
+
*/
|
|
7
18
|
export type Response<T> = {
|
|
8
19
|
type: 'success';
|
|
9
20
|
value: T;
|
|
@@ -11,32 +22,69 @@ export type Response<T> = {
|
|
|
11
22
|
type: 'error';
|
|
12
23
|
value: ValueTypeOf<typeof ErrorType>;
|
|
13
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Request options for authenticated API calls.
|
|
27
|
+
*
|
|
28
|
+
* The token is mandatory to ensure callers explicitly handle authentication (or not).
|
|
29
|
+
*/
|
|
30
|
+
export interface RequestOptions {
|
|
31
|
+
/** Bearer token for authentication (optional depending on server) */
|
|
32
|
+
token: string | null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* API error with typed error details.
|
|
36
|
+
*/
|
|
37
|
+
export declare class ApiError extends Error {
|
|
38
|
+
readonly code: string;
|
|
39
|
+
readonly details?: unknown | undefined;
|
|
40
|
+
constructor(code: string, details?: unknown | undefined);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Authentication error (401 response).
|
|
44
|
+
*/
|
|
45
|
+
export declare class AuthError extends Error {
|
|
46
|
+
constructor(message: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Fetch with consistent auth header injection and 401 → AuthError handling.
|
|
50
|
+
*
|
|
51
|
+
* Use this for multi-step transfer flows where raw `fetch` is needed
|
|
52
|
+
* (e.g. upload/download to presigned URLs, polling endpoints).
|
|
53
|
+
*/
|
|
54
|
+
export declare function fetchWithAuth(input: string, init: RequestInit, options: RequestOptions): Promise<globalThis.Response>;
|
|
14
55
|
/**
|
|
15
56
|
* Make a GET request and decode BEAST2 response.
|
|
57
|
+
* @throws {ApiError} On application-level errors
|
|
58
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
16
59
|
*/
|
|
17
|
-
export declare function get<T extends EastType>(url: string, path: string, successType: T): Promise<
|
|
60
|
+
export declare function get<T extends EastType>(url: string, path: string, successType: T, options: RequestOptions): Promise<ValueTypeOf<T>>;
|
|
18
61
|
/**
|
|
19
62
|
* Make a POST request with BEAST2 body and decode BEAST2 response.
|
|
63
|
+
* @throws {ApiError} On application-level errors
|
|
64
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
20
65
|
*/
|
|
21
|
-
export declare function post<Req extends EastType, Res extends EastType>(url: string, path: string, body: ValueTypeOf<Req>, requestType: Req, successType: Res): Promise<
|
|
66
|
+
export declare function post<Req extends EastType, Res extends EastType>(url: string, path: string, body: ValueTypeOf<Req>, requestType: Req, successType: Res, options: RequestOptions): Promise<ValueTypeOf<Res>>;
|
|
22
67
|
/**
|
|
23
68
|
* Make a PUT request with BEAST2 body and decode BEAST2 response.
|
|
69
|
+
* @throws {ApiError} On application-level errors
|
|
70
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
24
71
|
*/
|
|
25
|
-
export declare function put<Req extends EastType, Res extends EastType>(url: string, path: string, body: ValueTypeOf<Req>, requestType: Req, successType: Res): Promise<
|
|
72
|
+
export declare function put<Req extends EastType, Res extends EastType>(url: string, path: string, body: ValueTypeOf<Req>, requestType: Req, successType: Res, options: RequestOptions): Promise<ValueTypeOf<Res>>;
|
|
26
73
|
/**
|
|
27
74
|
* Make a DELETE request and decode BEAST2 response.
|
|
75
|
+
* @throws {ApiError} On application-level errors
|
|
76
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
28
77
|
*/
|
|
29
|
-
export declare function del<T extends EastType>(url: string, path: string, successType: T): Promise<
|
|
78
|
+
export declare function del<T extends EastType>(url: string, path: string, successType: T, options: RequestOptions): Promise<ValueTypeOf<T>>;
|
|
30
79
|
/**
|
|
31
|
-
*
|
|
80
|
+
* Make a PUT request without body and decode BEAST2 response.
|
|
81
|
+
* @throws {ApiError} On application-level errors
|
|
82
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
32
83
|
*/
|
|
33
|
-
export declare function
|
|
84
|
+
export declare function putEmpty<T extends EastType>(url: string, path: string, successType: T, options: RequestOptions): Promise<ValueTypeOf<T>>;
|
|
34
85
|
/**
|
|
35
|
-
*
|
|
86
|
+
* Unwrap a response, throwing on error.
|
|
87
|
+
* @deprecated Functions now throw ApiError on error; this function is no longer needed.
|
|
36
88
|
*/
|
|
37
|
-
export declare
|
|
38
|
-
readonly code: string;
|
|
39
|
-
readonly details: unknown;
|
|
40
|
-
constructor(code: string, details: unknown);
|
|
41
|
-
}
|
|
89
|
+
export declare function unwrap<T>(response: Response<T>): T;
|
|
42
90
|
//# sourceMappingURL=http.d.ts.map
|
package/dist/src/http.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE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elaraai/east';
|
|
6
|
+
import { BEAST2_CONTENT_TYPE } from '@elaraai/e3-core';
|
|
6
7
|
import { ResponseType } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* API error with typed error details.
|
|
10
|
+
*/
|
|
11
|
+
export class ApiError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
details;
|
|
14
|
+
constructor(code, details) {
|
|
15
|
+
super(`API error: ${code}`);
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.details = details;
|
|
18
|
+
this.name = 'ApiError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Authentication error (401 response).
|
|
23
|
+
*/
|
|
24
|
+
export class AuthError extends Error {
|
|
25
|
+
constructor(message) {
|
|
26
|
+
super(`Authentication failed: ${message}`);
|
|
27
|
+
this.name = 'AuthError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Fetch with consistent auth header injection and 401 → AuthError handling.
|
|
32
|
+
*
|
|
33
|
+
* Use this for multi-step transfer flows where raw `fetch` is needed
|
|
34
|
+
* (e.g. upload/download to presigned URLs, polling endpoints).
|
|
35
|
+
*/
|
|
36
|
+
export async function fetchWithAuth(input, init, options) {
|
|
37
|
+
const headers = { ...init.headers };
|
|
38
|
+
if (options.token) {
|
|
39
|
+
headers['Authorization'] = `Bearer ${options.token}`;
|
|
40
|
+
}
|
|
41
|
+
const response = await fetch(input, { ...init, headers });
|
|
42
|
+
if (response.status === 401) {
|
|
43
|
+
throw new AuthError(await response.text());
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parse error details from response text.
|
|
49
|
+
* Returns an ApiError with the error code from the body if available, otherwise uses fallback.
|
|
50
|
+
*/
|
|
51
|
+
function parseErrorBody(text, fallbackCode) {
|
|
52
|
+
try {
|
|
53
|
+
const json = JSON.parse(text);
|
|
54
|
+
if (json.error?.type) {
|
|
55
|
+
return new ApiError(json.error.type, json.error.message);
|
|
56
|
+
}
|
|
57
|
+
if (json.message) {
|
|
58
|
+
return new ApiError(fallbackCode, json.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// Not JSON
|
|
63
|
+
}
|
|
64
|
+
return new ApiError(fallbackCode, text || undefined);
|
|
65
|
+
}
|
|
66
|
+
/** HTTP status code to error code mapping */
|
|
67
|
+
const STATUS_CODES = {
|
|
68
|
+
400: 'bad_request',
|
|
69
|
+
401: 'unauthorized',
|
|
70
|
+
403: 'forbidden',
|
|
71
|
+
404: 'not_found',
|
|
72
|
+
405: 'method_not_allowed',
|
|
73
|
+
409: 'conflict',
|
|
74
|
+
415: 'unsupported_media_type',
|
|
75
|
+
422: 'unprocessable_entity',
|
|
76
|
+
429: 'too_many_requests',
|
|
77
|
+
500: 'internal_server_error',
|
|
78
|
+
502: 'bad_gateway',
|
|
79
|
+
503: 'service_unavailable',
|
|
80
|
+
504: 'gateway_timeout',
|
|
81
|
+
};
|
|
7
82
|
/**
|
|
8
83
|
* Make a GET request and decode BEAST2 response.
|
|
84
|
+
* @throws {ApiError} On application-level errors
|
|
85
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
9
86
|
*/
|
|
10
|
-
export async function get(url, path, successType) {
|
|
11
|
-
const response = await fetch(`${url}${path}`, {
|
|
87
|
+
export async function get(url, path, successType, options) {
|
|
88
|
+
const response = await fetch(`${url}/api${path}`, {
|
|
12
89
|
method: 'GET',
|
|
13
90
|
headers: {
|
|
14
|
-
'Accept':
|
|
91
|
+
'Accept': BEAST2_CONTENT_TYPE,
|
|
92
|
+
...(options.token ? { 'Authorization': `Bearer ${options.token}` } : {}),
|
|
15
93
|
},
|
|
16
94
|
});
|
|
17
95
|
return decodeResponse(response, successType);
|
|
18
96
|
}
|
|
19
97
|
/**
|
|
20
98
|
* Make a POST request with BEAST2 body and decode BEAST2 response.
|
|
99
|
+
* @throws {ApiError} On application-level errors
|
|
100
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
21
101
|
*/
|
|
22
|
-
export async function post(url, path, body, requestType, successType) {
|
|
102
|
+
export async function post(url, path, body, requestType, successType, options) {
|
|
23
103
|
const encode = encodeBeast2For(requestType);
|
|
24
|
-
const response = await fetch(`${url}${path}`, {
|
|
104
|
+
const response = await fetch(`${url}/api${path}`, {
|
|
25
105
|
method: 'POST',
|
|
26
106
|
headers: {
|
|
27
|
-
'Content-Type':
|
|
28
|
-
'Accept':
|
|
107
|
+
'Content-Type': BEAST2_CONTENT_TYPE,
|
|
108
|
+
'Accept': BEAST2_CONTENT_TYPE,
|
|
109
|
+
...(options.token ? { 'Authorization': `Bearer ${options.token}` } : {}),
|
|
29
110
|
},
|
|
30
111
|
body: encode(body),
|
|
31
112
|
});
|
|
@@ -33,14 +114,17 @@ export async function post(url, path, body, requestType, successType) {
|
|
|
33
114
|
}
|
|
34
115
|
/**
|
|
35
116
|
* Make a PUT request with BEAST2 body and decode BEAST2 response.
|
|
117
|
+
* @throws {ApiError} On application-level errors
|
|
118
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
36
119
|
*/
|
|
37
|
-
export async function put(url, path, body, requestType, successType) {
|
|
120
|
+
export async function put(url, path, body, requestType, successType, options) {
|
|
38
121
|
const encode = encodeBeast2For(requestType);
|
|
39
|
-
const response = await fetch(`${url}${path}`, {
|
|
122
|
+
const response = await fetch(`${url}/api${path}`, {
|
|
40
123
|
method: 'PUT',
|
|
41
124
|
headers: {
|
|
42
|
-
'Content-Type':
|
|
43
|
-
'Accept':
|
|
125
|
+
'Content-Type': BEAST2_CONTENT_TYPE,
|
|
126
|
+
'Accept': BEAST2_CONTENT_TYPE,
|
|
127
|
+
...(options.token ? { 'Authorization': `Bearer ${options.token}` } : {}),
|
|
44
128
|
},
|
|
45
129
|
body: encode(body),
|
|
46
130
|
});
|
|
@@ -48,32 +132,62 @@ export async function put(url, path, body, requestType, successType) {
|
|
|
48
132
|
}
|
|
49
133
|
/**
|
|
50
134
|
* Make a DELETE request and decode BEAST2 response.
|
|
135
|
+
* @throws {ApiError} On application-level errors
|
|
136
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
51
137
|
*/
|
|
52
|
-
export async function del(url, path, successType) {
|
|
53
|
-
const response = await fetch(`${url}${path}`, {
|
|
138
|
+
export async function del(url, path, successType, options) {
|
|
139
|
+
const response = await fetch(`${url}/api${path}`, {
|
|
54
140
|
method: 'DELETE',
|
|
55
141
|
headers: {
|
|
56
|
-
'Accept':
|
|
142
|
+
'Accept': BEAST2_CONTENT_TYPE,
|
|
143
|
+
...(options.token ? { 'Authorization': `Bearer ${options.token}` } : {}),
|
|
57
144
|
},
|
|
58
145
|
});
|
|
59
146
|
return decodeResponse(response, successType);
|
|
60
147
|
}
|
|
61
148
|
/**
|
|
62
|
-
*
|
|
149
|
+
* Make a PUT request without body and decode BEAST2 response.
|
|
150
|
+
* @throws {ApiError} On application-level errors
|
|
151
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
152
|
+
*/
|
|
153
|
+
export async function putEmpty(url, path, successType, options) {
|
|
154
|
+
const response = await fetch(`${url}/api${path}`, {
|
|
155
|
+
method: 'PUT',
|
|
156
|
+
headers: {
|
|
157
|
+
'Accept': BEAST2_CONTENT_TYPE,
|
|
158
|
+
...(options.token ? { 'Authorization': `Bearer ${options.token}` } : {}),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
return decodeResponse(response, successType);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Decode a BEAST2 response, throwing on errors.
|
|
165
|
+
* @throws {ApiError} On application-level errors (including BEAST2 error responses)
|
|
166
|
+
* @throws {AuthError} On 401 Unauthorized
|
|
63
167
|
*/
|
|
64
168
|
async function decodeResponse(response, successType) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
169
|
+
// Handle HTTP-level errors
|
|
170
|
+
if (!response.ok) {
|
|
171
|
+
const text = await response.text();
|
|
172
|
+
const error = parseErrorBody(text, STATUS_CODES[response.status] ?? `http_${response.status}`);
|
|
173
|
+
if (response.status === 401) {
|
|
174
|
+
throw new AuthError(error.details ?? 'Authentication required');
|
|
175
|
+
}
|
|
176
|
+
throw error;
|
|
70
177
|
}
|
|
178
|
+
// Decode BEAST2 response
|
|
71
179
|
const buffer = await response.arrayBuffer();
|
|
72
180
|
const decode = decodeBeast2For(ResponseType(successType));
|
|
73
|
-
|
|
181
|
+
const result = decode(new Uint8Array(buffer));
|
|
182
|
+
// Handle application-level errors in BEAST2 response
|
|
183
|
+
if (result.type === 'error') {
|
|
184
|
+
throw new ApiError(result.value.type, result.value.value);
|
|
185
|
+
}
|
|
186
|
+
return result.value;
|
|
74
187
|
}
|
|
75
188
|
/**
|
|
76
189
|
* Unwrap a response, throwing on error.
|
|
190
|
+
* @deprecated Functions now throw ApiError on error; this function is no longer needed.
|
|
77
191
|
*/
|
|
78
192
|
export function unwrap(response) {
|
|
79
193
|
if (response.type === 'error') {
|
|
@@ -82,17 +196,4 @@ export function unwrap(response) {
|
|
|
82
196
|
}
|
|
83
197
|
return response.value;
|
|
84
198
|
}
|
|
85
|
-
/**
|
|
86
|
-
* API error with typed error details.
|
|
87
|
-
*/
|
|
88
|
-
export class ApiError extends Error {
|
|
89
|
-
code;
|
|
90
|
-
details;
|
|
91
|
-
constructor(code, details) {
|
|
92
|
-
super(`API error: ${code}`);
|
|
93
|
-
this.code = code;
|
|
94
|
-
this.details = details;
|
|
95
|
-
this.name = 'ApiError';
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
199
|
//# sourceMappingURL=http.js.map
|
package/dist/src/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEjE,OAAO,EAAE,YAAY,EAAa,MAAM,YAAY,CAAC;
|
|
1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