Code coverage report for express-dinja/index.js

Statements: 97.67% (42 / 43)      Branches: 93.75% (15 / 16)      Functions: 100% (10 / 10)      Lines: 100% (39 / 39)      Ignored: none     

All files » express-dinja/ » index.js
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71    1 1 1   1 8 8   8   1   12   12 30 22 14   8 1     7 7   7 1     6 4   4         8 6   6 6 6   6 6 6 6 4 1   3         6     8 8 1     7      
'use strict';
 
var utils = require('express/lib/utils');
var argnames = require('get-parameter-names');
var async = require('async');
 
module.exports = function (app) {
    var dependencies = {};
    var route = app._router.route;
 
    var resolvePath;
 
    function resolveInjections(params, req, res, next, done) {
        /*jshint validthis:true */
        var self = this;
 
        async.mapSeries(params, function (dependency, callback) {
            if (dependency === 'req') { return callback(null, req); }
            if (dependency === 'res') { return callback(null, res); }
            if (dependency === 'next') { return callback(null, next); }
 
            if (resolvePath.indexOf(dependency) !== -1) {
                throw new Error('Circular dependencies: ' + resolvePath.join(' -> ') + ' -> ' + dependency);
            }
 
            resolvePath.push(dependency);
            var constructor = dependencies[dependency];
 
            if (!constructor) {
                throw new Error('Unknown dependency: ' + dependency);
            }
 
            resolveInjections(argnames(constructor), req, res, function (err, result) {
                callback(err, result);
            }, function (err, results) {
                constructor.apply(self, results);
            });
        }, done);
    }
 
    app._router.route = function (method, path) {
        var callbacks = utils.flatten([].slice.call(arguments, 2));
 
        callbacks = callbacks.map(function (fn) {
            Iif (typeof fn !== 'function') { return fn; }
            var params = argnames(fn);
 
            return function (req, res, next) {
                var self = this;
                resolvePath = [];
                resolveInjections.bind(self)(params, req, res, next, function (err, results) {
                    if (err) {
                        return next(err);
                    }
                    fn.apply(self, results);
                });
            };
        });
 
        route.call(this, method, path, callbacks);
    };
 
    return function (dependency, fn) {
        if (typeof fn !== 'function') {
            throw new Error('inject() requires a function, but got a ' + typeof fn);
        }
 
        dependencies[dependency] = fn;
    };
};