![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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.
__v
from mongo
results;{
data: [...],
error: {},
validations: []
}
{
data: [],
error: {
code: "some-code",
message: "Some message for humans"
},
validations: []
}
{
data: [],
error: {
code: "validation-error",
message: "The request was not processed due to validation issues"
},
validations: [
{
code: "some-code",
message: "Some message for humans",
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,
SERVICE_UNAVAILABLE: 503
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'
},
NOT_IMPLEMENTED: {
code: 'not-implemented',
message: 'The requested route was not implemented'
},
SERVICE_UNAVAILABLE: {
code: 'service-unavailable',
message: 'The server is currently unavailable'
}
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.