import {z} from 'zod' 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 BatchDeleteDocumentsRequest = {document_ids?: (Array | null) | undefined, filters?: (LogicalOperatorInput | null) | undefined, reason?: (string | null) | undefined}; export type DeleteApiV1CollectionsCollectionIdentifierDocumentsBatchArgs = {collection_identifier: string, body: BatchDeleteDocumentsRequest}; export type DeleteApiV1CollectionsCollectionIdentifierDocumentsBatchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const batchDocumentDeleteResult = z.object({document_id: z.string(), success: z.boolean(), error: z.string().nullable().optional()}); export const batchDeleteDocumentsResponse = z.object({task_id: z.string().nullable().optional(), status: z.string().nullable().optional(), deleted_count: z.number().int().nullable().optional(), failed_count: z.number().int().optional(), results: z.array(batchDocumentDeleteResult).optional(), limits: z.record(z.string(), z.number().int()).nullable().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 Delete Documents * * DELETE /v1/collections/{collection_identifier}/documents/batch */ export const deleteApiV1CollectionsCollectionIdentifierDocumentsBatch = async (args: DeleteApiV1CollectionsCollectionIdentifierDocumentsBatchArgs, options?: DeleteApiV1CollectionsCollectionIdentifierDocumentsBatchOptions) => { 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: "DELETE", 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(batchDeleteDocumentsResponse, await res.json()) };