@nathapp/nestjs-health 3.3.0
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.
- package/.eslintrc.js +8 -0
- package/.nax/features/nestjs-health/.nax-acceptance.test.ts +756 -0
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +15 -0
- package/README.md +19 -0
- package/dist/constant/health.constants.d.ts +5 -0
- package/dist/constant/health.constants.js +9 -0
- package/dist/constant/health.constants.js.map +1 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/controller/health.controller.d.ts +5 -0
- package/dist/controller/health.controller.js +48 -0
- package/dist/controller/health.controller.js.map +1 -0
- package/dist/controller/index.d.ts +1 -0
- package/dist/controller/index.js +18 -0
- package/dist/controller/index.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/indicator/cache-health.check.d.ts +20 -0
- package/dist/indicator/cache-health.check.js +50 -0
- package/dist/indicator/cache-health.check.js.map +1 -0
- package/dist/indicator/generic-health.check.d.ts +13 -0
- package/dist/indicator/generic-health.check.js +51 -0
- package/dist/indicator/generic-health.check.js.map +1 -0
- package/dist/indicator/index.d.ts +4 -0
- package/dist/indicator/index.js +21 -0
- package/dist/indicator/index.js.map +1 -0
- package/dist/indicator/prisma-health.check.d.ts +20 -0
- package/dist/indicator/prisma-health.check.js +53 -0
- package/dist/indicator/prisma-health.check.js.map +1 -0
- package/dist/indicator/queue-health.check.d.ts +22 -0
- package/dist/indicator/queue-health.check.js +56 -0
- package/dist/indicator/queue-health.check.js.map +1 -0
- package/dist/interface/health-indicator.interface.d.ts +4 -0
- package/dist/interface/health-indicator.interface.js +3 -0
- package/dist/interface/health-indicator.interface.js.map +1 -0
- package/dist/interface/health-module-options.interface.d.ts +22 -0
- package/dist/interface/health-module-options.interface.js +19 -0
- package/dist/interface/health-module-options.interface.js.map +1 -0
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +19 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/module/health.module.d.ts +8 -0
- package/dist/module/health.module.js +58 -0
- package/dist/module/health.module.js.map +1 -0
- package/dist/module/index.d.ts +1 -0
- package/dist/module/index.js +18 -0
- package/dist/module/index.js.map +1 -0
- package/dist/service/health-executor.service.d.ts +20 -0
- package/dist/service/health-executor.service.js +50 -0
- package/dist/service/health-executor.service.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +18 -0
- package/dist/service/index.js.map +1 -0
- package/lib/constant/health.constants.ts +23 -0
- package/lib/constant/index.ts +1 -0
- package/lib/controller/health.controller.ts +37 -0
- package/lib/controller/index.ts +1 -0
- package/lib/index.ts +6 -0
- package/lib/indicator/cache-health.check.ts +59 -0
- package/lib/indicator/generic-health.check.ts +43 -0
- package/lib/indicator/index.ts +4 -0
- package/lib/indicator/prisma-health.check.ts +59 -0
- package/lib/indicator/queue-health.check.ts +72 -0
- package/lib/interface/health-indicator.interface.ts +18 -0
- package/lib/interface/health-module-options.interface.ts +85 -0
- package/lib/interface/index.ts +2 -0
- package/lib/module/health.module.ts +62 -0
- package/lib/module/index.ts +1 -0
- package/lib/service/health-executor.service.ts +69 -0
- package/lib/service/index.ts +1 -0
- package/package.json +67 -0
- package/test/constant/health.constants.spec.ts +34 -0
- package/test/controller/health.controller.spec.ts +117 -0
- package/test/indicator/cache-health.check.spec.ts +88 -0
- package/test/indicator/generic-health.check.spec.ts +61 -0
- package/test/indicator/prisma-health.check.spec.ts +69 -0
- package/test/indicator/queue-health.check.spec.ts +88 -0
- package/test/interface/health-module-options.spec.ts +59 -0
- package/test/jest-nax.json +27 -0
- package/test/jest-test.json +28 -0
- package/test/module/health.module.spec.ts +311 -0
- package/test/service/health-executor.service.spec.ts +186 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./health-indicator.interface"), exports);
|
|
18
|
+
__exportStar(require("./health-module-options.interface"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/interface/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C;AAC7C,oEAAkD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { HealthAsyncModuleOptions, HealthModuleOptions } from '../interface';
|
|
3
|
+
declare const HealthModule_base: import("@nathapp/nestjs-common").ConfigurableModuleCls<HealthModuleOptions, "register", "createOptions", {}>;
|
|
4
|
+
export declare class HealthModule extends HealthModule_base {
|
|
5
|
+
static register(options?: HealthModuleOptions): DynamicModule;
|
|
6
|
+
static registerAsync(options: HealthAsyncModuleOptions): DynamicModule;
|
|
7
|
+
}
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.HealthModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
12
|
+
const terminus_1 = require("@nestjs/terminus");
|
|
13
|
+
const cache_health_check_1 = require("../indicator/cache-health.check");
|
|
14
|
+
const generic_health_check_1 = require("../indicator/generic-health.check");
|
|
15
|
+
const prisma_health_check_1 = require("../indicator/prisma-health.check");
|
|
16
|
+
const queue_health_check_1 = require("../indicator/queue-health.check");
|
|
17
|
+
const health_executor_service_1 = require("../service/health-executor.service");
|
|
18
|
+
const health_controller_1 = require("../controller/health.controller");
|
|
19
|
+
const interface_1 = require("../interface");
|
|
20
|
+
const ADAPTERS = [
|
|
21
|
+
cache_health_check_1.CacheHealthCheck,
|
|
22
|
+
prisma_health_check_1.PrismaHealthCheck,
|
|
23
|
+
queue_health_check_1.QueueHealthCheck,
|
|
24
|
+
generic_health_check_1.GenericHealthCheck,
|
|
25
|
+
];
|
|
26
|
+
function assembleHealthModule(baseModule, controllerOptions) {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const controller = (0, interface_1.resolveHealthControllerOptions)(controllerOptions);
|
|
29
|
+
const controllers = controller === false ? [] : [(0, health_controller_1.createHealthController)(controller)];
|
|
30
|
+
return {
|
|
31
|
+
...baseModule,
|
|
32
|
+
global: true,
|
|
33
|
+
module: HealthModule,
|
|
34
|
+
imports: [terminus_1.TerminusModule, ...((_a = baseModule.imports) !== null && _a !== void 0 ? _a : [])],
|
|
35
|
+
controllers,
|
|
36
|
+
providers: [
|
|
37
|
+
...((_b = baseModule.providers) !== null && _b !== void 0 ? _b : []),
|
|
38
|
+
...ADAPTERS,
|
|
39
|
+
health_executor_service_1.HealthExecutorService,
|
|
40
|
+
],
|
|
41
|
+
exports: [health_executor_service_1.HealthExecutorService],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let HealthModule = class HealthModule extends (0, nestjs_common_1.ConfigurableModuleClass)(interface_1.HEALTH_MODULE_OPTIONS) {
|
|
45
|
+
static register(options) {
|
|
46
|
+
const baseModule = super.register(options !== null && options !== void 0 ? options : {});
|
|
47
|
+
return assembleHealthModule(baseModule, options === null || options === void 0 ? void 0 : options.controller);
|
|
48
|
+
}
|
|
49
|
+
static registerAsync(options) {
|
|
50
|
+
const baseModule = super.registerAsync(options);
|
|
51
|
+
return assembleHealthModule(baseModule, options.controller);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
exports.HealthModule = HealthModule;
|
|
55
|
+
exports.HealthModule = HealthModule = __decorate([
|
|
56
|
+
(0, common_1.Module)({})
|
|
57
|
+
], HealthModule);
|
|
58
|
+
//# sourceMappingURL=health.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.module.js","sourceRoot":"","sources":["../../lib/module/health.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAuD;AACvD,0DAAiE;AACjE,+CAAkD;AAClD,wEAAmE;AACnE,4EAAuE;AACvE,0EAAqE;AACrE,wEAAmE;AACnE,gFAA2E;AAC3E,uEAAyE;AACzE,4CAKsB;AAEtB,MAAM,QAAQ,GAAG;IACf,qCAAgB;IAChB,uCAAiB;IACjB,qCAAgB;IAChB,yCAAkB;CACnB,CAAC;AAEF,SAAS,oBAAoB,CAC3B,UAAyB,EACzB,iBAAoD;;IAEpD,MAAM,UAAU,GAAG,IAAA,0CAA8B,EAAC,iBAAiB,CAAC,CAAC;IACrE,MAAM,WAAW,GACf,UAAU,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAA,0CAAsB,EAAC,UAAU,CAAC,CAAC,CAAC;IAEnE,OAAO;QACL,GAAG,UAAU;QACb,MAAM,EAAE,IAAI;QACZ,MAAM,EAAE,YAAY;QACpB,OAAO,EAAE,CAAC,yBAAc,EAAE,GAAG,CAAC,MAAA,UAAU,CAAC,OAAO,mCAAI,EAAE,CAAC,CAAC;QACxD,WAAW;QACX,SAAS,EAAE;YACT,GAAG,CAAC,MAAA,UAAU,CAAC,SAAS,mCAAI,EAAE,CAAC;YAC/B,GAAG,QAAQ;YACX,+CAAqB;SACtB;QACD,OAAO,EAAE,CAAC,+CAAqB,CAAC;KACjC,CAAC;AACJ,CAAC;AAGM,IAAM,YAAY,GAAlB,MAAM,YAAa,SAAQ,IAAA,uCAAuB,EACvD,iCAAqB,CACtB;IACC,MAAM,CAAC,QAAQ,CAAC,OAA6B;QAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,QAAQ,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC,CAAC;QACjD,OAAO,oBAAoB,CAAC,UAAU,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,CAAC,CAAC;IAC/D,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,OAAiC;QACpD,MAAM,UAAU,GACd,KAAK,CAAC,aACP,CAAC,OAAO,CAAC,CAAC;QACX,OAAO,oBAAoB,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAC9D,CAAC;CACF,CAAA;AAdY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CAcxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './health.module';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./health.module"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/module/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ModuleRef } from '@nestjs/core';
|
|
2
|
+
import { HealthCheckService } from '@nestjs/terminus';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
import { HealthModuleOptions } from '../interface/health-module-options.interface';
|
|
5
|
+
export interface HealthIndicatorLike {
|
|
6
|
+
isHealthy(key?: string): Promise<Record<string, {
|
|
7
|
+
status: 'up' | 'down';
|
|
8
|
+
}>> | Record<string, {
|
|
9
|
+
status: 'up' | 'down';
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
export declare class HealthExecutorService {
|
|
13
|
+
private readonly health;
|
|
14
|
+
private readonly moduleRef;
|
|
15
|
+
private readonly options;
|
|
16
|
+
constructor(health: HealthCheckService, moduleRef: ModuleRef, options: HealthModuleOptions);
|
|
17
|
+
runLiveness(): Observable<import('@nestjs/terminus').HealthCheckResult>;
|
|
18
|
+
runReadiness(): Observable<import('@nestjs/terminus').HealthCheckResult>;
|
|
19
|
+
private run;
|
|
20
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.HealthExecutorService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const terminus_1 = require("@nestjs/terminus");
|
|
19
|
+
const rxjs_1 = require("rxjs");
|
|
20
|
+
const health_module_options_interface_1 = require("../interface/health-module-options.interface");
|
|
21
|
+
let HealthExecutorService = class HealthExecutorService {
|
|
22
|
+
constructor(health, moduleRef, options) {
|
|
23
|
+
this.health = health;
|
|
24
|
+
this.moduleRef = moduleRef;
|
|
25
|
+
this.options = options;
|
|
26
|
+
}
|
|
27
|
+
runLiveness() {
|
|
28
|
+
var _a;
|
|
29
|
+
return this.run((_a = this.options.liveness) !== null && _a !== void 0 ? _a : []);
|
|
30
|
+
}
|
|
31
|
+
runReadiness() {
|
|
32
|
+
var _a;
|
|
33
|
+
return this.run((_a = this.options.readiness) !== null && _a !== void 0 ? _a : []);
|
|
34
|
+
}
|
|
35
|
+
run(types) {
|
|
36
|
+
const checks = types.map((T) => {
|
|
37
|
+
const inst = this.moduleRef.get(T, { strict: false });
|
|
38
|
+
return () => inst.isHealthy();
|
|
39
|
+
});
|
|
40
|
+
return (0, rxjs_1.from)(this.health.check(checks));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
exports.HealthExecutorService = HealthExecutorService;
|
|
44
|
+
exports.HealthExecutorService = HealthExecutorService = __decorate([
|
|
45
|
+
(0, common_1.Injectable)(),
|
|
46
|
+
__param(2, (0, common_1.Inject)(health_module_options_interface_1.HEALTH_MODULE_OPTIONS)),
|
|
47
|
+
__metadata("design:paramtypes", [terminus_1.HealthCheckService,
|
|
48
|
+
core_1.ModuleRef, Object])
|
|
49
|
+
], HealthExecutorService);
|
|
50
|
+
//# sourceMappingURL=health-executor.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health-executor.service.js","sourceRoot":"","sources":["../../lib/service/health-executor.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA0D;AAC1D,uCAAyC;AACzC,+CAAsD;AACtD,+BAAwC;AACxC,kGAGsD;AAoC/C,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAChC,YACmB,MAA0B,EAC1B,SAAoB,EAEpB,OAA4B;QAH5B,WAAM,GAAN,MAAM,CAAoB;QAC1B,cAAS,GAAT,SAAS,CAAW;QAEpB,YAAO,GAAP,OAAO,CAAqB;IAC5C,CAAC;IAEJ,WAAW;;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,MAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,mCAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,YAAY;;QACV,OAAO,IAAI,CAAC,GAAG,CAAC,MAAA,IAAI,CAAC,OAAO,CAAC,SAAS,mCAAI,EAAE,CAAC,CAAC;IAChD,CAAC;IAEO,GAAG,CACT,KAAsB;QAEtB,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAsB,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;YAC3E,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QAChC,CAAC,CAAC,CAAC;QACH,OAAO,IAAA,WAAI,EAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACzC,CAAC;CACF,CAAA;AAzBY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,uDAAqB,CAAC,CAAA;qCAFL,6BAAkB;QACf,gBAAS;GAH5B,qBAAqB,CAyBjC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './health-executor.service';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./health-executor.service"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Default route path for the liveness probe. */
|
|
2
|
+
export const DEFAULT_LIVENESS_PATH = 'health/live';
|
|
3
|
+
|
|
4
|
+
/** Default route path for the readiness probe. */
|
|
5
|
+
export const DEFAULT_READINESS_PATH = 'health/ready';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* DI token for the pluggable cache health source.
|
|
9
|
+
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
10
|
+
*/
|
|
11
|
+
export const CACHE_HEALTH_SOURCE = Symbol.for('CACHE_HEALTH_SOURCE');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* DI token for the pluggable Prisma health source.
|
|
15
|
+
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
16
|
+
*/
|
|
17
|
+
export const PRISMA_HEALTH_SOURCE = Symbol.for('PRISMA_HEALTH_SOURCE');
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* DI token for the pluggable queue health source.
|
|
21
|
+
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
22
|
+
*/
|
|
23
|
+
export const QUEUE_HEALTH_SOURCE = Symbol.for('QUEUE_HEALTH_SOURCE');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './health.constants';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Controller, Get, Type } from '@nestjs/common';
|
|
2
|
+
import { HealthCheck } from '@nestjs/terminus';
|
|
3
|
+
import { HealthExecutorService } from '../service/health-executor.service';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds a NestJS controller class whose `@Get()` paths are **closed over**
|
|
7
|
+
* the resolved `livenessPath` / `readinessPath` options supplied at
|
|
8
|
+
* `register()` time.
|
|
9
|
+
*
|
|
10
|
+
* The factory exists because decorator metadata (e.g. `@Get('foo')`) is
|
|
11
|
+
* captured statically at class-definition time — there is no Nest-supported
|
|
12
|
+
* way to read a runtime option inside a `@Get()` argument. Returning a fresh
|
|
13
|
+
* class per options object is the standard escape hatch.
|
|
14
|
+
*/
|
|
15
|
+
export function createHealthController(paths: {
|
|
16
|
+
livenessPath: string;
|
|
17
|
+
readinessPath: string;
|
|
18
|
+
}): Type<unknown> {
|
|
19
|
+
@Controller()
|
|
20
|
+
class HealthController {
|
|
21
|
+
constructor(private readonly executor: HealthExecutorService) {}
|
|
22
|
+
|
|
23
|
+
@Get(paths.livenessPath)
|
|
24
|
+
@HealthCheck()
|
|
25
|
+
live() {
|
|
26
|
+
return this.executor.runLiveness();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Get(paths.readinessPath)
|
|
30
|
+
@HealthCheck()
|
|
31
|
+
ready() {
|
|
32
|
+
return this.executor.runReadiness();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return HealthController;
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './health.controller';
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
+
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
+
import { CACHE_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result shape produced by a cache health source.
|
|
7
|
+
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
+
* require `@nathapp/nestjs-cache` to be installed at load time.
|
|
9
|
+
*/
|
|
10
|
+
export interface CacheHealthSource {
|
|
11
|
+
isHealthy(): Promise<{ healthy: boolean; connections?: unknown[] }>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Default key used when callers don't pass one to {@link CacheHealthCheck.isHealthy}. */
|
|
15
|
+
export const DEFAULT_CACHE_KEY = 'cache';
|
|
16
|
+
|
|
17
|
+
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Terminus-compatible health check that adapts a pluggable cache source
|
|
21
|
+
* bound to {@link CACHE_HEALTH_SOURCE}.
|
|
22
|
+
*
|
|
23
|
+
* Behaviour:
|
|
24
|
+
* - No source bound (consumer did not provide the token) → `down` with
|
|
25
|
+
* `reason: 'source not provided'`. Never throws.
|
|
26
|
+
* - Source returns `{ healthy: true, connections }` → `up` with the
|
|
27
|
+
* `connections` array on the indicator.
|
|
28
|
+
* - Source returns `{ healthy: false, ... }` → `down` with the
|
|
29
|
+
* `connections` array on the indicator.
|
|
30
|
+
* - Source rejects → `down` with the error message. Never throws out.
|
|
31
|
+
*/
|
|
32
|
+
@Injectable()
|
|
33
|
+
export class CacheHealthCheck {
|
|
34
|
+
constructor(
|
|
35
|
+
private readonly healthIndicatorService: HealthIndicatorService,
|
|
36
|
+
@Optional()
|
|
37
|
+
@Inject(CACHE_HEALTH_SOURCE)
|
|
38
|
+
private readonly source?: CacheHealthSource,
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
async isHealthy(key: string = DEFAULT_CACHE_KEY): Promise<TerminusResult> {
|
|
42
|
+
const indicator = this.healthIndicatorService.check(key);
|
|
43
|
+
|
|
44
|
+
if (!this.source) {
|
|
45
|
+
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const result = await this.source.isHealthy();
|
|
50
|
+
const details = { connections: result.connections };
|
|
51
|
+
return result.healthy
|
|
52
|
+
? (indicator.up(details) as unknown as TerminusResult)
|
|
53
|
+
: (indicator.down(details) as unknown as TerminusResult);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
56
|
+
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Injectable, Optional } from '@nestjs/common';
|
|
2
|
+
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
+
import type {
|
|
4
|
+
HealthIndicatorSource,
|
|
5
|
+
HealthNormalize,
|
|
6
|
+
} from '../interface/health-indicator.interface';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reusable Terminus-compatible health check that adapts an arbitrary
|
|
10
|
+
* `{ isHealthy() }` source through a caller-supplied `normalize` predicate.
|
|
11
|
+
*
|
|
12
|
+
* Source failures (rejections) are caught and surfaced as `down` so the
|
|
13
|
+
* wrapping executor never sees an exception from this check.
|
|
14
|
+
*/
|
|
15
|
+
@Injectable()
|
|
16
|
+
export class GenericHealthCheck<TResult = unknown> {
|
|
17
|
+
constructor(
|
|
18
|
+
private readonly healthIndicatorService: HealthIndicatorService,
|
|
19
|
+
@Optional() private readonly source?: HealthIndicatorSource<TResult>,
|
|
20
|
+
@Optional() private readonly normalize?: HealthNormalize<TResult>,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
async isHealthy(key = 'generic'): Promise<{ [k: string]: { status: 'up' | 'down' } }> {
|
|
24
|
+
if (!this.source || !this.normalize) {
|
|
25
|
+
const session = this.healthIndicatorService.check(key);
|
|
26
|
+
return session.down();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let result: TResult;
|
|
30
|
+
try {
|
|
31
|
+
result = await this.source.isHealthy();
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return this.healthIndicatorService
|
|
34
|
+
.check(key)
|
|
35
|
+
.down({ error: err instanceof Error ? err.message : String(err) });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const isUp = this.normalize(result);
|
|
39
|
+
return isUp
|
|
40
|
+
? this.healthIndicatorService.check(key).up()
|
|
41
|
+
: this.healthIndicatorService.check(key).down();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
+
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
+
import { PRISMA_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result shape produced by a Prisma health source.
|
|
7
|
+
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
+
* require `@nathapp/nestjs-prisma` to be installed at load time.
|
|
9
|
+
*/
|
|
10
|
+
export interface PrismaHealthSource {
|
|
11
|
+
isHealthy(): Promise<{ status: 'up' | 'down'; message?: string }>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Default key used when callers don't pass one to {@link PrismaHealthCheck.isHealthy}. */
|
|
15
|
+
export const DEFAULT_PRISMA_KEY = 'prisma';
|
|
16
|
+
|
|
17
|
+
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Terminus-compatible health check that adapts a pluggable Prisma source
|
|
21
|
+
* bound to {@link PRISMA_HEALTH_SOURCE}.
|
|
22
|
+
*
|
|
23
|
+
* Behaviour:
|
|
24
|
+
* - No source bound → `down` with `reason: 'source not provided'`. Never throws.
|
|
25
|
+
* - Source returns `{ status: 'up' }` → `up` (message included if present).
|
|
26
|
+
* - Source returns `{ status: 'down', message }` → `down` with `message`.
|
|
27
|
+
* - Source rejects → `down` with the error message. Never throws out.
|
|
28
|
+
*/
|
|
29
|
+
@Injectable()
|
|
30
|
+
export class PrismaHealthCheck {
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly healthIndicatorService: HealthIndicatorService,
|
|
33
|
+
@Optional()
|
|
34
|
+
@Inject(PRISMA_HEALTH_SOURCE)
|
|
35
|
+
private readonly source?: PrismaHealthSource,
|
|
36
|
+
) {}
|
|
37
|
+
|
|
38
|
+
async isHealthy(key: string = DEFAULT_PRISMA_KEY): Promise<TerminusResult> {
|
|
39
|
+
const indicator = this.healthIndicatorService.check(key);
|
|
40
|
+
|
|
41
|
+
if (!this.source) {
|
|
42
|
+
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const result = await this.source.isHealthy();
|
|
47
|
+
const details: { message?: string } = {};
|
|
48
|
+
if (result.message !== undefined) {
|
|
49
|
+
details.message = result.message;
|
|
50
|
+
}
|
|
51
|
+
return result.status === 'up'
|
|
52
|
+
? (indicator.up(details) as unknown as TerminusResult)
|
|
53
|
+
: (indicator.down(details) as unknown as TerminusResult);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
56
|
+
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
+
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
+
import { QUEUE_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result shape produced by a queue health source.
|
|
7
|
+
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
+
* require `@nathapp/nestjs-queue` to be installed at load time.
|
|
9
|
+
*/
|
|
10
|
+
export interface QueueHealthSource {
|
|
11
|
+
isHealthy(): Promise<{
|
|
12
|
+
healthy: boolean;
|
|
13
|
+
provider: string;
|
|
14
|
+
connections: unknown[];
|
|
15
|
+
latencyMs?: number;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Default key used when callers don't pass one to {@link QueueHealthCheck.isHealthy}. */
|
|
20
|
+
export const DEFAULT_QUEUE_KEY = 'queue';
|
|
21
|
+
|
|
22
|
+
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Terminus-compatible health check that adapts a pluggable queue source
|
|
26
|
+
* bound to {@link QUEUE_HEALTH_SOURCE}.
|
|
27
|
+
*
|
|
28
|
+
* Behaviour:
|
|
29
|
+
* - No source bound → `down` with `reason: 'source not provided'`. Never throws.
|
|
30
|
+
* - Source returns `{ healthy: true, provider, connections, latencyMs? }` → `up`
|
|
31
|
+
* with those details.
|
|
32
|
+
* - Source returns `{ healthy: false, ... }` → `down` with those details.
|
|
33
|
+
* - Source rejects → `down` with the error message. Never throws out.
|
|
34
|
+
*/
|
|
35
|
+
@Injectable()
|
|
36
|
+
export class QueueHealthCheck {
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly healthIndicatorService: HealthIndicatorService,
|
|
39
|
+
@Optional()
|
|
40
|
+
@Inject(QUEUE_HEALTH_SOURCE)
|
|
41
|
+
private readonly source?: QueueHealthSource,
|
|
42
|
+
) {}
|
|
43
|
+
|
|
44
|
+
async isHealthy(key: string = DEFAULT_QUEUE_KEY): Promise<TerminusResult> {
|
|
45
|
+
const indicator = this.healthIndicatorService.check(key);
|
|
46
|
+
|
|
47
|
+
if (!this.source) {
|
|
48
|
+
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const result = await this.source.isHealthy();
|
|
53
|
+
const details: {
|
|
54
|
+
provider?: string;
|
|
55
|
+
connections?: unknown[];
|
|
56
|
+
latencyMs?: number;
|
|
57
|
+
} = {
|
|
58
|
+
provider: result.provider,
|
|
59
|
+
connections: result.connections,
|
|
60
|
+
};
|
|
61
|
+
if (result.latencyMs !== undefined) {
|
|
62
|
+
details.latencyMs = result.latencyMs;
|
|
63
|
+
}
|
|
64
|
+
return result.healthy
|
|
65
|
+
? (indicator.up(details) as unknown as TerminusResult)
|
|
66
|
+
: (indicator.down(details) as unknown as TerminusResult);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
69
|
+
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic health source contract.
|
|
3
|
+
*
|
|
4
|
+
* A source exposes a single `isHealthy()` call returning an arbitrary result
|
|
5
|
+
* shape. The `GenericHealthCheck` consumes this contract together with a
|
|
6
|
+
* caller-supplied `normalize` predicate that maps the source result to a
|
|
7
|
+
* boolean. Sources are intentionally agnostic of Terminus so cache, Prisma,
|
|
8
|
+
* and queue packages can plug in without leaking Terminus types.
|
|
9
|
+
*/
|
|
10
|
+
export interface HealthIndicatorSource<TResult = unknown> {
|
|
11
|
+
isHealthy(): PromiseLike<TResult> | TResult;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Normalizer contract: maps a source's `isHealthy()` result to a boolean.
|
|
16
|
+
* Returning `true` → "up"; returning `false` → "down".
|
|
17
|
+
*/
|
|
18
|
+
export type HealthNormalize<TResult = unknown> = (result: TResult) => boolean;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Type } from '@nestjs/common';
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_LIVENESS_PATH,
|
|
4
|
+
DEFAULT_READINESS_PATH,
|
|
5
|
+
} from '../constant/health.constants';
|
|
6
|
+
|
|
7
|
+
/** Controller path overrides for the default liveness/readiness routes. */
|
|
8
|
+
export interface HealthControllerOptions {
|
|
9
|
+
livenessPath?: string;
|
|
10
|
+
readinessPath?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolved controller contract:
|
|
15
|
+
* - `false` means "do not register a controller at all" (services are still provided).
|
|
16
|
+
* - Otherwise the merged liveness/readiness paths are returned.
|
|
17
|
+
*/
|
|
18
|
+
export type ResolvedHealthControllerOptions =
|
|
19
|
+
| false
|
|
20
|
+
| {
|
|
21
|
+
livenessPath: string;
|
|
22
|
+
readinessPath: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolve the controller option block into a concrete value usable by
|
|
27
|
+
* `HealthModule`.
|
|
28
|
+
*
|
|
29
|
+
* - `false` → returned as `false` (no controller).
|
|
30
|
+
* - `undefined` / object → returns merged paths with defaults applied.
|
|
31
|
+
* - Empty or whitespace-only strings fall back to the defaults.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveHealthControllerOptions(
|
|
34
|
+
controller?: false | HealthControllerOptions,
|
|
35
|
+
): ResolvedHealthControllerOptions {
|
|
36
|
+
if (controller === false) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const livenessPath =
|
|
41
|
+
controller?.livenessPath && controller.livenessPath.trim().length > 0
|
|
42
|
+
? controller.livenessPath
|
|
43
|
+
: DEFAULT_LIVENESS_PATH;
|
|
44
|
+
const readinessPath =
|
|
45
|
+
controller?.readinessPath && controller.readinessPath.trim().length > 0
|
|
46
|
+
? controller.readinessPath
|
|
47
|
+
: DEFAULT_READINESS_PATH;
|
|
48
|
+
|
|
49
|
+
return { livenessPath, readinessPath };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Module options consumed by `HealthModule.register()`.
|
|
54
|
+
*
|
|
55
|
+
* - `controller` controls whether and where the default HTTP controller is mounted.
|
|
56
|
+
* - `liveness` lists adapters that run for the liveness probe.
|
|
57
|
+
* - `readiness` lists adapters that run for the readiness probe.
|
|
58
|
+
*/
|
|
59
|
+
export interface HealthModuleOptions {
|
|
60
|
+
controller?: false | HealthControllerOptions;
|
|
61
|
+
liveness?: Type<unknown>[];
|
|
62
|
+
readiness?: Type<unknown>[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Options token consumed by HealthModule (ConfigurableModuleClass). */
|
|
66
|
+
export const HEALTH_MODULE_OPTIONS = 'HEALTH_MODULE_OPTIONS';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Async registration options for HealthModule.
|
|
70
|
+
*
|
|
71
|
+
* `controller` is resolved synchronously (mirroring `register()`), not via
|
|
72
|
+
* `useFactory` — the controller class is built from static `@Get()` decorator
|
|
73
|
+
* metadata at `registerAsync()` call time, before any async-resolved value
|
|
74
|
+
* exists, so its shape cannot depend on the factory's output. `liveness` and
|
|
75
|
+
* `readiness` remain fully async since the executor resolves them lazily via
|
|
76
|
+
* injected `HEALTH_MODULE_OPTIONS`.
|
|
77
|
+
*/
|
|
78
|
+
export interface HealthAsyncModuleOptions {
|
|
79
|
+
controller?: false | HealthControllerOptions;
|
|
80
|
+
useFactory?: (
|
|
81
|
+
...args: unknown[]
|
|
82
|
+
) => Promise<HealthModuleOptions> | HealthModuleOptions;
|
|
83
|
+
inject?: unknown[];
|
|
84
|
+
imports?: unknown[];
|
|
85
|
+
}
|