ts-api-core
Version:
Nodejs api framework core
136 lines • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestContext = void 0;
const app_1 = require("../base/app");
const log_leval_1 = require("../model/log.leval");
const error_1 = require("../base/error");
const ioc_decorator_1 = require("../mvc/ioc.decorator");
/** 请求上下文 */
class RequestContext {
/** 请求ID */
get reqId() {
return undefined;
}
/** 租户ID、客户ID */
get tid() {
return undefined;
}
/** 用户ID */
get uid() {
return undefined;
}
/** 设备ID */
get esn() {
return undefined;
}
/** 用户 token */
get token() {
return undefined;
}
/** 请求结束时触发 */
release(result) {
ioc_decorator_1.IocContainer.destroy(this);
}
/** 写日志 */
log(level, data) { return; }
logD(data) {
this.log(log_leval_1.LogLevel.debug, data);
}
logE(data) {
this.log(log_leval_1.LogLevel.error, data);
}
logI(data) {
this.log(log_leval_1.LogLevel.info, data);
}
logW(data) {
this.log(log_leval_1.LogLevel.warn, data);
}
/** 记录SQL执行日志,返回日志索引号 */
logSQL(sql, values) {
return -1;
}
/** 通知SQL执行完成 */
notifySqlExecuteFinish(index, time) { return; }
/** 通知SQL事务执行完成 */
notifySqlTransactionFinish(time) { return; }
/**
* 当前的 WebServer
*/
get server() {
const port = this.req ? this.req.socket.localPort : undefined;
if (port) {
const svr = app_1.App.getWebServer(port);
return svr;
}
return undefined;
}
/** HTTP 客户端对象 */
get http() {
// 优化使用上下文服务器配置的 api 基址
const apiBaseUrl = app_1.App.getConfig("apiBaseUrl", ioc_decorator_1.ConfigScope.server, this);
const apiTimeout = app_1.App.getConfig("apiTimeOut", undefined, this, 60000);
const result = this.resolve('HttpAccess', apiBaseUrl, apiTimeout);
if (!result) {
throw new error_1.SysError('当前没有注册HTTP实例');
}
return result;
}
/**
* Radis 缓存对象
*/
get cache() {
const config = app_1.App.getConfig('cacheConfig', ioc_decorator_1.ConfigScope.config, this);
if (!config) {
throw new error_1.SysError('当前上下文没有缓存配置');
}
const result = this.resolve('CacheAccess');
if (!result) {
throw new error_1.SysError('当前没有注册缓存实例');
}
result.config = config;
return result;
}
/** 阿里云 OSS 对象 */
get oss() {
const config = app_1.App.getConfig('ossConfig', ioc_decorator_1.ConfigScope.config, this);
if (!config) {
throw new error_1.SysError('当前上下文没有oss配置');
}
const result = this.resolve('OssAccess', this, config);
if (!result) {
throw new error_1.SysError('当前没有注册缓存实例');
}
return result;
}
/** RabbitMQ 对象 */
get rabbitMQ() {
var _a;
const config = (_a = this.rabbitMQConfig) !== null && _a !== void 0 ? _a : app_1.App.getConfig('rabbitmqConfig', ioc_decorator_1.ConfigScope.config, this);
if (!config) {
throw new error_1.SysError('当前上下文没有rabbitMQ配置');
}
const result = this.resolve('RabbitMQAccess', config);
if (!result) {
throw new error_1.SysError('当前没有注册RabbitMQ实例');
}
return result;
}
/**
* 获取对象实例
* @param identifier 标识符
* @param args 初始化参数
* @returns
*/
resolve(identifier, ...args) {
return app_1.App.resolve(identifier, this, ...args);
}
/**
* 动态注入属性
* @param instance 对象实例
*/
resolveInject(instance, target) {
ioc_decorator_1.IocContainer.resolveInject(this, instance, target !== null && target !== void 0 ? target : instance.__proto__.constructor);
}
}
exports.RequestContext = RequestContext;
//# sourceMappingURL=request.context.js.map