"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterceptorHandler = exports.InterceptorDefinition = void 0;
const error_1 = require("../../error");
const magic_function_1 = require("../magic.function");
const modules_1 = require("../modules");
const utils_1 = require("../utils");
/**
 * 动态异步方法
 * @param script
 * @return {*}
 * @constructor
 */
const InterceptorDefinition = (script) => {
    return (0, utils_1.ScriptFunction)(script, [
        'ctx',
        'next',
        'route',
        'importModule',
        'importFunction',
        'BizError',
        'BizErrorEnum',
        'db',
        'cache',
        'logger',
    ]);
};
exports.InterceptorDefinition = InterceptorDefinition;
/**
 * 函数处理器
 * @param ctx 上下文
 * @param next 继续执行
 * @param route 路由定义
 * @param interceptorDefinition 执行脚本
 */
const InterceptorHandler = async (ctx, next, route, interceptorDefinition) => {
    const requestContext = ctx.requestContext;
    const db = await requestContext.getAsync(modules_1.SQLModule);
    const cache = await requestContext.getAsync(modules_1.CacheModule);
    const magicFunction = await requestContext.getAsync(magic_function_1.MagicFunction);
    const logger = ctx.getLogger('magic');
    const importModule = async (moduleName) => {
        return await requestContext.getAsync(moduleName);
    };
    try {
        //执行script
        return await (0, utils_1.executeScriptFunction)(interceptorDefinition, ctx, next, route, importModule, magicFunction.getFunction.bind(magicFunction), error_1.BizError, error_1.BizErrorEnum, db, cache, logger);
    }
    finally {
        await db.release();
    }
};
exports.InterceptorHandler = InterceptorHandler;
//# sourceMappingURL=InterceptorDefinition.js.map