import {z} from 'zod' export type Cursor = string; export type PaginationDirection = 'next' | 'prev'; export type DatasetStatus = 'idle' | 'processing'; export type GetApiDocumentsV2DatasetsArgs = {cursor?: (Cursor | null) | undefined, direction?: PaginationDirection | undefined, limit?: number | undefined, status?: (DatasetStatus | null) | undefined, name?: (string | null) | undefined}; export type GetApiDocumentsV2DatasetsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const datasetStatus = z.enum(["idle", "processing"]); export const paginatedResultDataset = z.object({items: z.array(z.object({name: z.string(), dataset_id: z.string(), description: z.string().nullable().optional(), status: datasetStatus, created_at: z.string(), updated_at: z.string()})), 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 */ export const getApiDocumentsV2Datasets = async (args: GetApiDocumentsV2DatasetsArgs, options?: GetApiDocumentsV2DatasetsOptions) => { 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.name != null) query.append("name", String(args.name)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/documents/v2/datasets${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(paginatedResultDataset, await res.json()) };