@mimik/response-helper
v4.0.10
Published
HTTP response helper for mimik microservices
Readme
responseHelper
Example
// Default import
import responseHelper from '@mimik/response-helper';Example
// Named imports
import { ERROR_CODE, RESPONSE_CODE, sendError, sendResult, getRichError, cleanObj, getErrorStatusCode } from '@mimik/response-helper';- responseHelper
- ~cleanObj(mObj, properties) ⇒ object
- ~sendError(error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters) ⇒ void
- ~sendResult(result, statusCode, response, swaggerOptions, logLevel, parameters) ⇒ void
- ~getErrorStatusCode(type, error) ⇒ number
- ~getRichError(val, message, info, origErr, logLevel, correlationId) ⇒ Error
responseHelper~cleanObj(mObj, properties) ⇒ object
Clean a mongoose object
Kind: inner method of responseHelper
Returns: object - The cleaned Mongoose object (or array), or the original value if not an object/array.
Category: sync
| Param | Type | Description | | --- | --- | --- | | mObj | object | Mongoose object to clean. If null, not an array, or not an object, returns mObj as-is. | | properties | array | Properties to remove from the object. Top level only. If null or not an array, the default ['_id', '__v'] is used. |
responseHelper~sendError(error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters) ⇒ void
The response body contains either error or info, never both:
- If the
errorparam is anErrorinstance, the body includes anerrorfield. - Otherwise, the body includes an
infofield with the raw value.
{
"statusCode": <HTTP code>,
"title": "<HTTP title>",
"message": "error.message or 'no message' if not present",
"error": "<present when error is an Error instance>",
"info": "<present when error is NOT an Error instance>"
}Swagger options:
{
"swagger": {
"method": "<request method>",
"path": "<request path>",
"operationId": "<operation id for the route>"
},
"correlationId": "<value from request headers if present; otherwise a UUID>"
}Parameters (optional):
{
"rfc": "use RFC 7807 format when set to '7807' (may also be present on the error itself)",
"headers": { "<headerName>": "<value>", ... } // extra headers to add to the response
}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.
Kind: inner method of responseHelper
Category: sync
Requires: module:@mimik/sumologic-winston-logger
| Param | Type | Description |
| --- | --- | --- |
| error | Error | string | The error to include in the response. |
| response | object | The HTTP response object. |
| otherErrorStatusCode | number | Status code that overrides error.statusCode for the response. |
| swaggerOptions | object | Object containing Swagger values used to set up the error. |
| logLevel | boolean | string | 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. |
| parameters | object | Parameters to add or configure the response |
responseHelper~sendResult(result, statusCode, response, swaggerOptions, logLevel, parameters) ⇒ void
If result contains a data field, it is sent as-is (after optional cleanup). Otherwise, the payload is wrapped under a data property.
Parameters (optional):
{
"noTouch": true // send `result` exactly as provided (no wrapping/cleanup). If Buffer, writes raw bytes.
"headers": { "<headerName>": "<value>", ... }, // extra headers to add
"cleanUp": ["propA", "propB"] // top-level properties to remove from the object(s) in `data`
}Kind: inner method of responseHelper
Category: sync
Requires: module:@mimik/sumologic-winston-logger, module:@mimik/lib-filters
| Param | Type | Description |
| --- | --- | --- |
| result | object | Buffer | The result to send. |
| statusCode | number | HTTP status code to send. |
| response | object | The HTTP response object. |
| swaggerOptions | object | Object containing values for logging (see sendError). |
| logLevel | string | boolean | Controls logging. If true, no log is written. If a valid level, uses that level. If null/undefined/invalid, defaults to info. |
| parameters | object | Parameters to add or configure the response. |
responseHelper~getErrorStatusCode(type, error) ⇒ number
Get the HTTP status code for a given error type.
Kind: inner method of responseHelper
Returns: number - The HTTP status code.
Category: sync
| Param | Type | Description |
| --- | --- | --- |
| type | string | 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. |
| error | object | 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). |
responseHelper~getRichError(val, message, info, origErr, logLevel, correlationId) ⇒ Error
Create a rich error.
Kind: inner method of responseHelper
Returns: Error - The rich error instance.
{
"statusCode": "<HTTP Code>",
"title": "<HTTP title>",
"name": "<error name>",
"info": "<additional info>",
"cause": "<encapsulated error, if any>",
"message": "<error message>"
}Category: sync
Requires: module:@mimik/sumologic-winston-logger
| Param | Type | Description |
| --- | --- | --- |
| val | string | number | If a string, the error name; if a number, the HTTP status code (e.g., 400, 500). Invalid values fall back to 500. |
| message | string | Message to associate with the error. |
| info | object | Arbitrary info to associate with the error. |
| origErr | * | Error to encapsulate as the cause. |
| logLevel | string | boolean | Logging behavior. If false, logs at 'error'. If a valid level, uses that level. If true or null/undefined, no log is written. |
| correlationId | string | Correlation ID to add to the log when logging is enabled. |
