ts-api-core
Version:
Nodejs api framework core
94 lines • 5.24 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseRouteExecuteWrapper = void 0;
const error_1 = require("../base/error");
const reqest_data_1 = require("../base/reqest.data");
const result_code_1 = require("../base/result.code");
const model_helper_1 = require("../utils/model.helper");
const base_route_raw_wrapper_1 = require("./base.route.raw.wrapper");
const ioc_decorator_1 = require("../mvc/ioc.decorator");
/**
* 动态执行请求数据加工基类
*/
class BaseRouteExecuteWrapper extends base_route_raw_wrapper_1.BaseRouteRawWrapper {
constructor(context, executeCallBack) {
super(context);
this.executeCallBack = executeCallBack;
this.TAG = "wrapper.execute";
}
executeRequest(context) {
const _super = Object.create(null, {
getRequestArgsItem: { get: () => super.getRequestArgsItem },
executeRequest: { get: () => super.executeRequest }
});
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this.executeCallBack) {
return this.executeCallBack(context);
}
const config = context.route.config ? context.route.config : undefined;
if (config) {
if (!config.func) {
throw new error_1.SysError("没有指定要执行的数据加工类函数", this.TAG, result_code_1.ResultCode.E_Valid_Param_NoFindConfig);
}
const svr = this.context.server;
const wrapperCls = svr === null || svr === void 0 ? void 0 : svr.registerRoutes.getCustomWrapper(config.wrapper);
if (!wrapperCls) {
throw new error_1.SysError("配置的数据加工类不存在或未注册", this.TAG, result_code_1.ResultCode.E_Valid_Param_NoFindConfig);
}
const args = (_a = ioc_decorator_1.IocContainer.getClassConstructorArgs(context, wrapperCls)) !== null && _a !== void 0 ? _a : [context];
const wrapper = new wrapperCls(...args);
const func = wrapper[config.func];
if (!func) {
throw new error_1.SysError("数据加工类中不存在指定的函数", this.TAG, result_code_1.ResultCode.E_Valid_Param_NoFindConfig);
}
if (typeof func !== 'function') {
throw new error_1.SysError("数据加工类中指定要执行的函数无效", this.TAG, result_code_1.ResultCode.E_Valid_Param_NoFindConfig);
}
// 动态注入属性
ioc_decorator_1.IocContainer.resolveInject(context, wrapper, wrapperCls);
// 动态执行 wrapper 中指定的函数
if (config.args) {
const args = [];
let paramTypes;
config.args.forEach((item, idx) => {
const v = _super.getRequestArgsItem.call(this, context, item, 1);
if (item.convert === true && v !== undefined && typeof v === 'object') {
if (!paramTypes) {
paramTypes = Reflect.getMetadata('design:paramtypes', wrapper, func.name);
}
if (paramTypes && idx < paramTypes.length && paramTypes[idx]) {
const dto = model_helper_1.ModelHelper.toModel(v, paramTypes[idx]);
args.push(dto);
return;
}
}
else if (item.type === reqest_data_1.RequestParamType.DateType) {
args.push(v === undefined || v === null ? undefined : Date.toDate(v));
return;
}
args.push(v);
});
// eslint-disable-next-line @typescript-eslint/ban-types
return func.apply(wrapper, args);
}
else {
// eslint-disable-next-line @typescript-eslint/ban-types
return func.apply(wrapper);
}
}
return _super.executeRequest.call(this, context);
});
}
}
exports.BaseRouteExecuteWrapper = BaseRouteExecuteWrapper;
//# sourceMappingURL=base.route.execute.wrapper.js.map