import {z} from 'zod' export type SpiderJobArg = {name: string, value: string}; export type SpiderJobEnvVar = {evid?: (number | null) | undefined, name: string, value: string, masked?: boolean | undefined}; export type SpiderJobTag = {name: string}; export type SpiderJob = {jid?: number | undefined, spider?: string | undefined, created?: string | undefined, name?: string | undefined, lifespan?: number | undefined, total_response_bytes?: number | undefined, item_count?: number | undefined, request_count?: number | undefined, args?: Array | undefined, env_vars?: Array | undefined, tags?: Array | undefined, job_status?: string | undefined, cronjob?: (number | null) | undefined, data_expiry_days?: (number | null) | undefined, data_status?: ('PERSISTENT' | 'PENDING' | 'DELETED') | undefined, database_insertion_progress?: string | undefined, storage_size?: string | undefined, resource_tier?: ('TINY' | 'XSMALL' | 'SMALL' | 'MEDIUM' | 'LARGE' | 'XLARGE' | 'HUGE' | 'XHUGE') | undefined, peak_memory?: string | undefined}; export type PatchApiApiProjectsPidSpidersSidJobsJidArgs = {body: SpiderJob}; export type PatchApiApiProjectsPidSpidersSidJobsJidOptions = {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 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 }; /** * PATCH /api/projects/{pid}/spiders/{sid}/jobs/{jid} */ export const patchApiApiProjectsPidSpidersSidJobsJid = async (args: PatchApiApiProjectsPidSpidersSidJobsJidArgs, options?: PatchApiApiProjectsPidSpidersSidJobsJidOptions) => { const {baseUrl = "http://127.0.0.1:8000", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects/${encodeURIComponent(String(args.pid))}/spiders/${encodeURIComponent(String(args.sid))}/jobs/${encodeURIComponent(String(args.jid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(spiderJob, await res.json()) };