99
1010import { Index } from './indexes'
1111import {
12+ KeyPayload ,
1213 Config ,
1314 IndexOptions ,
1415 IndexResponse ,
1516 EnqueuedTask ,
16- Keys ,
17+ Key ,
1718 Health ,
1819 Stats ,
1920 Version ,
2021 EnqueuedDump ,
2122 ErrorStatusCode ,
2223 Task ,
2324 Tasks ,
25+ // FIXME: Should be used in GET /keys
26+ // Result,
2427} from '../types'
2528import { HttpRequests } from './http-requests'
2629import { addProtocolIfNotPresent } from './utils'
@@ -62,7 +65,7 @@ class MeiliSearch {
6265 * @method getIndex
6366 * @template T
6467 * @param {string } indexUid The index UID
65- * @returns {Promise<Index<T>> } Promise containing Index instance
68+ * @returns {Promise<Index<T>> } Promise returning Index instance
6669 */
6770 async getIndex < T = any > ( indexUid : string ) : Promise < Index < T > > {
6871 return new Index < T > ( this . config , indexUid ) . fetchInfo ( )
@@ -74,7 +77,7 @@ class MeiliSearch {
7477 * @memberof MeiliSearch
7578 * @method getRawIndex
7679 * @param {string } indexUid The index UID
77- * @returns {Promise<IndexResponse> } Promise containing index information
80+ * @returns {Promise<IndexResponse> } Promise returning index information
7881 */
7982 async getRawIndex ( indexUid : string ) : Promise < IndexResponse > {
8083 return new Index ( this . config , indexUid ) . getRawInfo ( )
@@ -87,7 +90,7 @@ class MeiliSearch {
8790 * @template T
8891 * @param {string } uid The index UID
8992 * @param {IndexOptions } options Index options
90- * @returns {Promise<Index<T>> } Promise containing Index instance
93+ * @returns {Promise<Index<T>> } Promise returning Index instance
9194 */
9295 // TODO: to discuss
9396 // async getOrCreateIndex<T = any>(
@@ -109,7 +112,7 @@ class MeiliSearch {
109112 * Get all indexes in the database
110113 * @memberof MeiliSearch
111114 * @method getIndexes
112- * @returns {Promise<IndexResponse[]> } Promise containing array of raw index information
115+ * @returns {Promise<IndexResponse[]> } Promise returning array of raw index information
113116 */
114117 async getIndexes ( ) : Promise < IndexResponse [ ] > {
115118 const url = `indexes`
@@ -123,7 +126,7 @@ class MeiliSearch {
123126 * @template T
124127 * @param {string } uid The index UID
125128 * @param {IndexOptions } options Index options
126- * @returns {Promise<Index<T>> } Promise containing Index instance
129+ * @returns {Promise<Index<T>> } Promise returning Index instance
127130 */
128131 async createIndex (
129132 uid : string ,
@@ -139,7 +142,7 @@ class MeiliSearch {
139142 * @template T
140143 * @param {string } uid The index UID
141144 * @param {IndexOptions } options Index options to update
142- * @returns {Promise<Index<T>> } Promise containing Index instance after updating
145+ * @returns {Promise<Index<T>> } Promise returning Index instance after updating
143146 */
144147 async updateIndex (
145148 uid : string ,
@@ -186,7 +189,7 @@ class MeiliSearch {
186189 * Get the list of all client tasks
187190 * @memberof MeiliSearch
188191 * @method getTasks
189- * @returns {Promise<Tasks> } - Promise containing all tasks
192+ * @returns {Promise<Tasks> } - Promise returning all tasks
190193 */
191194 async getTasks ( ) : Promise < Tasks > {
192195 return await this . tasks . getClientTasks ( )
@@ -197,7 +200,7 @@ class MeiliSearch {
197200 * @memberof MeiliSearch
198201 * @method getTask
199202 * @param {number } taskId - Task identifier
200- * @returns {Promise<Task> } - Promise containing a task
203+ * @returns {Promise<Task> } - Promise returning a task
201204 */
202205 async getTask ( taskId : number ) : Promise < Task > {
203206 return await this . tasks . getClientTask ( taskId )
@@ -210,7 +213,7 @@ class MeiliSearch {
210213 * @param {number[] } taskIds - Tasks identifier
211214 * @param {WaitOptions } waitOptions - Options on timeout and interval
212215 *
213- * @returns {Promise<Tasks> } - Promise containing an array of tasks
216+ * @returns {Promise<Tasks> } - Promise returning an array of tasks
214217 */
215218 async waitForTasks (
216219 taskIds : number [ ] ,
@@ -233,7 +236,7 @@ class MeiliSearch {
233236 * @param {number } taskId - Task identifier
234237 * @param {WaitOptions } waitOptions - Options on timeout and interval
235238 *
236- * @returns {Promise<Task> } - Promise containing an array of tasks
239+ * @returns {Promise<Task> } - Promise returning an array of tasks
237240 */
238241 async waitForTask (
239242 taskId : number ,
@@ -253,14 +256,68 @@ class MeiliSearch {
253256 ///
254257
255258 /**
256- * Get private and public key
259+ * Get all API keys
260+ * @memberof MeiliSearch
261+ * @method getKeys
262+ * @returns {Promise<Keys> } Promise returning an object with keys
263+ */
264+ // FIXME: should be Result<Key[]>>
265+ async getKeys ( ) : Promise < Key [ ] > {
266+ const url = `keys`
267+ return await this . httpRequest . get < Key [ ] > ( url )
268+ }
269+
270+ /**
271+ * Get one API key
257272 * @memberof MeiliSearch
258273 * @method getKey
259- * @returns {Promise<Keys> } Promise containing an object with keys
274+ *
275+ * @param {string } key - Key
276+ * @returns {Promise<Keys> } Promise returning a key
277+ */
278+ async getKey ( key : string ) : Promise < Key > {
279+ const url = `keys/${ key } `
280+ return await this . httpRequest . get < Key > ( url )
281+ }
282+
283+ /**
284+ * Create one API key
285+ * @memberof MeiliSearch
286+ * @method createKey
287+ *
288+ * @param {KeyPayload } options - Key options
289+ * @returns {Promise<Key> } Promise returning an object with keys
260290 */
261- async getKeys ( ) : Promise < Keys > {
291+ async createKey ( options : KeyPayload ) : Promise < Key > {
262292 const url = `keys`
263- return await this . httpRequest . get < Keys > ( url )
293+ return await this . httpRequest . post ( url , options )
294+ }
295+
296+ /**
297+ * Update one API key
298+ * @memberof MeiliSearch
299+ * @method updateKey
300+ *
301+ * @param {string } key - Key
302+ * @param {KeyPayload } options - Key options
303+ * @returns {Promise<Key> } Promise returning an object with keys
304+ */
305+ async updateKey ( key : string , options : KeyPayload ) : Promise < Key > {
306+ const url = `keys/${ key } `
307+ return await this . httpRequest . patch ( url , options )
308+ }
309+
310+ /**
311+ * Delete one API key
312+ * @memberof MeiliSearch
313+ * @method deleteKey
314+ *
315+ * @param {string } key - Key
316+ * @returns {Promise<Void> }
317+ */
318+ async deleteKey ( key : string ) : Promise < void > {
319+ const url = `keys/${ key } `
320+ return await this . httpRequest . delete < any > ( url )
264321 }
265322
266323 ///
@@ -271,7 +328,7 @@ class MeiliSearch {
271328 * Checks if the server is healthy, otherwise an error will be thrown.
272329 * @memberof MeiliSearch
273330 * @method health
274- * @returns {Promise<Health> } Promise containing an object with health details
331+ * @returns {Promise<Health> } Promise returning an object with health details
275332 */
276333 async health ( ) : Promise < Health > {
277334 const url = `health`
@@ -282,7 +339,7 @@ class MeiliSearch {
282339 * Checks if the server is healthy, return true or false.
283340 * @memberof MeiliSearch
284341 * @method isHealthy
285- * @returns {Promise<boolean> } Promise containing a boolean
342+ * @returns {Promise<boolean> } Promise returning a boolean
286343 */
287344 async isHealthy ( ) : Promise < boolean > {
288345 try {
@@ -302,7 +359,7 @@ class MeiliSearch {
302359 * Get the stats of all the database
303360 * @memberof MeiliSearch
304361 * @method getStats
305- * @returns {Promise<Stats> } Promise containing object of all the stats
362+ * @returns {Promise<Stats> } Promise returning object of all the stats
306363 */
307364 async getStats ( ) : Promise < Stats > {
308365 const url = `stats`
@@ -317,7 +374,7 @@ class MeiliSearch {
317374 * Get the version of MeiliSearch
318375 * @memberof MeiliSearch
319376 * @method getVersion
320- * @returns {Promise<Version> } Promise containing object with version details
377+ * @returns {Promise<Version> } Promise returning object with version details
321378 */
322379 async getVersion ( ) : Promise < Version > {
323380 const url = `version`
@@ -332,7 +389,7 @@ class MeiliSearch {
332389 * Triggers a dump creation process
333390 * @memberof MeiliSearch
334391 * @method createDump
335- * @returns {Promise<EnqueuedDump> } Promise containing object of the enqueued update
392+ * @returns {Promise<EnqueuedDump> } Promise returning object of the enqueued update
336393 */
337394 async createDump ( ) : Promise < EnqueuedDump > {
338395 const url = `dumps`
@@ -344,7 +401,7 @@ class MeiliSearch {
344401 * @memberof MeiliSearch
345402 * @method getDumpStatus
346403 * @param {string } dumpUid Dump UID
347- * @returns {Promise<EnqueuedDump> } Promise containing object of the enqueued update
404+ * @returns {Promise<EnqueuedDump> } Promise returning object of the enqueued update
348405 */
349406 async getDumpStatus ( dumpUid : string ) : Promise < EnqueuedDump > {
350407 const url = `dumps/${ dumpUid } /status`
0 commit comments