import {z} from 'zod' export type GetApiApiProjectsPidJobsArgs = {page?: number | undefined, page_size?: number | undefined}; export type GetApiApiProjectsPidJobsOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const spiderJobArg = z.object({name: z.string().min(1).max(1000), value: z.string().min(1).max(1000)}); export const spiderJobEnvVar = z.object({evid: z.number().int().nullable().optional(), name: z.string().min(1).max(1000), value: z.string().min(1).max(12000), masked: z.boolean().optional()}); export const spiderJobTag = z.object({name: z.string().min(1).max(50)}); export const spiderJob = z.object({jid: z.number().int().optional(), spider: z.string().optional(), created: z.string().optional(), name: z.string().min(1).optional(), lifespan: z.number().optional(), total_response_bytes: z.number().int().gte(0).lte(18446744073709552000).optional(), item_count: z.number().int().gte(0).lte(18446744073709552000).optional(), request_count: z.number().int().gte(0).lte(18446744073709552000).optional(), args: z.array(spiderJobArg).optional(), env_vars: z.array(spiderJobEnvVar).optional(), tags: z.array(spiderJobTag).optional(), job_status: z.string().min(1).optional(), cronjob: z.number().int().nullable().optional(), data_expiry_days: z.number().int().gte(0).lte(65535).nullable().optional(), data_status: z.enum(["PERSISTENT", "PENDING", "DELETED"]).optional(), database_insertion_progress: z.string().optional(), storage_size: z.string().optional(), resource_tier: z.enum(["TINY", "XSMALL", "SMALL", "MEDIUM", "LARGE", "XLARGE", "HUGE", "XHUGE"]).optional(), peak_memory: z.string().optional()}); export const projectJob = z.object({results: z.array(spiderJob), count: z.number().int()}); 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 }; /** * GET /api/projects/{pid}/jobs */ export const getApiApiProjectsPidJobs = async (args: GetApiApiProjectsPidJobsArgs, options?: GetApiApiProjectsPidJobsOptions) => { const {baseUrl = "http://127.0.0.1:8000", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects/${encodeURIComponent(String(args.pid))}/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(projectJob, await res.json()) };