import {z} from 'zod' export type BatchDocumentUpdate = {document_id: string, update_data: {[key: string]: unknown}}; export type FilterOperator = 'eq' | 'ne' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'nin' | 'contains' | 'starts_with' | 'ends_with' | 'regex' | 'exists' | 'is_null' | 'text' | 'phrase' | 'words' | 'geo_radius' | 'geo_bounding_box' | 'geo_polygon'; export type DynamicValue = {type?: 'dynamic' | undefined, field: string}; export type FilterCondition = {field: string, operator?: FilterOperator | undefined, value: DynamicValue | unknown}; export type LogicalOperatorInput = {AND?: (Array | null) | undefined, OR?: (Array | null) | undefined, NOT?: (Array | null) | undefined, case_sensitive?: (boolean | null) | undefined} | {[key: string]: unknown}; export type BatchUpdateDocumentsRequest = {updates?: (Array | null) | undefined, filters?: (LogicalOperatorInput | null) | undefined, update_data?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiV1CollectionsCollectionIdentifierDocumentsBatchArgs = {collection_identifier: string, body: BatchUpdateDocumentsRequest}; export type CreateApiV1CollectionsCollectionIdentifierDocumentsBatchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const batchDocumentUpdateResult = z.object({document_id: z.string(), success: z.boolean(), error: z.string().nullable().optional()}); export const batchUpdateDocumentsResponse = z.object({updated_count: z.number().int(), failed_count: z.number().int().optional(), results: z.array(batchDocumentUpdateResult).optional(), message: z.string().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 }; /** * Batch Update Documents * * POST /v1/collections/{collection_identifier}/documents/batch */ export const createApiV1CollectionsCollectionIdentifierDocumentsBatch = async (args: CreateApiV1CollectionsCollectionIdentifierDocumentsBatchArgs, options?: CreateApiV1CollectionsCollectionIdentifierDocumentsBatchOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth, NamespaceHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) if (NamespaceHeader !== undefined) headers.set("X-Namespace", NamespaceHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/collections/${encodeURIComponent(String(args.collection_identifier))}/documents/batch${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(batchUpdateDocumentsResponse, await res.json()) };