import {z} from 'zod' export type CommonRunsFilter = {createdAt?: ({from?: string | undefined, to?: string | undefined, period?: string | undefined}) | undefined, status?: (Array<'PENDING_VERSION' | 'QUEUED' | 'EXECUTING' | 'REATTEMPTING' | 'FROZEN' | 'COMPLETED' | 'CANCELED' | 'FAILED' | 'CRASHED' | 'INTERRUPTED' | 'SYSTEM_FAILURE'>) | undefined, taskIdentifier?: Array | undefined, version?: Array | undefined, bulkAction?: string | undefined, schedule?: string | undefined, isTest?: boolean | undefined, tag?: Array | undefined, error?: string | undefined}; export type GetApiApiV1RunsArgs = {page?: ({size?: number | undefined, after?: string | undefined, before?: string | undefined}) | undefined, filter?: CommonRunsFilter | undefined}; export type GetApiApiV1RunsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tTL = z.union([z.string(), z.number()]); export const listRunItem = z.object({id: z.string(), status: z.enum(["PENDING_VERSION", "QUEUED", "EXECUTING", "REATTEMPTING", "FROZEN", "COMPLETED", "CANCELED", "FAILED", "CRASHED", "INTERRUPTED", "SYSTEM_FAILURE"]), taskIdentifier: z.string(), version: z.string().optional(), env: z.object({id: z.string(), name: z.string(), user: z.string().optional()}), idempotencyKey: z.string().optional(), isTest: z.boolean(), createdAt: z.string(), updatedAt: z.string(), 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(), costInCents: z.number().optional(), baseCostInCents: z.number().optional(), durationMs: z.number().optional()}); export const listRunsResult = z.object({data: z.array(listRunItem).optional(), pagination: z.object({next: z.string().optional(), previous: z.string().optional()}).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 }; /** * List runs * * GET /api/v1/runs */ export const getApiApiV1Runs = async (args: GetApiApiV1RunsArgs, options?: GetApiApiV1RunsOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/runs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listRunsResult, await res.json()) };