npm package: @mimik/response-helper
Description: HTTP response helper for mimik microservices
Author: mimik technology inc
Latest version: 4.0.13

<a name="module_responseHelper"></a>

## responseHelper
**Example**  
```js
// Default import
import responseHelper from '@mimik/response-helper';
```
**Example**  
```js
// Named imports
import { ERROR_CODE, RESPONSE_CODE, sendError, sendResult, getRichError, cleanObj, getErrorStatusCode } from '@mimik/response-helper';
```

* [responseHelper](#module_responseHelper)
    * [~cleanObj(mObj, properties)](#module_responseHelper..cleanObj) ⇒ <code>object</code>
    * [~sendError(error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters)](#module_responseHelper..sendError) ⇒ <code>void</code>
    * [~sendResult(result, statusCode, response, swaggerOptions, logLevel, parameters)](#module_responseHelper..sendResult) ⇒ <code>void</code>
    * [~getErrorStatusCode(type, error)](#module_responseHelper..getErrorStatusCode) ⇒ <code>number</code>
    * [~getRichError(val, message, info, origErr, logLevel, correlationId)](#module_responseHelper..getRichError) ⇒ <code>Error</code>

<a name="module_responseHelper..cleanObj"></a>

### responseHelper~cleanObj(mObj, properties) ⇒ <code>object</code>
Clean a mongoose object

**Kind**: inner method of [<code>responseHelper</code>](#module_responseHelper)  
**Returns**: <code>object</code> - The cleaned Mongoose object (or array), or the original value if not an object/array.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| mObj | <code>object</code> | Mongoose object to clean. If null, not an array, or not an object, returns mObj as-is. |
| properties | <code>array</code> | Properties to remove from the object. Top level only. If null or not an array, the default ['_id', '__v'] is used. |

<a name="module_responseHelper..sendError"></a>

### responseHelper~sendError(error, response, otherErrorStatusCode, swaggerOptions, logLevel, parameters) ⇒ <code>void</code>
The response body contains either `error` or `info`, never both:
- If the `error` param is an `Error` instance, the body includes an `error` field.
- Otherwise, the body includes an `info` field with the raw value.
```javascript
{
  "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:
```javascript
{
  "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):
```javascript
{
  "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 [<code>responseHelper</code>](#module_responseHelper)  
**Category**: sync  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>  

| Param | Type | Description |
| --- | --- | --- |
| error | <code>Error</code> \| <code>string</code> | The error to include in the response. |
| response | <code>object</code> | The HTTP response object. |
| otherErrorStatusCode | <code>number</code> | Status code that overrides error.statusCode for the response. |
| swaggerOptions | <code>object</code> | Object containing Swagger values used to set up the error. |
| logLevel | <code>boolean</code> \| <code>string</code> | 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 | <code>object</code> | Parameters to add or configure the response |

<a name="module_responseHelper..sendResult"></a>

### responseHelper~sendResult(result, statusCode, response, swaggerOptions, logLevel, parameters) ⇒ <code>void</code>
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):
```javascript
{
  "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 [<code>responseHelper</code>](#module_responseHelper)  
**Category**: sync  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/lib-filters</code>  

| Param | Type | Description |
| --- | --- | --- |
| result | <code>object</code> \| <code>Buffer</code> | The result to send. |
| statusCode | <code>number</code> | HTTP status code to send. |
| response | <code>object</code> | The HTTP response object. |
| swaggerOptions | <code>object</code> | Object containing values for logging (see sendError). |
| logLevel | <code>string</code> \| <code>boolean</code> | Controls logging. If `true`, no log is written. If a valid level, uses that level. If null/undefined/invalid, defaults to `info`. |
| parameters | <code>object</code> | Parameters to add or configure the response. |

<a name="module_responseHelper..getErrorStatusCode"></a>

### responseHelper~getErrorStatusCode(type, error) ⇒ <code>number</code>
Get the HTTP status code for a given error type.

**Kind**: inner method of [<code>responseHelper</code>](#module_responseHelper)  
**Returns**: <code>number</code> - The HTTP status code.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Error type name. Valid values: `System` (500), `Conflict` (409), `Forbidden` (403), `Gone` (410), `MultiStatus` (207), `NoContent` (204), `NotAllowed` (405), `NotFound` (404), `NotImplemented` (501), `Parameter` / `ParameterError` (400), `Partial` (206), `PaymentRequired` (402), `RequestTimeout` (408), `UnAuthorized` (401), `Unprocessable` (422). Null or unrecognized values default to 500. |
| error | <code>object</code> | 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). |

<a name="module_responseHelper..getRichError"></a>

### responseHelper~getRichError(val, message, info, origErr, logLevel, correlationId) ⇒ <code>Error</code>
Create a rich error.

**Kind**: inner method of [<code>responseHelper</code>](#module_responseHelper)  
**Returns**: <code>Error</code> - The rich error instance.

```javascript
{
  "statusCode": "<HTTP Code>",
  "title": "<HTTP title>",
  "name": "<error name>",
  "info": "<additional info>",
  "cause": "<encapsulated error, if any>",
  "message": "<error message>"
}
```  
**Category**: sync  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>  

| Param | Type | Description |
| --- | --- | --- |
| val | <code>string</code> \| <code>number</code> | If a string, the error name; if a number, the HTTP status code (e.g., 400, 500). Invalid values fall back to 500. |
| message | <code>string</code> | Message to associate with the error. |
| info | <code>object</code> | Arbitrary info to associate with the error. |
| origErr | <code>\*</code> | Error to encapsulate as the cause. |
| logLevel | <code>string</code> \| <code>boolean</code> | Logging behavior. If `false`, logs at 'error'. If a valid level, uses that level. If `true` or null/undefined, no log is written. |
| correlationId | <code>string</code> | Correlation ID to add to the log when logging is enabled. |