import {z} from 'zod' export type GetApiApiV0DocumentsSearchSearchIdArgs = {search_id: string}; export type GetApiApiV0DocumentsSearchSearchIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const documentRenditionResponse = z.object({kind: z.string(), file_path: z.string(), file_url: z.string().nullable().optional()}); export const batchStageProgress = z.object({stage: z.string(), state: z.string(), batches_total: z.number().int(), batches_closed: z.number().int(), requests_total: z.number().int(), requests_closed: z.number().int()}); export const documentLinkResponse = z.object({id: z.string(), name: z.string(), file_type: z.string()}); export const documentGetResponse = z.object({id: z.string(), name: z.string(), folder_id: z.string().nullable(), file_path: z.string().nullable(), file_type: z.string(), file_url: z.string().nullable(), renditions: z.array(documentRenditionResponse).optional(), summary: z.string(), visibility: z.string(), num_pages: z.number().int(), version: z.number().int(), job_status_id: z.string().nullable().optional(), job_status: z.string().nullable().optional(), batch_pending: z.boolean().optional(), batch_stages: z.array(batchStageProgress).optional(), created_at: z.string(), updated_at: z.string(), tags: z.array(z.string()), attachments: z.array(documentLinkResponse).optional(), arrived_on: documentLinkResponse.nullable().optional()}); export const documentSearchGetResponse = z.object({id: z.string(), user_id: z.string(), query: z.string(), status: z.string(), error_message: z.string().nullable(), documents: z.array(documentGetResponse), created_at: z.string()}); 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 }; /** * Get a document search by ID * * GET /api/v0/documents/search/{search_id} */ export const getApiApiV0DocumentsSearchSearchId = async (args: GetApiApiV0DocumentsSearchSearchIdArgs, options: GetApiApiV0DocumentsSearchSearchIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/documents/search/${encodeURIComponent(String(args.search_id))}${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(documentSearchGetResponse, await res.json()) };