All files / node-user-accounts-boilerplate-nahid/helper bootstrap.js

100% Statements 19/19
100% Branches 5/5
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 374x 4x 4x 4x 4x 4x                   32x 32x   32x   82x 82x   65x   65x   82x 82x 82x 82x 82x       4x  
const requestIp = require('request-ip');
const success = require('./success');
const error = require('./error');
const audit = require('./audit');
const resolve = require('./resolve');
const reject = require('./reject');
 
/**
 * Out boilerplate to make things easier.
 * 
 * @return {ExpressMiddleware} a middleware that will attach a bunch of convenience properties and functions to req and res.
 */
function bootstrap(app, config)
{
  // helper function
  config.audit = config.audit || app.audit || audit;
  app.audit = config.audit;
 
  return function (req, res, next)
  {
    req.clientIp = requestIp.getClientIp(req);
    req.audit = res.audit = function ()
    {
      let source = `${req.user ? req.user.id : 'anonymous'}@${req.clientIp}`;
 
      app.audit.apply(null, [source].concat(Array.prototype.slice.call(arguments)));
    };
    res.success = success.bind(null, res);
    res.error = error.bind(null, res);
    res.reject = reject.bind(null, res);
    res.resolve = resolve.bind(null, res);
    next();
  }
}
 
module.exports = bootstrap;