1.2.0 • Published 1 year ago

smart-http v1.2.0

Weekly downloads
72
License
MIT
Repository
-
Last release
1 year ago

smart-http

Custom http-error module which will used to customize rest api response as per eazyfin standards

Installation

This is a Node.js module available through the npm registry. It can be installed using the npm or yarn command line tools.

npm install smart-http --save

Tests

npm install
npm test

Usage

To use this middleware need to add the following code in your entry file generally which is either index.js, app.js or server.js

const nodeHttp = require('smart-http');
app.use('/', nodeHttp(), router);

// you want statusCode in response, that is for badRequest,serverError,postSuccessfully,getSuccessfully methods 
app.use('/', nodeHttp( { sendStatusCodeinRespose: true }), router);

Method

// HTTP Code:200
res.getSuccessfully({ data: [{userId:1}], message: 'successfully saved'});

// HTTP Code:201
res.postSuccessfully({ data: [{userId:1}], message: 'successfully saved'});

// HTTP Code:400 
res.badRequest('filed-validation', [{name:'userId',message:'invalid userId']);

// HTTP Code:401, unauthenticated request
res.unAuthorized();

// HTTP Code:403, unauthorized request
res.forbidden();

// HTTP Code:404
res.notFound();

// HTTP Code:412
res.concurrencyError();

// HTTP Code:500
res.serverError(error);

// HTTP Code:200, for get request
res.getRequest({ name: 'john Dow' });

// HTTP Code:201, for post request
res.postRequest();

// HTTP Code:204, for update,patch request
res.updated();

// HTTP Code:204, for delete request
res.deleted();

Reference

To get better vision about the response structure for 4.x.x series and 5.x.x series check the following link. HTTP response structure

Dependencies

None

Dev Dependencies

  • chai: BDD/TDD assertion library for node.js and the browser. Test framework agnostic.
  • mocha: simple, flexible, fun test framework
  • sinon: JavaScript test spies, stubs and mocks.

License

MIT