Skip to content

Commit

Permalink
Wrap get indexes routes in results object v0.28 (#1261)
Browse files Browse the repository at this point in the history
* Update tasks API

* Add additional tests on index tasks routes

* Update readme api references

* Make task typs an enum

* Update wait for tasks tests

* Update getTasks filter to arrays

* Fix taskUid parameter naming in waitForTask

* Make task tests more relevant

* Update the tests impacted by the task api changes

* Keep task destructing in tests

* Fix linting

* Wrap get indexes routes in results object
  • Loading branch information
bidoubiwa authored Jun 20, 2022
1 parent 1ee79b1 commit b8cec6e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 35 deletions.
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 }
}

/**
* 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

0 comments on commit b8cec6e

Please sign in to comment.