Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap get indexes routes in results object v0.28 #1261

Merged
merged 13 commits into from
Jun 20, 2022
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,13 @@ If you want to know more about the development workflow or want to contribute, p

### Indexes <!-- omit in toc -->

- [Get all indexes in Index instances](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):
- [Get all indexes as Index instances](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):

`client.getIndexes(): Promise<Index[]>`
`client.getIndexes(): Promise<Result<Index[]>>`

- [Get raw indexes in JSON response from Meilisearch](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):
- [Get all indexes](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):

`client.getRawIndexes(): Promise<IndexResponse[]>`
`client.getRawIndexes(): Promise<Result<IndexResponse[]>>`

- [Create a new index](https://docs.meilisearch.com/reference/api/indexes.html#create-an-index):

Expand Down
12 changes: 7 additions & 5 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,22 @@ class Client {
* Get all the indexes as Index instances.
* @memberof MeiliSearch
* @method getIndexes
* @returns {Promise<Index[]>} Promise returning array of raw index information
*
* @returns {Promise<Result<Index[]>>} Promise returning array of raw index information
*/
async getIndexes(): Promise<Index[]> {
const { results } = await this.getRawIndexes()
const indexes: Index[] = results.map(
async getIndexes(): Promise<Result<Index[]>> {
const rawIndexes = await this.getRawIndexes()
const indexes: Index[] = rawIndexes.results.map(
(index) => new Index(this.config, index.uid, index.primaryKey)
)
return indexes
return { ...rawIndexes, results: indexes }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name rawIndexes confusing because in my first read I understood that your method would result in this response:

{
   "results": [],
   results: [....]
}

I know that's impossible in JS, but in ruby for example isn't (because results: is a symbol, and "results" is a string a different type of object). Anyway, I think you could call the rawIndexes as just response?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand :/

The following will override the results key of rawIndexes with the new results key so that results is an array of Index() and not an array of { uid: 1 }

{ ...rawIndexes, results: indexes }

RawIndexes returns like this:

{
   results: [ { uid: 1 .. } ], 
   limit: 0, 
   // ...
}

and getIndexes returns like this

{
   results: [ Index(uid: 1), Index(uid: 2)], 
   limit: 0, 
   // ...
}

}

/**
* Get all the indexes in their raw value (no Index instances).
* @memberof MeiliSearch
* @method getRawIndexes
*
* @returns {Promise<Result<IndexResponse[]>>} Promise returning array of raw index information
*/
async getRawIndexes(): Promise<Result<IndexResponse[]>> {
Expand Down
2 changes: 1 addition & 1 deletion src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class Index<T = Record<string, any>> {
*/
async update(data: IndexOptions): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}`
return await this.httpRequest.put(url, data)
return await this.httpRequest.patch(url, data)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type IndexOptions = {

export type IndexResponse = {
uid: string
name?: string
primaryKey?: string
createdAt: Date
updatedAt: Date
Expand Down
24 changes: 9 additions & 15 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
IndexResponse,
ErrorStatusCode,
Health,
Version,
Stats,
TaskStatus,
} from '../src'
import { ErrorStatusCode, Health, Version, Stats } from '../src'
import {
clearAllIndexes,
getKey,
Expand Down Expand Up @@ -178,9 +171,10 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(

const task = await client.createIndex('test')
await client.waitForTask(task.taskUid)
const indexes = await client.getIndexes()

expect(indexes.length).toBe(1)
const { results } = await client.getIndexes()

expect(results.length).toBe(1)
})

describe('Test on indexes methods', () => {
Expand Down Expand Up @@ -232,8 +226,8 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
const { taskUid } = await client.createIndex(indexPk.uid)
await client.waitForTask(taskUid)

const response: IndexResponse[] = await client.getRawIndexes()
const indexes = response.map((index) => index.uid)
const { results } = await client.getRawIndexes()
const indexes = results.map((index) => index.uid)
expect(indexes).toEqual(expect.arrayContaining([indexPk.uid]))
expect(indexes.length).toEqual(1)
})
Expand Down Expand Up @@ -296,10 +290,10 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
await client.waitForTask(createTask)

const { taskUid: deleteTask } = await client.deleteIndex(indexNoPk.uid)
const task = await client.waitForTask(deleteTask)
await client.waitForTask(deleteTask)
const { results } = await client.getIndexes()

expect(task.status).toBe(TaskStatus.TASK_SUCCEEDED)
await expect(client.getIndexes()).resolves.toHaveLength(0)
expect(results).toHaveLength(0)
})

test(`${permission} key: create index with already existing uid should fail`, async () => {
Expand Down
18 changes: 11 additions & 7 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
const { taskUid } = await client.createIndex(indexPk.uid)
await client.waitForTask(taskUid)

const response = await client.getRawIndexes()
const { results } = await client.getRawIndexes()

expect(response.length).toEqual(1)
expect(response[0].uid).toEqual(indexPk.uid)
expect(results.length).toEqual(1)
expect(results[0].uid).toEqual(indexPk.uid)
})

test(`${permission} key: Get index that does not exist`, async () => {
Expand Down Expand Up @@ -280,11 +280,13 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
test(`${permission} key: delete index`, async () => {
const client = await getClient(permission)
const { taskUid: createTask } = await client.createIndex(indexNoPk.uid)
const { taskUid: updateTask } = await client.index(indexNoPk.uid).delete()
const { taskUid: deleteTask } = await client.index(indexNoPk.uid).delete()
await client.waitForTask(createTask)
await client.waitForTask(updateTask)
await client.waitForTask(deleteTask)

const { results } = await client.getIndexes()

await expect(client.getIndexes()).resolves.toHaveLength(0)
expect(results).toHaveLength(0)
})

test(`${permission} key: delete index using client`, async () => {
Expand All @@ -293,7 +295,9 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
await client.createIndex(indexPk.uid)
await client.waitForTask(taskUid)

await expect(client.getIndexes()).resolves.toHaveLength(0)
const { results } = await client.getIndexes()

expect(results).toHaveLength(0)
})

test(`${permission} key: fetch deleted index should fail`, async () => {
Expand Down
2 changes: 0 additions & 2 deletions tests/utils/meilisearch-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ const clearAllIndexes = async (config: Config): Promise<void> => {
}

await client.waitForTasks(taskIds)

await expect(client.getIndexes()).resolves.toHaveLength(0)
}

async function waitForDumpProcessing(
Expand Down