'use strict' ; import { Index } from './indexes' ; import { KeyPayload , Config , IndexOptions , IndexResponse , EnqueuedTask , Key , Health , Stats , Version , EnqueuedDump , ErrorStatusCode , Task , Result , } from '../types' ; import { HttpRequests } from './http-requests' ; import { addProtocolIfNotPresent } from './utils' ; import { TaskClient } from './task' ; class MeiliSearch { config : Config ; httpRequest : HttpRequests ; tasks : TaskClient ; param { Config } config constructor ( config ) { config . host = addProtocolIfNotPresent ( config . host ) ; config . host = HttpRequests . addTrailingSlash ( config . host ) ; this . config = config ; this . httpRequest = new HttpRequests ( config ) ; this . tasks = new TaskClient ( config ) ; } memberof method template T param { string } indexUid returns { Index < T > } index < T = any > ( indexUid ) { return new Index < T > ( this . config , indexUid ) ; } memberof method template T param { string } indexUid returns { Promise < Index < T > > } async getIndex < T = any > ( indexUid ) { return new Index < T > ( this . config , indexUid ) . fetchInfo ( ) ; } memberof method param { string } indexUid returns { Promise < IndexResponse > } async getRawIndex ( indexUid ) { return new Index ( this . config , indexUid ) . getRawInfo ( ) ; } memberof method returns { Promise < IndexResponse [ ] > } async getIndexes ( ) { const url = `indexes` ; return await this . httpRequest . get < IndexResponse [ ] > ( url ) ; } memberof method template T param { string } uid param { IndexOptions } options returns { Promise < Index < T > > } async createIndex ( uid , options = { } ) { return await Index . create ( uid , options , this . config ) ; } memberof method template T param { string } uid param { IndexOptions } options returns { Promise < Index < T > > } async updateIndex ( uid , options = { } ) { return await new Index ( this . config , uid ) . update ( options ) ; } memberof method param { string } uid returns { Promise < void > } async deleteIndex ( uid ) { return await new Index ( this . config , uid ) . delete ( ) ; } memberof method param { string } uid returns { Promise < boolean > } async deleteIndexIfExists ( uid ) { try { await this . deleteIndex ( uid ) ; return true ; } catch ( e ) { if ( e . code === ErrorStatusCode . INDEX_NOT_FOUND ) { return false ; } throw e ; } } memberof method returns { Promise < Result < Task [ ] > > } async getTasks ( ) { return await this . tasks . getClientTasks ( ) ; } memberof method param { number } taskId returns { Promise < Task > } async getTask ( taskId ) { return await this . tasks . getClientTask ( taskId ) ; } memberof method param { number [ ] } taskIds param { WaitOptions } waitOptions returns { Promise < Result < Task [ ] > > } async waitForTasks ( taskIds , { timeOutMs = 5000 , intervalMs = 50 , } = { } ) { return await this . tasks . waitForClientTasks ( taskIds , { timeOutMs , intervalMs } ) ; } memberof method param { number } taskId param { WaitOptions } waitOptions returns { Promise < Task > } async waitForTask ( taskId , { timeOutMs = 5000 , intervalMs = 50 , } = { } ) { return await this . tasks . waitForClientTask ( taskId , { timeOutMs , intervalMs } ) ; } memberof method returns { Promise < Keys > } async getKeys ( ) { const url = `keys` ; return await this . httpRequest . get < Result < Key [ ] > > ( url ) ; } memberof method param { string } key returns { Promise < Keys > } async getKey ( key ) { const url = `keys/${ key }` ; return await this . httpRequest . get < Key > ( url ) ; } memberof method param { KeyPayload } options returns { Promise < Key > } async createKey ( options ) { const url = `keys` ; return await this . httpRequest . post ( url , options ) ; } memberof method param { string } key param { KeyPayload } options returns { Promise < Key > } async updateKey ( key , options ) { const url = `keys/${ key }` ; return await this . httpRequest . patch ( url , options ) ; } memberof method param { string } key returns { Promise < Void > } async deleteKey ( key ) { const url = `keys/${ key }` ; return await this . httpRequest . delete < any > ( url ) ; } memberof method returns { Promise < Health > } async health ( ) { const url = `health` ; return await this . httpRequest . get < Health > ( url ) ; } memberof method returns { Promise < boolean > } async isHealthy ( ) { try { const url = `health` ; await this . httpRequest . get ( url ) ; return true ; } catch ( e ) { return false ; } } memberof method returns { Promise < Stats > } async getStats ( ) { const url = `stats` ; return await this . httpRequest . get < Stats > ( url ) ; } memberof method returns { Promise < Version > } async getVersion ( ) { const url = `version` ; return await this . httpRequest . get < Version > ( url ) ; } memberof method returns { Promise < EnqueuedDump > } async createDump ( ) { const url = `dumps` ; return await this . httpRequest . post < undefined , EnqueuedDump > ( url ) ; } memberof method param { string } dumpUid returns { Promise < EnqueuedDump > } async getDumpStatus ( dumpUid ) { const url = `dumps/${ dumpUid }/status` ; return await this . httpRequest . get < EnqueuedDump > ( url ) ; } } export { MeiliSearch } ;