import {z} from 'zod' export type GetApiV1JobsArgs = {limit?: number | undefined, offset?: number | undefined, status?: ('pending' | 'submitted' | 'polling' | 'processing' | 'completed' | 'failed') | undefined}; export type GetApiV1JobsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1JobsResponse = z.object({jobs: z.array(z.object({id: z.string().optional(), status: z.enum(["pending", "submitted", "polling", "processing", "completed", "failed"]).optional(), action: z.string().optional(), parent_job: z.string().nullable().optional(), model_slug: z.string().optional(), media_type: z.enum(["video", "image", "audio", "text", "unknown"]).optional(), cost: z.union([z.string(), z.number()]).nullable().optional(), cost_usd: z.number().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).and(z.record(z.string(), z.unknown()))).optional(), total: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().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 jobs * * GET /v1/jobs */ export const getApiV1Jobs = async (args: GetApiV1JobsArgs, options?: GetApiV1JobsOptions) => { const {baseUrl = "https://api.glio.io", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.status != null) query.append("status", String(args.status)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/jobs${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(getApiV1JobsResponse, await res.json()) };