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

Feat/snapshots #1603

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ faceted_search_1: |-
client.index('books').search('classic', { facets: ['genres', 'rating', 'language'] })
post_dump_1: |-
client.createDump()
create_snapshot_1: |-
client.createSnapshot()
phrase_search_1: |-
client.index('movies')
.search('"african american" horror')
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,13 @@ client.getVersion(): Promise<Version>
client.createDump(): Promise<EnqueuedTask>
```

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

#### [Trigger a snapshot on-demand process](https://www.meilisearch.com/docs/reference/api/snapshots#create-a-snapshot)

```ts
client.createSnapshot(): Promise<EnqueuedTask>
```
---

Meilisearch provides and maintains many SDKs and integration tools like this one. We want to provide everyone with an **amazing search experience for any kind of project**. For a full overview of everything we create and maintain, take a look at the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
16 changes: 16 additions & 0 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ class Client {
return new EnqueuedTask(task)
}

///
/// SNAPSHOTS
///

/**
* Creates a snapshot
*
* @returns Promise returning object of the enqueued task
*/
async createSnapshot(): Promise<EnqueuedTask> {
const url = `snapshots`
const task = await this.httpRequest.post<undefined, EnqueuedTaskObject>(url)

return new EnqueuedTask(task)
}

///
/// TOKENS
///
Expand Down
70 changes: 70 additions & 0 deletions tests/snapshots.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ErrorStatusCode } from '../src/types'
import {
clearAllIndexes,
config,
MeiliSearch,
BAD_HOST,
getClient,
} from './utils/meilisearch-test-utils'

beforeEach(async () => {
await clearAllIndexes(config)
})

describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
'Test on snapshot',
({ permission }) => {
test(`${permission} key: create a new snapshot`, async () => {
const client = await getClient(permission)
const { taskUid } = await client.createSnapshot()

await client.waitForTask(taskUid)
})
}
)

describe.each([{ permission: 'Search' }])(
'Test on snapshot with search api key should not have access',
({ permission }) => {
test(`${permission} key: try to create snapshot with search key and be denied`, async () => {
const client = await getClient(permission)
await expect(client.createSnapshot()).rejects.toHaveProperty(
'code',
ErrorStatusCode.INVALID_API_KEY
)
})
}
)

describe.each([{ permission: 'No' }])(
'Test on snapshot without api key should not have access',
({ permission }) => {
test(`${permission} key: try to create snapshot with no key and be denied`, async () => {
const client = await getClient(permission)
await expect(client.createSnapshot()).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
}
)

describe.each([
{ host: BAD_HOST, trailing: false },
{ host: `${BAD_HOST}/api`, trailing: false },
{ host: `${BAD_HOST}/trailing/`, trailing: true },
])('Tests on url construction', ({ host, trailing }) => {
test(`Test createSnapshot route`, async () => {
const route = `snapshots`
const client = new MeiliSearch({ host })
const strippedHost = trailing ? host.slice(0, -1) : host

await expect(client.createSnapshot()).rejects.toHaveProperty(
'message',
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
'http://',
''
)}`
)
})
})