import {z} from 'zod' export type FunctionUpdateRequest = {function: {name?: string | undefined, slug?: string | undefined, lock_version?: number | undefined, description?: string | undefined, code?: string | undefined, invoke_response_mode?: ('wrapped' | 'passthrough') | undefined, runtime_config?: {[key: string]: unknown} | undefined, public_endpoint?: boolean | undefined}}; export type PatchApiFunctionsFunctionIdArgs = {body: FunctionUpdateRequest}; export type PatchApiFunctionsFunctionIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const function = z.object({id: z.string(), name: z.string(), slug: z.string(), lock_version: z.number().int().optional(), description: z.string().nullable().optional(), code: z.string().optional(), version: z.number().int().nullable().optional(), status: z.enum(["draft", "deployed", "error"]), last_deployed_at: z.string().nullable().optional(), function_type: z.enum(["cloudflare_worker", "supabase_function"]), invoke_response_mode: z.enum(["wrapped", "passthrough"]).optional(), public_endpoint: z.boolean().optional(), runtime_config: z.record(z.string(), z.unknown()).nullable().optional(), endpoint_url: z.string().nullable().optional(), project_id: z.string().optional(), created_by_id: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const functionResponse = z.object({data: function}); 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 }; /** * Update function * * PATCH /functions/{function_id} */ export const patchApiFunctionsFunctionId = async (args: PatchApiFunctionsFunctionIdArgs, options?: PatchApiFunctionsFunctionIdOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/functions/${encodeURIComponent(String(args.function_id))}${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(functionResponse, await res.json()) };