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OAAO,EACjC,MAAM,EAAY,IAAI,MAAM,UAG5B,OADA,EAAU,KAAO,EACV,CAAS,EAKW,CAAW,IACX,EAAM,YAAhB,iBAAR,EAAoC,EACxB,mBAAmB,EAAK,EAAQ,QACvD,MAAM,EAAQ,IAAI,MAAM,GAzUC,aAyU8B,IAEtD,EAAM,WAAY,EAAM,KAAM,EAAM,OAAS,EAC9C,EAAM,KAAO,EACb,MAAM,EAAO,IACX,MAAM,EAAe,CACnB,WAAY,EAAM,WAClB,KAAM,EAAM,KACZ,MAAO,EAAM,OAGX,IAAM,EAAa,KAAO,GAC1B,EAAM,QAAO,EAAa,MAAQ,EAAM,OAC5C,OAAO,GAAO,EAAM,QAAS,EAAc,EAAc,EAS3D,OALI,WACe,IAAb,EAAoB,EAAI,OACnB,WAAW,SAAS,GAAW,EAAI,IACtB,IAAb,GAAmB,EAAI,QAE3B,CAAK,gBAGC,CACb,sBACA,4BACA,oBACA,sBACA,0BACA,kBACA","file":"/npm/@mimik/response-helper@4.0.10/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@mimik/response-helper@4.0.10/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { Buffer } from 'node:buffer';\nimport { STATUS_CODES } from 'node:http';\nimport { lengthyString } from '@mimik/lib-filters';\nimport logger from '@mimik/sumologic-winston-logger';\n/**\n * @module responseHelper\n * @example\n * // Default import\n * import responseHelper from '@mimik/response-helper';\n * @example\n * // Named imports\n * import { ERROR_CODE, RESPONSE_CODE, sendError, sendResult, getRichError, cleanObj, getErrorStatusCode } from '@mimik/response-helper';\n */\n\nconst DEFAULT_NO_MESSAGE = 'no message';\nconst DEFAULT_NO_ERROR_NAME = 'no error name';\nconst NOT_HTTP_CODE = 'not HTTP code';\nconst NO_ERROR = 'not an error object';\nconst LOG_LEVELS = logger.LEVELS;\n\nconst RFC_ERROR = '7807';\nconst ERROR = 'error';\nconst WARN = 'warn';\nconst INFO = 'info';\nconst DEFAULT_PROPERTIES = ['_id', '__v'];\n\nexport const RESPONSE_CODE = {\n  OK: 200,\n  CREATED: 201,\n  ACCEPTED: 202,\n  NO_CONTENT: 204,\n  PARTIAL: 206,\n  MULTI_STATUS: 207,\n};\n\nexport const ERROR_CODE = {\n  PARAMETER: 400,\n  UNAUTHORIZED: 401,\n  FORBIDDEN: 403,\n  NOT_FOUND: 404,\n  NOT_ALLOWED: 405,\n  CONFLICT: 409,\n  UNPROCESSABLE: 422,\n  SYSTEM: 500,\n  NOT_IMPLEMENTED: 501,\n};\n\nconst TAB = 0; // we don't need the response to be pretty\n\n/**\n *\n * Clean a mongoose object\n *\n * @function cleanObj\n * @category sync\n * @param {object} mObj - Mongoose object to clean. If null, not an array, or not an object, returns mObj as-is.\n * @param {array} properties - Properties to remove from the object. Top level only. If null or not an array, the default ['_id', '__v'] is used.\n * @return {object} The cleaned Mongoose object (or array), or the original value if not an object/array.\n *\n */\nexport const cleanObj = (mObj, properties) => {\n  if (!mObj) return mObj;\n  let updatedProp = properties;\n\n  if (!updatedProp || !Array.isArray(updatedProp)) updatedProp = DEFAULT_PROPERTIES;\n\n  const clean = (obj) => {\n    if (!obj) return obj;\n    const updatedObj = obj.toObject ? obj.toObject() : { ...obj };\n\n    updatedProp.forEach((prop) => {\n      if (prop in updatedObj) delete updatedObj[prop];\n    });\n    return updatedObj;\n  };\n\n  if (Array.isArray(mObj)) return mObj.map(clean);\n  if (typeof mObj === 'object') return clean(mObj);\n  return mObj;\n};\n\n/**\n * Send an error.\n *\n * @function sendError\n * @requires @mimik/sumologic-winston-logger\n * @category sync\n * @param {Error | string} error - The error to include in the response.\n * @param {object} response - The HTTP response object.\n * @param {number} otherErrorStatusCode - Status code that overrides error.statusCode for the response.\n * @param {object} swaggerOptions - Object containing Swagger values used to set up the error.\n * @param {boolean | string} logLevel - Controls logging. If `true`, no log is written. If a valid level (e.g., 'error', 'warn', 'info'), uses that level. If null/undefined/invalid, defaults to `error` for 500+ or `warn` otherwise.\n * @param {object} parameters - Parameters to add or configure the response\n * @return {void}\n *\n * @description The response body contains either `error` or `info`, never both:\n * - If the `error` param is an `Error` instance, the body includes an `error` field.\n * - Otherwise, the body includes an `info` field with the raw value.\n * ```javascript\n * {\n *   \"statusCode\": <HTTP code>,\n *   \"title\": \"<HTTP title>\",\n *   \"message\": \"error.message or 'no message' if not present\",\n *   \"error\": \"<present when error is an Error instance>\",\n *   \"info\": \"<present when error is NOT an Error instance>\"\n * }\n * ```\n * Swagger options:\n * ```javascript\n * {\n *   \"swagger\": {\n *     \"method\": \"<request method>\",\n *     \"path\": \"<request path>\",\n *     \"operationId\": \"<operation id for the route>\"\n *   },\n *   \"correlationId\": \"<value from request headers if present; otherwise a UUID>\"\n * }\n * ```\n * Parameters (optional):\n * ```javascript\n * {\n *   \"rfc\": \"use RFC 7807 format when set to '7807' (may also be present on the error itself)\",\n *   \"headers\": { \"<headerName>\": \"<value>\", ... } // extra headers to add to the response\n * }\n * ```\n * If a response with statusCode >= 500 is sent and logging is enabled but not explicitly set, an error-level log is emitted; otherwise a warn-level log is used.\n */\nexport const sendError = (error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters) => {\n  response.statusCode = otherErrorStatusCode || error.statusCode || ERROR_CODE.SYSTEM;\n  const { statusCode } = response;\n  const { method, path, operationId } = swaggerOptions?.swagger || {};\n  const { correlationId } = swaggerOptions || {};\n  const log = (level) => {\n    logger[level](`Error responding ${statusCode} for ${method} ${path}`, {\n      statusCode,\n      method,\n      path,\n      operationId,\n      error,\n    }, correlationId);\n  };\n\n  if (logLevel !== true) {\n    if (logLevel === null || logLevel === undefined) log(statusCode >= ERROR_CODE.SYSTEM ? ERROR : WARN);\n    else if (LOG_LEVELS.includes(logLevel)) log(logLevel);\n    else log(statusCode >= ERROR_CODE.SYSTEM ? ERROR : WARN);\n  }\n  if (correlationId) response.setHeader('x-correlation-id', correlationId);\n  response.setHeader('Content-Type', 'application/json');\n  const respError = {\n    statusCode,\n    title: STATUS_CODES[statusCode],\n    message: error && error.message ? error.message : DEFAULT_NO_MESSAGE,\n  };\n\n  if (parameters) {\n    if (parameters.headers) {\n      Object.entries(parameters.headers).forEach(([key, item]) => {\n        response.setHeader(key, item);\n      });\n    }\n    if (parameters.rfc === RFC_ERROR) {\n      const { rfc7807 } = error;\n      const respErrorRfc7807 = rfc7807\n        ? { ...rfc7807 }\n        : {\n            title: respError.title,\n            detail: respError.message,\n            type: 'about:blank',\n          };\n\n      respErrorRfc7807.status = statusCode;\n      response.end(JSON.stringify(respErrorRfc7807, null, TAB));\n      return;\n    }\n  }\n  if (error instanceof Error) respError.error = error;\n  else respError.info = error;\n  response.end(JSON.stringify(respError, null, TAB));\n};\n\n/**\n * Send a success response.\n *\n * @function sendResult\n * @requires @mimik/sumologic-winston-logger\n * @requires @mimik/lib-filters\n * @category sync\n * @param {object|Buffer} result - The result to send.\n * @param {number} statusCode - HTTP status code to send.\n * @param {object} response - The HTTP response object.\n * @param {object} swaggerOptions - Object containing values for logging (see sendError).\n * @param {string|boolean} logLevel - Controls logging. If `true`, no log is written. If a valid level, uses that level. If null/undefined/invalid, defaults to `info`.\n * @param {object} parameters - Parameters to add or configure the response.\n * @return {void}\n *\n * @description If `result` contains a `data` field, it is sent as-is (after optional cleanup). Otherwise, the payload is wrapped under a `data` property.\n *\n * Parameters (optional):\n * ```javascript\n * {\n *   \"noTouch\": true // send `result` exactly as provided (no wrapping/cleanup). If Buffer, writes raw bytes.\n *   \"headers\": { \"<headerName>\": \"<value>\", ... }, // extra headers to add\n *   \"cleanUp\": [\"propA\", \"propB\"] // top-level properties to remove from the object(s) in `data`\n * }\n * ```\n */\nexport const sendResult = (result, statusCode, response, swaggerOptions, logLevel, parameters) => {\n  let resp;\n  let cleanOption;\n  const { method, path, operationId } = swaggerOptions?.swagger || {};\n  const { correlationId } = swaggerOptions || {};\n  const log = (level) => {\n    logger[level](`Responding ${statusCode} for ${method} ${path}`, {\n      statusCode,\n      method,\n      path,\n      operationId,\n      result: lengthyString(result),\n    }, correlationId);\n  };\n\n  response.statusCode = statusCode;\n  if (logLevel !== true) {\n    if (logLevel === null || logLevel === undefined) log(INFO);\n    else if (LOG_LEVELS.includes(logLevel)) log(logLevel);\n    else log(INFO);\n  }\n\n  response.setHeader('Content-Type', 'application/json');\n  if (correlationId) response.setHeader('x-correlation-id', correlationId);\n  if (parameters) {\n    if (parameters.headers) {\n      Object.entries(parameters.headers).forEach(([key, item]) => {\n        response.setHeader(key, item);\n      });\n    }\n    if (parameters.noTouch) {\n      if (result instanceof Buffer) {\n        response.end(result);\n        return;\n      }\n      response.end(JSON.stringify(result, null, TAB));\n      return;\n    }\n    cleanOption = parameters.cleanUp;\n  }\n  if (result && (result.data !== undefined)) {\n    const cleanResult = { ...result };\n\n    cleanResult.data = cleanObj(cleanResult.data, cleanOption);\n    resp = cleanResult;\n  }\n  else resp = { data: cleanObj(result, cleanOption) };\n  response.end(JSON.stringify(resp, null, TAB));\n};\n\nconst NAME_TO_CODE = {\n  Conflict: ERROR_CODE.CONFLICT,\n  Forbidden: ERROR_CODE.FORBIDDEN,\n  MultiStatus: RESPONSE_CODE.MULTI_STATUS,\n  NoContent: RESPONSE_CODE.NO_CONTENT,\n  NotAllowed: ERROR_CODE.NOT_ALLOWED,\n  NotFound: ERROR_CODE.NOT_FOUND,\n  NotImplemented: ERROR_CODE.NOT_IMPLEMENTED,\n  Parameter: ERROR_CODE.PARAMETER,\n  ParameterError: ERROR_CODE.PARAMETER,\n  Partial: RESPONSE_CODE.PARTIAL,\n  System: ERROR_CODE.SYSTEM,\n  UnAuthorized: ERROR_CODE.UNAUTHORIZED,\n  Unprocessable: ERROR_CODE.UNPROCESSABLE,\n};\n\nconst CODE_TO_NAME = new Map(\n  Object.entries(NAME_TO_CODE)\n    .filter(([name]) => name !== 'ParameterError')\n    .map(([name, code]) => [code, name]),\n);\n\n/**\n * Get the HTTP status code for a given error type.\n *\n * @function getErrorStatusCode\n * @category sync\n * @param {string} type - Error type name. Valid values: `System` (500), `Conflict` (409), `Forbidden` (403), `MultiStatus` (207), `NoContent` (204), `NotAllowed` (405), `NotFound` (404), `NotImplemented` (501), `Parameter` / `ParameterError` (400), `Partial` (206), `UnAuthorized` (401), `Unprocessable` (422). Null or unrecognized values default to 500.\n * @param {object} error - Error object. Used as a fallback when `type` is unrecognized: if `error.name` is `'EntryError'` returns 409, if `error.name` is `'CastError'` returns 404 (when `error.kind` is `'ObjectId'`) or 400 (otherwise).\n * @return {number} The HTTP status code.\n */\nexport const getErrorStatusCode = (type, error) => {\n  if (!type) return ERROR_CODE.SYSTEM;\n  if (NAME_TO_CODE[type] !== undefined) return NAME_TO_CODE[type];\n  if (error && error.name === 'EntryError') return ERROR_CODE.CONFLICT;\n  if (error && error.name === 'CastError') return error.kind === 'ObjectId' ? ERROR_CODE.NOT_FOUND : ERROR_CODE.PARAMETER;\n  return ERROR_CODE.SYSTEM;\n};\n\nconst getErrorIds = (num) => {\n  const name = CODE_TO_NAME.get(num);\n\n  if (name) return [num, name, STATUS_CODES[num]];\n  if (STATUS_CODES[num]) return [num, STATUS_CODES[num], STATUS_CODES[num]];\n  return [ERROR_CODE.SYSTEM, DEFAULT_NO_ERROR_NAME, NOT_HTTP_CODE];\n};\n\n/**\n * Create a rich error.\n *\n * @function getRichError\n * @requires @mimik/sumologic-winston-logger\n * @category sync\n * @param {string|number} val - If a string, the error name; if a number, the HTTP status code (e.g., 400, 500). Invalid values fall back to 500.\n * @param {string} message - Message to associate with the error.\n * @param {object} info - Arbitrary info to associate with the error.\n * @param {*} origErr - Error to encapsulate as the cause.\n * @param {string|boolean} logLevel - Logging behavior. If `false`, logs at 'error'. If a valid level, uses that level. If `true` or null/undefined, no log is written.\n * @param {string} correlationId - Correlation ID to add to the log when logging is enabled.\n * @return {Error} The rich error instance.\n *\n * ```javascript\n * {\n *   \"statusCode\": \"<HTTP Code>\",\n *   \"title\": \"<HTTP title>\",\n *   \"name\": \"<error name>\",\n *   \"info\": \"<additional info>\",\n *   \"cause\": \"<encapsulated error, if any>\",\n *   \"message\": \"<error message>\"\n * }\n * ```\n */\nexport const getRichError = (val, message, info, origErr, logLevel, correlationId) => {\n  const buildCause = (err) => {\n    if (err instanceof Error) return err;\n    const tempError = new Error(NO_ERROR);\n\n    tempError.info = err;\n    return tempError;\n  };\n  let ids;\n  const options = {};\n\n  if (origErr) options.cause = buildCause(origErr);\n  if (typeof val === 'number') ids = getErrorIds(val);\n  else ids = getErrorIds(getErrorStatusCode(val, options.cause));\n  const error = new Error(message || DEFAULT_NO_MESSAGE, options);\n\n  [error.statusCode, error.name, error.title] = ids;\n  error.info = info;\n  const log = (level) => {\n    const displayError = {\n      statusCode: error.statusCode,\n      name: error.name,\n      title: error.title,\n    };\n\n    if (info) displayError.info = info;\n    if (error.cause) displayError.cause = error.cause;\n    logger[level](error.message, displayError, correlationId);\n  };\n\n  // If logLevel is null/undefined, do not log. Set to `false` to force 'error'-level logging, or to a valid level.\n  if (logLevel !== null && logLevel !== undefined) {\n    if (logLevel === false) log(ERROR);\n    else if (LOG_LEVELS.includes(logLevel)) log(logLevel);\n    else if (logLevel !== true) log(ERROR);\n  }\n  return error;\n};\n\nexport default {\n  ERROR_CODE,\n  RESPONSE_CODE,\n  sendError,\n  sendResult,\n  getRichError,\n  cleanObj,\n  getErrorStatusCode,\n};\n"]}