# reshelper

> Map response for REST API,Node REST API Helper

Latest version **1.0.4** (published 2022-06-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install reshelper
pnpm add reshelper
yarn add reshelper
bun add reshelper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-06-19 |
| First published | 2021-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hemin Tailor |
| Maintainers | hemintailor |
| Keywords | reshelper, helper, response, rest, res, api, json, express, http, status, content, data |

## Links

- npm: https://www.npmjs.com/package/reshelper
- Repository: https://github.com/hemintailor/reshelper
- Homepage: https://github.com/hemintailor/reshelper#readme
- Issues: https://github.com/hemintailor/reshelper/issues
- npm.io page: https://npm.io/package/reshelper

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-06-19
- 1.0.3 — 2021-12-10
- 1.0.2 — 2021-12-09
- 1.0.1 — 2021-11-07
- 1.0.0 — 2021-11-07

## README

# RESHELPER

Reshelper provides a simple way of returning basic REST API responses.

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

https://www.npmjs.com/package/reshelper

[![npm](https://nodei.co/npm/reshelper.png)](https://www.npmjs.com/package/reshelper)

### [Demo](https://github.com/hemintailor/node-mongo)

## Installation

Package is available at: https://www.npmjs.com/package/reshelper

Use package manager to install

```
  npm i reshelper
``` 

Now go to your main file (app.js / index.js) of node project. and set code shown below.

```
const resHelper = require('reshelper');

app.use(resHelper);
```
Yes that's it.

<br/>

## Usage

### Success response
```javascript
router.get('/', (req, res, next) => {
  return res.data({foo: 'bar'});
});
```

It will Generate the response shown below:

```json
{
  "code": 200,
  "success": true,
  "data": {
    "foo": "bar"
  },
  "error": null,
  "message": ""
}
```
<br/>

### Success response with a message and code.

if your data object contains "code" key, It will consider it as response code.
Your response status code will be changed to given code. You can ignore it by setting `.env` variable `RES_HALPER_IGNORE_STATUS_CODE=true`

```javascript
router.get('/', (req, res, next) => {
  const data = {foo: 'bar', code: 201}
  return res.data(data, 'Data found.');
});
```

It will Generate the response shown below:

```json
{
  "code": 201,
  "success": true,
  "data": {
    "foo": "bar",
    "code": 201
  },
  "error": null,
  "message": "Data found."
}
```

<br/>

### Error response
```javascript
router.get('/', (req, res, next) => {
  return res.error(new Error('Anything happen'))
});
```

It will Generate the response shown below:

```json
{
  "code": 500,
  "success": false,
  "data": null,
  "error": {
    "message": "Anything happen",
    "stack": "Error: Anything happen\n    at app\\routes\\index.js:9:20\n    at Layer.handle [as handle_request] (app\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at next (app\\node_modules\\express\\lib\\router\\route.js:137:13)\n    at Route.dispatch (app\\node_modules\\express\\lib\\router\\route.js:112:3)\n    at Layer.handle [as handle_request] (app\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at app\\node_modules\\express\\lib\\router\\index.js:281:22\n    at Function.process_params (app\\node_modules\\express\\lib\\router\\index.js:335:12)\n    at next (app\\node_modules\\express\\lib\\router\\index.js:275:10)\n    at Function.handle (app\\node_modules\\express\\lib\\router\\index.js:174:3)\n    at router (app\\node_modules\\express\\lib\\router\\index.js:47:12)"
  },
  "message": "Anything happen"
}
```

<br/>

### Error response with custom code
```javascript
router.get('/', (req, res, next) => {
  return res.error(new Error('Anything happen'), 400)
});
```

It will Generate the response shown below:
you can always remove "stack" from response, By simply setting the `.env` variable `RES_HALPER_DISABLE_ERR_STACK=false`.

```json
{
  "code": 400,
  "success": false,
  "data": null,
  "error": {
    "message": "Anything happen",
    "stack": "Error: Anything happen\n    at app\\routes\\index.js:9:20\n    at Layer.handle [as handle_request] (app\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at next (app\\node_modules\\express\\lib\\router\\route.js:137:13)\n    at Route.dispatch (app\\node_modules\\express\\lib\\router\\route.js:112:3)\n    at Layer.handle [as handle_request] (app\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at app\\node_modules\\express\\lib\\router\\index.js:281:22\n    at Function.process_params (app\\node_modules\\express\\lib\\router\\index.js:335:12)\n    at next (app\\node_modules\\express\\lib\\router\\index.js:275:10)\n    at Function.handle (app\\node_modules\\express\\lib\\router\\index.js:174:3)\n    at router (app\\node_modules\\express\\lib\\router\\index.js:47:12)"
  },
  "message": "Anything happen"
}
```

<br/>

### Message response
```javascript
router.put('/', (req, res, next) => {
  return res.message('Data updated.');
});
```

It will Generate the response shown below:

```json
{
  "code": 200,
  "success": true,
  "data": null,
  "error": null,
  "message": "Data updated."
}
```

<br/>

### Methods
```javascript
  return res.data({foo: 'bar'});
  return res.data({foo: 'bar'}, 'Your Message'); // with custom message
  return res.data({foo: 'bar', code: 201}, 'Your Message'); // with custom code
  return res.message('Your Message');
  return res.message('Your Message', 200); // with custom code
  return res.error(new Error('Anything happen'));
  return res.error(new Error('Anything happen'), 400);  // with custom code
```

<br/>

### Some ENV variables

| Variable | Description |
| --- | --- |
| RES_HALPER_IGNORE_STATUS_CODE | Boolean (default: false)<br/>true: will ignore status code, always set to 200. <br/>false: Will set status code according|
| RES_HALPER_ERR_CONSOLE | Boolean (default: false)<br/>true: will display error in console. <br/>false: Will disable error console|
| RES_HALPER_DISABLE_ERR_STACK | Boolean (default: false)<br/>true: will hide stack from error in response. <br/>false: Will display error stack in response|

<br/>

## Contributing

If you want to contribute in any of these ways:

- Give your ideas or feedback
- Enhance the code or its documentation
- Point out a problem or issue
- Question something
- Help in any other way

You can (and should) [open an issue](https://github.com/hemintailor/reshelper/issues/new) or even a [pull request](https://github.com/hemintailor/reshelper/compare)!

Thanks for your interest in contributing to this repo!

<br/>

## Author

[Hemin Tailor](https://github.com/hemintailor) (flyingtailorbird@gmail.com)

<br/>

## License

This code is licensed under the [MIT License](https://github.com/hemintailor/reshelper/blob/master/LICENSE). See the [LICENSE](https://github.com/hemintailor/reshelper/blob/master/LICENSE) file for more info.

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