๐Ÿ“ฆ nestjs / terminus

๐Ÿ“„ health-check.schema.ts ยท 52 lines
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
40
41
42
43
44
45
46
47
48
49
50
51
52import { type HealthCheckStatus } from './health-check-result.interface';
import { type HealthIndicatorResult } from '../health-indicator';
import type {} from '@nestjs/swagger';

// These examples will be displayed on Swagger
const DB_EXAMPLE: HealthIndicatorResult = { database: { status: 'up' } };
const REDIS_EXAMPLE: HealthIndicatorResult = {
  redis: { status: 'down', message: 'Could not connect' },
};
const COMBINED_EXAMPLE: HealthIndicatorResult = {
  ...DB_EXAMPLE,
  ...REDIS_EXAMPLE,
};

const healthIndicatorSchema = (example: HealthIndicatorResult) => ({
  type: 'object',
  example,
  additionalProperties: {
    type: 'object',
    required: ['status'],
    properties: {
      status: {
        type: 'string',
      },
    },
    additionalProperties: true,
  },
});

export function getHealthCheckSchema(status: HealthCheckStatus) {
  return {
    type: 'object',
    properties: {
      status: {
        type: 'string',
        example: status,
      },
      info: {
        ...healthIndicatorSchema(DB_EXAMPLE),
        nullable: true,
      },
      error: {
        ...healthIndicatorSchema(status === 'error' ? REDIS_EXAMPLE : {}),
        nullable: true,
      },
      details: healthIndicatorSchema(
        status === 'error' ? COMBINED_EXAMPLE : DB_EXAMPLE,
      ),
    },
  };
}