New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lengoo-api-response-formatter

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lengoo-api-response-formatter

A lib to keep your responses formatted and consistent through your project

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Lengoo API Response Formatter

A library to keep your responses formatted and consistent through your project.

Introduction

This library intends to create a consistent response format to be used through your project.

Features:
  • Most used HTTP Status codes
  • Predefined error messages
  • Response formatter
Output format
Success
{
    data: [...],
    error: {},
    validations: []
}
Error
{
    data: [],
    error: {
        code: "some-code",
        message: "Human message"
    },
    validations: []
}
Validation error
{
    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"
        },
        ...
    ]
}

Install

npm install --save lengoo-api-response-formatter

Usage

...

module.exports.getById = (req, res) => {
    let employee = _.find(employees, { _id: req.swagger.params.id.value });
    let response = new Response();

    if (employee === undefined) {
        response.error = Response.ERRORS.NOT_FOUND;
        
        // You can also especify your own error
        // response.error = {
        //     code: 'not-found',
        //     message: 'There is no employee to the given ID'
        // }  

        res.status(Response.CODES.NOT_FOUND);
        res.json(response.get());
    } else {
        response.data = employee;
        res.json(response.get());
    }
};

...
Validations

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 didn't specify an error, the default error for validations will be added to the response.

Tests

Coming soon

Keywords

FAQs

Package last updated on 17 Apr 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc