Skip to content

Commit 3984c9f

Browse files
bors[bot]artfuldev
andauthored
Merge #1051
1051: Add an API to get raw index data r=bidoubiwa a=artfuldev Fixes #1041 Contributes to #1046 This PR adds a new shortcut API is added to get the raw index information (the raw JSON response from Meilisearch). I've made the following assumptions: 1. We can use similar comments/language as in `getIndex<T = any>(indexUid: string): Promise<Index<T>>`. 2. Tests need to be added. 3. Tests can be added as a separate commit. 4. We need individual tests to guarantee behavior - I mean, we need to provide tests of the `getRawIndex` API as opposed to adding a single test where we just validate that `index(indexUid).getRawInfo()` is called within `getRawIndex`. 5. We need to add a separate test for `getRawIndex route`. 6. We don't need to update references of `client.index(indexUid).getRawInfo()` to `client.getRawIndex(indexUid)` in existing/prior test cases even if it can be an improvement. Please let me know if any of my assumptions are wrong so I can rework my PR. I've run the `test`, `style:fix`, and `build` scripts locally to verify everything works. Please let me know anything else that needs to be changed. Co-authored-by: Sudarsan Balaji <[email protected]>
2 parents 8c9287a + 2537d89 commit 3984c9f

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ If you want to know more about the development workflow or want to contribute, p
345345
- [Get an index instance completed with information fetched from MeiliSearch](https://docs.meilisearch.com/reference/api/indexes.html#get-one-index):
346346
`client.getIndex<T>(uid: string): Promise<Index<T>>`
347347

348+
- [Get the raw index JSON response from MeiliSearch](https://docs.meilisearch.com/reference/api/indexes.html#get-one-index):
349+
`client.getRawIndex(uid: string): Promise<IndexResponse>`
350+
348351
- Get or create an index if it does not exist:
349352

350353
`client.getOrCreateIndex<T>(uid: string, options?: IndexOptions): Promise<Index<T>>`

src/lib/meilisearch.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class MeiliSearch {
5252
return new Index<T>(this.config, indexUid).fetchInfo()
5353
}
5454

55+
/**
56+
* Gather information about an index by calling MeiliSearch and
57+
* return the raw JSON response
58+
* @memberof MeiliSearch
59+
* @method getRawIndex
60+
*/
61+
async getRawIndex(indexUid: string): Promise<IndexResponse> {
62+
return new Index(this.config, indexUid).getRawInfo()
63+
}
64+
5565
/**
5666
* Get an index or create it if it does not exist
5767
* @memberof MeiliSearch

tests/index_tests.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,26 @@ describe.each([
8989
})
9090
})
9191

92+
test(`${permission} key: Get raw index that exists`, async () => {
93+
await client.createIndex(indexPk.uid)
94+
const response = await client.getRawIndex(indexPk.uid)
95+
expect(response).toHaveProperty('uid', indexPk.uid)
96+
})
97+
9298
test(`${permission} key: Get index that does not exist`, async () => {
9399
await expect(client.getIndex('does_not_exist')).rejects.toHaveProperty(
94100
'errorCode',
95101
ErrorStatusCode.INDEX_NOT_FOUND
96102
)
97103
})
98104

105+
test(`${permission} key: Get raw index that does not exist`, async () => {
106+
await expect(client.getRawIndex('does_not_exist')).rejects.toHaveProperty(
107+
'errorCode',
108+
ErrorStatusCode.INDEX_NOT_FOUND
109+
)
110+
})
111+
99112
test(`${permission} key: Get index info with primary key`, async () => {
100113
const index = await client.createIndex(indexPk.uid, {
101114
primaryKey: indexPk.primaryKey,
@@ -106,6 +119,15 @@ describe.each([
106119
})
107120
})
108121

122+
test(`${permission} key: Get raw index info with primary key`, async () => {
123+
await client.createIndex(indexPk.uid, {
124+
primaryKey: indexPk.primaryKey,
125+
})
126+
const response = await client.getRawIndex(indexPk.uid)
127+
expect(response).toHaveProperty('uid', indexPk.uid)
128+
expect(response).toHaveProperty('primaryKey', indexPk.primaryKey)
129+
})
130+
109131
test(`${permission} key: Get index info with NO primary key`, async () => {
110132
const index = await client.createIndex(indexNoPk.uid)
111133
await index.getRawInfo().then((response: IndexResponse) => {
@@ -114,6 +136,13 @@ describe.each([
114136
})
115137
})
116138

139+
test(`${permission} key: Get raw index info with NO primary key`, async () => {
140+
await client.createIndex(indexNoPk.uid)
141+
const response = await client.getRawIndex(indexNoPk.uid)
142+
expect(response).toHaveProperty('uid', indexNoPk.uid)
143+
expect(response).toHaveProperty('primaryKey', null)
144+
})
145+
117146
test(`${permission} key: fetch index with primary key`, async () => {
118147
const index = await client.createIndex(indexPk.uid, {
119148
primaryKey: indexPk.primaryKey,
@@ -198,6 +227,13 @@ describe.each([
198227
)
199228
})
200229

230+
test(`${permission} key: get deleted raw index should fail`, async () => {
231+
await expect(client.getRawIndex(indexNoPk.uid)).rejects.toHaveProperty(
232+
'errorCode',
233+
ErrorStatusCode.INDEX_NOT_FOUND
234+
)
235+
})
236+
201237
test(`${permission} key: delete index with uid that does not exist should fail`, async () => {
202238
const index = client.index(indexNoPk.uid)
203239
await expect(index.delete()).rejects.toHaveProperty(
@@ -266,6 +302,13 @@ describe.each([{ client: publicClient, permission: 'Public' }])(
266302
).rejects.toHaveProperty('errorCode', ErrorStatusCode.INVALID_TOKEN)
267303
})
268304

305+
test(`${permission} key: try to get raw index and be denied`, async () => {
306+
await expect(client.getRawIndex(indexNoPk.uid)).rejects.toHaveProperty(
307+
'errorCode',
308+
ErrorStatusCode.INVALID_TOKEN
309+
)
310+
})
311+
269312
test(`${permission} key: try to delete index and be denied`, async () => {
270313
await expect(client.index(indexPk.uid).delete()).rejects.toHaveProperty(
271314
'errorCode',
@@ -311,6 +354,13 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
311354
)
312355
})
313356

357+
test(`${permission} key: try to get raw index and be denied`, async () => {
358+
await expect(client.getRawIndex(indexNoPk.uid)).rejects.toHaveProperty(
359+
'errorCode',
360+
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
361+
)
362+
})
363+
314364
test(`${permission} key: try to delete index and be denied`, async () => {
315365
await expect(client.index(indexPk.uid).delete()).rejects.toHaveProperty(
316366
'errorCode',
@@ -364,6 +414,23 @@ describe.each([
364414
)
365415
})
366416

417+
test(`Test getRawIndex route`, async () => {
418+
const route = `indexes/${indexPk.uid}`
419+
const client = new MeiliSearch({ host })
420+
const strippedHost = trailing ? host.slice(0, -1) : host
421+
await expect(client.getRawIndex(indexPk.uid)).rejects.toHaveProperty(
422+
'message',
423+
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
424+
'http://',
425+
''
426+
)}`
427+
)
428+
await expect(client.getRawIndex(indexPk.uid)).rejects.toHaveProperty(
429+
'type',
430+
'MeiliSearchCommunicationError'
431+
)
432+
})
433+
367434
test(`Test updateIndex route`, async () => {
368435
const route = `indexes/${indexPk.uid}`
369436
const client = new MeiliSearch({ host })

0 commit comments

Comments
 (0)