All files / app/middleware skip.middleware.js

100% Statements 24/24
90.48% Branches 38/42
100% Functions 4/4
100% Lines 24/24
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 501x   1x 61x   61x 61x   61x 61x   61x   61x 52x     61x     61x 5x 6x         61x     61x 2x 3x       61x     61x 2x     61x 9x     52x    
const url = require('url');
 
module.exports = function (options) {
    const parent = this;
 
    const opts = typeof options === 'function' ? {custom: options} : options;
    opts.useOriginalUrl = (typeof opts.useOriginalUrl === 'undefined') ? true : opts.useOriginalUrl;
 
    return function (ctx, next) {
        const requestedUrl = url.parse((opts.useOriginalUrl ? ctx.originalUrl : ctx.url) || '', true);
 
        let skip = false;
 
        if (opts.custom) {
            skip = skip || opts.custom(ctx);
        }
 
        const paths = !opts.path || Array.isArray(opts.path) ?
            opts.path : [opts.path];
 
        if (paths) {
            skip = skip || paths.some(function (p) {
                    return (typeof p === 'string' && p === requestedUrl.pathname) ||
                        (p instanceof RegExp && !!p.exec(requestedUrl.pathname));
                });
        }
 
        const exts = !opts.ext || Array.isArray(opts.ext) ?
            opts.ext : [opts.ext];
 
        if (exts) {
            skip = skip || exts.some(function (ext) {
                    return requestedUrl.pathname.substr(ext.length * -1) === ext;
                });
        }
 
        const methods = !opts.method || Array.isArray(opts.method) ?
            opts.method : [opts.method];
 
        if (methods) {
            skip = skip || !!~methods.indexOf(ctx.method);
        }
 
        if (skip) {
            return next();
        }
 
        return parent(ctx, next);
    };
}