import {z} from 'zod' export type SearchIndexCreateRequest = {collectionName: string, database: string, name: string, type?: ('search' | 'vectorSearch') | undefined}; export type CreateApiApiAtlasV2GroupsGroupIdClustersClusterNameSearchIndexesArgs = {envelope?: boolean | undefined, groupId: string, pretty?: boolean | undefined, clusterName: string, body: SearchIndexCreateRequest}; export type CreateApiApiAtlasV2GroupsGroupIdClustersClusterNameSearchIndexesOptions = {baseUrl?: string | undefined, ServiceAccounts?: string | undefined, fetch?: typeof fetch | undefined}; export const searchIndexDefinition = z.object({numPartitions: z.number().int().optional(), storedSource: z.object({}).optional()}); export const searchIndexDefinitionVersion = z.object({createdAt: z.string().optional(), version: z.number().int().optional()}); export const searchMainIndexStatusDetail = z.object({definition: searchIndexDefinition.optional(), definitionVersion: searchIndexDefinitionVersion.optional(), message: z.string().optional(), queryable: z.boolean().optional(), status: z.enum(["DELETING", "FAILED", "STALE", "PENDING", "BUILDING", "READY", "DOES_NOT_EXIST"]).optional()}); export const searchStagedIndexStatusDetail = z.object({definition: searchIndexDefinition.optional(), definitionVersion: searchIndexDefinitionVersion.optional(), message: z.string().optional(), queryable: z.boolean().optional(), status: z.enum(["DELETING", "FAILED", "STALE", "PENDING", "BUILDING", "READY", "DOES_NOT_EXIST"]).optional()}); export const searchHostStatusDetail = z.object({hostname: z.string().optional(), mainIndex: searchMainIndexStatusDetail.optional(), queryable: z.boolean().optional(), stagedIndex: searchStagedIndexStatusDetail.optional(), status: z.enum(["DELETING", "FAILED", "STALE", "PENDING", "BUILDING", "READY", "DOES_NOT_EXIST"]).optional()}); export const searchIndexResponse = z.object({collectionName: z.string().optional(), database: z.string().optional(), indexID: z.string().optional(), latestDefinition: searchIndexDefinition.optional(), latestDefinitionVersion: searchIndexDefinitionVersion.optional(), name: z.string().optional(), queryable: z.boolean().optional(), status: z.enum(["DELETING", "FAILED", "STALE", "PENDING", "BUILDING", "READY", "DOES_NOT_EXIST"]).optional(), statusDetail: z.array(searchHostStatusDetail).optional(), type: z.enum(["search", "vectorSearch"]).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 One Atlas Search Index * * POST /api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes * * Also accepts auth: DigestAuth */ export const createApiApiAtlasV2GroupsGroupIdClustersClusterNameSearchIndexes = async (args: CreateApiApiAtlasV2GroupsGroupIdClustersClusterNameSearchIndexesArgs, options?: CreateApiApiAtlasV2GroupsGroupIdClustersClusterNameSearchIndexesOptions) => { const {baseUrl = "https://cloud.mongodb.com", fetch: fetchFn = fetch, ServiceAccounts} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.atlas.2024-05-30+json") headers.set('accept', "application/vnd.atlas.2024-05-30+json") if (args.envelope != null) query.append("envelope", String(args.envelope)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (ServiceAccounts !== undefined) headers.set('authorization', `Bearer ${ServiceAccounts}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/atlas/v2/groups/${encodeURIComponent(String(args.groupId))}/clusters/${encodeURIComponent(String(args.clusterName))}/search/indexes${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(searchIndexResponse, await res.json()) };