@abinnovision/nestjs-healthz

中文文档
0.2.0发布于 2026/10/12Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1935

Self-mounting health check module for NestJS with cross-module attestor discovery, per-attestor caching, and Kubernetes-style /livez and /readyz endpoints.

原始使用文档
文档目录15 节
  1. Installation
  2. Quick Start
  3. 1. Mount the module
  4. 2. Declare an attestor inside the module that owns the depe…
  5. Endpoints
  6. Status model
  7. Rate-limiting check execution
  8. Timeouts and thrown errors
  9. Response shape
  10. API Reference
  11. HealthzModule.forRoot()
  12. @HealthAttestor(options)
  13. HealthAttestor (interface)
  14. HealthCheckOutcome
  15. License

@abinnovision/nestjs-healthz

npm

Self-mounting health check module for NestJS. Mounts Kubernetes-style /livez, /readyz, and /healthz endpoints, and discovers health attestors decorated across the application — no central registry, no per-module wiring.

Installation

yarn add @abinnovision/nestjs-healthz

Quick Start

1. Mount the module

import { HealthzModule } from "@abinnovision/nestjs-healthz";

@Module({
  imports: [DatabaseModule, HealthzModule.forRoot()],
})
export class AppModule {}

HealthzModule.forRoot() is global — every module that loads into the app graph is scanned for attestors.

2. Declare an attestor inside the module that owns the dependency

import {
  HealthAttestor,
  type HealthCheckOutcome,
} from "@abinnovision/nestjs-healthz";

@HealthAttestor({ name: "database", critical: true })
export class DatabaseAttestor implements HealthAttestor {
  public constructor(private readonly db: DatabaseService) {}

  public async check(): Promise<HealthCheckOutcome> {
    await this.db.query("SELECT 1");
    return { status: "ok" };
  }
}

@HealthAttestor() applies @Injectable() internally — the same way @Controller() and @Resolver() do — so no need to also write @Injectable(). The class still needs to be registered as a provider:

@Module({
  providers: [DatabaseService, DatabaseAttestor],
})
export class DatabaseModule {}

That's it. /healthz, /readyz, and /livez are live.

Endpoints

PathBehaviorUse case
GET /livezReturns 200 { status, timestamp }. Runs no attestors.Kubernetes liveness probe — "process is alive".
GET /readyzRuns every discovered attestor and returns the aggregate status.Kubernetes readiness probe — "ready for traffic".
GET /healthzAlias of /readyz.Canonical "is the application healthy?" name.

/livez intentionally runs no attestors so that a slow downstream dependency cannot trigger pod restarts. Wire only /readyz (or /healthz) to readiness checks that gate traffic.

Status model

The aggregate status is three-state:

AggregateHTTPTrigger
"ok"200All attestors reported ok.
"degraded"200Only non-critical attestors failed.
"down"503At least one attestor with critical: true (default) failed.

Each attestor declares whether it is critical:

@HealthAttestor({ name: "queue-depth", critical: false })

Non-critical attestors are still reported, but their failure does not take traffic away.

Rate-limiting check execution

Each attestor may declare a minimum interval between actual executions. There is no rate-limit by default — every request runs every check.

@HealthAttestor({
  name: "redis",
  minIntervalMs: 10_000,
})

The previous outcome — including a previous down outcome — is reused for requests that arrive within minIntervalMs of the last execution. The next request after the interval elapses triggers a fresh check.

This is primarily a guard against probe traffic flooding the underlying subsystem. /readyz is typically polled by liveness controllers, load balancers, and dashboards in parallel; without minIntervalMs, a misconfigured client can turn that into a steady stream of SELECT 1 queries against your database. The bound is process-local — each replica enforces its own rate.

Timeouts and thrown errors

check() is bounded by a per-attestor timeout (default 2000 ms). A timed-out check is reported as { "status": "down" }. Exceptions thrown by check() are caught inside the runner and produce the same outcome — no exception ever escapes the controller.

The underlying error message is logged via the NestJS logger for operator diagnostics but is intentionally not included in the probe response, so a misbehaving subsystem cannot leak internal error text to whoever curls /readyz.

@HealthAttestor({ name: "slow-api", timeoutMs: 5000 })

Response shape

{
  "status": "ok",
  "checks": [
    {
      "name": "database",
      "status": "ok",
      "durationMs": 12
    },
    {
      "name": "redis",
      "status": "down",
      "durationMs": 2003
    }
  ],
  "timestamp": "2026-05-13T12:34:56.000Z"
}

The HTTP status code is 503 when the aggregate status is "down" and 200 otherwise.

API Reference

HealthzModule.forRoot()

Registers the module globally and mounts the three endpoints. Takes no arguments today; reserved as the canonical entry point for any future module configuration.

HealthzModule.forRoot();

@HealthAttestor(options)

Marks a class as a health attestor. The class must implement check() returning HealthCheckOutcome (sync or async). Internally applies @Injectable().

interface HealthAttestorOptions {
  name: string;
  critical?: boolean; // default true
  minIntervalMs?: number; // when set, check runs at most once per interval
  timeoutMs?: number; // default 2000
}

HealthAttestor (interface)

The contract every attestor must satisfy. Shares its name with the decorator function — TypeScript's separate value and type namespaces let @HealthAttestor(...) and implements HealthAttestor coexist.

interface HealthAttestor {
  check(): Promise<HealthCheckOutcome> | HealthCheckOutcome;
}

HealthCheckOutcome

interface HealthCheckOutcome {
  status: "ok" | "down";
  details?: Record<string, string>;
}

details is surfaced under checks[].details in the response — useful for small, safe-to-publish diagnostics (e.g. { "lag_seconds": "12" }). Values are restricted to strings so the response stays predictable and no caller can accidentally publish unbounded objects or sensitive fields.

License

Apache-2.0

版本与发布记录

@abinnovision/nestjs-healthz 共发布 8 个版本,首次发布于 2026/05/14;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/10/1227 天前
0.2.0-beta.4beta2026/10/1227 天前
0.2.0-beta.32026/10/081 个月前
0.2.0-beta.12026/05/185 个月前
0.1.12026/05/145 个月前
0.1.1-beta.12026/05/145 个月前

关于 @abinnovision/nestjs-healthz 的常见问题

@abinnovision/nestjs-healthz 是什么?

Self-mounting health check module for NestJS with cross-module attestor discovery, per-attestor caching, and Kubernetes-style /livez and /readyz endpoints.

如何安装 @abinnovision/nestjs-healthz?

在项目目录运行 npm install @abinnovision/nestjs-healthz,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/core(^11.0.0 || ^12.0.0)、@nestjs/common(^11.0.0 || ^12.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@abinnovision/nestjs-healthz 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@abinnovision/nestjs-healthz 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@abinnovision/nestjs-healthz 可以商用吗?

@abinnovision/nestjs-healthz 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@abinnovision/nestjs-healthz 的下载量有多少?

根据 npm 官方下载统计,@abinnovision/nestjs-healthz 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,935 次,日均约 65 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@abinnovision/nestjs-healthz 还在维护吗?

最新版本 0.2.0 发布于 2026/10/12(27 天前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/05/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。