Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
lengoo-api-response-formatter
Advanced tools
A lib to keep your responses formatted and consistent through your project
A library to keep your responses formatted and consistent through your project.
This library intends to create a consistent response format to be used through your project.
{
data: [...],
error: {},
validations: []
}
{
data: [],
error: {
code: "some-code",
message: "Human message"
},
validations: []
}
{
data: [],
error: {
code: "validation-error",
message: "The request was not processed due to validation issues"
},
validations: [
{
code: "some-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
},
...
]
}
npm install --save lengoo-api-response-formatter
...
module.exports.get = (req, res) => {
let response = new Response();
Model.findOne({_id: req.swagger.params.id.value}, (err, doc) => {
if (err) {
response.mongoError = err;
} else if (company === null) {
response.error = Response.ERRORS.NOT_FOUND;
} else {
response.data = doc;
}
res.status(response.status);
res.json(response.get());
});
};
...
You can add validations in two ways:
...
response.validation = [{
code: "some-validation-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
},
{
code: "another-validation-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
}];
...
Or:
...
response.addValidation = {
code: "another-validation-code",
message: "Human message",
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
};
...
If you do not specify an error, the default error for validations will be added to the response.
Default: 200
Define the HTTP response code.
let response = new Response();
response.status = Response.CODES.BAD_REQUEST;
// Or
// response.status = 400;
Get the HTTP response code.
let response = new Response();
response.status = Response.CODES.BAD_REQUEST;
console.log(response.status);
Set the data to be returned on data
response property.
Note: The 200
status code will be added to the response.
let response = new Response();
response.data = [{
_id: 5ad60b78779c4e0ec54aa0d7,
name: 'Lengoo'
}];
Set the error to be returned on error
response property.
Important: If the given value is one of the predefined error messages, the library will try to set the response status
.
Custom error:
let response = new Response();
response.error = [{
code: 'some-custom-code',
message: 'Something went wrong.'
}];
Predefined error:
let response = new Response();
response.status = Response.CODES.BAD_REQUEST;
Set the mongo error to be treated by the library.
The library will try to identify the error type, set the correct HTTP status and construct the appropriated response data;
let response = new Response();
Model.find({}, (err, doc) => {
if (err) {
response.mongoError = err;
} else {
response.data = doc;
}
res.status(response.status);
res.json(response.get());
});
Set a collection of validation errors to be returned on validations
response property.
Important: the items of the given array must to be objects.
let response = new Response();
response.validations = [
{
code: "some-validation-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
},
{
code: "another-validation-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
}
];
Append a single validation error to the validations collection.
let response = new Response();
response.addValidation({
code: "another-validation-code",
message: Human message"',
attribute: "attribute-that-caused-the-error",
value: "given-value-for-attribute"
});
Retrieve the formatted response.
let response = new Response();
response.error = Response.ERRORS.BAD_REQUEST;
res.json(response.get());
SUCCESS: 200,
CREATED: 201,
ACCEPTED: 202,
NO_CONTENT: 204,
BAD_REQUEST: 400,
MOVED_PERMANENTLY: 301,
FOUND: 302,
SEE_OTHER: 303,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404,
CONFLICT: 409,
UNPROCESSABLE_ENTITY: 422,
NOT_IMPLEMENTED: 501
let response = new Response();
response.status = Response.CODES.NOT_FOUND;
BAD_REQUEST: {
code: 'bad-request',
message: 'The given data could not been processed'
},
UNAUTHORIZED: {
code: 'unauthorized',
message: 'Authentication failed or was not provided'
},
FORBIDDEN: {
code: 'forbidden',
message: 'You don\'t have the required permissions to complete this operation'
},
NOT_FOUND: {
code: 'not-found',
message: 'The requested object was not found'
},
CONFLICT: {
code: 'conflict',
message: 'The request was not processed due to a conflict'
},
UNPROCESSABLE_ENTITY: {
code: 'validation-error',
message: 'The request was not processed due to validation issues'
}
let response = new Response();
response.error = Response.ERRORS.UNPROCESSED_ENTITY;
Coming soon
FAQs
A lib to keep your responses formatted and consistent through your project
The npm package lengoo-api-response-formatter receives a total of 0 weekly downloads. As such, lengoo-api-response-formatter popularity was classified as not popular.
We found that lengoo-api-response-formatter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.