-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/compt 82 custom indicator api and health service #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
afcf4dd
2e42a1d
f856137
c74b8ae
41d7af3
8f9af5e
5dff0e2
af6f235
a5d8377
914f341
60eedae
7891e1e
b9e39f6
dbe9e52
801e648
2e5d559
83cd1d6
0b633fa
10b4423
4fdefba
bbb1f18
80cdcaf
e29be84
eff4978
a73cb21
7898821
543a07a
63c1336
f8e8e01
d4947d7
36a99ab
15c05f7
042c48c
f63633f
052cecf
86f470c
8eae15f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1 @@ | ||
| #!/usr/bin/env sh | ||
| . "$(dirname -- "$0")/_/husky.sh" | ||
|
|
||
| npx lint-staged | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,80 @@ | ||
| // @ts-check | ||
| import eslint from "@eslint/js"; | ||
| import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"; | ||
| import globals from "globals"; | ||
| import importPlugin from "eslint-plugin-import"; | ||
| import tseslint from "typescript-eslint"; | ||
|
|
||
| export default tseslint.config( | ||
| export default [ | ||
| { | ||
| ignores: ["eslint.config.mjs"], | ||
| ignores: [ | ||
| "dist/**", | ||
| "coverage/**", | ||
| "node_modules/**", | ||
| // Ignore all example files for CSR architecture | ||
| "src/example-kit.*", | ||
| "src/controllers/example.controller.ts", | ||
| "src/services/example.service.ts", | ||
| "src/entities/example.entity.ts", | ||
| "src/repositories/example.repository.ts", | ||
| "src/guards/example.guard.ts", | ||
| "src/decorators/example.decorator.ts", | ||
| "src/dto/create-example.dto.ts", | ||
| "src/dto/update-example.dto.ts", | ||
| ], | ||
| }, | ||
|
|
||
| eslint.configs.recommended, | ||
| ...tseslint.configs.recommendedTypeChecked, | ||
| eslintPluginPrettierRecommended, | ||
|
|
||
| // TypeScript ESLint (includes recommended rules) | ||
| ...tseslint.configs.recommended, | ||
|
|
||
| // Base TS rules (all TS files) | ||
| { | ||
| files: ["**/*.ts"], | ||
| languageOptions: { | ||
| globals: { | ||
| ...globals.node, | ||
| ...globals.jest, | ||
| }, | ||
| sourceType: "commonjs", | ||
| parser: tseslint.parser, | ||
| parserOptions: { | ||
| projectService: true, | ||
| project: "./tsconfig.eslint.json", | ||
| tsconfigRootDir: import.meta.dirname, | ||
| ecmaVersion: "latest", | ||
| sourceType: "module", | ||
|
Comment on lines
+35
to
+40
|
||
| }, | ||
| globals: { ...globals.node, ...globals.jest }, | ||
| }, | ||
| plugins: { | ||
| "@typescript-eslint": tseslint.plugin, | ||
| import: importPlugin, | ||
| }, | ||
| rules: { | ||
| "@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }], | ||
| "@typescript-eslint/consistent-type-imports": ["error", { prefer: "type-imports" }], | ||
|
|
||
| "import/no-duplicates": "error", | ||
| "import/order": [ | ||
| "error", | ||
| { | ||
| "newlines-between": "always", | ||
| alphabetize: { order: "asc", caseInsensitive: true }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
|
|
||
| // Architecture boundary: core must not import Nest | ||
| { | ||
| files: ["src/core/**/*.ts"], | ||
| rules: { | ||
| "@typescript-eslint/no-explicit-any": "off", | ||
| "@typescript-eslint/no-floating-promises": "warn", | ||
| "@typescript-eslint/no-unsafe-argument": "warn", | ||
| "@typescript-eslint/no-unused-vars": [ | ||
| "no-restricted-imports": [ | ||
| "error", | ||
| { | ||
| argsIgnorePattern: "^_", | ||
| varsIgnorePattern: "^_", | ||
| patterns: [ | ||
| { | ||
| group: ["@nestjs/*"], | ||
| message: "Do not import NestJS in core/. Keep core framework-free.", | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| "no-unused-vars": "off", | ||
| }, | ||
| }, | ||
| ); | ||
| ]; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||
| { | ||||||
| "name": "@ciscode/nestjs-developerkit", | ||||||
| "name": "@ciscode/health-kit", | ||||||
|
||||||
| "name": "@ciscode/health-kit", | |
| "name": "@ciscode/nestjs-developerkit", |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||
| import { ServiceUnavailableException } from "@nestjs/common"; | ||||||||||
| import type { TestingModule } from "@nestjs/testing"; | ||||||||||
| import { Test } from "@nestjs/testing"; | ||||||||||
| import { HealthService } from "@services/health.service"; | ||||||||||
| import type { HealthCheckResult } from "@services/health.service"; | ||||||||||
|
|
||||||||||
| import { createHealthController } from "./health.controller"; | ||||||||||
|
|
||||||||||
| // ── Helpers ────────────────────────────────────────────────────────────────── | ||||||||||
|
|
||||||||||
| const makeService = (liveness: "ok" | "error", readiness: "ok" | "error") => | ||||||||||
| ({ | ||||||||||
| checkLiveness: jest.fn().mockResolvedValue({ status: liveness, indicators: [] }), | ||||||||||
| checkReadiness: jest.fn().mockResolvedValue({ status: readiness, indicators: [] }), | ||||||||||
|
Comment on lines
+13
to
+14
|
||||||||||
| checkLiveness: jest.fn().mockResolvedValue({ status: liveness, indicators: [] }), | |
| checkReadiness: jest.fn().mockResolvedValue({ status: readiness, indicators: [] }), | |
| checkLiveness: jest.fn().mockResolvedValue({ status: liveness, results: [] }), | |
| checkReadiness: jest.fn().mockResolvedValue({ status: readiness, results: [] }), |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| HttpCode, | ||
| HttpStatus, | ||
| ServiceUnavailableException, | ||
| Type, | ||
| } from "@nestjs/common"; | ||
| import { HealthService } from "@services/health.service"; | ||
| import type { HealthCheckResult } from "@services/health.service"; | ||
|
|
||
| /** | ||
| * Factory that returns a NestJS controller class configured with the | ||
| * caller-supplied `path` prefix (e.g. `"health"`). | ||
| * | ||
| * Platform-agnostic — works with Express and Fastify. | ||
| * Returns 200 when all indicators are "up", | ||
| * throws ServiceUnavailableException (503) when any indicator is "down". | ||
| */ | ||
| export function createHealthController(path: string): Type<unknown> { | ||
| @Controller(path) | ||
| class HealthController { | ||
| constructor(private readonly healthService: HealthService) {} | ||
|
|
||
| @Get("live") | ||
| @HttpCode(HttpStatus.OK) | ||
| async live(): Promise<HealthCheckResult> { | ||
| const result = await this.healthService.checkLiveness(); | ||
| if (result.status === "error") throw new ServiceUnavailableException(result); | ||
| return result; | ||
| } | ||
|
|
||
| @Get("ready") | ||
| @HttpCode(HttpStatus.OK) | ||
| async ready(): Promise<HealthCheckResult> { | ||
| const result = await this.healthService.checkReadiness(); | ||
| if (result.status === "error") throw new ServiceUnavailableException(result); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| return HealthController; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import "reflect-metadata"; | ||
| import { HEALTH_INDICATOR_METADATA, HealthIndicator } from "./health-indicator.decorator"; | ||
|
|
||
| class SomeIndicator {} | ||
|
Check warning on line 4 in src/decorators/health-indicator.decorator.spec.ts
|
||
| class AnotherIndicator {} | ||
|
Check warning on line 5 in src/decorators/health-indicator.decorator.spec.ts
|
||
| class UndecotratedIndicator {} | ||
|
Check warning on line 6 in src/decorators/health-indicator.decorator.spec.ts
|
||
|
||
|
|
||
| @HealthIndicator("liveness") | ||
| class LivenessIndicator extends SomeIndicator {} | ||
|
|
||
| @HealthIndicator("readiness") | ||
| class ReadinessIndicator extends AnotherIndicator {} | ||
|
|
||
| describe("@HealthIndicator decorator", () => { | ||
| it("attaches liveness metadata to the target class", () => { | ||
| const scope = Reflect.getMetadata(HEALTH_INDICATOR_METADATA, LivenessIndicator); | ||
| expect(scope).toBe("liveness"); | ||
| }); | ||
|
|
||
| it("attaches readiness metadata to the target class", () => { | ||
| const scope = Reflect.getMetadata(HEALTH_INDICATOR_METADATA, ReadinessIndicator); | ||
| expect(scope).toBe("readiness"); | ||
| }); | ||
|
|
||
| it("returns undefined for undecorated classes", () => { | ||
| const scope = Reflect.getMetadata(HEALTH_INDICATOR_METADATA, UndecotratedIndicator); | ||
| expect(scope).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("does not affect other classes when decorating one", () => { | ||
| const livScope = Reflect.getMetadata(HEALTH_INDICATOR_METADATA, LivenessIndicator); | ||
| const readScope = Reflect.getMetadata(HEALTH_INDICATOR_METADATA, ReadinessIndicator); | ||
| expect(livScope).toBe("liveness"); | ||
| expect(readScope).toBe("readiness"); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hook now contains only
npx lint-stagedand removed the standard Husky shim + shebang. Without a shebang and Husky initialization, the hook can fail to execute reliably across environments. Restore the recommended Husky pre-commit header or ensure the hook remains executable and portable.