@clipmx/node.response

2.1.6 • Public • Published

node.response

Introduction

This is a nodejs response module for Clip's node applications. This will standardize the errors and OK 2XX responses.

Install

npm install @clipmx/node.response --save

to use the example.js file for dev and testing npm install --only=dev

Setup

const Response = require(‘@clipmx/node.response’);
// -- middleware --
// Body Parser
// Routes here
// response must be used after the routes.. order matters
const responseConfig = {
    useLogger: true,
    logger,
};

const response = new Response(responseConfig);
app.use(response.responseHandler);

pass in your instance of logger into the responseConfig. You can also turn off the logger as well by setting useLogger:false

Endpoints

Endpoints for the example.js are in postman

https://documenter.getpostman.com/view/3824314/RVg59T3u

Usage

Use in your routes! See code below or see the example.js

Pass this object into next() to fully utilize the response module for error responses.

{
    error: {
        httpStatus: 400, // or httpResponseHint will work in the object
        code: 103,
        moreInfo: 'put more info here for the error thrown if needed',
        errorObj: errorObj // this is the actual error object
    }
}

Pass this object into next() to fully utilize the response module for OK 2XX responses. see /lib/httpOk

{ 
    httpStatus: 201,
    results: 'created'
}

or

{ 
    transactionId: 1111,
    merchant: 111,
    comment: 'hey these are my results'
}
  • set the httpStatus code: 3XX, 4XX, 5XX see example.js or the code below to see the status codes available

  • set the code: see the /lib/httpError to see which codes are available for the http status code.

  • set moreInfo: 'whatever additional information you feel is necessary'

  • If you pass in the errorObj the logger will log the file name, method, and line #.

const expressValidator = require('express-validator');
const { validationResult } = require('express-validator/check');
const { check } = require('express-validator/check');

const validator = [
    check('id').exists().withMessage('id is a required attribute').isMongoId()
    .withMessage('Must be MongoId'),
];

const validator2 = [
    check('id').exists().withMessage('id is a required attribute').isMongoId()
    .withMessage('Must be MongoId'),
    check('comment').exists().withMessage('comment is required'),
    check('number').exists().withMessage('number is required').isNumeric()
    .withMessage('must be a number'),
];

const validate = (req, res, next) => {
    const validationRes = validationResult(req);
    if (!validationRes.isEmpty()) {
        const errObj = { error: { httpStatus: 400 } };
        errObj.error.moreInfo = validationRes.mapped();
        next(errObj);
    } else {
        next();
    }
};

app.get('/test200', (req, res, next) => {
    next({ data: 'some data' });
});

app.get('/test201', (req, res, next) => {
    next({ httpStatus: 201, results: 'created' });
});

app.get('/test204', (req, res, next) => {
    next({ httpStatus: 204 });
});

app.get('/test400', (req, res, next) => {
    next({ error: { httpStatus: 400, code: 103, moreInfo: 'Bad Request We have an Error' } });
});

app.get('/test400ParamValidator/:id', validator, validate, (req, res, next) => {
    next({ error: { httpStatus: 400, moreInfo: 'Bad Request We have an Error' } });
});

app.post('/test400ParamValidator2', validator2, validate, (req, res, next) => {
    next({ httpStatus: 200, results: 'Success' });
});

app.get('/test401', (req, res, next) => {
    next({ error: { httpStatus: 401, code: 101, moreInfo: 'Unauthorized' } });
});

// using httpResponseHint also works :D
app.get('/test401', (req, res, next) => {
    next({ error: { httpResponseHint: 401, code: 101, moreInfo: 'Unauthorized' } });
});

app.get('/test403', (req, res, next) => {
    next({ error: { httpStatus: 403, code: 502, moreInfo: 'Forbidden' } });
});

app.get('/test404', (req, res, next) => {
    next({ error: { httpStatus: 404, code: 104, moreInfo: 'Not Found' } });
});

app.get('/test405', (req, res, next) => {
    next({ error: { httpStatus: 405, code: 503, moreInfo: 'Method not allowed' } });
});

app.get('/test409', (req, res, next) => {
    next({ error: { httpStatus: 409, code: 501, moreInfo: 'Conflict' } });
});

app.get('/testOtherError', (req, res, next) => {
    const email = req.user.email;
    next({ data: email });
});

app.get('/testOtherError2', (req, res, next) => {
     next(new Error('an Error was thrown'));
});

app.get('/test500', (req, res, next) => {
    const err = new Error('This is an internal Error.');
    next({ error: { httpStatus: 500, moreInfo: 'woa internal error', errorObj: err } });
});

app.get('/test503', (req, res, next) => {
    next({ error: { httpStatus: 503, code: 106, moreInfo: 'Service Unavailable' } });
});

Codes

Corresponding HTTP STATUS CODES and INTERNAL CODES

  • HTTP STATUS CODE - 400 has 103, 102, 200, 300, 500
  • HTTP STATUS CODE - 401 has 101, 111, 112, 113, 114
  • HTTP STATUS CODE - 403 has 502
  • HTTP STATUS CODE - 404 has 104, 404
  • HTTP STATUS CODE - 405 has 503
  • HTTP STATUS CODE - 409 has 501
  • HTTP STATUS CODE - 500 has 105
  • HTTP STATUS CODE - 503 has 106, 107

Readme

Keywords

none

Package Sidebar

Install

npm i @clipmx/node.response

Weekly Downloads

4

Version

2.1.6

License

UNLICENSED

Unpacked Size

56 kB

Total Files

33

Last publish

Collaborators

  • akira_fes
  • itpayclip
  • dhollerbach