UNPKG

@visulima/health-check

Version:

A library built to provide support for defining service health for node services. It allows you to register async health checks for your dependencies and the service itself, provides a health endpoint that exposes their status, and health metrics.

25 lines (21 loc) 978 B
'use strict'; const httpStatusCodes = require('http-status-codes'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const healthcheck = /* @__PURE__ */ __name((healthCheck, sendHeader = true) => async (_, response) => { const { healthy, report } = await healthCheck.getReport(); const payload = { appName: process.env.APP_NAME ?? "unknown", appVersion: process.env.APP_VERSION ?? "unknown", message: healthy ? "Health check successful" : "Health check failed", reports: report, status: healthy ? "ok" : "error", timestamp: (/* @__PURE__ */ new Date()).toISOString() }; response.statusCode = healthy ? httpStatusCodes.StatusCodes.OK : httpStatusCodes.StatusCodes.SERVICE_UNAVAILABLE; if (sendHeader) { response.setHeader("Content-Type", "application/json"); } response.end(JSON.stringify(payload, null, 2)); }, "default"); module.exports = healthcheck;