import {z} from 'zod' export type PostApiViewSemanticFieldDef = {'[field-name]': {size: number, distanceType: 'L2' | 'IP' | 'COSINE', indexConfig: {type: 'hnsw' | 'ivf' | 'flat', maxSize?: number | undefined, nLinks?: number | undefined, efConstruction?: number | undefined, efSearch?: number | undefined, nProbe?: number | undefined, sample?: number | undefined}, quantizationConfig?: ({type: 'none' | 'pq' | 'sq', bits?: number | undefined}) | undefined}}; export type PostApiViewSemanticFields = {fields?: PostApiViewSemanticFieldDef | undefined}; export type PostApiViewLinkSemanticProps = {'[collection-name]'?: PostApiViewSemanticFields | undefined}; export type PostApiViewSemantic = {name: string, links?: PostApiViewLinkSemanticProps | undefined, type: string}; export type PostApiViewFields = {'[field-name]'?: Record | undefined}; export type PostApiViewLinkProps = {analyzers?: Array | undefined, fields?: PostApiViewFields | undefined, includeAllFields?: boolean | undefined, storeValues?: string | undefined, trackListPositions?: boolean | undefined}; export type PostApiViewLinks = {'[collection-name]'?: PostApiViewLinkProps | undefined}; export type PostApiViewIresearch = {name: string, primarySort?: (Array<{field?: string | undefined, direction?: string | undefined}>) | undefined, links?: PostApiViewLinks | undefined, type: string}; export type CreateApiFabricFabricApiSearchViewArgs = {fabric: unknown, body: PostApiViewSemantic | PostApiViewIresearch}; export type CreateApiFabricFabricApiSearchViewOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postApiViewFields = z.object({"[field-name]": z.object({}).optional()}); export const postApiViewLinkProps = z.object({analyzers: z.array(z.string()).optional(), fields: postApiViewFields.optional(), includeAllFields: z.boolean().optional(), storeValues: z.string().optional(), trackListPositions: z.boolean().optional()}); export const postApiViewLinks = z.object({"[collection-name]": postApiViewLinkProps.optional()}); export const apiSearchPostViewHttpExamplesRc201 = z.object({globallyUniqueId: z.string().optional(), id: z.string().optional(), name: z.string().optional(), primarySort: z.array(z.object({field: z.string().optional(), direction: z.string().optional()})).optional(), links: postApiViewLinks.optional(), type: 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 }; /** * Create search view * * POST /_fabric/{fabric}/_api/search/view * * Also accepts auth: BearerAuth */ export const createApiFabricFabricApiSearchView = async (args: CreateApiFabricFabricApiSearchViewArgs, options: CreateApiFabricFabricApiSearchViewOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/_fabric/${encodeURIComponent(String(args.fabric))}/_api/search/view${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(apiSearchPostViewHttpExamplesRc201, await res.json()) };