import {z} from 'zod' export type GetApiApiV1HooksRunsArgs = {request_id?: string | undefined, uuid?: string | undefined, hook?: number | undefined, timestamp_before?: string | undefined, timestamp_after?: string | undefined, start_before?: string | undefined, start_after?: string | undefined, status?: ('waiting' | 'running' | 'completed' | 'cancelled' | 'failed') | undefined, status_code?: number | undefined, queue?: Array | undefined, annotation?: Array | undefined, email?: number | undefined, search?: string | undefined, page_size?: number | undefined}; export type GetApiApiV1HooksRunsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const hookLogsResponse = z.object({results: z.array(z.object({timestamp: z.string().optional(), request_id: z.string().optional(), event: z.string().optional(), action: z.string().optional(), annotation_id: z.number().int().nullable().optional(), queue_id: z.number().int().nullable().optional(), email_id: z.number().int().nullable().optional(), hook_id: z.number().int().optional(), hook_type: z.string().optional(), organization_id: z.number().int().optional(), log_level: z.enum(["INFO", "WARNING", "ERROR"]).optional(), message: z.string().optional(), request: z.string().nullable().optional(), response: z.string().nullable().optional(), output: z.string().nullable().optional(), start: z.string().optional(), end: z.string().nullable().optional(), settings: z.object({}).nullable().optional(), status: z.enum(["waiting", "running", "completed", "cancelled", "failed"]).optional(), status_code: z.number().int().optional(), uuid: z.string().optional()})), 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 }; /** * List hook runs * * GET /api/v1/hooks/runs */ export const getApiApiV1HooksRuns = async (args: GetApiApiV1HooksRunsArgs, options?: GetApiApiV1HooksRunsOptions) => { const {baseUrl = "https://example.rossum.app", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.request_id != null) query.append("request_id", String(args.request_id)) if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.hook != null) query.append("hook", String(args.hook)) if (args.timestamp_before != null) query.append("timestamp_before", String(args.timestamp_before)) if (args.timestamp_after != null) query.append("timestamp_after", String(args.timestamp_after)) if (args.start_before != null) query.append("start_before", String(args.start_before)) if (args.start_after != null) query.append("start_after", String(args.start_after)) if (args.status != null) query.append("status", String(args.status)) if (args.status_code != null) query.append("status_code", String(args.status_code)) if (args.queue != null) query.append("queue", args.queue.join(',')) if (args.annotation != null) query.append("annotation", args.annotation.join(',')) if (args.email != null) query.append("email", String(args.email)) if (args.search != null) query.append("search", String(args.search)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/hooks/runs${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(hookLogsResponse, await res.json()) };