import {z} from 'zod' export type UserDetail = {username: string, email?: string | undefined}; export type SpiderJobEnvVar = {evid?: (number | null) | undefined, name: string, value: string, masked?: boolean | undefined}; export type ProjectUpdate = {pid?: string | undefined, name?: string | undefined, users?: Array | undefined, email?: string | undefined, action?: ('remove' | 'add' | 'update') | undefined, framework?: ('SCRAPY' | 'REQUESTS') | undefined, env_vars?: Array | undefined, permission?: ('ADMIN' | 'DEVELOPER' | 'VIEWER' | 'OWNER') | undefined, data_status?: ('PERSISTENT' | 'PENDING') | undefined, data_expiry_days?: number | undefined, apply_to_existing_spiders?: boolean | undefined}; export type UpdateApiApiProjectsPidArgs = {body: ProjectUpdate}; export type UpdateApiApiProjectsPidOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const userDetail = z.object({username: z.string().min(1).max(150), email: z.string().max(254).optional()}); 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 projectUpdate = z.object({pid: z.string().optional(), name: z.string().min(1).optional(), users: z.array(userDetail).optional(), email: z.string().min(1).optional(), action: z.enum(["remove", "add", "update"]).optional(), framework: z.enum(["SCRAPY", "REQUESTS"]).optional(), env_vars: z.array(spiderJobEnvVar).optional(), permission: z.enum(["ADMIN", "DEVELOPER", "VIEWER", "OWNER"]).optional(), data_status: z.enum(["PERSISTENT", "PENDING"]).optional(), data_expiry_days: z.number().int().optional(), apply_to_existing_spiders: z.boolean().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 }; /** * PUT /api/projects/{pid} */ export const updateApiApiProjectsPid = async (args: UpdateApiApiProjectsPidArgs, options?: UpdateApiApiProjectsPidOptions) => { 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))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(projectUpdate, await res.json()) };