Skip to content

Fixed issue with asResponse and withResponse not working on runs.retrieve #1648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/lazy-carpets-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@trigger.dev/sdk": patch
"@trigger.dev/core": patch
---

Fixed issue with asResponse and withResponse not working on runs.retrieve
11 changes: 9 additions & 2 deletions packages/core/src/v3/apiClient/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ export const defaultRetryOptions = {
randomize: false,
} satisfies RetryOptions;

export type ZodFetchOptions = {
export type ZodFetchOptions<T = unknown> = {
retry?: RetryOptions;
tracer?: TriggerTracer;
name?: string;
attributes?: Attributes;
icon?: string;
onResponseBody?: (body: unknown, span: Span) => void;
prepareData?: (data: T) => Promise<T> | T;
};

export type AnyZodFetchOptions = ZodFetchOptions<any>;

export type ApiRequestOptions = Pick<ZodFetchOptions, "retry">;

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

if (options?.prepareData) {
result.data = await options.prepareData(result.data);
}

return result;
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/v3/apiClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { taskContext } from "../task-context-api.js";
import { AnyRunTypes, TriggerJwtOptions } from "../types/tasks.js";
import {
AnyZodFetchOptions,
ApiRequestOptions,
CursorPagePromise,
ZodFetchOptions,
Expand Down Expand Up @@ -863,9 +864,9 @@ function createSearchQueryForListRuns(query?: ListRunsQueryParams): URLSearchPar
}

export function mergeRequestOptions(
defaultOptions: ZodFetchOptions,
defaultOptions: AnyZodFetchOptions,
options?: ApiRequestOptions
): ZodFetchOptions {
): AnyZodFetchOptions {
if (!options) {
return defaultOptions;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/trigger-sdk/src/v3/runs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type {
TaskRunShape,
AnyBatchedRunHandle,
AsyncIterableStream,
ApiPromise,
} from "@trigger.dev/core/v3";
import {
ApiPromise,
CanceledRunResponse,
CursorPagePromise,
ListRunResponseItem,
Expand Down Expand Up @@ -187,15 +187,14 @@ function retrieveRun<TRunId extends AnyRunHandle | AnyBatchedRunHandle | AnyTask
style: "codepath",
}),
},
prepareData: resolvePayloadAndOutputUrls,
},
requestOptions
);

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

return apiClient.retrieveRun($runId, $requestOptions).then((retrievedRun) => {
return resolvePayloadAndOutputUrls(retrievedRun);
}) as ApiPromise<RetrieveRunResult<TRunId>>;
return apiClient.retrieveRun($runId, $requestOptions) as ApiPromise<RetrieveRunResult<TRunId>>;
}

async function resolvePayloadAndOutputUrls(run: AnyRetrieveRunResult) {
Expand Down