import {z} from 'zod' export type TaskCreateRequest = {type: string, queue?: string | undefined, payload: unknown, payload_base64?: string | undefined, deadline?: string | undefined, max_tries?: number | undefined, dependencies?: Array | undefined, load_dependencies?: boolean | undefined, signature?: string | undefined}; export type CreateApiV1TasksArgs = {'Idempotency-Key'?: string | undefined, body: TaskCreateRequest}; export type CreateApiV1TasksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const taskResult = z.object({payload: z.unknown(), completed: z.string()}); export const taskState = z.enum(["new", "active", "retry", "expired", "terminated", "complete", "queue_error", "blocked", "unreachable"]); export const task = z.object({id: z.string(), type: z.string(), queue: z.string().optional(), dependencies: z.array(z.string()).optional(), dependency_results: z.record(z.string(), taskResult).optional(), load_dependencies: z.boolean().optional(), payload: z.string(), deadline: z.string().optional(), max_tries: z.number().int().optional(), result: taskResult.optional(), state: taskState, created: z.string(), tried: z.string().optional(), tries: z.number().int().optional(), last_err: z.string().optional(), signature: 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 }; /** * Create and enqueue a task. * * POST /v1/tasks */ export const createApiV1Tasks = async (args: CreateApiV1TasksArgs, options: CreateApiV1TasksOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks${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(task, await res.json()) };