import {z} from 'zod' export type RescheduleRunRequestBody = {delay?: (string | string) | undefined}; export type CreateApiApiV1RunsRunIdRescheduleArgs = {body: RescheduleRunRequestBody}; export type CreateApiApiV1RunsRunIdRescheduleOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tTL = z.union([z.string(), z.number()]); export const commonRunObject = z.object({id: z.string(), status: z.enum(["PENDING_VERSION", "DELAYED", "QUEUED", "EXECUTING", "REATTEMPTING", "FROZEN", "COMPLETED", "CANCELED", "FAILED", "CRASHED", "INTERRUPTED", "SYSTEM_FAILURE"]), taskIdentifier: z.string(), version: z.string().optional(), idempotencyKey: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), isTest: z.boolean().optional(), startedAt: z.string().optional(), finishedAt: z.string().optional(), delayedUntil: z.string().optional(), ttl: tTL.optional(), expiredAt: z.string().optional(), tags: z.array(z.string()).optional(), metadata: z.object({}).optional(), costInCents: z.number().optional(), baseCostInCents: z.number().optional(), durationMs: z.number().optional(), depth: z.number().int().optional(), batchId: z.string().optional(), triggerFunction: z.enum(["trigger", "triggerAndWait", "batchTrigger", "batchTriggerAndWait"]).optional()}); export const serializedError = z.object({message: z.string(), name: z.string().optional(), stackTrace: z.string().optional()}); export const retrieveRunResponse = z.object({id: z.string(), status: z.enum(["PENDING_VERSION", "DELAYED", "QUEUED", "EXECUTING", "REATTEMPTING", "FROZEN", "COMPLETED", "CANCELED", "FAILED", "CRASHED", "INTERRUPTED", "SYSTEM_FAILURE"]), taskIdentifier: z.string(), version: z.string().optional(), idempotencyKey: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), isTest: z.boolean().optional(), startedAt: z.string().optional(), finishedAt: z.string().optional(), delayedUntil: z.string().optional(), ttl: tTL.optional(), expiredAt: z.string().optional(), tags: z.array(z.string()).optional(), metadata: z.object({}).optional(), costInCents: z.number().optional(), baseCostInCents: z.number().optional(), durationMs: z.number().optional(), depth: z.number().int().optional(), batchId: z.string().optional(), triggerFunction: z.enum(["trigger", "triggerAndWait", "batchTrigger", "batchTriggerAndWait"]).optional(), payload: z.object({}).optional(), payloadPresignedUrl: z.string().optional(), output: z.object({}).optional(), outputPresignedUrl: z.string().optional(), relatedRuns: z.object({root: commonRunObject.optional(), parent: commonRunObject.optional(), children: z.array(commonRunObject).optional()}).optional(), schedule: z.object({id: z.string(), externalId: z.string().optional(), deduplicationKey: z.string().optional(), generator: z.object({type: z.literal("CRON").optional(), expression: z.string().optional(), description: z.string().optional()})}).optional(), attempts: z.array(z.object({id: z.string(), status: z.enum(["PENDING", "EXECUTING", "PAUSED", "COMPLETED", "FAILED", "CANCELED"]), error: serializedError.optional(), createdAt: z.string(), updatedAt: z.string(), startedAt: z.string().optional(), completedAt: z.string().optional()}))}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Rescheduled a delayed run * * POST /api/v1/runs/{runId}/reschedule */ export const createApiApiV1RunsRunIdReschedule = async (args: CreateApiApiV1RunsRunIdRescheduleArgs, options?: CreateApiApiV1RunsRunIdRescheduleOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/runs/${encodeURIComponent(String(args.runId))}/reschedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(retrieveRunResponse, await res.json()) };