This project help you to response your data from backend to frontend more clear.
$ npm install clean-response
First you add project the file.
const SuccessDataResult = require("clean-response")
You must give data and status code that you will return to frontend.There is example below.First field should be status code.SuccessResponse will understand which status code is used thus you can use all status code here.Second field should be data that you want to response to frontend.Finally you must call dataResult function.
var data = {
name: "emin",
surname: "oz",
email: "example@gmail.com",
contury: "turkey",
hobbies: ["cooking", "learn new languages", "travel"],
};
const response = new SuccessDataResult(201, data).dataResult()
Frontend will see this clean data.There are all information that frontend need such as data, status message, status code etc. Now frontend handle that data more easily.
{
success: true,
statusCode: 201,
message: 'Created',
data: {
name: 'emin',
surname: 'oz',
email: 'example@gmail.com',
country: 'turkey',
hobbies: [ 'cooking', 'learn new languages', 'travel' ]
}
}
This packege use for error response as well.Here the example. Firstly ErrorResponse is added
const ErrorResult = require("clean-response")
You can give all error status code and the call the dataResult function.
const response = new ErrorResult(400).dataResult()
Frontend will see this clean data.
{ success: false, statusCode: 400, message: 'Bad Request' }