'use strict' ; import { MeiliSearchError } from '../errors' ; import { Config , Task , SearchResponse , SearchParams , Filter , SearchRequestGET , IndexResponse , IndexOptions , IndexStats , GetDocumentsParams , GetDocumentsResponse , Document , AddDocumentParams , EnqueuedTask , Settings , Synonyms , StopWords , RankingRules , DistinctAttribute , FilterableAttributes , SortableAttributes , SearchableAttributes , DisplayedAttributes , Result , } from '../types' ; import { removeUndefinedFromObject } from './utils' ; import { HttpRequests } from './http-requests' ; import { TaskClient } from './task' ; class Index < T = Record < string , any > > { uid : string ; primaryKey : string | undefined ; httpRequest : HttpRequests ; tasks : TaskClient ; param { Config } config param { string } uid param { string } primaryKey constructor ( config , uid , primaryKey ? ) { this . uid = uid ; this . primaryKey = primaryKey ; this . httpRequest = new HttpRequests ( config ) ; this . tasks = new TaskClient ( config ) ; } memberof method template T param { string | null } query param { SearchParams } options param { Partial < Request > } config returns { Promise < SearchResponse < T > > } async search < T = Record < string , any > > ( query ? , options ? , config ? ) { const url = `indexes/${ this . uid }/search` ; return await this . httpRequest . post ( url , removeUndefinedFromObject ( { ... options , q : query } ) , undefined , config ) ; } memberof method template T param { string | null } query param { SearchParams } options param { Partial < Request > } config returns { Promise < SearchResponse < T > > } async searchGet < T = Record < string , any > > ( query ? , options ? , config ? ) { const url = `indexes/${ this . uid }/search` ; const parseFilter = ( filter ? ) : string | undefined => { if ( typeof filter === 'string' ) return filter ; else if ( Array . isArray ( filter ) ) throw new MeiliSearchError ( 'The filter query parameter should be in string format when using searchGet' ) ; else return undefined ; } ; const getParams = { q : query , ... options , filter : parseFilter ( options ?. filter ) , sort : options ?. sort ? options . sort . join ( ',' ) : undefined , facetsDistribution : options ?. facetsDistribution ? options . facetsDistribution . join ( ',' ) : undefined , attributesToRetrieve : options ?. attributesToRetrieve ? options . attributesToRetrieve . join ( ',' ) : undefined , attributesToCrop : options ?. attributesToCrop ? options . attributesToCrop . join ( ',' ) : undefined , attributesToHighlight : options ?. attributesToHighlight ? options . attributesToHighlight . join ( ',' ) : undefined } ; return await this . httpRequest . get < SearchResponse < T > > ( url , removeUndefinedFromObject ( getParams ) , config ) ; } memberof method returns { Promise < IndexResponse > } async getRawInfo ( ) { const url = `indexes/${ this . uid }` ; const res = await this . httpRequest . get < IndexResponse > ( url ) ; this . primaryKey = res . primaryKey ; return res ; } memberof method returns { Promise < this > } async fetchInfo ( ) { await this . getRawInfo ( ) ; return this ; } memberof method returns { Promise < string | undefined > } async fetchPrimaryKey ( ) { this . primaryKey = ( await this . getRawInfo ( ) ) . primaryKey ; return this . primaryKey ; } memberof method template T param { string } uid param { IndexOptions } options param { Config } config returns { Promise < Index < T > > } static async create ( uid , options = { } , config ) { const url = `indexes` ; const req = new HttpRequests ( config ) ; return req . post ( url , { ... options , uid } ) ; } memberof method param { IndexOptions } data returns { Promise < this > } async update ( data ) { const url = `indexes/${ this . uid }` ; return await this . httpRequest . put ( url , data ) ; } memberof method returns { Promise < void > } async delete ( ) { const url = `indexes/${ this . uid }` ; return await this . httpRequest . delete ( url ) ; } memberof method returns { Promise < Result < Task [ ] > > } async getTasks ( ) { return await this . tasks . getIndexTasks ( this . uid ) ; } memberof method param { number } taskId returns { Promise < Task > } async getTask ( taskId ) { return await this . tasks . getIndexTask ( this . uid , 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 < IndexStats > } async getStats ( ) { const url = `indexes/${ this . uid }/stats` ; return await this . httpRequest . get < IndexStats > ( url ) ; } memberof method template T param { GetDocumentsParams < T > } options returns { Promise < GetDocumentsResponse < T > > } async getDocuments < T = Record < string , any > > ( options ? ) { const url = `indexes/${ this . uid }/documents` ; let attr ; if ( options !== undefined && Array . isArray ( options . attributesToRetrieve ) ) { attr = options . attributesToRetrieve . join ( ',' ) ; } return await this . httpRequest . get < GetDocumentsResponse < T > > ( url , { ... options , ... ( attr !== undefined ? { attributesToRetrieve : attr } : { } ) } ) ; } memberof method template T param { string | number } documentId returns { Promise < Document < T > > } async getDocument ( documentId ) { const url = `indexes/${ this . uid }/documents/${ documentId }` ; return await this . httpRequest . get < Document < T > > ( url ) ; } memberof method template T param { Array < Document < T > > } documents param { AddDocumentParams } options returns { Promise < EnqueuedTask > } async addDocuments ( documents , options ? ) { const url = `indexes/${ this . uid }/documents` ; return await this . httpRequest . post ( url , documents , options ) ; } memberof method template T param { Array < Document < T > > } documents param { number } batchSize param { AddDocumentParams } options returns { Promise < EnqueuedTasks > } async addDocumentsInBatches ( documents , batchSize = 1000 , options ? ) { const updates = [ ] ; for ( let i = 0 ; i < documents . length ; i += batchSize ) { updates . push ( await this . addDocuments ( documents . slice ( i , i + batchSize ) , options ) ) ; } return updates ; } memberof method param { Array < Document < T > > } documents param { AddDocumentParams } options returns { Promise < EnqueuedTask > } async updateDocuments ( documents , options ? ) { const url = `indexes/${ this . uid }/documents` ; return await this . httpRequest . put ( url , documents , options ) ; } memberof method template T param { Array < Document < T > > } documents param { number } batchSize param { AddDocumentParams } options returns { Promise < EnqueuedTasks > } async updateDocumentsInBatches ( documents , batchSize = 1000 , options ? ) { const updates = [ ] ; for ( let i = 0 ; i < documents . length ; i += batchSize ) { updates . push ( await this . updateDocuments ( documents . slice ( i , i + batchSize ) , options ) ) ; } return updates ; } memberof method param { string | number } documentId returns { Promise < EnqueuedTask > } async deleteDocument ( documentId ) { const url = `indexes/${ this . uid }/documents/${ documentId }` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method param { string [ ] | number [ ] } documentsIds returns { Promise < EnqueuedTask > } async deleteDocuments ( documentsIds ) { const url = `indexes/${ this . uid }/documents/delete-batch` ; return await this . httpRequest . post ( url , documentsIds ) ; } memberof method returns { Promise < EnqueuedTask > } async deleteAllDocuments ( ) { const url = `indexes/${ this . uid }/documents` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < Settings > } async getSettings ( ) { const url = `indexes/${ this . uid }/settings` ; return await this . httpRequest . get < Settings > ( url ) ; } memberof method param { Settings } settings returns { Promise < EnqueuedTask > } async updateSettings ( settings ) { const url = `indexes/${ this . uid }/settings` ; return await this . httpRequest . post ( url , settings ) ; } memberof method returns { Promise < EnqueuedTask > } async resetSettings ( ) { const url = `indexes/${ this . uid }/settings` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < object > } async getSynonyms ( ) { const url = `indexes/${ this . uid }/settings/synonyms` ; return await this . httpRequest . get < object > ( url ) ; } memberof method param { Synonyms } synonyms returns { Promise < EnqueuedTask > } async updateSynonyms ( synonyms ) { const url = `indexes/${ this . uid }/settings/synonyms` ; return await this . httpRequest . post ( url , synonyms ) ; } memberof method returns { Promise < EnqueuedTask > } async resetSynonyms ( ) { const url = `indexes/${ this . uid }/settings/synonyms` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] > } async getStopWords ( ) { const url = `indexes/${ this . uid }/settings/stop-words` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { StopWords } stopWords returns { Promise < EnqueuedTask > } async updateStopWords ( stopWords ) { const url = `indexes/${ this . uid }/settings/stop-words` ; return await this . httpRequest . post ( url , stopWords ) ; } memberof method returns { Promise < EnqueuedTask > } async resetStopWords ( ) { const url = `indexes/${ this . uid }/settings/stop-words` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] } async getRankingRules ( ) { const url = `indexes/${ this . uid }/settings/ranking-rules` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { RankingRules } rankingRules returns { Promise < EnqueuedTask > } async updateRankingRules ( rankingRules ) { const url = `indexes/${ this . uid }/settings/ranking-rules` ; return await this . httpRequest . post ( url , rankingRules ) ; } memberof method returns { Promise < EnqueuedTask > } async resetRankingRules ( ) { const url = `indexes/${ this . uid }/settings/ranking-rules` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string | null > } async getDistinctAttribute ( ) { const url = `indexes/${ this . uid }/settings/distinct-attribute` ; return await this . httpRequest . get < string | null > ( url ) ; } memberof method param { DistinctAttribute } distinctAttribute returns { Promise < EnqueuedTask > } async updateDistinctAttribute ( distinctAttribute ) { const url = `indexes/${ this . uid }/settings/distinct-attribute` ; return await this . httpRequest . post ( url , distinctAttribute ) ; } memberof method returns { Promise < EnqueuedTask > } async resetDistinctAttribute ( ) { const url = `indexes/${ this . uid }/settings/distinct-attribute` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] > } async getFilterableAttributes ( ) { const url = `indexes/${ this . uid }/settings/filterable-attributes` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { FilterableAttributes } filterableAttributes returns { Promise < EnqueuedTask > } async updateFilterableAttributes ( filterableAttributes ) { const url = `indexes/${ this . uid }/settings/filterable-attributes` ; return await this . httpRequest . post ( url , filterableAttributes ) ; } memberof method returns { Promise < EnqueuedTask > } async resetFilterableAttributes ( ) { const url = `indexes/${ this . uid }/settings/filterable-attributes` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] > } async getSortableAttributes ( ) { const url = `indexes/${ this . uid }/settings/sortable-attributes` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { SortableAttributes } sortableAttributes returns { Promise < EnqueuedTask > } async updateSortableAttributes ( sortableAttributes ) { const url = `indexes/${ this . uid }/settings/sortable-attributes` ; return await this . httpRequest . post ( url , sortableAttributes ) ; } memberof method returns { Promise < EnqueuedTask > } async resetSortableAttributes ( ) { const url = `indexes/${ this . uid }/settings/sortable-attributes` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] > } async getSearchableAttributes ( ) { const url = `indexes/${ this . uid }/settings/searchable-attributes` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { SearchableAttributes } searchableAttributes returns { Promise < EnqueuedTask > } async updateSearchableAttributes ( searchableAttributes ) { const url = `indexes/${ this . uid }/settings/searchable-attributes` ; return await this . httpRequest . post ( url , searchableAttributes ) ; } memberof method returns { Promise < EnqueuedTask > } async resetSearchableAttributes ( ) { const url = `indexes/${ this . uid }/settings/searchable-attributes` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } memberof method returns { Promise < string [ ] > } async getDisplayedAttributes ( ) { const url = `indexes/${ this . uid }/settings/displayed-attributes` ; return await this . httpRequest . get < string [ ] > ( url ) ; } memberof method param { DisplayedAttributes } displayedAttributes returns { Promise < EnqueuedTask > } async updateDisplayedAttributes ( displayedAttributes ) { const url = `indexes/${ this . uid }/settings/displayed-attributes` ; return await this . httpRequest . post ( url , displayedAttributes ) ; } memberof method returns { Promise < EnqueuedTask > } async resetDisplayedAttributes ( ) { const url = `indexes/${ this . uid }/settings/displayed-attributes` ; return await this . httpRequest . delete < EnqueuedTask > ( url ) ; } } export { Index } ;