"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("reflect-metadata");
const routing_controllers_1 = require("@gem-mine/routing-controllers");
const config_1 = require("../config");
const exception_1 = require("../util/exception");
const exception_2 = require("../config/constant/exception");
// 路由中间件
// 更多配置查看：https://github.com/typestack/routing-controllers
function default_1(app) {
    routing_controllers_1.useKoaServer(app, {
        // 不要转换，在 jest 中存在 bug 会影响原生对象的判断
        // https://github.com/facebook/jest/issues/2549
        classTransformer: false,
        // 关闭异常捕获，由 koa 中间件进行处理
        defaultErrorHandler: false,
        controllers: [`${config_1.default.APP}/controller/**/*.${config_1.default.isInTsNode ? 'ts' : 'js'}`],
        // 权限验证
        authorizationChecker: (action, roles) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            const token = action.request.headers['authorization'];
            // 一般这个 token 存放在 redis 或者 mysql 之类的持久化存储中
            if (token === 'it_is_a_mock_token') {
                // 往上下文插入数据，这样整个请求周期内就可以直接使用此数据，这里是 ctx.user
                action.context.user = {
                    id: 1000,
                    name: 'jerry'
                };
                return true;
            }
            return exception_1.default(exception_2.default.UNAUTHORIZED);
        })
    });
}
exports.default = default_1;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udHJvbGxlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9taWRkbGV3YXJlL2NvbnRyb2xsZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQ0EsNEJBQXlCO0FBQ3pCLHVFQUFvRTtBQUNwRSxzQ0FBOEI7QUFDOUIsaURBQXFDO0FBQ3JDLDREQUFvRDtBQUVwRCxRQUFRO0FBQ1IsMERBQTBEO0FBQzFELG1CQUF3QixHQUFRO0lBQzlCLGtDQUFZLENBQUMsR0FBRyxFQUFFO1FBQ2hCLGlDQUFpQztRQUNqQywrQ0FBK0M7UUFDL0MsZ0JBQWdCLEVBQUUsS0FBSzt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