-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
health-check.decorator.ts
39 lines (31 loc) · 1.03 KB
/
health-check.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { getHealthCheckSchema } from './health-check.schema';
type Swagger = typeof import('@nestjs/swagger');
const noop = () => {};
function getSwaggerDefinitions(swagger: Swagger) {
const { ApiOkResponse, ApiServiceUnavailableResponse } = swagger;
// Possible HTTP Status
const ServiceUnavailable = ApiServiceUnavailableResponse({
description: 'The Health Check is not successful',
schema: getHealthCheckSchema('error'),
});
const Ok = ApiOkResponse({
description: 'The Health Check is successful',
schema: getHealthCheckSchema('ok'),
});
// Combine all the SwaggerDecorators
return (target: any, key: any, descriptor: PropertyDescriptor) => {
ServiceUnavailable(target, key, descriptor);
Ok(target, key, descriptor);
};
}
export const HealthCheck = () => {
let swagger: Swagger | null = null;
try {
// Dynamically load swagger, in case it is not installed
swagger = require('@nestjs/swagger');
} catch {}
if (swagger) {
return getSwaggerDefinitions(swagger);
}
return noop;
};