Skip to content

Commit a9b92f1

Browse files
author
Lucas
committed
fix: improved api client for first gui draft
1 parent 013e058 commit a9b92f1

File tree

8 files changed

+42
-28
lines changed

8 files changed

+42
-28
lines changed

libs/clients/client.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { assert, assertExists } from "../../deps.ts";
1+
import { assert } from "../../deps.ts";
2+
import { ApiKeyRequiredError } from "../errors.ts";
23

34
export type FnsConfig = {
45
dev?: boolean;
@@ -27,7 +28,7 @@ export class BaseClient {
2728
body?: unknown,
2829
idempotencyKey?: string | null,
2930
): Promise<T> {
30-
assertExists(this.options.apiKey, "apiKey is required");
31+
if (!this.options.apiKey) throw new ApiKeyRequiredError();
3132
assert(typeof this.options.apiKey === "string", "apiKey must be a string");
3233
const parsedURL = url instanceof URL
3334
? url
@@ -42,7 +43,10 @@ export class BaseClient {
4243
},
4344
body: body ? JSON.stringify(body) : null,
4445
});
45-
if (!res.ok) throw new Error(`Failed to fetch ${parsedURL.pathname}`);
46+
if (!res.ok) {
47+
const error = await res.text();
48+
throw new Error(error);
49+
}
4650
return await res.json() as T;
4751
}
4852
}

libs/clients/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ export class ErrorsClient extends BaseClient {
2424
/**
2525
* List all errors of a run
2626
* @example
27-
* const errors = await fns.errors.list({ id: "..." })
27+
* const errors = await fns.errors.list({ execution_id: "...", run_id: "..." });
2828
*/
2929
list(params: ErrorsListParams): Promise<Pagination<Error>> {
3030
const url = new URL(
31-
`/api/v1/errors/${params.execution_id}/${params.run_id}`,
31+
`/v1/errors/${params.execution_id}/${params.run_id}`,
3232
this.options.baseUrl,
3333
);
3434
if (params.limit) {

libs/clients/executions.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ export type Execution = {
1414
created_at: string;
1515
updated_at: string;
1616
};
17+
export type ExecutionRetrieveParams = {
18+
id: string;
19+
};
1720
export type ExecutionInvokeParams = {
1821
id?: string;
1922
name: string;
20-
data: unknown;
23+
data?: unknown | null;
2124
wait?: boolean;
2225
idempotencyKey?: string;
2326
};
@@ -59,7 +62,7 @@ export class ExecutionsClient extends BaseClient {
5962
* const executions = await fns.executions.list()
6063
*/
6164
list(params?: ExecutionListParams): Promise<Pagination<Execution>> {
62-
const url = new URL("/api/v1/executions", this.options.baseUrl);
65+
const url = new URL("/v1/executions", this.options.baseUrl);
6366
if (params) {
6467
if (params.limit) {
6568
url.searchParams.set("limit", String(params.limit));
@@ -80,10 +83,10 @@ export class ExecutionsClient extends BaseClient {
8083
/**
8184
* Retrieve a specific execution by its ID.
8285
* @example
83-
* const execution = await fns.executions.retrieve("id")
86+
* const execution = await fns.executions.retrieve({ id: "..." })
8487
*/
85-
retrieve(id: string): Promise<Execution> {
86-
return this.request<Execution>(`/api/v1/executions/${id}`, "GET");
88+
retrieve(params: ExecutionRetrieveParams): Promise<Execution> {
89+
return this.request<Execution>(`/v1/executions/${params.id}`, "GET");
8790
}
8891

8992
/**
@@ -99,7 +102,7 @@ export class ExecutionsClient extends BaseClient {
99102
params: ExecutionInvokeParams,
100103
): Promise<T | Execution> {
101104
const execution = await this.request<Execution>(
102-
"/api/v1/executions",
105+
"/v1/executions",
103106
"POST",
104107
params,
105108
);
@@ -116,7 +119,7 @@ export class ExecutionsClient extends BaseClient {
116119
*/
117120
trigger(params: ExecutionTriggerParams): Promise<void> {
118121
return this.request<void>(
119-
`/api/v1/executions/${params.id}/trigger`,
122+
`/v1/executions/${params.id}/trigger`,
120123
"POST",
121124
params.data ?? null,
122125
params.idempotencyKey,
@@ -130,19 +133,19 @@ export class ExecutionsClient extends BaseClient {
130133
*/
131134
abort(params: ExecutionAbortParams): Promise<Execution> {
132135
return this.request<Execution>(
133-
`/api/v1/executions/${params.id}/abort`,
136+
`/v1/executions/${params.id}/abort`,
134137
"POST",
135138
);
136139
}
137140

138141
/**
139142
* Pause an execution.
140143
* @example
141-
* const execution = await fns.executions.pause("id")
144+
* const execution = await fns.executions.pause({ id: "..." })
142145
*/
143146
pause(params: ExecutionPauseParams): Promise<Execution> {
144147
return this.request<Execution>(
145-
`/api/v1/executions/${params.id}/pause`,
148+
`/v1/executions/${params.id}/pause`,
146149
"POST",
147150
null,
148151
params.idempotencyKey,
@@ -152,11 +155,11 @@ export class ExecutionsClient extends BaseClient {
152155
/**
153156
* Resume an execution.
154157
* @example
155-
* const execution = await fns.executions.resume("id")
158+
* const execution = await fns.executions.resume({ id: "..." })
156159
*/
157160
resume(params: ExecutionResumeParams): Promise<Execution> {
158161
return this.request<Execution>(
159-
`/api/v1/executions/${params.id}/resume`,
162+
`/v1/executions/${params.id}/resume`,
160163
"POST",
161164
null,
162165
params.idempotencyKey,
@@ -166,10 +169,10 @@ export class ExecutionsClient extends BaseClient {
166169
/**
167170
* Get or wait for the result of an execution.
168171
* @example
169-
* const result = await fns.executions.result("id")
172+
* const result = await fns.executions.result({ id: "..." })
170173
*/
171174
result<T = unknown>(params: ExecutionResultParams): Promise<T> {
172-
return this.request<T>(`/api/v1/executions/${params.id}/result`, "GET");
175+
return this.request<T>(`/v1/executions/${params.id}/result`, "GET");
173176
}
174177

175178
/**
@@ -178,6 +181,6 @@ export class ExecutionsClient extends BaseClient {
178181
* const data = await fns.executions.data({ id: "..." })
179182
*/
180183
data<T = unknown>(params: ExecutionDataParams): Promise<T> {
181-
return this.request<T>(`/api/v1/executions/${params.id}/data`, "GET");
184+
return this.request<T>(`/v1/executions/${params.id}/data`, "GET");
182185
}
183186
}

libs/clients/queries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class QueriesClient extends BaseClient {
3636
*/
3737
list(params: QueryListParams): Promise<Pagination<Query>> {
3838
const url = new URL(
39-
`/api/v1/queries/${params.execution_id}`,
39+
`/v1/queries/${params.execution_id}`,
4040
this.options.baseUrl,
4141
);
4242
if (params.limit) {
@@ -55,7 +55,7 @@ export class QueriesClient extends BaseClient {
5555
*/
5656
retrieve<T = unknown>(params: QueryRetrieveParams): Promise<QueryValue<T>> {
5757
return this.request<QueryValue<T>>(
58-
`/api/v1/queries/${params.id}/${params.query}`,
58+
`/v1/queries/${params.id}/${params.query}`,
5959
"GET",
6060
);
6161
}

libs/clients/runs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class RunsClient extends BaseClient {
3232
*/
3333
list(params: RunListParams): Promise<Pagination<Run>> {
3434
const url = new URL(
35-
`/api/v1/runs/${params.execution_id}`,
35+
`/v1/runs/${params.execution_id}`,
3636
this.options.baseUrl,
3737
);
3838
if (params.limit) {
@@ -51,7 +51,7 @@ export class RunsClient extends BaseClient {
5151
*/
5252
retrieve(params: RunRetrieveParams): Promise<Run> {
5353
return this.request<Run>(
54-
`/api/v1/runs/${params.execution_id}/${params.id}`,
54+
`/v1/runs/${params.execution_id}/${params.id}`,
5555
"GET",
5656
);
5757
}

libs/clients/steps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class StepsClient extends BaseClient {
3737
*/
3838
list(params: StepListParams): Promise<Pagination<Step>> {
3939
const url = new URL(
40-
`/api/v1/steps/${params.execution_id}`,
40+
`/v1/steps/${params.execution_id}`,
4141
this.options.baseUrl,
4242
);
4343
if (params.limit) {
@@ -56,7 +56,7 @@ export class StepsClient extends BaseClient {
5656
*/
5757
retrieve(params: StepRetrieveParams): Promise<Step> {
5858
return this.request<Step>(
59-
`/api/v1/steps/${params.execution_id}/${params.id}`,
59+
`/v1/steps/${params.execution_id}/${params.id}`,
6060
"GET",
6161
);
6262
}

libs/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export class SigningKeyRequiredError extends NonRetriableError {
2626
this.name = "SigningKeyRequiredError";
2727
}
2828
}
29+
export class ApiKeyRequiredError extends Error {
30+
constructor(message?: string) {
31+
super(message);
32+
Object.setPrototypeOf(this, ApiKeyRequiredError.prototype);
33+
this.name = "ApiKeyRequiredError";
34+
}
35+
}
2936
export class FunctionNotFoundError extends NonRetriableError {
3037
constructor(message?: string) {
3138
super(message);

libs/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ export class Fns {
9191
): Promise<FnsResponse> {
9292
const definition = this.definitions.get(event.name);
9393
if (!definition) throw new Error(`Function ${event.name} not found`);
94-
if (definition.version !== event.version) {
94+
/*if (definition.version !== event.version) {
9595
throw new Error(
9696
`Function ${event.name} version ${definition.version} mismatch with request version ${event.version}`,
9797
);
98-
}
98+
}*/
9999
const output = await this.engine(event, definition.fn, abortSignal);
100100
return output as FnsResponse;
101101
}

0 commit comments

Comments
 (0)