Skip to content

Commit 2140aa3

Browse files
Merge #1603
1603: Feat/snapshots r=brunoocasali a=brunoocasali Add the ability to create snapshots according to the v1.5 spec. Co-authored-by: Bruno Casali <[email protected]>
2 parents d37d50a + d9b93ad commit 2140aa3

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ faceted_search_1: |-
549549
client.index('books').search('classic', { facets: ['genres', 'rating', 'language'] })
550550
post_dump_1: |-
551551
client.createDump()
552+
create_snapshot_1: |-
553+
client.createSnapshot()
552554
phrase_search_1: |-
553555
client.index('movies')
554556
.search('"african american" horror')

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,13 @@ client.getVersion(): Promise<Version>
10391039
client.createDump(): Promise<EnqueuedTask>
10401040
```
10411041

1042+
### Snapshots <!-- omit in toc -->
1043+
1044+
#### [Trigger a snapshot on-demand process](https://www.meilisearch.com/docs/reference/api/snapshots#create-a-snapshot)
1045+
1046+
```ts
1047+
client.createSnapshot(): Promise<EnqueuedTask>
1048+
```
10421049
---
10431050

10441051
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.

src/clients/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ class Client {
443443
return new EnqueuedTask(task)
444444
}
445445

446+
///
447+
/// SNAPSHOTS
448+
///
449+
450+
/**
451+
* Creates a snapshot
452+
*
453+
* @returns Promise returning object of the enqueued task
454+
*/
455+
async createSnapshot(): Promise<EnqueuedTask> {
456+
const url = `snapshots`
457+
const task = await this.httpRequest.post<undefined, EnqueuedTaskObject>(url)
458+
459+
return new EnqueuedTask(task)
460+
}
461+
446462
///
447463
/// TOKENS
448464
///

tests/snapshots.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { ErrorStatusCode } from '../src/types'
2+
import {
3+
clearAllIndexes,
4+
config,
5+
MeiliSearch,
6+
BAD_HOST,
7+
getClient,
8+
} from './utils/meilisearch-test-utils'
9+
10+
beforeEach(async () => {
11+
await clearAllIndexes(config)
12+
})
13+
14+
describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
15+
'Test on snapshot',
16+
({ permission }) => {
17+
test(`${permission} key: create a new snapshot`, async () => {
18+
const client = await getClient(permission)
19+
const { taskUid } = await client.createSnapshot()
20+
21+
await client.waitForTask(taskUid)
22+
})
23+
}
24+
)
25+
26+
describe.each([{ permission: 'Search' }])(
27+
'Test on snapshot with search api key should not have access',
28+
({ permission }) => {
29+
test(`${permission} key: try to create snapshot with search key and be denied`, async () => {
30+
const client = await getClient(permission)
31+
await expect(client.createSnapshot()).rejects.toHaveProperty(
32+
'code',
33+
ErrorStatusCode.INVALID_API_KEY
34+
)
35+
})
36+
}
37+
)
38+
39+
describe.each([{ permission: 'No' }])(
40+
'Test on snapshot without api key should not have access',
41+
({ permission }) => {
42+
test(`${permission} key: try to create snapshot with no key and be denied`, async () => {
43+
const client = await getClient(permission)
44+
await expect(client.createSnapshot()).rejects.toHaveProperty(
45+
'code',
46+
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
47+
)
48+
})
49+
}
50+
)
51+
52+
describe.each([
53+
{ host: BAD_HOST, trailing: false },
54+
{ host: `${BAD_HOST}/api`, trailing: false },
55+
{ host: `${BAD_HOST}/trailing/`, trailing: true },
56+
])('Tests on url construction', ({ host, trailing }) => {
57+
test(`Test createSnapshot route`, async () => {
58+
const route = `snapshots`
59+
const client = new MeiliSearch({ host })
60+
const strippedHost = trailing ? host.slice(0, -1) : host
61+
62+
await expect(client.createSnapshot()).rejects.toHaveProperty(
63+
'message',
64+
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
65+
'http://',
66+
''
67+
)}`
68+
)
69+
})
70+
})

0 commit comments

Comments
 (0)