import {z} from 'zod' export type CreateApiCasesTemplatesArgs = {'X-CSRF-Protection': boolean, body: {case_definition: {inputs: Array<{choices?: Array | undefined, default_value: string, description?: string | undefined, label: string, max?: number | undefined, min?: number | undefined, unit?: string | undefined, value_type: 'number' | 'smiles' | 'string' | 'tuple' | 'array' | 'enum' | 'file', variable_name: string}>, outputs: Array<{cell_index?: number | undefined, description?: string | undefined, figure_index?: number | undefined, name: string, type: 'number' | 'image', unit?: string | undefined, variable_name?: string | undefined}>, validations: Array<{cell_index?: number | undefined, method: 'prompt', name: string, prompt: string}>}, case_template_group: {id: string} | {description: string, title: string}, description: string, files?: {[key: string]: string} | undefined, notebook_content: {[key: string]: unknown}, steps?: Array | undefined, title: string}}; export type CreateApiCasesTemplatesOptions = {baseUrl: string, backendAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCasesTemplatesResponse = z.object({data: z.object({case_definition: z.object({inputs: z.array(z.object({choices: z.array(z.string()), default_value: z.string(), description: z.string(), label: z.string(), max: z.number().optional(), min: z.number().optional(), unit: z.string().optional(), value_type: z.enum(["number", "smiles", "string", "tuple", "array", "enum", "file"]), variable_name: z.string()})), outputs: z.array(z.object({cell_index: z.number().int().gte(0).optional(), description: z.string(), figure_index: z.number().int().gte(0).optional(), name: z.string(), type: z.enum(["number", "image"]), unit: z.string().optional(), variable_name: z.string().optional()})), validations: z.array(z.object({cell_index: z.number().int().gte(0).optional(), method: z.literal("prompt"), name: z.string(), prompt: z.string()}))}), case_template_group_id: z.string(), created_at: z.string(), description: z.string(), id: z.string(), is_official: z.boolean(), owner: z.object({id: z.string(), name: z.string()}).optional(), steps: z.array(z.string()), title: z.string().min(1).max(255), updated_at: z.string()})}); 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 }; /** * Create a new case template * * POST /cases/templates */ export const createApiCasesTemplates = async (args: CreateApiCasesTemplatesArgs, options: CreateApiCasesTemplatesOptions) => { const {baseUrl, fetch: fetchFn = fetch, backendAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-CSRF-Protection'] != null) headers.set("X-CSRF-Protection", String(args['X-CSRF-Protection'])) if (backendAuth !== undefined) cookies.push("mtls-prd=" + encodeURIComponent(backendAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/cases/templates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiCasesTemplatesResponse, await res.json()) };