import {z} from 'zod' export type Cursor = string; export type PaginationDirection = 'next' | 'prev'; export type ParseStatus = 'pending' | 'processing' | 'successful' | 'failure'; export type GetApiDocumentsV2DatasetsDatasetIdDataArgs = {dataset_id: string, cursor?: (Cursor | null) | undefined, direction?: PaginationDirection | undefined, limit?: number | undefined, status?: (ParseStatus | null) | undefined, parse_id?: (string | null) | undefined, file_name?: (string | null) | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, finished_after?: (string | null) | undefined, finished_before?: (string | null) | undefined}; export type GetApiDocumentsV2DatasetsDatasetIdDataOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const parseStatus = z.enum(["pending", "processing", "successful", "failure"]); export const pageFragmentType = z.enum(["section_header", "title", "text", "table", "figure", "formula", "form", "key_value_region", "document_index", "list_item", "table_caption", "figure_caption", "formula_caption", "page_footer", "page_header", "page_number", "signature", "strikethrough"]); export const pageFragmentTableCell = z.object({text: z.string(), bounding_box: z.record(z.string(), z.number())}); export const pageFragmentTable = z.object({content: z.string(), summary: z.string().nullable().optional(), cells: z.array(pageFragmentTableCell), html: z.string().nullable().optional(), markdown: z.string().nullable().optional()}); export const pageFragmentFigure = z.object({content: z.string(), summary: z.string().nullable().optional()}); export const pageFragmentListItem = z.object({content: z.string()}); export const pageFragmentSignature = z.object({content: z.string()}); export const pageFragmentTypeContent = z.union([z.string(), pageFragmentTable, pageFragmentFigure, pageFragmentListItem, pageFragmentSignature]); export const pageFragment = z.object({fragment_type: pageFragmentType, content: pageFragmentTypeContent, reading_order: z.number().int().nullable().optional(), bbox: z.record(z.string(), z.number()).nullable().optional()}); export const page = z.object({page_number: z.number().int().gte(0), page_fragments: z.array(pageFragment).nullable().optional(), dimensions: z.array(z.number().int()).nullable().optional()}); export const chunk = z.object({content: z.string(), page_number: z.number().int().gte(0)}); export const oneOrManyUsize = z.union([z.number().int().gte(0), z.array(z.number().int().gte(0))]); export const structuredData = z.object({data: z.unknown(), page_numbers: oneOrManyUsize, schema_name: z.string().nullable().optional()}); export const pageClass = z.object({page_class: z.string(), page_numbers: z.array(z.number().int())}); export const paginatedResultParseResult = z.object({items: z.array(z.object({parse_id: z.string().optional(), dataset_id: z.string().nullable().optional(), parsed_pages_count: z.number().int().gte(0).optional(), status: parseStatus.optional(), error: z.string().nullable().optional(), pages: z.array(page).nullable().optional(), chunks: z.array(chunk).optional(), structured_data: z.array(structuredData).nullable().optional(), page_classes: z.array(pageClass).nullable().optional(), created_at: z.string().optional(), finished_at: z.string().nullable().optional(), labels: z.record(z.string(), z.unknown()).optional()})), has_more: z.boolean(), next_cursor: z.string().nullable().optional(), prev_cursor: z.string().nullable().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 }; /** * GET /documents/v2/datasets/{dataset_id}/data */ export const getApiDocumentsV2DatasetsDatasetIdData = async (args: GetApiDocumentsV2DatasetsDatasetIdDataArgs, options?: GetApiDocumentsV2DatasetsDatasetIdDataOptions) => { const {baseUrl = "https://api.tensorlake.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.status != null) query.append("status", String(args.status)) if (args.parse_id != null) query.append("parse_id", String(args.parse_id)) if (args.file_name != null) query.append("file_name", String(args.file_name)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.finished_after != null) query.append("finished_after", String(args.finished_after)) if (args.finished_before != null) query.append("finished_before", String(args.finished_before)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/documents/v2/datasets/${encodeURIComponent(String(args.dataset_id))}/data${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(paginatedResultParseResult, await res.json()) };