import {z} from 'zod' export type GetApiApiCalibrationArgs = {limit?: number | undefined, offset?: number | undefined, search?: string | undefined, 'x-csrf-token'?: string | undefined, 'api-key'?: string | undefined, session_id?: string | undefined}; export type GetApiApiCalibrationOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const calibrationStates = z.enum(["processing", "calibrated", "error"]); export const calibrationResponse = z.object({id: z.string(), name: z.string(), user_id: z.string(), f0: z.number().optional(), algorithm: z.string().optional(), bucket: z.string(), key: z.string(), state: calibrationStates, error: z.string().optional(), created_at: z.string(), calibrated_at: z.string().optional(), enabled: z.boolean()}); export const pagination = z.object({count: z.number().int(), limit: z.number().int(), offset: z.number().int()}); export const calibrationListResponse = z.object({list: z.array(calibrationResponse), 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 }; /** * Calibration List * * GET /api/calibration */ export const getApiApiCalibration = async (args: GetApiApiCalibrationArgs, options: GetApiApiCalibrationOptions) => { 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.search != null) query.append("search", String(args.search)) 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/calibration${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(calibrationListResponse, await res.json()) };