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

Update http methods in settings routes v0.28 #1262

Merged
merged 16 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 13 additions & 13 deletions 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 Expand Up @@ -355,7 +355,7 @@ class Index<T = Record<string, any>> {
options?: AddDocumentParams
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
return await this.httpRequest.post(url, documents, options)
return await this.httpRequest.put(url, documents, options)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -446,7 +446,7 @@ class Index<T = Record<string, any>> {
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents/delete-batch`

return await this.httpRequest.post(url, documentsIds)
return await this.httpRequest.put(url, documentsIds)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -485,7 +485,7 @@ class Index<T = Record<string, any>> {
*/
async updateSettings(settings: Settings): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings`
return await this.httpRequest.post(url, settings)
return await this.httpRequest.patch(url, settings)
}

/**
Expand Down Expand Up @@ -523,7 +523,7 @@ class Index<T = Record<string, any>> {
*/
async updateSynonyms(synonyms: Synonyms): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/synonyms`
return await this.httpRequest.post(url, synonyms)
return await this.httpRequest.put(url, synonyms)
}

/**
Expand Down Expand Up @@ -561,7 +561,7 @@ class Index<T = Record<string, any>> {
*/
async updateStopWords(stopWords: StopWords): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/stop-words`
return await this.httpRequest.post(url, stopWords)
return await this.httpRequest.put(url, stopWords)
}

/**
Expand Down Expand Up @@ -599,7 +599,7 @@ class Index<T = Record<string, any>> {
*/
async updateRankingRules(rankingRules: RankingRules): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/ranking-rules`
return await this.httpRequest.post(url, rankingRules)
return await this.httpRequest.put(url, rankingRules)
}

/**
Expand Down Expand Up @@ -639,7 +639,7 @@ class Index<T = Record<string, any>> {
distinctAttribute: DistinctAttribute
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/distinct-attribute`
return await this.httpRequest.post(url, distinctAttribute)
return await this.httpRequest.put(url, distinctAttribute)
}

/**
Expand Down Expand Up @@ -679,7 +679,7 @@ class Index<T = Record<string, any>> {
filterableAttributes: FilterableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/filterable-attributes`
return await this.httpRequest.post(url, filterableAttributes)
return await this.httpRequest.put(url, filterableAttributes)
}

/**
Expand Down Expand Up @@ -719,7 +719,7 @@ class Index<T = Record<string, any>> {
sortableAttributes: SortableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/sortable-attributes`
return await this.httpRequest.post(url, sortableAttributes)
return await this.httpRequest.put(url, sortableAttributes)
}

/**
Expand Down Expand Up @@ -759,7 +759,7 @@ class Index<T = Record<string, any>> {
searchableAttributes: SearchableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/searchable-attributes`
return await this.httpRequest.post(url, searchableAttributes)
return await this.httpRequest.put(url, searchableAttributes)
}

/**
Expand Down Expand Up @@ -799,7 +799,7 @@ class Index<T = Record<string, any>> {
displayedAttributes: DisplayedAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/displayed-attributes`
return await this.httpRequest.post(url, displayedAttributes)
return await this.httpRequest.put(url, displayedAttributes)
}

/**
Expand Down Expand Up @@ -839,7 +839,7 @@ class Index<T = Record<string, any>> {
typoTolerance: TypoTolerance
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/typo-tolerance`
return await this.httpRequest.post(url, typoTolerance)
return await this.httpRequest.patch(url, typoTolerance)
}

/**
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
4 changes: 2 additions & 2 deletions tests/synonyms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
({ permission }) => {
beforeEach(async () => {
const client = await getClient('Master')
const { uid } = await client.index(index.uid).addDocuments(dataset)
await client.waitForTask(uid)
const { taskUid } = await client.index(index.uid).addDocuments(dataset)
await client.waitForTask(taskUid)
})

test(`${permission} key: Get default synonyms`, async () => {
Expand Down
3 changes: 0 additions & 3 deletions tests/utils/meilisearch-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ const clearAllIndexes = async (config: Config): Promise<void> => {
const { taskUid } = await client.index(indexUid).delete()
taskIds.push(taskUid)
}

await client.waitForTasks(taskIds)

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

async function waitForDumpProcessing(
Expand Down