Skip to content

Commit ad7d81d

Browse files
author
Lucas
committed
refactor: iterative improvement of the API to match the server
1 parent ba790c2 commit ad7d81d

File tree

6 files changed

+96
-129
lines changed

6 files changed

+96
-129
lines changed

libs/clients/client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ export type FnsConfig = {
77
apiKey?: string;
88
fetch?: typeof fetch;
99
};
10-
export type Pagined<T> = {
11-
data: T[];
10+
export type Pagination<T> = {
11+
results: T[];
1212
hasMore: boolean;
1313
cursor: string;
1414
};
1515
export type PaginationParams<T> = T & {
16-
ending_before?: string;
17-
starting_after?: string;
16+
cursor?: string;
1817
limit?: number;
1918
};
2019
export class BaseClient {

libs/clients/errors.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
BaseClient,
33
type FnsConfig,
44
type PaginationParams,
5-
type Pagined,
5+
type Pagination,
66
} from "./client.ts";
77

88
type Error = {
@@ -26,18 +26,17 @@ export class ErrorsClient extends BaseClient {
2626
* @example
2727
* const errors = await fns.errors.list({ id: "..." })
2828
*/
29-
list(params: ErrorsListParams): Promise<Pagined<Error>> {
29+
list(params: ErrorsListParams): Promise<Pagination<Error>> {
3030
const url = new URL(
3131
`/api/v1/errors/${params.execution_id}/${params.run_id}`,
3232
this.options.baseUrl,
3333
);
34-
url.searchParams.set("limit", String(params.limit ?? 10));
35-
if (params.ending_before) {
36-
url.searchParams.set("ending_before", params.ending_before);
34+
if (params.limit) {
35+
url.searchParams.set("limit", String(params.limit));
3736
}
38-
if (params.starting_after) {
39-
url.searchParams.set("starting_after", params.starting_after);
37+
if (params.cursor) {
38+
url.searchParams.set("cursor", params.cursor);
4039
}
41-
return this.request<Pagined<Error>>(url, "GET");
40+
return this.request<Pagination<Error>>(url, "GET");
4241
}
4342
}

libs/clients/executions.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {
22
BaseClient,
33
type FnsConfig,
44
type PaginationParams,
5-
type Pagined,
5+
type Pagination,
66
} from "./client.ts";
77

8-
type Execution = {
8+
export type Execution = {
99
id: string;
1010
name: string;
1111
data: { size: number } | null;
@@ -14,43 +14,39 @@ type Execution = {
1414
created_at: string;
1515
updated_at: string;
1616
};
17-
type ExecutionInvokeParams = {
17+
export type ExecutionInvokeParams = {
1818
id?: string;
1919
name: string;
2020
data: unknown;
2121
wait?: boolean;
2222
idempotencyKey?: string;
2323
};
24-
type ExecutionTriggerParams = {
24+
export type ExecutionTriggerParams = {
2525
id: string;
2626
signal: string;
2727
data?: unknown;
2828
idempotencyKey?: string;
2929
};
30-
type ExecutionAbortParams = {
30+
export type ExecutionAbortParams = {
3131
id: string;
3232
};
33-
type ExecutionPauseParams = {
33+
export type ExecutionPauseParams = {
3434
id: string;
3535
idempotencyKey?: string;
3636
};
37-
type ExecutionResumeParams = {
37+
export type ExecutionResumeParams = {
3838
id: string;
3939
idempotencyKey?: string;
4040
};
41-
type ExecutionResultParams = {
41+
export type ExecutionResultParams = {
4242
id: string;
4343
};
44-
type ExecutionDataParams = {
44+
export type ExecutionDataParams = {
4545
id: string;
4646
};
47-
type ExecutionListParams = PaginationParams<{
48-
created?: {
49-
gt?: string;
50-
gte?: string;
51-
lt?: string;
52-
lte?: string;
53-
};
47+
export type ExecutionListParams = PaginationParams<{
48+
status?: "running" | "waiting" | "completed" | "failed" | "aborted";
49+
name?: string;
5450
}>;
5551

5652
export class ExecutionsClient extends BaseClient {
@@ -62,30 +58,23 @@ export class ExecutionsClient extends BaseClient {
6258
* @example
6359
* const executions = await fns.executions.list()
6460
*/
65-
list(params: ExecutionListParams): Promise<Pagined<Execution>> {
61+
list(params?: ExecutionListParams): Promise<Pagination<Execution>> {
6662
const url = new URL("/api/v1/executions", this.options.baseUrl);
67-
url.searchParams.set("limit", String(params.limit ?? 10));
68-
if (params.ending_before) {
69-
url.searchParams.set("ending_before", params.ending_before);
70-
}
71-
if (params.starting_after) {
72-
url.searchParams.set("starting_after", params.starting_after);
73-
}
74-
if (params.created) {
75-
if (params.created.gt) {
76-
url.searchParams.set("created[gt]", params.created.gt);
63+
if (params) {
64+
if (params.limit) {
65+
url.searchParams.set("limit", String(params.limit));
7766
}
78-
if (params.created.gte) {
79-
url.searchParams.set("created[gte]", params.created.gte);
67+
if(params.status) {
68+
url.searchParams.set("status", params.status);
8069
}
81-
if (params.created.lt) {
82-
url.searchParams.set("created[lt]", params.created.lt);
70+
if(params.name) {
71+
url.searchParams.set("name", params.name);
8372
}
84-
if (params.created.lte) {
85-
url.searchParams.set("created[lte]", params.created.lte);
73+
if (params.cursor) {
74+
url.searchParams.set("cursor", params.cursor);
8675
}
8776
}
88-
return this.request<Pagined<Execution>>(url, "GET");
77+
return this.request<Pagination<Execution>>(url, "GET");
8978
}
9079

9180
/**

libs/clients/queries.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ import {
22
BaseClient,
33
type FnsConfig,
44
type PaginationParams,
5-
type Pagined,
5+
type Pagination,
66
} from "./client.ts";
77

8-
type Query = {
8+
export type Query = {
99
name: string;
1010
size: number;
1111
version: number;
1212
updated_at: string;
1313
};
14-
type QueryValue<T> = {
14+
export type QueryValue<T> = {
1515
value: T;
1616
timestamp: string;
1717
version: number;
18-
1918
updated_at: string;
2019
};
21-
type QueryRetrieveParams = {
20+
export type QueryRetrieveParams = {
2221
id: string;
2322
query: string;
2423
};
25-
type QueryListParams = PaginationParams<{
24+
export type QueryListParams = PaginationParams<{
2625
execution_id: string;
2726
}>;
2827

@@ -35,19 +34,18 @@ export class QueriesClient extends BaseClient {
3534
* @example
3635
* const queries = await fns.queries.list({ id: "..." })
3736
*/
38-
list(params: QueryListParams): Promise<Pagined<Query>> {
37+
list(params: QueryListParams): Promise<Pagination<Query>> {
3938
const url = new URL(
4039
`/api/v1/queries/${params.execution_id}`,
4140
this.options.baseUrl,
4241
);
43-
url.searchParams.set("limit", String(params.limit ?? 10));
44-
if (params.ending_before) {
45-
url.searchParams.set("ending_before", params.ending_before);
42+
if (params.limit) {
43+
url.searchParams.set("limit", String(params.limit));
4644
}
47-
if (params.starting_after) {
48-
url.searchParams.set("starting_after", params.starting_after);
45+
if (params.cursor) {
46+
url.searchParams.set("cursor", params.cursor);
4947
}
50-
return this.request<Pagined<Query>>(url, "GET");
48+
return this.request<Pagination<Query>>(url, "GET");
5149
}
5250

5351
/**

libs/clients/runs.ts

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@ import {
22
BaseClient,
33
type FnsConfig,
44
type PaginationParams,
5-
type Pagined,
5+
type Pagination,
66
} from "./client.ts";
7-
type Query = {
8-
name: string;
9-
size: number;
10-
version: number;
11-
updated_at: string;
12-
};
13-
type QueryValue<T> = {
14-
value: T;
15-
timestamp: string;
16-
version: number;
177

8+
export type Run = {
9+
tenant_id: string;
10+
execution_id: string;
11+
id: string;
12+
status: "running" | "completed" | "failed" | "aborted";
13+
created_at: string;
1814
updated_at: string;
1915
};
20-
type QueryRetrieveParams = {
16+
export type RunRetrieveParams = {
2117
id: string;
22-
query: string;
18+
execution_id: string;
2319
};
24-
type QueryListParams = PaginationParams<{
20+
export type RunListParams = PaginationParams<{
2521
execution_id: string;
2622
}>;
2723

@@ -30,46 +26,33 @@ export class RunsClient extends BaseClient {
3026
super(config);
3127
}
3228
/**
33-
* List all queries of an execution.
29+
* List all runs of an execution.
3430
* @example
35-
* const queries = await fns.queries.list({ id: "..." })
31+
* const runs = await fns.runs.list({ execution_id: "..." })
3632
*/
37-
list(params: QueryListParams): Promise<Pagined<Query>> {
33+
list(params: RunListParams): Promise<Pagination<Run>> {
3834
const url = new URL(
39-
`/api/v1/queries/${params.execution_id}`,
35+
`/api/v1/runs/${params.execution_id}`,
4036
this.options.baseUrl,
4137
);
42-
url.searchParams.set("limit", String(params.limit ?? 10));
43-
if (params.ending_before) {
44-
url.searchParams.set("ending_before", params.ending_before);
38+
if (params.limit) {
39+
url.searchParams.set("limit", String(params.limit));
4540
}
46-
if (params.starting_after) {
47-
url.searchParams.set("starting_after", params.starting_after);
41+
if (params.cursor) {
42+
url.searchParams.set("cursor", params.cursor);
4843
}
49-
return this.request<Pagined<Query>>(url, "GET");
44+
return this.request<Pagination<Run>>(url, "GET");
5045
}
5146

5247
/**
53-
* Retrieve a specific query by its ID.
48+
* Retrieve a specific run by its ID.
5449
* @example
55-
* const value = await fns.queries.retrieve({ id: "...", query: "..." })
50+
* const value = await fns.runs.retrieve({ execution_id: "...", id: "..." })
5651
*/
57-
retrieve<T = unknown>(params: QueryRetrieveParams): Promise<QueryValue<T>> {
58-
return this.request<QueryValue<T>>(
59-
`/api/v1/queries/${params.id}/${params.query}`,
52+
retrieve(params: RunRetrieveParams): Promise<Run> {
53+
return this.request<Run>(
54+
`/api/v1/runs/${params.execution_id}/${params.id}`,
6055
"GET",
6156
);
6257
}
63-
}
64-
65-
/*
66-
67-
queries.get()
68-
queries.list()
69-
70-
steps.get()
71-
steps.list()
72-
73-
errors.get()
74-
errors.list()
75-
*/
58+
}

0 commit comments

Comments
 (0)