1.0.9 • Published 1 year ago
responsify-requests v1.0.9
responsify-requests
A small-sized library for handling the response of your APIs, in a well structured way. This library will form a response to your api by having some parameters in function call.
Installation
Install this package with npm
$ npm install responsify-requests
Parameters
Parameter | Type | Description |
---|---|---|
res | object | Required. Response object of your api. |
data | object | Required. Data to show on success. |
status | number | Required. Status of your response (0 on failure and 1 on success). |
message | string | Required. Certain message to show on api call. |
response | string | Required. Allowed Values SUCCESS, UNAUTHORIZED, NOT_FOUND, BAD_REQUEST, TOO_MANY_REQUESTS, UNPROCESSABLE_ENTITY |
Status codes
response (parameter) | Status Code |
---|---|
SUCCESS | 200 |
UNAUTHORIZED | 401 |
NOT_FOUND | 404 |
BAD_REQUEST | 400 |
TOO_MANY_REQUESTS | 429 |
UNPROCESSABLE_ENTITY | 422 |
Note
If you do not pass any response parameter to method, Then it will consider as "Internal Server Error" with status code of 500.
Usage/Examples
const response = require('responsify-requests');
app.get('your_url', (req,res)=> {
try {
// if you want to send this data as response...
let data = {
id: 1,
name: "XYZ"
}
return response(res, data, 1, "Data recieved succesfully", "SUCCESS");
} catch (e) {
return response(res, {}, 0, "Error occured", "BAD_REQUEST");
}
})
Output Format
Success Response
{
"message" : "Data recieved succesfully",
"data" : {
"id": 1,
"name": "XYZ"
},
"status": 1
}
Failure Response
{
"message" : "Error occured",
"status": 0
}