Skip to content

Commit 979bee5

Browse files
committed
Fix return type of runs.retrieve, and allow passing the type of the task to runs.retrieve
1 parent 086a0f9 commit 979bee5

File tree

5 files changed

+94
-57
lines changed

5 files changed

+94
-57
lines changed

.changeset/flat-onions-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Fix return type of runs.retrieve, and allow passing the type of the task to runs.retrieve

packages/trigger-sdk/src/v3/runs.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ import {
1717
isRequestOptions,
1818
mergeRequestOptions,
1919
} from "@trigger.dev/core/v3";
20-
import { Prettify, RunHandle, apiClientMissingError } from "./shared";
20+
import { AnyTask, Prettify, RunHandle, Task, apiClientMissingError } from "./shared";
2121
import { tracer } from "./tracer";
2222

23-
export type RetrieveRunResult<TOutput> = Prettify<
24-
TOutput extends RunHandle<infer THandleOutput>
25-
? Omit<RetrieveRunResponse, "output"> & { output?: THandleOutput }
26-
: Omit<RetrieveRunResponse, "output"> & { output?: TOutput }
23+
export type RetrieveRunResult<TRunId> = Prettify<
24+
TRunId extends RunHandle<infer TOutput>
25+
? Omit<RetrieveRunResponse, "output"> & { output?: TOutput }
26+
: TRunId extends Task<string, any, infer TTaskOutput>
27+
? Omit<RetrieveRunResponse, "output"> & { output?: TTaskOutput }
28+
: TRunId extends string
29+
? RetrieveRunResponse
30+
: never
2731
>;
2832

2933
export const runs = {
@@ -139,8 +143,17 @@ function listRunsRequestOptions(
139143
);
140144
}
141145

142-
function retrieveRun<TRunId extends RunHandle<any> | string>(
143-
runId: TRunId,
146+
// Extract out the expected type of the id, can be either a string or a RunHandle
147+
type RunId<TRunId> = TRunId extends RunHandle<any>
148+
? TRunId
149+
: TRunId extends AnyTask
150+
? string
151+
: TRunId extends string
152+
? TRunId
153+
: never;
154+
155+
function retrieveRun<TRunId extends RunHandle<any> | AnyTask | string>(
156+
runId: RunId<TRunId>,
144157
requestOptions?: ApiRequestOptions
145158
): ApiPromise<RetrieveRunResult<TRunId>> {
146159
const apiClient = apiClientManager.client;
@@ -286,15 +299,15 @@ export type PollOptions = { pollIntervalMs?: number };
286299

287300
const MAX_POLL_ATTEMPTS = 500;
288301

289-
async function poll<TRunHandle extends RunHandle<any> | string>(
290-
handle: TRunHandle,
302+
async function poll<TRunId extends RunHandle<any> | AnyTask | string>(
303+
runId: RunId<TRunId>,
291304
options?: { pollIntervalMs?: number },
292305
requestOptions?: ApiRequestOptions
293306
) {
294307
let attempts = 0;
295308

296309
while (attempts++ < MAX_POLL_ATTEMPTS) {
297-
const run = await runs.retrieve(handle, requestOptions);
310+
const run = await runs.retrieve(runId, requestOptions);
298311

299312
if (run.isCompleted) {
300313
return run;
@@ -303,5 +316,9 @@ async function poll<TRunHandle extends RunHandle<any> | string>(
303316
await new Promise((resolve) => setTimeout(resolve, options?.pollIntervalMs ?? 1000));
304317
}
305318

306-
throw new Error(`Run ${handle} did not complete after ${MAX_POLL_ATTEMPTS} attempts`);
319+
throw new Error(
320+
`Run ${
321+
typeof runId === "string" ? runId : runId.id
322+
} did not complete after ${MAX_POLL_ATTEMPTS} attempts`
323+
);
307324
}

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
337337
batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
338338
}
339339

340-
type AnyTask = Task<string, any, any>;
340+
export type AnyTask = Task<string, any, any>;
341341

342342
export type TaskPayload<TTask extends AnyTask> = TTask extends Task<string, infer TInput, any>
343343
? TInput
@@ -678,7 +678,7 @@ export async function batchTrigger<TTask extends AnyTask>(
678678
id: TaskIdentifier<TTask>,
679679
items: Array<BatchItem<TaskPayload<TTask>>>,
680680
requestOptions?: ApiRequestOptions
681-
): Promise<BatchRunHandle<TTask>> {
681+
): Promise<BatchRunHandle<TaskOutput<TTask>>> {
682682
return await batchTrigger_internal<TaskPayload<TTask>, TaskOutput<TTask>>(
683683
"tasks.batchTrigger()",
684684
id,

packages/trigger-sdk/src/v3/tasks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
TaskOutput,
2222
TaskIdentifier,
2323
TaskRunOptions,
24+
AnyTask,
2425
} from "./shared";
2526

2627
export type {
@@ -36,6 +37,7 @@ export type {
3637
TaskOutput,
3738
TaskIdentifier,
3839
TaskRunOptions,
40+
AnyTask,
3941
};
4042

4143
/** Creates a task that can be triggered

references/v3-catalog/src/clientUsage.ts

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,71 @@ async function main() {
2424

2525
const anyRun = await runs.retrieve(anyHandle);
2626

27-
console.log(`Run ${anyHandle.id} status: ${anyRun.status}, ttl: ${anyRun.ttl}`);
27+
console.log(`Run ${anyHandle.id} status: ${anyRun.status}, ttl: ${anyRun.ttl}`, anyRun.output);
28+
29+
const typedRun = await runs.retrieve<typeof createJsonHeroDoc>(anyHandle.id);
30+
31+
console.log(`Run ${anyHandle.id} status: ${typedRun.status}`, typedRun.output);
2832

2933
await new Promise((resolve) => setTimeout(resolve, 121000)); // wait for 2 minutes
3034

3135
const expiredRun = await runs.retrieve(anyRun.id);
3236

3337
console.log(
34-
`Run ${anyHandle.id} status: ${expiredRun.status}, expired at: ${expiredRun.expiredAt}`
38+
`Run ${anyHandle.id} status: ${expiredRun.status}, expired at: ${expiredRun.expiredAt}`,
39+
expiredRun.output
3540
);
3641

37-
// const handle = await tasks.trigger<typeof createJsonHeroDoc>("create-jsonhero-doc", {
38-
// title: "Hello World",
39-
// content: {
40-
// message: "Hello, World!",
41-
// },
42-
// });
43-
44-
// console.log(handle);
45-
46-
// const completedRun = await runs.poll(handle, { pollIntervalMs: 100 });
47-
48-
// console.log(`Run ${handle.id} completed with output:`, completedRun.output);
49-
50-
// const run = await tasks.triggerAndPoll<typeof createJsonHeroDoc>("create-jsonhero-doc", {
51-
// title: "Hello World",
52-
// content: {
53-
// message: "Hello, World!",
54-
// },
55-
// });
56-
57-
// console.log(`Run ${run.id} completed with output: `, run.output);
58-
59-
// const batchHandle = await tasks.batchTrigger<typeof createJsonHeroDoc>("create-jsonhero-doc", [
60-
// {
61-
// payload: {
62-
// title: "Hello World",
63-
// content: {
64-
// message: "Hello, World!",
65-
// },
66-
// },
67-
// },
68-
// {
69-
// payload: {
70-
// title: "Hello World 2",
71-
// content: {
72-
// message: "Hello, World 2!",
73-
// },
74-
// },
75-
// },
76-
// ]);
77-
78-
// const run2 = await runs.retrieve(batchHandle.runs[0]);
42+
const handle = await tasks.trigger<typeof createJsonHeroDoc>("create-jsonhero-doc", {
43+
title: "Hello World",
44+
content: {
45+
message: "Hello, World!",
46+
},
47+
});
48+
49+
console.log(handle);
50+
51+
const typedRetrieveRun = await runs.retrieve(handle);
52+
53+
console.log(`Run ${handle.id} status: ${typedRetrieveRun.status}`, typedRetrieveRun.output);
54+
55+
const completedRun = await runs.poll(handle, { pollIntervalMs: 100 });
56+
57+
console.log(`Run ${handle.id} completed with output:`, completedRun.output);
58+
59+
const run = await tasks.triggerAndPoll<typeof createJsonHeroDoc>("create-jsonhero-doc", {
60+
title: "Hello World",
61+
content: {
62+
message: "Hello, World!",
63+
},
64+
});
65+
66+
console.log(`Run ${run.id} completed with output: `, run.output);
67+
68+
const batchHandle = await tasks.batchTrigger<typeof createJsonHeroDoc>("create-jsonhero-doc", [
69+
{
70+
payload: {
71+
title: "Hello World",
72+
content: {
73+
message: "Hello, World!",
74+
},
75+
},
76+
},
77+
{
78+
payload: {
79+
title: "Hello World 2",
80+
content: {
81+
message: "Hello, World 2!",
82+
},
83+
},
84+
},
85+
]);
86+
87+
const firstRunHandle = batchHandle.runs[0];
88+
89+
const run2 = await runs.retrieve(firstRunHandle);
90+
91+
console.log(`Run ${run2.id} completed with output: `, run2.output);
7992
}
8093

8194
main().catch(console.error);

0 commit comments

Comments
 (0)