import { createNodeHttpRequester } from '@algolia/requester-node-http' ; import type { SearchClient , SearchIndex } from 'algoliasearch' ; import algoliasearch from 'algoliasearch' ; import type { Config } from '../config' ; import { httpAgent , httpsAgent , USER_AGENT } from '../utils/request' ; const requester = createNodeHttpRequester ( { agent : httpsAgent , httpAgent , httpsAgent , } ) ; function createClient ( { appId , apiKey , indexName , } ) { const client = algoliasearch ( appId , apiKey , { requester , } ) ; client . addAlgoliaAgent ( USER_AGENT ) ; return { index : client . initIndex ( indexName ) , client , } ; } /**
 * Prepare algolia for indexing.
 */ async function prepare ( config ) { if ( ! config . apiKey ) { throw new Error ( 'npm-search: Please provide the `apiKey` env variable and restart' ) ; } const { index : mainIndex , client } = createClient ( config ) ; const { index : bootstrapIndex } = createClient ( { appId : config . appId , apiKey : config . apiKey , indexName : config . bootstrapIndexName , } ) ; await mainIndex . setSettings ( { } ) ; await bootstrapIndex . setSettings ( { } ) ; return { client , mainIndex , bootstrapIndex , } ; } async function putDefaultSettings ( index , config ) { await index . setSettings ( config . indexSettings ) ; await index . saveSynonyms ( config . indexSynonyms , { replaceExistingSynonyms : true , } ) ; const { taskID } = await index . saveRules ( config . indexRules , { replaceExistingRules : true , } ) ; await index . waitTask ( taskID ) ; } export { prepare , putDefaultSettings } ;