import {z} from 'zod' export type ProjectSortFields = 'name' | 'created_at' | 'last_recording_at'; export type Direction = 'asc' | 'desc'; export type GetApiApiProjectsArgs = {limit?: number | undefined, offset?: number | undefined, sort?: ProjectSortFields | undefined, direction?: Direction | undefined, owner?: string | undefined, 'x-csrf-token'?: string | undefined, 'api-key'?: string | undefined, session_id?: string | undefined}; export type GetApiApiProjectsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const projectResponse = z.object({name: z.string().max(40), slug: z.string().max(60), owner: z.string(), url: z.string().max(200), models: z.record(z.string(), z.record(z.string(), z.string())), id: z.string(), active: z.boolean(), created_at: z.string(), last_recording_at: z.string()}); export const pagination = z.object({count: z.number().int(), limit: z.number().int(), offset: z.number().int()}); export const projectListResponse = z.object({list: z.array(projectResponse), pagination: pagination}); 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 }; /** * Project List * * GET /api/projects */ export const getApiApiProjects = async (args: GetApiApiProjectsArgs, options: GetApiApiProjectsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.owner != null) query.append("owner", String(args.owner)) if (args['x-csrf-token'] != null) headers.set("x-csrf-token", String(args['x-csrf-token'])) if (args['api-key'] != null) headers.set("api-key", String(args['api-key'])) if (args.session_id != null) cookies.push("session_id=" + encodeURIComponent(String(args.session_id))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(projectListResponse, await res.json()) };