1.0.8 • Published 4 years ago
objectify-response v1.0.8
objectify-response
- Returns 200 from all server responses regardless the server returns with status code 400 and up.
- Helps you avoid sending 400 and up status codes to your client-side applications.
- Gives you cleaner responses displayed in browser console.
Installation
npm install --save objectify-response
Importing
import objectifyResp from 'objectify-response'; // ES6
var objectifyResp = require('objectify-response'); // ES5 with npm
Dependencies
http-status
Usage
Example
router.post('/', async (req, res) => {
const token = generateToken()
return objectifyResp(res, token)
})
router.post('/', async (req, res) => {
const { item } = getItem()
if(!item)
return objectifyResp(res, 'Item is not found.', 404)
})
router.post('/', async (req, res) => {
const object = {}
if(!item)
return objectifyResp(res, object, 404)
})
Returns
Returns 200 from all server responses regardless the server returns 400 and up.
Example return from the first usage example.
HTTP/1.1 200 OK
{
code: 200
name: 'OK' //auto generated name of the error based on the provided error code.
message: 'Returns the generated token string'
}
Example return from the second usage example.
HTTP/1.1 200 OK
{
code: 404
name: 'NOT_FOUND' //auto generated name of the error based on the provided error code.
message: 'Item is not found.'
}
Example return from the third usage example.
HTTP/1.1 200 OK
{
code: 404
name: 'NOT_FOUND' //auto generated name of the error based on the provided error code.
message: {}
}