Skip to content

Commit 7347158

Browse files
authored
Fixed issue with asResponse and withResponse not working on runs.retrieve (#1648)
1 parent 813d73d commit 7347158

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

.changeset/lazy-carpets-reply.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Fixed issue with asResponse and withResponse not working on runs.retrieve

packages/core/src/v3/apiClient/core.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ export const defaultRetryOptions = {
2626
randomize: false,
2727
} satisfies RetryOptions;
2828

29-
export type ZodFetchOptions = {
29+
export type ZodFetchOptions<T = unknown> = {
3030
retry?: RetryOptions;
3131
tracer?: TriggerTracer;
3232
name?: string;
3333
attributes?: Attributes;
3434
icon?: string;
3535
onResponseBody?: (body: unknown, span: Span) => void;
36+
prepareData?: (data: T) => Promise<T> | T;
3637
};
3738

39+
export type AnyZodFetchOptions = ZodFetchOptions<any>;
40+
3841
export type ApiRequestOptions = Pick<ZodFetchOptions, "retry">;
3942

4043
type KeysEnum<T> = { [P in keyof Required<T>]: true };
@@ -67,7 +70,7 @@ export function zodfetch<TResponseBodySchema extends z.ZodTypeAny>(
6770
schema: TResponseBodySchema,
6871
url: string,
6972
requestInit?: RequestInit,
70-
options?: ZodFetchOptions
73+
options?: ZodFetchOptions<z.output<TResponseBodySchema>>
7174
): ApiPromise<z.output<TResponseBodySchema>> {
7275
return new ApiPromise(_doZodFetch(schema, url, requestInit, options));
7376
}
@@ -197,6 +200,10 @@ async function _doZodFetch<TResponseBodySchema extends z.ZodTypeAny>(
197200
options.onResponseBody(result.data, span);
198201
}
199202

203+
if (options?.prepareData) {
204+
result.data = await options.prepareData(result.data);
205+
}
206+
200207
return result;
201208
});
202209
}

packages/core/src/v3/apiClient/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { taskContext } from "../task-context-api.js";
3333
import { AnyRunTypes, TriggerJwtOptions } from "../types/tasks.js";
3434
import {
35+
AnyZodFetchOptions,
3536
ApiRequestOptions,
3637
CursorPagePromise,
3738
ZodFetchOptions,
@@ -863,9 +864,9 @@ function createSearchQueryForListRuns(query?: ListRunsQueryParams): URLSearchPar
863864
}
864865

865866
export function mergeRequestOptions(
866-
defaultOptions: ZodFetchOptions,
867+
defaultOptions: AnyZodFetchOptions,
867868
options?: ApiRequestOptions
868-
): ZodFetchOptions {
869+
): AnyZodFetchOptions {
869870
if (!options) {
870871
return defaultOptions;
871872
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import type {
1414
TaskRunShape,
1515
AnyBatchedRunHandle,
1616
AsyncIterableStream,
17+
ApiPromise,
1718
} from "@trigger.dev/core/v3";
1819
import {
19-
ApiPromise,
2020
CanceledRunResponse,
2121
CursorPagePromise,
2222
ListRunResponseItem,
@@ -187,15 +187,14 @@ function retrieveRun<TRunId extends AnyRunHandle | AnyBatchedRunHandle | AnyTask
187187
style: "codepath",
188188
}),
189189
},
190+
prepareData: resolvePayloadAndOutputUrls,
190191
},
191192
requestOptions
192193
);
193194

194195
const $runId = typeof runId === "string" ? runId : runId.id;
195196

196-
return apiClient.retrieveRun($runId, $requestOptions).then((retrievedRun) => {
197-
return resolvePayloadAndOutputUrls(retrievedRun);
198-
}) as ApiPromise<RetrieveRunResult<TRunId>>;
197+
return apiClient.retrieveRun($runId, $requestOptions) as ApiPromise<RetrieveRunResult<TRunId>>;
199198
}
200199

201200
async function resolvePayloadAndOutputUrls(run: AnyRetrieveRunResult) {

0 commit comments

Comments
 (0)