UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

49 lines (48 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterResponseForLogging = filterResponseForLogging; const isPlainObject_1 = require("./isPlainObject"); const deepCopy_1 = require("./deepCopy"); const config_1 = require("../config"); const dotProp = require("dot-prop"); /** * Prepares acton response for logging. * Hides any sensitive data as defined by `api.config.general.filteredResponse` * Truncates long strings via `api.config.logger.maxLogStringLength` */ function filterResponseForLogging(response) { var _a, _b, _c; response = (0, deepCopy_1.deepCopy)(response); const sanitizedResponse = {}; for (const i in response) { if (Array.isArray(response[i]) && response[i].length > ((_b = (_a = config_1.config.logger) === null || _a === void 0 ? void 0 : _a.maxLogArrayLength) !== null && _b !== void 0 ? _b : 10)) { response[i] = `${response[i].length} items`; } if ((0, isPlainObject_1.isPlainObject)(response[i])) { sanitizedResponse[i] = response[i]; } else if (typeof response[i] === "string") { sanitizedResponse[i] = response[i].substring(0, config_1.config.logger.maxLogStringLength); } else if (response[i] instanceof Error) { sanitizedResponse[i] = (_c = response[i].message) !== null && _c !== void 0 ? _c : String(response[i]); } else { sanitizedResponse[i] = response[i]; } } let filteredResponse; if (typeof config_1.config.general.filteredResponse === "function") { filteredResponse = config_1.config.general.filteredResponse(); } else { filteredResponse = config_1.config.general.filteredResponse; } filteredResponse.forEach((configParam) => { if (dotProp.get(response, configParam) !== undefined) { dotProp.set(sanitizedResponse, configParam, "[FILTERED]"); } }); return sanitizedResponse; }