import {z} from 'zod' export type GetApiApiV1WatchesWatchIdDeliveriesArgs = {watch_id: string, limit?: number | undefined, beforeId?: (number | null) | undefined}; export type GetApiApiV1WatchesWatchIdDeliveriesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryResponse = z.object({id: z.number().int(), watchId: z.string(), refreshLogId: z.number().int().nullable().optional(), attemptedAt: z.string(), statusCode: z.number().int().nullable().optional(), success: z.boolean(), error: z.string().nullable().optional(), attemptNumber: z.number().int()}); export const deliveriesPage = z.object({deliveries: z.array(deliveryResponse), consecutiveFailures: z.number().int(), consecutiveFailuresTruncated: z.boolean(), deliveryHealth: z.enum(["ok", "failing", "blocked"])}); export const responseMeta = z.object({processingTimeMs: z.number(), creditsConsumed: z.number(), cursor: z.number().int().nullable().optional(), nextBeforeId: z.number().int().nullable().optional(), hasMore: z.boolean().nullable().optional()}); export const deliveriesEnvelope = z.object({data: deliveriesPage, meta: responseMeta}); 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 delivery history for a watch * * GET /api/v1/watches/{watch_id}/deliveries * * Also accepts auth: ApiKeyHeader */ export const getApiApiV1WatchesWatchIdDeliveries = async (args: GetApiApiV1WatchesWatchIdDeliveriesArgs, options?: GetApiApiV1WatchesWatchIdDeliveriesOptions) => { const {baseUrl = "https://api.vaquill.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.beforeId != null) query.append("beforeId", String(args.beforeId)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/watches/${encodeURIComponent(String(args.watch_id))}/deliveries${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(deliveriesEnvelope, await res.json()) };