import {z} from 'zod' export type UserDetail = {username: string, email?: string | undefined}; export type Permission = {user?: UserDetail | undefined, permission?: ('OWNER' | 'ADMIN' | 'DEVELOPER' | 'VIEWER') | undefined}; export type SpiderJobEnvVar = {evid?: (number | null) | undefined, name: string, value: string, masked?: boolean | undefined}; export type Project = {pid?: string | undefined, name: string, category?: ('NOT SPECIFIED' | 'E-COMMERCE' | 'LOGISTICS' | 'FINANCE' | 'EDUCATIONAL' | 'TECHNOLOGY' | 'OTHER_CATEGORY') | undefined, framework?: ('SCRAPY' | 'REQUESTS') | undefined, container_image?: string | undefined, users?: Array | undefined, env_vars?: Array | undefined, data_status?: ('PERSISTENT' | 'PENDING') | undefined, data_expiry_days?: number | undefined, created?: string | undefined, last_modified?: string | undefined, owner_org_id?: (string | null) | undefined, owner_email?: (string | null) | undefined, billing_account_id?: string | undefined, pending_owner_email?: string | undefined}; export type PatchApiApiProjectsPidArgs = {body: Project}; export type PatchApiApiProjectsPidOptions = {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 permission = z.object({user: userDetail.optional(), permission: z.enum(["OWNER", "ADMIN", "DEVELOPER", "VIEWER"]).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 project = z.object({pid: z.string().optional(), name: z.string().min(1).max(1000), category: z.enum(["NOT SPECIFIED", "E-COMMERCE", "LOGISTICS", "FINANCE", "EDUCATIONAL", "TECHNOLOGY", "OTHER_CATEGORY"]).optional(), framework: z.enum(["SCRAPY", "REQUESTS"]).optional(), container_image: z.string().min(1).optional(), users: z.array(permission).optional(), env_vars: z.array(spiderJobEnvVar).optional(), data_status: z.enum(["PERSISTENT", "PENDING"]).optional(), data_expiry_days: z.number().int().gte(0).lte(65535).optional(), created: z.string().optional(), last_modified: z.string().optional(), owner_org_id: z.string().min(1).max(255).nullable().optional(), owner_email: z.string().min(1).nullable().optional(), billing_account_id: z.string().min(1).optional(), pending_owner_email: z.string().min(1).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} */ export const patchApiApiProjectsPid = async (args: PatchApiApiProjectsPidArgs, options?: PatchApiApiProjectsPidOptions) => { 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: "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(project, await res.json()) };