npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reshelper

v1.0.4

Published

Map response for REST API,Node REST API Helper

Downloads

15

Readme

RESHELPER

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

License: MIT

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

npm

Demo

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 ([email protected])

License

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