# @mimik/response-helper

> HTTP response helper for mimik microservices

Latest version **4.1.1** (published 2026-07-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mimik/response-helper
pnpm add @mimik/response-helper
yarn add @mimik/response-helper
bun add @mimik/response-helper
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 4.1.1 |
| Published | 2026-07-08 |
| First published | 2018-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=24.0.0 |
| Dependencies | 2 |
| Unpacked size | 24.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mimik technology inc |
| Maintainers | mimik-npm-editor, hofachiang, miburger, mimikopensource, sasan.raisdana |
| Keywords | mimik, microservice |

## Links

- npm: https://www.npmjs.com/package/@mimik/response-helper
- Repository: https://bitbucket.org/mimiktech/response-helper
- Homepage: https://bitbucket.org/mimiktech/response-helper#readme
- Issues: https://bitbucket.org/mimiktech/response-helper/issues
- npm.io page: https://npm.io/package/@mimik/response-helper

## Dependencies (2)

- [@mimik/lib-filters](https://npm.io/package/@mimik/lib-filters.md) ^2.1.1
- [@mimik/sumologic-winston-logger](https://npm.io/package/@mimik/sumologic-winston-logger.md) ^2.3.1

## Recent versions

- 4.1.1 (latest) — 2026-07-08
- 4.0.13 — 2026-03-22
- 4.0.12 — 2026-03-22
- 4.0.11 — 2026-03-12
- 4.0.10 — 2026-02-23
- 4.0.9 — 2026-02-16
- 4.0.8 — 2025-12-10
- 4.0.7 — 2025-11-20
- 4.0.6 — 2025-09-13
- 4.0.4 — 2025-07-19
- 4.0.3 — 2025-07-18
- 4.0.1 — 2025-04-16
- 4.0.0 — 2025-03-25
- 3.1.0 — 2024-03-25
- 3.0.1 — 2023-12-05
- … 18 more at https://npm.io/package/@mimik/response-helper/versions

## README

<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>string</code> \| <code>boolean</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` or any other invalid value (e.g., `'unknown'`), 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. |

---
_Source: https://npm.io/package/@mimik/response-helper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
