Skip to content

Commit 766f323

Browse files
alallemabidoubiwa
andcommitted
Adding health method and modify isHealthy method
Co-authored-by: cvermand <[email protected]>
1 parent bc0f2fd commit 766f323

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ get_index_stats_1: |-
146146
get_indexes_stats_1: |-
147147
client.stats()
148148
get_health_1: |-
149-
client.isHealthy()
149+
client.health()
150150
get_version_1: |-
151151
client.version()
152152
distinct_attribute_guide_1: |-

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,17 @@ Or using the index object:
494494

495495
`client.getKeys(): Promise<Keys>`
496496

497-
### Healthy <!-- omit in toc -->
497+
### isHealthy <!-- omit in toc -->
498+
499+
- Return `true` or `false` depending on the health of the server.
500+
501+
`client.isHealthy(): Promise<boolean>`
502+
503+
### Health <!-- omit in toc -->
498504

499505
- Check if the server is healthy
500506

501-
`client.isHealthy(): Promise<true>`
507+
`client.health(): Promise<Health>`
502508

503509
### Stats <!-- omit in toc -->
504510

src/meilisearch.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class MeiliSearch implements Types.MeiliSearchInterface {
2222
} = {
2323
listIndexes: 'indexes',
2424
getKeys: 'keys',
25-
isHealthy: 'health',
25+
health: 'health',
2626
stats: 'stats',
2727
version: 'version',
2828
createDump: 'dumps',
@@ -152,12 +152,27 @@ export class MeiliSearch implements Types.MeiliSearchInterface {
152152
* Checks if the server is healthy, otherwise an error will be thrown.
153153
*
154154
* @memberof MeiliSearch
155+
* @method health
156+
*/
157+
async health(): Promise<Types.Health> {
158+
return await this.httpRequest.get<Types.Health>(
159+
MeiliSearch.apiRoutes.health
160+
)
161+
}
162+
163+
/**
164+
* Checks if the server is healthy, return true or false.
165+
*
166+
* @memberof MeiliSearch
155167
* @method isHealthy
156168
*/
157-
async isHealthy(): Promise<true> {
158-
return await this.httpRequest
159-
.get(MeiliSearch.apiRoutes.isHealthy)
160-
.then(() => true)
169+
async isHealthy(): Promise<boolean> {
170+
try {
171+
await this.httpRequest.get(MeiliSearch.apiRoutes.health)
172+
return true
173+
} catch (e) {
174+
return false
175+
}
161176
}
162177

163178
///

src/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ export interface EnqueuedDump {
200200
status: 'in_progress' | 'failed' | 'done'
201201
}
202202

203+
/*
204+
*** HEALTH
205+
*/
206+
207+
export interface Health {
208+
status: 'available'
209+
}
210+
203211
/*
204212
*** STATS
205213
*/
@@ -256,7 +264,8 @@ export interface MeiliSearchInterface {
256264
) => Promise<Index<T>>
257265
deleteIndex: (uid: string) => Promise<void>
258266
getKeys: () => Promise<Keys>
259-
isHealthy: () => Promise<true>
267+
health: () => Promise<Health>
268+
isHealthy: () => Promise<boolean>
260269
stats: () => Promise<Stats>
261270
version: () => Promise<Version>
262271
createDump: () => Promise<EnqueuedDump>

tests/index_tests.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,24 @@ describe.each([
245245
})
246246
describe('Test on base routes', () => {
247247
test(`${permission} key: get health`, async () => {
248+
await client.health().then((response: Types.Health) => {
249+
expect(response).toHaveProperty(
250+
'status',
251+
expect.stringMatching('available')
252+
)
253+
})
254+
})
255+
test(`${permission} key: is server healthy`, async () => {
248256
await client.isHealthy().then((response: boolean) => {
249257
expect(response).toBe(true)
250258
})
251259
})
260+
test(`${permission} key: is healthy return false on bad host`, async () => {
261+
const client = new MeiliSearch({ host: 'http://localhost:9345' })
262+
await client.isHealthy().then((response: boolean) => {
263+
expect(response).toBe(false)
264+
})
265+
})
252266
test(`${permission} key: get version`, async () => {
253267
await client.version().then((response: Types.Version) => {
254268
expect(response).toHaveProperty('commitSha', expect.any(String))
@@ -263,11 +277,19 @@ describe.each([
263277
expect(response).toHaveProperty('indexes', expect.any(Object))
264278
})
265279
})
280+
test(`${permission} key: bad host raise CommunicationError on health route`, async () => {
281+
const client = new MeiliSearch({ host: 'http://localhost:9345' })
282+
try {
283+
await client.health()
284+
} catch (e) {
285+
expect(e.type).toEqual('MeiliSearchCommunicationError')
286+
}
287+
})
266288
})
267289
})
268290

269291
describe.each([{ client: publicClient, permission: 'Public' }])(
270-
'Test on routes where public key should not have access',
292+
'Test on routes with public key',
271293
({ client, permission }) => {
272294
describe('Test on indexes', () => {
273295
test(`${permission} key: try to get all indexes and be denied`, async () => {
@@ -322,6 +344,14 @@ describe.each([{ client: publicClient, permission: 'Public' }])(
322344
})
323345
})
324346
describe('Test on base routes', () => {
347+
test(`${permission} key: get health`, async () => {
348+
await client.health().then((response: Types.Health) => {
349+
expect(response).toHaveProperty(
350+
'status',
351+
expect.stringMatching('available')
352+
)
353+
})
354+
})
325355
test(`${permission} key: try to get version and be denied`, async () => {
326356
await expect(client.version()).rejects.toHaveProperty(
327357
'errorCode',
@@ -339,7 +369,7 @@ describe.each([{ client: publicClient, permission: 'Public' }])(
339369
)
340370

341371
describe.each([{ client: anonymousClient, permission: 'No' }])(
342-
'Test on routes where client without api key should not have access',
372+
'Test on routes without an API key',
343373
({ client, permission }) => {
344374
describe('Test on indexes', () => {
345375
test(`${permission} key: try to get all indexes and be denied`, async () => {
@@ -394,6 +424,14 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
394424
})
395425
})
396426
describe('Test on base routes', () => {
427+
test(`${permission} key: get health`, async () => {
428+
await client.health().then((response: Types.Health) => {
429+
expect(response).toHaveProperty(
430+
'status',
431+
expect.stringMatching('available')
432+
)
433+
})
434+
})
397435
test(`${permission} key: try to get version and be denied`, async () => {
398436
await expect(client.version()).rejects.toHaveProperty(
399437
'errorCode',

0 commit comments

Comments
 (0)