response-format
v1.2.0
Published
Module which will provide HTTP-friendly error objects
Downloads
1,434
Readme
Response Format
Module which provides HTTP-friendly error objects
Author: Gaurav Joshi
- Response-format
- Methods
create(statusCode, [error, message, data])
success([message, data])
badRequest([message, data])
unAuthorized([message, data])
forbidden([message, data])
notFound([message, data])
notAllowed([message, data])
requestTimeout([message, data])
internalError([message, data])
badGateway([message, data])
unavailable([message, data])
gatewayTimeout([message, data])
- Methods
Start
const Format = require('response-format');
Each utility returns an Object which includes the following properties:
error
- true, false or null (if not specified).statusCode
- the HTTP status code.message
- string containing message (if not specified will return a pre-defined set of messages according to status code )data
- response payload (null in case of blank or error)
Methods
create(statusCode, [error, message, data])
Generates a decorated response object where:
statusCode
- an HTTP error code number.message
- optional message string.data
- response payload.error
- true, false or null (if not specified).
success([message, data])
Returns a 200 Success where:
statusCode
- 200error
- falsemessage
- optional message.data
- optional payload.
Format.success();
Generates the following response payload:
{
"statusCode": 200,
"error": false,
"message": "OK",
"data": null
}
badRequest([message, data])
Returns a 400 Error where:
statusCode
- 400error
- truemessage
- optional message.data
- optional payload.
Format.badRequest();
Generates the following response payload:
{
"statusCode": 400,
"error": true,
"message": "Bad Request",
"data": null
}
unAuthorized([message, data])
Returns a 402 Error where:
statusCode
- 402error
- truemessage
- optional message.data
- optional payload.
Format.unAuthorized();
Generates the following response payload:
{
"statusCode": 402,
"error": true,
"message": "Unauthorized",
"data": null
}
forbidden([message, data])
Returns a 403 Error where:
statusCode
- 403error
- truemessage
- optional message.data
- optional payload.
Format.forbidden();
Generates the following response payload:
{
"statusCode": 403,
"error": true,
"message": "Forbidden",
"data": null
}
notFound([message, data])
Returns a 404 Error where:
statusCode
- 404error
- truemessage
- optional message.data
- optional payload.
Format.notFound();
Generates the following response payload:
{
"statusCode": 404,
"error": true,
"message": "Not Found",
"data": null
}
notAllowed([message, data])
Returns a 405 Error where:
statusCode
- 405error
- truemessage
- optional message.data
- optional payload.
Format.notAllowed();
Generates the following response payload:
{
"statusCode": 405,
"error": true,
"message": "Method Not Allowed",
"data": null
}
requestTimeout([message, data])
Returns a 408 Error where:
statusCode
- 408error
- truemessage
- optional message.data
- optional payload.
Format.requestTimeout();
Generates the following response payload:
{
"statusCode": 408,
"error": true,
"message": "Request Timeout",
"data": null
}
internalError([message, data])
Returns a 500 Error where:
statusCode
- 500error
- truemessage
- optional message.data
- optional payload.
Format.internalError();
Generates the following response payload:
{
"statusCode": 500,
"error": true,
"message": "Internal Server Error",
"data": null
}
badGateway([message, data])
Returns a 502 Error where:
statusCode
- 502error
- truemessage
- optional message.data
- optional payload.
Format.badGateway();
Generates the following response payload:
{
"statusCode": 502,
"error": true,
"message": "Bad Gateway",
"data": null
}
unavailable([message, data])
Returns a 503 Error where:
statusCode
- 503error
- truemessage
- optional message.data
- optional payload.
Format.unavailable();
Generates the following response payload:
{
"statusCode": 503,
"error": true,
"message": "Service Unavailable",
"data": null
}
gatewayTimeout([message, data])
Returns a 504 Error where:
statusCode
- 504error
- truemessage
- optional message.data
- optional payload.
Format.gatewayTimeout();
Generates the following response payload:
{
"statusCode": 504,
"error": true,
"message": "Gateway Timeout",
"data": null
}