import {z} from 'zod' export type BoundingBox = {x0: number, y0: number, x1: number, y1: number, dpi?: number | undefined, class_name?: (string | null) | undefined, text?: (string | null) | undefined, confidence?: (number | null) | undefined}; export type ComponentCreateSpec = {type: string, page_number: number, bbox: BoundingBox, label?: (string | null) | undefined, parent_id?: (string | null) | undefined}; export type ComponentUpdateSpec = {component_id: string, type?: (string | null) | undefined, bbox?: (BoundingBox | null) | undefined}; export type ComponentMergeSpec = {component_ids: Array}; export type ComponentReprocessingRequest = {component_ids?: (Array | null) | undefined, reprocess_all_pending?: boolean | undefined, creates?: (Array | null) | undefined, updates?: (Array | null) | undefined, deletes?: (Array | null) | undefined, merges?: (Array | null) | undefined}; export type CreateApiApiV0DocumentsDocumentIdComponentReprocessingJobsArgs = {document_id: string, body: ComponentReprocessingRequest}; export type CreateApiApiV0DocumentsDocumentIdComponentReprocessingJobsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const componentReprocessingResponse = z.object({job_status_id: z.string(), status: z.string(), component_count: z.number().int()}); 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 }; /** * Create a component reprocessing job * * POST /api/v0/documents/{document_id}/component-reprocessing-jobs */ export const createApiApiV0DocumentsDocumentIdComponentReprocessingJobs = async (args: CreateApiApiV0DocumentsDocumentIdComponentReprocessingJobsArgs, options: CreateApiApiV0DocumentsDocumentIdComponentReprocessingJobsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/documents/${encodeURIComponent(String(args.document_id))}/component-reprocessing-jobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(componentReprocessingResponse, await res.json()) };