Files
WRNexusJS/packages/observability/src/health.ts
T
Clintchiz 586a6db8ff
Quality / quality (ubuntu-latest) (push) Failing after 21s
Quality / quality (windows-latest) (push) Canceled after 0s
release: WRNexusJS 0.8.0
2026-08-02 23:18:51 +05:30

36 lines
1.2 KiB
TypeScript

import type { HealthRegistry } from "@wrnexus/core";
export interface HealthHandlerOptions {
exposeDetails?: boolean;
cacheControl?: string;
}
export function createLivenessHandler(options: HealthHandlerOptions = {}) {
return async (request: Request): Promise<Response> => {
if (request.method !== "GET" && request.method !== "HEAD") {
return new Response("Method Not Allowed", { status: 405, headers: { allow: "GET, HEAD" } });
}
return Response.json(
{ status: "up" },
{ status: 200, headers: { "cache-control": options.cacheControl ?? "no-store" } },
);
};
}
export function createReadinessHandler(
registry: HealthRegistry,
options: HealthHandlerOptions = {},
) {
return async (request: Request): Promise<Response> => {
if (request.method !== "GET" && request.method !== "HEAD") {
return new Response("Method Not Allowed", { status: 405, headers: { allow: "GET, HEAD" } });
}
const result = await registry.check();
const body = options.exposeDetails ? result : { status: result.status };
return Response.json(body, {
status: result.status === "down" ? 503 : 200,
headers: { "cache-control": options.cacheControl ?? "no-store" },
});
};
}