import {z} from 'zod' export type GetApiDocumentsArgs = {limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiDocumentsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const documentListItem = z.object({id: z.string(), workspace_id: z.string(), filename: z.string(), content_hash: z.string(), size_bytes: z.number().int(), type: z.enum(["markdown", "text", "pdf", "html", "docx", "pptx", "xlsx", "image"]), mime_type: z.string().nullable().optional(), conversion_status: z.enum(["not_required", "pending", "completed", "failed"]).optional(), original_filename: z.string().nullable().optional(), original_sha256: z.string().nullable().optional(), normalized_text_hash: z.string().nullable().optional(), converter: z.string().nullable().optional(), uploaded_by_contributor_id: z.string().nullable().optional(), author: z.string().nullable().optional(), tags: z.array(z.string()).optional(), source_url: z.string().nullable().optional(), description: z.string().nullable().optional(), source_authority: z.string().nullable().optional(), original_date: z.string().nullable().optional(), effective_from: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), extraction_count: z.number().int().optional(), scope_summary: z.record(z.string(), z.number().int()).nullable().optional()}); export const documentListResponse = z.object({documents: z.array(documentListItem), next_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 }; /** * List documents in workspace * * GET /documents */ export const getApiDocuments = async (args: GetApiDocumentsArgs, options: GetApiDocumentsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = 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.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/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()) };