reshelper

Map response for REST API,Node REST API Helper

Usage no npm install needed!

<script type="module">
  import reshelper from 'https://cdn.skypack.dev/reshelper';
</script>

README

RESHELPER

Reshelper provides a simple way of returning basic REST API responses.

License: MIT

https://www.npmjs.com/package/reshelper

npm

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.


Usage

Success response

router.get('/', (req, res, next) => {
  return res.data({foo: 'bar'});
});

It will Generate the response shown below:

{
  "code": 200,
  "success": true,
  "data": {
    "foo": "bar"
  },
  "error": null,
  "message": ""
}

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

router.get('/', (req, res, next) => {
  const data = {foo: 'bar', code: 201}
  return res.data(data, 'Data found.');
});

It will Generate the response shown below:

{
  "code": 201,
  "success": true,
  "data": {
    "foo": "bar",
    "code": 201
  },
  "error": null,
  "message": "Data found."
}

Error response

router.get('/', (req, res, next) => {
  return res.error(new Error('Anything happen'))
});

It will Generate the response shown below:

{
  "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"
}

Error response with custom code

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.

{
  "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"
}

Message response

router.put('/', (req, res, next) => {
  return res.message('Data updated.');
});

It will Generate the response shown below:

{
  "code": 200,
  "success": true,
  "data": null,
  "error": null,
  "message": "Data updated."
}

Methods

  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

Some ENV variables

Variable Description
RES_HALPER_IGNORE_STATUS_CODE Boolean (default: false)
true: will ignore status code, always set to 200.
false: Will set status code according
RES_HALPER_ERR_CONSOLE Boolean (default: false)
true: will display error in console.
false: Will disable error console
RES_HALPER_DISABLE_ERR_STACK Boolean (default: false)
true: will hide stack from error in response.
false: Will display error stack in response

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 or even a pull request!

Thanks for your interest in contributing to this repo!


Author

Hemin Tailor (flyingtailorbird@gmail.com)


License

This code is licensed under the MIT License. See the LICENSE file for more info.