'use strict'; const _ = require('lodash'); const assert = require('assert'); const AppContext = require('./lib/contexts/AppContext'); exports.createAppContext = function (options) { return new AppContext(options); }; exports.middleware = function (options) { assert(_.isObject(options), 'options should be an object'); assert(options.appContext instanceof AppContext, '"appContext" should be an application context object'); assert(options.logNetwork === undefined || _.isBoolean(options.logNetwork), '"logNetwork" (if provided) should be a boolean'); return function createLoggingContext(req, res, next) { req.loggingContext = options.appContext.createLogContext({ req }); res.setHeader('x-request-id', req.loggingContext.id); if (options.logNetwork) { req.loggingContext.enableNetworkLog(res); } next(); }; };