import {z} from 'zod' export type GetApiApiV1PagesPageIdTextElementsArgs = {page_id: string, bbox_x_min?: (number | string | null) | undefined, bbox_x_max?: (number | string | null) | undefined, bbox_y_min?: (number | string | null) | undefined, bbox_y_max?: (number | string | null) | undefined}; export type GetApiApiV1PagesPageIdTextElementsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const textElement = z.object({id: z.string(), page_id: z.string(), extraction_id: z.string(), content: z.string(), bbox_x: z.string(), bbox_y: z.string(), bbox_width: z.string(), bbox_height: z.string(), font_family: z.string().nullable().optional(), font_size: z.string().nullable().optional(), rotation: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional()}); export const textElementListResponse = z.object({items: z.array(textElement), truncated: z.boolean().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 }; /** * List Text Elements * * GET /api/v1/pages/{page_id}/text-elements */ export const getApiApiV1PagesPageIdTextElements = async (args: GetApiApiV1PagesPageIdTextElementsArgs, options: GetApiApiV1PagesPageIdTextElementsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bbox_x_min != null) query.append("bbox_x_min", String(args.bbox_x_min)) if (args.bbox_x_max != null) query.append("bbox_x_max", String(args.bbox_x_max)) if (args.bbox_y_min != null) query.append("bbox_y_min", String(args.bbox_y_min)) if (args.bbox_y_max != null) query.append("bbox_y_max", String(args.bbox_y_max)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/pages/${encodeURIComponent(String(args.page_id))}/text-elements${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(textElementListResponse, await res.json()) };