import {z} from 'zod' export type CreateApiOcrPdfToLinesWithLocationArgs = {language?: string | undefined, preprocessing?: string | undefined, body: BodyInit}; export type CreateApiOcrPdfToLinesWithLocationOptions = {baseUrl?: string | undefined, Apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const ocrWordElement = z.object({WordText: z.string().optional(), LineNumber: z.number().int().optional(), WordNumber: z.number().int().optional(), XLeft: z.number().int().optional(), YTop: z.number().int().optional(), Width: z.number().int().optional(), Height: z.number().int().optional(), ConfidenceLevel: z.number().optional(), BlockNumber: z.number().int().optional(), ParagraphNumber: z.number().int().optional(), PageNumber: z.number().int().optional()}); export const ocrLineElement = z.object({LineText: z.string().optional(), Words: z.array(ocrWordElement).optional()}); export const ocrPageResultWithLinesWithLocation = z.object({PageNumber: z.number().int().optional(), Successful: z.boolean().optional(), Lines: z.array(ocrLineElement).optional()}); export const pdfToLinesWithLocationResult = z.object({Successful: z.boolean().optional(), OcrPages: z.array(ocrPageResultWithLinesWithLocation).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 }; /** * Convert a PDF into text lines with location * * POST /ocr/pdf/to/lines-with-location */ export const createApiOcrPdfToLinesWithLocation = async (args: CreateApiOcrPdfToLinesWithLocationArgs, options?: CreateApiOcrPdfToLinesWithLocationOptions) => { const {baseUrl = "http://localhost", fetch: fetchFn = fetch, Apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) headers.set("language", String(args.language)) if (args.preprocessing != null) headers.set("preprocessing", String(args.preprocessing)) if (Apikey !== undefined) headers.set("Apikey", Apikey) const url = `${baseUrl.replace(/\/+$/, '')}/ocr/pdf/to/lines-with-location${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pdfToLinesWithLocationResult, await res.json()) };