UNPKG

proactive-deps

Version:

A library for managing proactive dependency checks in Node.js applications.

56 lines (55 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = formatCheckResult; const constants_1 = require("../constants"); const objectifyError = (error) => error ? { name: error.name, message: error.message, stack: error.stack, } : undefined; function formatCheckResult(dependency, result, latencyMs = 0, skipped = false) { const resultCode = typeof result === 'number' ? result : result.code; const resultError = typeof result === 'number' ? undefined : result.error; const resultErrorMessage = typeof result === 'number' ? undefined : result.errorMessage; const status = { name: dependency.name, lastChecked: new Date().toISOString(), description: dependency.description, impact: dependency.impact, contact: dependency.contact, healthy: false, health: { state: constants_1.ERROR_STATUS_MESSAGE, code: constants_1.ERROR_STATUS_CODE, latency: latencyMs, skipped: false, }, checkDetails: dependency.checkDetails, }; if (skipped) { status.health.skipped = true; status.healthy = true; status.health.code = constants_1.SUCCESS_STATUS_CODE; status.health.state = constants_1.SUCCESS_STATUS_MESSAGE; return status; } switch (resultCode) { case constants_1.SUCCESS_STATUS_CODE: status.healthy = true; status.health.code = constants_1.SUCCESS_STATUS_CODE; status.health.state = constants_1.SUCCESS_STATUS_MESSAGE; break; case constants_1.WARNING_STATUS_CODE: status.healthy = true; status.health.code = constants_1.WARNING_STATUS_CODE; status.health.state = constants_1.WARNING_STATUS_MESSAGE; break; case constants_1.ERROR_STATUS_CODE: status.error = objectifyError(resultError); status.errorMessage = resultErrorMessage; break; } return status; }