import {z} from 'zod' export type Cursor = string; export type PaginationDirection = 'next' | 'prev'; export type GetApiDocumentsV2FilesArgs = {cursor?: (Cursor | null) | undefined, direction?: PaginationDirection | undefined, limit?: number | undefined, file_name?: (string | null) | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined}; export type GetApiDocumentsV2FilesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mimeType = z.enum(["application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.apple.keynote", "image/jpeg", "text/plain", "text/html", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel.sheet.macroEnabled.12", "application/vnd.ms-excel", "text/csv", "image/png"]); export const paginatedResultFileMetadataResponse = z.object({items: z.array(z.object({file_id: z.string(), file_name: z.string().nullable().optional(), mime_type: mimeType, file_size: z.number().int().gte(0), checksum_sha256: z.string(), created_at: z.string(), 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/files */ export const getApiDocumentsV2Files = async (args: GetApiDocumentsV2FilesArgs, options?: GetApiDocumentsV2FilesOptions) => { 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.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/documents/v2/files${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(paginatedResultFileMetadataResponse, await res.json()) };