@nathapp/nestjs-health 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/package.json +5 -5
  2. package/.eslintrc.js +0 -8
  3. package/.nax/features/nestjs-health/.nax-acceptance.test.ts +0 -756
  4. package/.turbo/turbo-build.log +0 -4
  5. package/CHANGELOG.md +0 -45
  6. package/lib/constant/health.constants.ts +0 -23
  7. package/lib/constant/index.ts +0 -1
  8. package/lib/controller/health.controller.ts +0 -37
  9. package/lib/controller/index.ts +0 -1
  10. package/lib/index.ts +0 -6
  11. package/lib/indicator/cache-health.check.ts +0 -59
  12. package/lib/indicator/generic-health.check.ts +0 -43
  13. package/lib/indicator/index.ts +0 -4
  14. package/lib/indicator/prisma-health.check.ts +0 -59
  15. package/lib/indicator/queue-health.check.ts +0 -72
  16. package/lib/interface/health-indicator.interface.ts +0 -18
  17. package/lib/interface/health-module-options.interface.ts +0 -85
  18. package/lib/interface/index.ts +0 -2
  19. package/lib/module/health.module.ts +0 -62
  20. package/lib/module/index.ts +0 -1
  21. package/lib/service/health-executor.service.ts +0 -69
  22. package/lib/service/index.ts +0 -1
  23. package/test/constant/health.constants.spec.ts +0 -34
  24. package/test/controller/health.controller.spec.ts +0 -117
  25. package/test/indicator/cache-health.check.spec.ts +0 -88
  26. package/test/indicator/generic-health.check.spec.ts +0 -61
  27. package/test/indicator/prisma-health.check.spec.ts +0 -69
  28. package/test/indicator/queue-health.check.spec.ts +0 -88
  29. package/test/interface/health-module-options.spec.ts +0 -59
  30. package/test/jest-nax.json +0 -27
  31. package/test/jest-test.json +0 -28
  32. package/test/module/health.module.spec.ts +0 -311
  33. package/test/service/health-executor.service.spec.ts +0 -186
  34. package/tsconfig.json +0 -9
@@ -1,311 +0,0 @@
1
- import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
2
- import { INestApplication } from '@nestjs/common';
3
- import { Test } from '@nestjs/testing';
4
- import * as supertest from 'supertest';
5
- import {
6
- CacheHealthCheck,
7
- CACHE_HEALTH_SOURCE,
8
- GenericHealthCheck,
9
- HealthExecutorService,
10
- HealthModule,
11
- PrismaHealthCheck,
12
- QueueHealthCheck,
13
- } from '../../lib';
14
-
15
- describe('HealthModule', () => {
16
- describe('AC1: register() returns a dynamic module marked global', () => {
17
- it('imports the HealthModule class and exposes register()', () => {
18
- expect(HealthModule).toBeDefined();
19
- expect(typeof HealthModule.register).toBe('function');
20
- });
21
-
22
- it('register({}) returns a DynamicModule with global=true, module=HealthModule, and providers', () => {
23
- const dynamicModule = HealthModule.register({});
24
-
25
- expect(dynamicModule).toBeDefined();
26
- expect(dynamicModule.module).toBe(HealthModule);
27
- expect(dynamicModule.global).toBe(true);
28
- expect(Array.isArray(dynamicModule.providers)).toBe(true);
29
- expect((dynamicModule.providers as unknown[]).length).toBeGreaterThan(0);
30
- });
31
-
32
- it('register() imports TerminusModule so consumers get HealthCheckService', () => {
33
- const dynamicModule = HealthModule.register({});
34
-
35
- expect(Array.isArray(dynamicModule.imports)).toBe(true);
36
- const imports = dynamicModule.imports as unknown[];
37
- expect(imports.length).toBeGreaterThan(0);
38
- expect(
39
- imports.some((imp) => {
40
- const name = (imp as { name?: string }).name;
41
- return name === 'TerminusModule';
42
- }),
43
- ).toBe(true);
44
- });
45
-
46
- it('register() auto-provides the four adapters and the executor', () => {
47
- const dynamicModule = HealthModule.register({});
48
- const providers = (dynamicModule.providers ?? []) as unknown[];
49
-
50
- const providerTokens = providers
51
- .map((p) => {
52
- if (typeof p === 'function') {
53
- return p;
54
- }
55
- if (p && typeof p === 'object') {
56
- const obj = p as { provide?: unknown; useClass?: unknown };
57
- return obj.provide ?? obj.useClass;
58
- }
59
- return undefined;
60
- })
61
- .filter(Boolean);
62
-
63
- const hasToken = (token: unknown) =>
64
- providerTokens.some((t) => t === token);
65
-
66
- expect(hasToken(CacheHealthCheck)).toBe(true);
67
- expect(hasToken(PrismaHealthCheck)).toBe(true);
68
- expect(hasToken(QueueHealthCheck)).toBe(true);
69
- expect(hasToken(GenericHealthCheck)).toBe(true);
70
- expect(hasToken(HealthExecutorService)).toBe(true);
71
- });
72
- });
73
-
74
- describe('AC2: default app serves GET /health/live at HTTP 200', () => {
75
- let app: INestApplication;
76
-
77
- beforeAll(async () => {
78
- const moduleRef = await Test.createTestingModule({
79
- imports: [HealthModule.register({})],
80
- }).compile();
81
-
82
- app = moduleRef.createNestApplication();
83
- await app.init();
84
- });
85
-
86
- afterAll(async () => {
87
- await app?.close();
88
- });
89
-
90
- it('GET /health/live returns HTTP 200', async () => {
91
- const res = await supertest(app.getHttpServer()).get('/health/live');
92
- expect(res.status).toBe(200);
93
- });
94
- });
95
-
96
- describe('AC3: empty readiness array serves GET /health/ready at HTTP 200 with status ok', () => {
97
- let app: INestApplication;
98
-
99
- beforeAll(async () => {
100
- const moduleRef = await Test.createTestingModule({
101
- imports: [HealthModule.register({ readiness: [] })],
102
- }).compile();
103
-
104
- app = moduleRef.createNestApplication();
105
- await app.init();
106
- });
107
-
108
- afterAll(async () => {
109
- await app?.close();
110
- });
111
-
112
- it('GET /health/ready returns HTTP 200', async () => {
113
- const res = await supertest(app.getHttpServer()).get('/health/ready');
114
- expect(res.status).toBe(200);
115
- });
116
-
117
- it('GET /health/ready body has overall status ok', async () => {
118
- const res = await supertest(app.getHttpServer()).get('/health/ready');
119
- expect(res.body?.status).toBe('ok');
120
- });
121
- });
122
-
123
- describe('AC4: auto-provided CacheHealthCheck is resolved by executor with a down source returning 503', () => {
124
- let app: INestApplication;
125
-
126
- beforeAll(async () => {
127
- const source = {
128
- isHealthy: () =>
129
- Promise.resolve({
130
- healthy: false,
131
- connections: [{ name: 'redis', status: 'error' }],
132
- }),
133
- };
134
-
135
- const moduleRef = await Test.createTestingModule({
136
- imports: [HealthModule.register({ readiness: [CacheHealthCheck] })],
137
- providers: [
138
- { provide: CACHE_HEALTH_SOURCE, useValue: source },
139
- ],
140
- }).compile();
141
-
142
- app = moduleRef.createNestApplication();
143
- await app.init();
144
- });
145
-
146
- afterAll(async () => {
147
- await app?.close();
148
- });
149
-
150
- it('GET /health/ready returns HTTP 503 when CacheHealthCheck resolves through the executor and reports down', async () => {
151
- const res = await supertest(app.getHttpServer()).get('/health/ready');
152
- expect(res.status).toBe(503);
153
- });
154
- });
155
-
156
- describe('AC5: custom controller paths are served and defaults are not bound', () => {
157
- let app: INestApplication;
158
-
159
- beforeAll(async () => {
160
- const moduleRef = await Test.createTestingModule({
161
- imports: [
162
- HealthModule.register({
163
- controller: {
164
- livenessPath: 'healthz',
165
- readinessPath: 'readyz',
166
- },
167
- }),
168
- ],
169
- }).compile();
170
-
171
- app = moduleRef.createNestApplication();
172
- await app.init();
173
- });
174
-
175
- afterAll(async () => {
176
- await app?.close();
177
- });
178
-
179
- it('GET /healthz returns HTTP 200', async () => {
180
- const res = await supertest(app.getHttpServer()).get('/healthz');
181
- expect(res.status).toBe(200);
182
- });
183
-
184
- it('GET /health/live returns HTTP 404 (no route bound at default path)', async () => {
185
- const res = await supertest(app.getHttpServer()).get('/health/live');
186
- expect(res.status).toBe(404);
187
- });
188
- });
189
-
190
- describe('AC6: controller: false binds no routes but executor still resolves', () => {
191
- let app: INestApplication;
192
-
193
- beforeAll(async () => {
194
- const moduleRef = await Test.createTestingModule({
195
- imports: [HealthModule.register({ controller: false })],
196
- }).compile();
197
-
198
- app = moduleRef.createNestApplication();
199
- await app.init();
200
- });
201
-
202
- afterAll(async () => {
203
- await app?.close();
204
- });
205
-
206
- it('GET /health/live returns HTTP 404 (no controller bound)', async () => {
207
- const res = await supertest(app.getHttpServer()).get('/health/live');
208
- expect(res.status).toBe(404);
209
- });
210
-
211
- it('HealthExecutorService still resolves from the module injector', () => {
212
- const executor = app.get(HealthExecutorService);
213
- expect(executor).toBeDefined();
214
- expect(executor).toBeInstanceOf(HealthExecutorService);
215
- });
216
- });
217
-
218
- describe('AC7: standalone boot with readiness [CacheHealthCheck] and no source bound', () => {
219
- let app: INestApplication;
220
-
221
- beforeAll(async () => {
222
- const moduleRef = await Test.createTestingModule({
223
- imports: [HealthModule.register({ readiness: [CacheHealthCheck] })],
224
- }).compile();
225
-
226
- app = moduleRef.createNestApplication();
227
- await app.init();
228
- });
229
-
230
- afterAll(async () => {
231
- await app?.close();
232
- });
233
-
234
- it('boots successfully and GET /health/ready returns HTTP 503 with cache entry down', async () => {
235
- const res = await supertest(app.getHttpServer()).get('/health/ready');
236
- expect(res.status).toBe(503);
237
- expect(res.body?.error?.cache?.status).toBe('down');
238
- });
239
- });
240
-
241
- describe('AC8: registerAsync with useFactory is applied', () => {
242
- let app: INestApplication;
243
-
244
- beforeAll(async () => {
245
- const moduleRef = await Test.createTestingModule({
246
- imports: [
247
- HealthModule.registerAsync({
248
- useFactory: () => ({ readiness: [] }),
249
- }),
250
- ],
251
- }).compile();
252
-
253
- app = moduleRef.createNestApplication();
254
- await app.init();
255
- });
256
-
257
- afterAll(async () => {
258
- await app?.close();
259
- });
260
-
261
- it('GET /health/ready returns HTTP 200 (async options applied)', async () => {
262
- const res = await supertest(app.getHttpServer()).get('/health/ready');
263
- expect(res.status).toBe(200);
264
- });
265
- });
266
-
267
- describe('AC8b: registerAsync with a synchronous controller field', () => {
268
- it('controller: false binds no routes', async () => {
269
- const moduleRef = await Test.createTestingModule({
270
- imports: [
271
- HealthModule.registerAsync({
272
- controller: false,
273
- useFactory: () => ({ readiness: [] }),
274
- }),
275
- ],
276
- }).compile();
277
-
278
- const app = moduleRef.createNestApplication();
279
- await app.init();
280
-
281
- const res = await supertest(app.getHttpServer()).get('/health/live');
282
- expect(res.status).toBe(404);
283
-
284
- await app.close();
285
- });
286
-
287
- it('custom controller paths are served and defaults are not bound', async () => {
288
- const moduleRef = await Test.createTestingModule({
289
- imports: [
290
- HealthModule.registerAsync({
291
- controller: { livenessPath: 'healthz', readinessPath: 'readyz' },
292
- useFactory: () => ({ readiness: [] }),
293
- }),
294
- ],
295
- }).compile();
296
-
297
- const app = moduleRef.createNestApplication();
298
- await app.init();
299
-
300
- const healthz = await supertest(app.getHttpServer()).get('/healthz');
301
- expect(healthz.status).toBe(200);
302
-
303
- const defaultLive = await supertest(app.getHttpServer()).get(
304
- '/health/live',
305
- );
306
- expect(defaultLive.status).toBe(404);
307
-
308
- await app.close();
309
- });
310
- });
311
- });
@@ -1,186 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { Inject, Injectable } from '@nestjs/common';
3
- import { TerminusModule } from '@nestjs/terminus';
4
- import { Test } from '@nestjs/testing';
5
- import { firstValueFrom, of, catchError } from 'rxjs';
6
- import {
7
- HealthExecutorService,
8
- HEALTH_MODULE_OPTIONS,
9
- } from '../../lib';
10
-
11
- describe('HealthExecutorService', () => {
12
- it('AC1: is resolvable as an injectable provider exposing runLiveness and runReadiness', async () => {
13
- const moduleRef = await Test.createTestingModule({
14
- imports: [TerminusModule],
15
- providers: [
16
- HealthExecutorService,
17
- { provide: HEALTH_MODULE_OPTIONS, useValue: {} },
18
- ],
19
- }).compile();
20
-
21
- const executor = moduleRef.get(HealthExecutorService);
22
- expect(executor).toBeDefined();
23
- expect(executor).toBeInstanceOf(HealthExecutorService);
24
- expect(typeof executor.runLiveness).toBe('function');
25
- expect(typeof executor.runReadiness).toBe('function');
26
- await moduleRef.close();
27
- });
28
-
29
- it('AC2: runReadiness with one up adapter returns terminus result with status ok and the adapter entry', async () => {
30
- const RESOLVED_DEP = Symbol.for('AC2_RESOLVED_DEP');
31
-
32
- @Injectable()
33
- class UpAdapter {
34
- constructor(@Inject(RESOLVED_DEP) private readonly dep: string) {}
35
- async isHealthy() {
36
- return {
37
- readySvc: {
38
- status: 'up' as const,
39
- dep: this.dep,
40
- },
41
- };
42
- }
43
- }
44
-
45
- const moduleRef = await Test.createTestingModule({
46
- imports: [TerminusModule],
47
- providers: [
48
- HealthExecutorService,
49
- UpAdapter,
50
- { provide: RESOLVED_DEP, useValue: 'injected' },
51
- {
52
- provide: HEALTH_MODULE_OPTIONS,
53
- useValue: { readiness: [UpAdapter] },
54
- },
55
- ],
56
- }).compile();
57
-
58
- const executor = moduleRef.get(HealthExecutorService);
59
- const result: any = await firstValueFrom(executor.runReadiness());
60
-
61
- expect(result?.status).toBe('ok');
62
- const entry = result?.details?.readySvc ?? result?.info?.readySvc;
63
- expect(entry?.status).toBe('up');
64
- expect(entry?.dep).toBe('injected');
65
- await moduleRef.close();
66
- });
67
-
68
- it('AC3: runReadiness with one up and one down adapter returns status error with the down entry in error section', async () => {
69
- @Injectable()
70
- class UpAdapter {
71
- async isHealthy() {
72
- return { upSvc: { status: 'up' as const } };
73
- }
74
- }
75
- @Injectable()
76
- class DownAdapter {
77
- async isHealthy() {
78
- return { downSvc: { status: 'down' as const } };
79
- }
80
- }
81
-
82
- const moduleRef = await Test.createTestingModule({
83
- imports: [TerminusModule],
84
- providers: [
85
- HealthExecutorService,
86
- UpAdapter,
87
- DownAdapter,
88
- {
89
- provide: HEALTH_MODULE_OPTIONS,
90
- useValue: { readiness: [UpAdapter, DownAdapter] },
91
- },
92
- ],
93
- }).compile();
94
-
95
- const executor = moduleRef.get(HealthExecutorService);
96
- const result: any = await firstValueFrom(
97
- executor.runReadiness().pipe(
98
- catchError((err: any) =>
99
- of(err?.response ?? err?.getResponse?.() ?? err),
100
- ),
101
- ),
102
- );
103
-
104
- expect(result?.status).toBe('error');
105
- const downEntry = result?.error?.downSvc ?? result?.details?.downSvc;
106
- expect(downEntry?.status).toBe('down');
107
- await moduleRef.close();
108
- });
109
-
110
- it('AC4: runLiveness with empty (or unset) liveness array returns terminus result with status ok', async () => {
111
- const moduleRef = await Test.createTestingModule({
112
- imports: [TerminusModule],
113
- providers: [
114
- HealthExecutorService,
115
- { provide: HEALTH_MODULE_OPTIONS, useValue: {} },
116
- ],
117
- }).compile();
118
-
119
- const executor = moduleRef.get(HealthExecutorService);
120
- const result: any = await firstValueFrom(executor.runLiveness());
121
-
122
- expect(result?.status).toBe('ok');
123
-
124
- const readinessResult: any = await firstValueFrom(executor.runReadiness());
125
- expect(readinessResult?.status).toBe('ok');
126
- await moduleRef.close();
127
- });
128
-
129
- it('runLiveness with a configured up adapter resolves it via ModuleRef and returns status ok with the adapter entry', async () => {
130
- @Injectable()
131
- class LivenessAdapter {
132
- async isHealthy() {
133
- return { liveSvc: { status: 'up' as const } };
134
- }
135
- }
136
-
137
- const moduleRef = await Test.createTestingModule({
138
- imports: [TerminusModule],
139
- providers: [
140
- HealthExecutorService,
141
- LivenessAdapter,
142
- {
143
- provide: HEALTH_MODULE_OPTIONS,
144
- useValue: { liveness: [LivenessAdapter] },
145
- },
146
- ],
147
- }).compile();
148
-
149
- const executor = moduleRef.get(HealthExecutorService);
150
- const result: any = await firstValueFrom(executor.runLiveness());
151
-
152
- expect(result?.status).toBe('ok');
153
- expect(result?.details?.liveSvc?.status).toBe('up');
154
- await moduleRef.close();
155
- });
156
-
157
- it('preserves the canonical key a built-in adapter applies when its isHealthy() is invoked with no argument', async () => {
158
- const { CacheHealthCheck, CACHE_HEALTH_SOURCE } = await import(
159
- '../../lib'
160
- );
161
- const source = {
162
- isHealthy: () => Promise.resolve({ healthy: true, connections: [] }),
163
- };
164
-
165
- const moduleRef = await Test.createTestingModule({
166
- imports: [TerminusModule],
167
- providers: [
168
- HealthExecutorService,
169
- CacheHealthCheck,
170
- { provide: CACHE_HEALTH_SOURCE, useValue: source },
171
- {
172
- provide: HEALTH_MODULE_OPTIONS,
173
- useValue: { readiness: [CacheHealthCheck] },
174
- },
175
- ],
176
- }).compile();
177
-
178
- const executor = moduleRef.get(HealthExecutorService);
179
- const result: any = await firstValueFrom(executor.runReadiness());
180
-
181
- expect(result?.status).toBe('ok');
182
- expect(result?.details?.cache?.status).toBe('up');
183
- expect(result?.details?.CacheHealthCheck).toBeUndefined();
184
- await moduleRef.close();
185
- });
186
- });
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "@nathapp/typescript-config/base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./lib"
6
- },
7
- "include": ["lib/**/*", "../index.ts", "../test/**/*"],
8
- "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
9
- }