jsonresponse2

0.3.3 • Public • Published

GitHub license npm
NPM

jsonresponse

A JSON response helper forked from jsonresponse.

A standard success response would have the format:

{
  "success": true,
  "error": null,
  "results": {
    id: 123,
    firstname: "Brian",
    lastname: "Mancini",
    age: 32
  }
}

An error response would have the format:

{
  "success": false,
  "error": {
    "message": "Code did something bad",
    "stack": "Error: Code did something bad\n    at Object.exports.throwError [as handle] ... "
  },
  "results": null
}

Usage

Install jsonresponse2 from NPM:

npm install jsonresponse2

You can then include it in a module via:

var jsonresponse2 = require('jsonresponse2')

jsonresponse2

jsonresponse2 function takes three arguments, error, result and stringify.

To create a success response, pass in the results object, array, or value as the second argument:

var results = { foo: 'bar' }
var json = jsonresponse2(null, results)

To create an error response, pass in the error result as the first argument.

var error = new Error('Boom!')
var json = jsonresponse2(error)

You can optionally pass a third argument, a boolean, indicating whether JSON.stringify should be done on the response.

var json = jsonresponse2(null, { foo: 'bar' }, true)

Readme

Keywords

none

Package Sidebar

Install

npm i jsonresponse2

Weekly Downloads

1

Version

0.3.3

License

none

Last publish

Collaborators

  • mkozjak