UNPKG

ts-api-core

Version:

Nodejs api framework core

140 lines 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.App = void 0; const ioc_decorator_1 = require("../mvc/ioc.decorator"); const web_server_1 = require("../server/web.server"); const model_hump_helper_1 = require("../utils/model.hump.helper"); const request_context_1 = require("../context/request.context"); /** TS-API-Core 应用 */ // eslint-disable-next-line @typescript-eslint/no-extraneous-class class App { /** 根据端口号获取服务信息 */ static getWebServer(port) { return this.serversMap.get(port); } /** 全局上下文对象 */ static get context() { return this._context; } /** 初始化配置信息 */ static initConfig(config) { this.config = config; if (config.modelHelperConfig) { model_hump_helper_1.ModelHumpHelper.init(config.modelHelperConfig); } if (config.loadOpts) { ioc_decorator_1.IocContainer.loadDirectory(config.loadOpts); } } static initServer(server) { const servers = Array.isArray(server) ? server : [server]; for (const item of servers) { const svr = new web_server_1.WebServer(item); this.serversMap.set(svr.port, svr); } } static registerSystemInterface() { } /** * 初始化应用 * @param serviceName 服务名称 * @param options 初始化选项 */ static initialize(serviceName, options) { var _a, _b; this.serviceName = serviceName; ioc_decorator_1.IocContainer.config = options; if (options === null || options === void 0 ? void 0 : options.config) { this.initConfig(options.config); } this.registerSystemInterface(); this.deubgHttpRequest = (_a = options === null || options === void 0 ? void 0 : options.deubgHttpRequest) !== null && _a !== void 0 ? _a : true; this.deubgDBConnection = (_b = options === null || options === void 0 ? void 0 : options.deubgDBConnection) !== null && _b !== void 0 ? _b : false; if (options === null || options === void 0 ? void 0 : options.server) { this.initServer(options.server); } /** * 全局同步异常处理 */ process.on("unhandledRejection", (reason, promise) => { console.error("未处理的 异步错误:", promise, "原因:", reason); }); process.on("uncaughtException", (err) => { console.error("未处理的 异常:", JSON.stringify(err.message)); }); } /** * 注册对象定义 * @param identifier 标识名称, 不区分大小写 * @param scope 作用域, 默认 `ScopeEnum.Request` * @param module 实现类 */ static bind(module, identifier, scope = ioc_decorator_1.ScopeEnum.Request) { ioc_decorator_1.IocContainer.bind(module, identifier, scope); } /** * 单例模式注册 * @param identifier 标识名称, 不区分大小写 * @param module 实现类 */ static singleton(module, identifier) { ioc_decorator_1.IocContainer.bind(module, identifier, ioc_decorator_1.ScopeEnum.Singleton); } /** * 注入已有对象 * @param identifier 标识名称, 不区分大小写 * @param obj 对象 */ static registerObject(identifier, obj) { ioc_decorator_1.IocContainer.registerObject(identifier, obj); } /** * 绑定动态 Wrapper 函数定义 * @param identifier 标识名称, 不区分大小写 * @param provider 实现函数 * @description ` * export function xxx(context: RequestContext): any { * return () => { return data; } * }` */ static injectWrapper(provider, identifier) { ioc_decorator_1.IocContainer.bindInjectWrapper(provider, identifier); } /** * 获取对象实例 * @param identifier 标识符 * @param context 上下文对象 * @param ...args 参数列表 */ static resolve(identifier, context, ...args) { return ioc_decorator_1.IocContainer.get(identifier, context, ...args); } /** * 获取配置信息 * @param path 配置信息的名称完整路径,默认使用字段名称,不区分大小写 * @param scope 配置信息来源, 默认 `ConfigScope.config`。如果是数组,则以先后顺序作为优先级获取 * @param defaultValue 默认值 * @param context 上下文对象 */ static getConfig(path, scope, context, defaultValue) { return ioc_decorator_1.IocContainer.getConfig(path, scope, context, defaultValue); } /** * 注入属性 * @param instance 对象实例 */ static resolveInject(context, instance, target) { ioc_decorator_1.IocContainer.resolveInject(context, instance, target !== null && target !== void 0 ? target : instance.__proto__.constructor); } } exports.App = App; App.serversMap = new Map(); App._context = new request_context_1.RequestContext(); /** * 是否在控制台显示 Http 请求调试信息,默认 `true` */ App.deubgHttpRequest = true; /** * 是否在控制台打印数据库查询调试信息,默认 `false` (数组元素为数据包类型名,如 `["ComQueryPacket"]` ) */ App.deubgDBConnection = false; //# sourceMappingURL=app.js.map