@obi-tec/express-response-models
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Express Response Models

🚀 A simple library to manage response (success or error) using express library

🏁 Content


Install

npm install @obi-tec/express-response-models

See all tags clicking here.


Usage

In your main root file, where you import express library, you must set the following settings in middleware:

const { response, ErrorHttp } = require('@obi-tec/express-response-models');

// Add function success to express response object 
app.use(function(req, res, next) {
  res.success = (body, statusCode = 200, headers = null, cache = 0) => {
      return response.success(res, body, statusCode, headers, cache);
  };

  res.ErrorHttp = ErrorHttp;

  next();
});

// Handler to ErrorHttp Class
app.use((err, request, response, next) => {
  if (err instanceof ErrorHttp) {
    return response.status(err.httpStatusCode).json({
      message : err.message,
      code    : err.businessStatusCode
    });
  }

  return response.status(500).json({
    message : 'Internal server error',
    code    : 'internal-server-error'
  });
});
// 

After declaring in your main file, you are able to use this library to respond to:

  • A success request
      return res.success(body, 200);
  • An error request
      throw new res.ErrorHttp('Error message', 400, 'api-name-400_error-message');
    
      // OR just
      throw new ErrorHttp('Error message', 400, 'api-name-400_error-message');

Status

🚧 Open for contribuitions... 🚧

Readme

Keywords

Package Sidebar

Install

npm i @obi-tec/express-response-models

Weekly Downloads

229

Version

1.1.1

License

Apache License

Unpacked Size

20.8 kB

Total Files

9

Last publish

Collaborators

  • brunocamboim
  • jonatas.saraiva
  • dev-obi-tec