import {z} from 'zod' export type GetIteratorRequest = {document_id?: (number | null) | undefined, skip?: number | undefined, take?: number | undefined, include_deleted?: boolean | undefined, include_document?: boolean | undefined, fields?: Array | undefined}; export type CreateApiApiV1IndexIndexIdIteratorArgs = {apikey: string, index_id: number, body: GetIteratorRequest}; export type CreateApiApiV1IndexIndexIdIteratorOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const iteratorResultItem = z.object({doc_id: z.number().int().gte(0), doc: z.record(z.string(), z.unknown()).nullable().optional()}); export const iteratorResult = z.object({skip: z.number().int().gte(0), results: z.array(iteratorResultItem)}); 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 }; /** * Document iterator * * POST /api/v1/index/{index_id}/iterator */ export const createApiApiV1IndexIndexIdIterator = async (args: CreateApiApiV1IndexIndexIdIteratorArgs, options?: CreateApiApiV1IndexIndexIdIteratorOptions) => { const {baseUrl = "http://127.0.0.1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.apikey != null) headers.set("apikey", String(args.apikey)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/index/${encodeURIComponent(String(args.index_id))}/iterator${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(iteratorResult, await res.json()) };