Skip to content

Commit 023c835

Browse files
bidoubiwaalallema
andauthored
Update key route (#1123)
* Change updates to tasks * Complete implementation of asynchronous index CRUD * Add client tests * Add index tests * Add documents test * Improve js doc * Add distinct attributes tests * Add distrinct attributes tests * Remove unused variable from tests * Add dump tests * Add filterable attributes tests * Add ranking rules tests * Add settings tests * Add sortable attr tests * Add stop words * Fix types for Tasks * Add search tests * Finish tests compatibility * Fix tasks issues * Fix tests * Fix task type * Add await in return statement * Fix tests * Fix node12 jest tests * Fix tests environments * Remove obsolete test * Fix tests * Fix document tests * Update update information in readme * Add type check test * Fix types of env * Put back express tests to check for CORS issues * Remove env from typescript type test * Add new keys API * Apply suggestions from code review Co-authored-by: Amélie <[email protected]> Co-authored-by: Amélie <[email protected]>
1 parent 9eb183d commit 023c835

File tree

5 files changed

+382
-25
lines changed

5 files changed

+382
-25
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,26 @@ Using the index object:
600600

601601
### Keys <!-- omit in toc -->
602602

603-
- [Get keys](https://docs.meilisearch.com/reference/api/keys.html#get-keys):
603+
- [Get keys](https://docs.meilisearch.com/reference/api/keys.html#get-all-keys):
604604

605605
`client.getKeys(): Promise<Keys>`
606606

607+
- [Get one key](https://docs.meilisearch.com/reference/api/keys.html#get-one-key):
608+
609+
`client.getKey(key: string): Promise<Key>`
610+
611+
- [Create a key](https://docs.meilisearch.com/reference/api/keys.html#create-a-key):
612+
613+
`client.createKey(options: KeyPayload): Promise<Key>`
614+
615+
- [Update a key](https://docs.meilisearch.com/reference/api/keys.html#update-a-key):
616+
617+
`client.updateKey(key: string, options: KeyPayload): Promise<Key>`
618+
619+
- [Delete a key](https://docs.meilisearch.com/reference/api/keys.html#delete-a-key):
620+
621+
`client.deleteKey(key: string): Promise<void>`
622+
607623
### isHealthy <!-- omit in toc -->
608624

609625
- [Return `true` or `false` depending on the health of the server](https://docs.meilisearch.com/reference/api/health.html#get-health):

src/lib/http-requests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ class HttpRequests {
136136
})
137137
}
138138

139+
async patch(
140+
url: string,
141+
data?: any,
142+
params?: { [key: string]: any },
143+
config?: Record<string, any>
144+
): Promise<any> {
145+
return await this.request({
146+
method: 'PATCH',
147+
url,
148+
body: data,
149+
params,
150+
config,
151+
})
152+
}
153+
139154
async delete(
140155
url: string,
141156
data?: any,

src/lib/meilisearch.ts

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99

1010
import { Index } from './indexes'
1111
import {
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'
2528
import { HttpRequests } from './http-requests'
2629
import { 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`

src/types/types.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export type Config = {
1010
headers?: object
1111
}
1212

13+
export type Result<T> = {
14+
results: T
15+
}
16+
1317
///
1418
/// Request specific interfaces
1519
///
@@ -235,9 +239,21 @@ export type Stats = {
235239
** Keys
236240
*/
237241

238-
export type Keys = {
239-
private: string | null
240-
public: string | null
242+
export type Key = {
243+
description: string
244+
key: string
245+
actions: string[]
246+
indexes: string[]
247+
expiresAt: string
248+
createdAt: string
249+
updateAt: string
250+
}
251+
252+
export type KeyPayload = {
253+
description?: string
254+
actions: string[]
255+
indexes: string[]
256+
expiresAt: string | null
241257
}
242258

243259
/*
@@ -355,6 +371,24 @@ export const enum ErrorStatusCode {
355371
/** @see https://docs.meilisearch.com/errors/#invalid_api_key */
356372
INVALID_API_KEY = 'invalid_api_key',
357373

374+
/** @see https://docs.meilisearch.com/errors/#invalid_api_key_description */
375+
INVALID_API_KEY_DESCRIPTION = 'invalid_api_key_description',
376+
377+
/** @see https://docs.meilisearch.com/errors/#invalid_api_key_actions */
378+
INVALID_API_KEY_ACTIONS = 'invalid_api_key_actions',
379+
380+
/** @see https://docs.meilisearch.com/errors/#invalid_api_key_indexes */
381+
INVALID_API_KEY_INDEXES = 'invalid_api_key_indexes',
382+
383+
/** @see https://docs.meilisearch.com/errors/#invalid_api_key_expires_at */
384+
INVALID_API_KEY_EXPIRES_AT = 'invalid_api_key_expires_at',
385+
386+
/** @see https://docs.meilisearch.com/errors/#api_key_not_found */
387+
API_KEY_NOT_FOUND = 'api_key_not_found',
388+
389+
/** @see https://docs.meilisearch.com/errors/#missing_parameter */
390+
MISSING_PARAMETER = 'missing_parameter',
391+
358392
/** @see https://docs.meilisearch.com/errors/#missing_authorization_header */
359393
MISSING_AUTHORIZATION_HEADER = 'missing_authorization_header',
360394

0 commit comments

Comments
 (0)