import {z} from 'zod' export type ProbeStatus = 'pending-review' | 'approved' | 'rejected'; export type GetApiProbesArgs = {status?: (ProbeStatus | null) | undefined, limit?: (number | null) | undefined, cursor?: (string | null) | undefined, authorization?: (string | null) | undefined}; export type GetApiProbesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const probeStatus = z.enum(["pending-review", "approved", "rejected"]); export const probeComment = z.object({author: z.string(), verdict: z.enum(["approved", "rejected"]), comment: z.string().nullable(), at: z.string()}); export const probeState = z.object({id: z.string(), site: z.string(), slot: z.string(), subsystem: z.string(), status: probeStatus, mint_run: z.string(), kind: z.enum(["repair", "feature.implement.authored"]).optional(), created_at: z.string(), reviewed_by: z.string().nullable().optional(), reviewed_at: z.string().nullable().optional(), comments: z.array(probeComment).optional()}); export const listProbesResponse = z.object({probes: z.array(probeState), next_cursor: z.string().nullable().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 }; /** * List Probes * * GET /probes */ export const getApiProbes = async (args: GetApiProbesArgs, options: GetApiProbesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/probes${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(listProbesResponse, await res.json()) };