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.

52 lines (49 loc) 1.68 kB
import { deepStrictEqual } from 'node:assert'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const DISPLAY_NAME = "HTTP check for"; const httpCheck = /* @__PURE__ */ __name((host, options) => async () => { try { const response = await fetch(host, options?.fetchOptions ?? {}); if (options?.expected?.status !== void 0 && options.expected.status !== response.status) { throw new Error(`${DISPLAY_NAME} ${host} returned status ${response.status} instead of ${options.expected.status}`); } if (options?.expected?.body !== void 0) { const textBody = await response.text(); try { deepStrictEqual(textBody, options.expected.body); } catch { throw new Error( `${DISPLAY_NAME} ${host} returned body ${JSON.stringify(textBody)} instead of ${JSON.stringify(options.expected.body)}` ); } } return { displayName: `${DISPLAY_NAME} ${host}`, health: { healthy: true, message: `${DISPLAY_NAME} ${host} was successful.`, timestamp: (/* @__PURE__ */ new Date()).toISOString() }, meta: { host, method: options?.fetchOptions?.method ?? "GET", status: response.status } }; } catch (error) { return { displayName: `${DISPLAY_NAME} ${host}`, health: { healthy: false, message: error.message, timestamp: (/* @__PURE__ */ new Date()).toISOString() }, meta: { host, method: options?.fetchOptions?.method ?? "GET" } }; } }, "httpCheck"); export { httpCheck as default };