import {z} from 'zod' export type GetApiApiV0DocumentsArgs = {limit?: number | undefined, offset?: number | undefined, cursor?: (string | null) | undefined, folder_id?: (string | null) | undefined, tag_name?: (string | null) | undefined, owner_id?: (string | null) | undefined, knowledge_base_id?: (string | null) | undefined}; export type GetApiApiV0DocumentsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const documentRenditionResponse = z.object({kind: z.string(), file_path: z.string(), file_url: z.string().nullable().optional()}); export const batchStageProgress = z.object({stage: z.string(), state: z.string(), batches_total: z.number().int(), batches_closed: z.number().int(), requests_total: z.number().int(), requests_closed: z.number().int()}); export const documentLinkResponse = z.object({id: z.string(), name: z.string(), file_type: z.string()}); export const documentGetResponse = z.object({id: z.string(), name: z.string(), folder_id: z.string().nullable(), file_path: z.string().nullable(), file_type: z.string(), file_url: z.string().nullable(), renditions: z.array(documentRenditionResponse).optional(), summary: z.string(), visibility: z.string(), num_pages: z.number().int(), version: z.number().int(), job_status_id: z.string().nullable().optional(), job_status: z.string().nullable().optional(), batch_pending: z.boolean().optional(), batch_stages: z.array(batchStageProgress).optional(), created_at: z.string(), updated_at: z.string(), tags: z.array(z.string()), attachments: z.array(documentLinkResponse).optional(), arrived_on: documentLinkResponse.nullable().optional()}); export const documentListPagination = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), has_more: z.boolean(), next_cursor: z.string().nullable().optional()}); export const documentListResponse = z.object({documents: z.array(documentGetResponse), pagination: documentListPagination}); 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 all documents * * GET /api/v0/documents */ export const getApiApiV0Documents = async (args: GetApiApiV0DocumentsArgs, options: GetApiApiV0DocumentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.folder_id != null) query.append("folder_id", String(args.folder_id)) if (args.tag_name != null) query.append("tag_name", String(args.tag_name)) if (args.owner_id != null) query.append("owner_id", String(args.owner_id)) if (args.knowledge_base_id != null) query.append("knowledge_base_id", String(args.knowledge_base_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/documents${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(documentListResponse, await res.json()) };