all files / lib/ routes.js

93.1% Statements 54/58
79.17% Branches 19/24
100% Functions 4/4
94% Lines 47/50
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    1×   1× 1×   1×     8× 8× 8×       7× 7× 5× 2× 2× 2×   5×       5×   5×       5× 5×     5×       5× 5× 4× 4× 2×         2× 2× 2×         1× 7×   7× 7× 7× 1×         6× 6×           6× 6× 30× 6×         5× 5×   5×           5×   5×           5×             5× 5×    
'use strict';
 
const PATH = require('path');
 
const Memory = new WeakMap();
const Name = 'plugins:router';
 
module.exports = class Routes extends Array {
 
    constructor(feliz){
        super();
        Memory.set(this, feliz);
        feliz.debug(Name, 'routes:constructor');
    }
 
    load(path, route){
        const feliz = Memory.get(this);
        route = validator.call(feliz, path, route);
        const route$ = resolver.call(feliz, route).do(route => {
            this.push(route);
            feliz.debug(Name, 'routes:load', route);
            feliz.events.emit(`${Name}:routes`, this);
        });
        return route$;
    }
}
 
function resolver(route){ return this.observable
    .of(route)
    .mergeMap(route => this.util.rx
        .path(route.bundle.path)
        .isDir()
        .map(isDir => {
            if (isDir) route.bundle.path = PATH.join(route.bundle.path, 'index');
            return route;
        })
    )
    .mergeMap(route => this.util.rx
        .path(route.bundle.path + this.path.ext)
        .isFile()
        .map(isFile => {
            const {path, name} = route.bundle;
            if (!isFile) throw this.error(`Invalid bundle: ${name} (${path})`);
            const data = require(path);
            if (!this.util.is(data).function())
                throw this.error.type({
                    name: `${info}.bundle:${name}`,
                    type: 'function',
                    data: data
                });
            route.bundle.data = data;
            this.debug(Name, 'routes:resolver', route);
            return route;
        })
    );
}
 
function validator(path, route){
    const methods = this.conf.router.methods;
    // Methods
    if (!route.method) route.method = [methods[0].name];
    const is = this.util.is(route.method);
    if (!is.string() && !is.array())
        throw this.error.type({
            name : `${Name}:route.method`,
            type : 'String or Array',
            data : route.method
        });
    Iif (is.string()) route.method = [route.method];
    Iif (!route.method.length)
        throw this.error.type({
            name : `${Name}:route.method`,
            type : 'Non empty Array',
            data : route.method
        });
    const types = [];
    route.method.forEach(method => {
        const avail = methods.filter(m => m.name === method);
        if (!avail.length) throw this.error.type({
            name : `${Name}:route.method`,
            type : `valid method (${avail.join(',')})`,
            data : method
        });
        const type = avail.shift().type;
        Eif (types.indexOf(type) === -1) types.push(type);
    });
    Iif (types.length > 1)
        throw this.error.type({
            name: `${Name}:route.method(type)`,
            type: 'same type',
            data: types
        });
    route.type = types[0];
    // Bundles
    Iif (!this.util.is(route.bundle).string())
        throw this.error.type({
            name: `${Name}:route.bundle`,
            type: 'String',
            data: route.bundle
        });
    route.bundle = {
        name: route.bundle,
        path: PATH.join(
            this.path.bundles,
            route.bundle.replace(/\//g, PATH.sep)
        )
    };
    this.debug(Name, 'routes:validator', route);
    return route;
}