All files / app/lib controller.class.js

91.84% Statements 45/49
86.67% Branches 13/15
93.75% Functions 15/16
91.84% Lines 45/49
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x 1x 1x 1x 1x   1x       42x 42x   42x 42x   42x 2524x 1767x   2x     1x       757x 1x   756x         42x                   4x                 2x 1x   1x         2x 1x   1x       1x       2x       1x       37x         5x   5x   1x     1x   1x       1x   3x         3x     5x       1x       1x         1x   1x 1x       1x    
const fs = require('fs');
const co = require('co');
const path = require('path');
const assert = require('assert');
const common = require('./../common');
 
module.exports = class Controller {
 
    constructor(ctx, next) {
 
        assert(ctx, 'SyntaxError: missing super(ctx) call in constructor');
        assert(next, 'SyntaxError: missing super(next) call in constructor');
 
        this.ctx = ctx;
        this.next = next;
 
        for (let name in ctx) {
            if (typeof ctx[name] !== 'function') {
                Object.defineProperty(this, name, {
                    get: () => {
                        return this.ctx[name];
                    },
                    set: value => {
                        this.ctx[name] = value;
                    }
                });
            } else {
                if (common.isGeneratorFunction(ctx[name])) {
                    this[name] = co.wrap(ctx[name]).bind(ctx);
                } else {
                    this[name] = ctx[name];
                }
            }
        }
 
        for (let name in koahub.common) {
            if (!this[name]) {
                this[name] = koahub.common[name];
            } else {
                throw new Error(`Don\'t use the "${name}" method in the common.js`);
            }
        }
    }
 
    get hook() {
        return koahub.hook;
    }
 
    get config() {
        return koahub.config;
    }
 
    isGet() {
 
        if (this.ctx.method === 'GET') {
            return true;
        }
        return false;
    }
 
    isPost() {
 
        if (this.ctx.method === 'POST') {
            return true;
        }
        return false;
    }
 
    isAjax() {
        return this.ctx.get('X-Requested-With') === 'XMLHttpRequest';
    }
 
    isPjax() {
        return this.ctx.get('X-PJAX') || false;
    }
 
    isMethod(method) {
        return this.ctx.method === method.toUpperCase();
    }
 
    view(data) {
        this.ctx.body = data;
    }
 
    json(data, msg, code) {
 
        let body = {};
 
        switch (arguments.length) {
            case 1:
                body = {
                    data: data,
                }
                break;
            case 2:
                body = {
                    data: data,
                    msg: msg,
                }
                break;
            case 3:
                body = {
                    data: data,
                    msg: msg,
                    code: code
                }
                break;
        }
 
        this.view(body);
    }
 
    success(data, msg) {
        this.json(data, msg, 1);
    }
 
    error(data, msg) {
        this.json(data, msg, 0);
    }
 
    download(file) {
 
        const filename = path.relative(path.dirname(file), file);
 
        this.ctx.set('Content-disposition', 'attachment; filename=' + filename);
        this.view(fs.createReadStream(file));
    }
 
    async action(path, ...args) {
        return await common.runHttp(Object.assign(this.ctx, {path: path}), this.next, ...args);
    }
}