Skip to content

Commit 2947d4e

Browse files
committed
simple way to share component api
1 parent 4e03548 commit 2947d4e

File tree

4 files changed

+25
-302
lines changed

4 files changed

+25
-302
lines changed

example/convex/_generated/api.d.ts

Lines changed: 4 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @module
99
*/
1010

11+
import { ComponentApi } from "../../../src/client/types.js";
1112
import type * as admin from "../admin.js";
1213
import type * as example from "../example.js";
1314
import type * as transcription from "../transcription.js";
@@ -43,199 +44,7 @@ export declare const internal: FilterApi<
4344
>;
4445

4546
export declare const components: {
46-
workflow: {
47-
journal: {
48-
load: FunctionReference<
49-
"query",
50-
"internal",
51-
{ workflowId: string },
52-
{
53-
journalEntries: Array<{
54-
_creationTime: number;
55-
_id: string;
56-
step: {
57-
args: any;
58-
argsSize: number;
59-
completedAt?: number;
60-
functionType: "query" | "mutation" | "action";
61-
handle: string;
62-
inProgress: boolean;
63-
name: string;
64-
runResult?:
65-
| { kind: "success"; returnValue: any }
66-
| { error: string; kind: "failed" }
67-
| { kind: "canceled" };
68-
startedAt: number;
69-
workId?: string;
70-
};
71-
stepNumber: number;
72-
workflowId: string;
73-
}>;
74-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
75-
ok: boolean;
76-
workflow: {
77-
_creationTime: number;
78-
_id: string;
79-
args: any;
80-
generationNumber: number;
81-
logLevel?: any;
82-
name?: string;
83-
onComplete?: { context?: any; fnHandle: string };
84-
runResult?:
85-
| { kind: "success"; returnValue: any }
86-
| { error: string; kind: "failed" }
87-
| { kind: "canceled" };
88-
startedAt?: any;
89-
state?: any;
90-
workflowHandle: string;
91-
};
92-
}
93-
>;
94-
startSteps: FunctionReference<
95-
"mutation",
96-
"internal",
97-
{
98-
generationNumber: number;
99-
steps: Array<{
100-
retry?:
101-
| boolean
102-
| { base: number; initialBackoffMs: number; maxAttempts: number };
103-
schedulerOptions?: { runAt?: number } | { runAfter?: number };
104-
step: {
105-
args: any;
106-
argsSize: number;
107-
completedAt?: number;
108-
functionType: "query" | "mutation" | "action";
109-
handle: string;
110-
inProgress: boolean;
111-
name: string;
112-
runResult?:
113-
| { kind: "success"; returnValue: any }
114-
| { error: string; kind: "failed" }
115-
| { kind: "canceled" };
116-
startedAt: number;
117-
workId?: string;
118-
};
119-
}>;
120-
workflowId: string;
121-
workpoolOptions?: {
122-
defaultRetryBehavior?: {
123-
base: number;
124-
initialBackoffMs: number;
125-
maxAttempts: number;
126-
};
127-
logLevel?: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
128-
maxParallelism?: number;
129-
retryActionsByDefault?: boolean;
130-
};
131-
},
132-
Array<{
133-
_creationTime: number;
134-
_id: string;
135-
step: {
136-
args: any;
137-
argsSize: number;
138-
completedAt?: number;
139-
functionType: "query" | "mutation" | "action";
140-
handle: string;
141-
inProgress: boolean;
142-
name: string;
143-
runResult?:
144-
| { kind: "success"; returnValue: any }
145-
| { error: string; kind: "failed" }
146-
| { kind: "canceled" };
147-
startedAt: number;
148-
workId?: string;
149-
};
150-
stepNumber: number;
151-
workflowId: string;
152-
}>
153-
>;
154-
};
155-
workflow: {
156-
cancel: FunctionReference<
157-
"mutation",
158-
"internal",
159-
{ workflowId: string },
160-
null
161-
>;
162-
cleanup: FunctionReference<
163-
"mutation",
164-
"internal",
165-
{ workflowId: string },
166-
boolean
167-
>;
168-
complete: FunctionReference<
169-
"mutation",
170-
"internal",
171-
{
172-
generationNumber: number;
173-
runResult:
174-
| { kind: "success"; returnValue: any }
175-
| { error: string; kind: "failed" }
176-
| { kind: "canceled" };
177-
workflowId: string;
178-
},
179-
null
180-
>;
181-
create: FunctionReference<
182-
"mutation",
183-
"internal",
184-
{
185-
maxParallelism?: number;
186-
onComplete?: { context?: any; fnHandle: string };
187-
startAsync?: boolean;
188-
workflowArgs: any;
189-
workflowHandle: string;
190-
workflowName: string;
191-
},
192-
string
193-
>;
194-
getStatus: FunctionReference<
195-
"query",
196-
"internal",
197-
{ workflowId: string },
198-
{
199-
inProgress: Array<{
200-
_creationTime: number;
201-
_id: string;
202-
step: {
203-
args: any;
204-
argsSize: number;
205-
completedAt?: number;
206-
functionType: "query" | "mutation" | "action";
207-
handle: string;
208-
inProgress: boolean;
209-
name: string;
210-
runResult?:
211-
| { kind: "success"; returnValue: any }
212-
| { error: string; kind: "failed" }
213-
| { kind: "canceled" };
214-
startedAt: number;
215-
workId?: string;
216-
};
217-
stepNumber: number;
218-
workflowId: string;
219-
}>;
220-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
221-
workflow: {
222-
_creationTime: number;
223-
_id: string;
224-
args: any;
225-
generationNumber: number;
226-
logLevel?: any;
227-
name?: string;
228-
onComplete?: { context?: any; fnHandle: string };
229-
runResult?:
230-
| { kind: "success"; returnValue: any }
231-
| { error: string; kind: "failed" }
232-
| { kind: "canceled" };
233-
startedAt?: any;
234-
state?: any;
235-
workflowHandle: string;
236-
};
237-
}
238-
>;
239-
};
240-
};
47+
workflow: ComponentApi<
48+
typeof import("../../../src/component/_generated/api.js").api
49+
>;
24150
};

src/client/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { ObjectType, PropertyValidators, Validator } from "convex/values";
1717
import type { Step } from "../component/schema.js";
1818
import type { OnCompleteArgs, WorkflowId } from "../types.js";
1919
import { safeFunctionName } from "./safeFunctionName.js";
20-
import type { OpaqueIds, WorkflowComponent, WorkflowStep } from "./types.js";
20+
import type { WorkflowComponent, WorkflowStep } from "./types.js";
2121
import { workflowMutation } from "./workflowMutation.js";
2222

2323
export { vWorkflowId, type WorkflowId } from "../types.js";
@@ -71,7 +71,7 @@ export type WorkflowDefinition<
7171
};
7272

7373
export type WorkflowStatus =
74-
| { type: "inProgress"; running: OpaqueIds<Step>[] }
74+
| { type: "inProgress"; running: Step[] }
7575
| { type: "completed" }
7676
| { type: "canceled" }
7777
| { type: "failed"; error: string };

src/client/types.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { RetryOption, WorkId } from "@convex-dev/workpool";
1+
import type { RetryOption } from "@convex-dev/workpool";
22
import type {
33
Expand,
44
FunctionArgs,
55
FunctionReference,
66
FunctionReturnType,
77
} from "convex/server";
88
import type { api } from "../component/_generated/api.js";
9-
import type { GenericId } from "convex/values";
9+
import type { GenericId, Value } from "convex/values";
1010
import type { WorkflowId } from "../types.js";
1111

12-
export type WorkflowComponent = UseApi<typeof api>;
12+
export type WorkflowComponent = ComponentApi<typeof api>;
1313

1414
export type RunOptions = {
1515
/**
@@ -83,31 +83,27 @@ export type WorkflowStep = {
8383
): Promise<FunctionReturnType<Action>>;
8484
};
8585

86-
export type UseApi<API> = Expand<{
86+
export type ComponentApi<API> = Expand<{
8787
[mod in keyof API]: API[mod] extends FunctionReference<
8888
infer FType,
8989
"public",
9090
infer FArgs,
91-
infer FReturnType,
92-
infer FComponentPath
91+
infer FReturnType
9392
>
9493
? FunctionReference<
9594
FType,
9695
"internal",
97-
OpaqueIds<FArgs>,
98-
OpaqueIds<FReturnType>,
99-
FComponentPath
96+
StringifyIds<FArgs>,
97+
StringifyIds<FReturnType>
10098
>
101-
: UseApi<API[mod]>;
99+
: ComponentApi<API[mod]>;
102100
}>;
103101

104-
export type OpaqueIds<T> =
105-
T extends GenericId<infer _T>
102+
export type StringifyIds<T> =
103+
T extends GenericId<string>
106104
? string
107-
: T extends WorkId
108-
? string
109-
: T extends (infer U)[]
110-
? OpaqueIds<U>[]
111-
: T extends object
112-
? { [K in keyof T]: OpaqueIds<T[K]> }
113-
: T;
105+
: T extends (infer U)[]
106+
? StringifyIds<U>[]
107+
: T extends Record<string, Value | undefined>
108+
? { [K in keyof T]: StringifyIds<T[K]> }
109+
: T;

src/component/_generated/api.d.ts

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @module
99
*/
1010

11+
import type { ComponentApi } from "../../client/types.ts";
1112
import type * as journal from "../journal.js";
1213
import type * as logging from "../logging.js";
1314
import type * as model from "../model.js";
@@ -247,90 +248,7 @@ export declare const internal: FilterApi<
247248
>;
248249

249250
export declare const components: {
250-
workpool: {
251-
lib: {
252-
cancel: FunctionReference<
253-
"mutation",
254-
"internal",
255-
{
256-
id: string;
257-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
258-
},
259-
any
260-
>;
261-
cancelAll: FunctionReference<
262-
"mutation",
263-
"internal",
264-
{
265-
before?: number;
266-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
267-
},
268-
any
269-
>;
270-
enqueue: FunctionReference<
271-
"mutation",
272-
"internal",
273-
{
274-
config: {
275-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
276-
maxParallelism: number;
277-
};
278-
fnArgs: any;
279-
fnHandle: string;
280-
fnName: string;
281-
fnType: "action" | "mutation" | "query";
282-
onComplete?: { context?: any; fnHandle: string };
283-
retryBehavior?: {
284-
base: number;
285-
initialBackoffMs: number;
286-
maxAttempts: number;
287-
};
288-
runAt: number;
289-
},
290-
string
291-
>;
292-
enqueueBatch: FunctionReference<
293-
"mutation",
294-
"internal",
295-
{
296-
config: {
297-
logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
298-
maxParallelism: number;
299-
};
300-
items: Array<{
301-
fnArgs: any;
302-
fnHandle: string;
303-
fnName: string;
304-
fnType: "action" | "mutation" | "query";
305-
onComplete?: { context?: any; fnHandle: string };
306-
retryBehavior?: {
307-
base: number;
308-
initialBackoffMs: number;
309-
maxAttempts: number;
310-
};
311-
runAt: number;
312-
}>;
313-
},
314-
Array<string>
315-
>;
316-
status: FunctionReference<
317-
"query",
318-
"internal",
319-
{ id: string },
320-
| { previousAttempts: number; state: "pending" }
321-
| { previousAttempts: number; state: "running" }
322-
| { state: "finished" }
323-
>;
324-
statusBatch: FunctionReference<
325-
"query",
326-
"internal",
327-
{ ids: Array<string> },
328-
Array<
329-
| { previousAttempts: number; state: "pending" }
330-
| { previousAttempts: number; state: "running" }
331-
| { state: "finished" }
332-
>
333-
>;
334-
};
335-
};
251+
workpool: ComponentApi<
252+
typeof import("../../../node_modules/@convex-dev/workpool/dist/component/_generated/api.js").api
253+
>;
336254
};

0 commit comments

Comments
 (0)