UNPKG

ts-api-core

Version:

Nodejs api framework core

142 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.APIRequestContext = void 0; const logger_1 = require("../base/logger"); const log_leval_1 = require("../model/log.leval"); const utils_1 = require("../utils/utils"); const request_context_1 = require("./request.context"); /** * API 请求上下文 */ class APIRequestContext extends request_context_1.RequestContext { constructor(logger, loggerPackage) { super(); this._logger = logger; this.package = loggerPackage !== null && loggerPackage !== void 0 ? loggerPackage : this.logger.package; this.startTime = utils_1.Utils.currentTimeMillis(); } get reqId() { if (!this._reqId) { this._reqId = utils_1.Utils.uuid(); } return this._reqId; } /** 租户ID、客户ID */ get tid() { var _a; return (_a = this.req.get("tid")) !== null && _a !== void 0 ? _a : String.str(this.req.query["tid"]); } /** 用户ID */ get uid() { var _a; return (_a = this.req.get("uid")) !== null && _a !== void 0 ? _a : String.str(this.req.query["uid"]); } /** 设备ID */ get esn() { var _a; return (_a = this.req.get("esn")) !== null && _a !== void 0 ? _a : String.str(this.req.query["esn"]); } /** 用户 token */ get token() { return this.req.get("token"); } get logger() { if (!this._logger) { this._logger = new logger_1.DefaultSaveLogger(); } return this._logger; } /** 写日志 */ log(level, data) { this.logger.log(level, data); if (this.package) { if (!this._logList) { this._logList = []; } this._logList.push({ time: utils_1.Utils.getTimeString(true), level, log: data }); } } /** SQL日志 */ logSQL(sql, values) { if (this.package) { if (!this._sqlList) { this._sqlList = []; } const index = this._sqlList.length; let logTxt = "# SQLScript " + (index + 1).toString() + ':\n--------------\n' + "**ExecTime:** " + utils_1.Utils.getNowString(true) + "\n\n```sql\n" + sql + "\n```\n"; if (values) { logTxt += "**Args:** \n" + JSON.stringify(values) + "\n"; } this._sqlList.push(logTxt + "\n"); return index; } else { return -1; } } /** 通知SQL执行完成 */ notifySqlExecuteFinish(index, time) { if (index < 0 || !this._sqlList) { return; } this._sqlList[index] += "**Duration:** " + time.toString() + " ms\n\n"; } /** 通知SQL执行完成 */ notifySqlTransactionFinish(time) { var _a; this.transDuration = ((_a = this.transDuration) !== null && _a !== void 0 ? _a : 0) + time; } /** 请求结束时触发 */ release(result) { var _a, _b; if (this.hasRelease === true) { return; } const endTime = utils_1.Utils.currentTimeMillis(); const duration = endTime - this.startTime; this.hasRelease = true; super.release(result); if (this.package) { if (!APIRequestContext._hostName) { APIRequestContext._hostName = utils_1.Utils.getIPAddress(); } this.logger.save({ reqId: (_a = (result ? result.reqId : undefined)) !== null && _a !== void 0 ? _a : this.reqId, tid: this.tid, uid: this.uid, api: this.req.path, url: this.req.url, esn: this.esn, duration, transDuration: (_b = this.transDuration) !== null && _b !== void 0 ? _b : 0, body: this.req.body, exception: this.error, level: log_leval_1.LogLevel.info, createdTime: new Date(this.startTime), hostName: APIRequestContext._hostName, clientName: this.getClientIP(this.req), data: this._logList && this._logList.length > 0 ? JSON.stringify(this._logList) : undefined, result, sql: this._sqlList ? this._sqlList.join("\n\n") : undefined, }); } } getClientIP(req) { var _a, _b, _c; // eslint-disable-next-line @typescript-eslint/no-unsafe-call let ip = req.get('x-forwarded-for') // 判断是否有反向代理 IP || ((_a = req.connection) === null || _a === void 0 ? void 0 : _a.remoteAddress) // 判断 connection 的远程 IP || ((_b = req.socket) === null || _b === void 0 ? void 0 : _b.remoteAddress); if (ip && ip.indexOf(',') > 0) { ip = ip.substr(0, ip.indexOf(',')); } return (_c = ip === null || ip === void 0 ? void 0 : ip.replace('::ffff:', '')) !== null && _c !== void 0 ? _c : ''; } } exports.APIRequestContext = APIRequestContext; //# sourceMappingURL=api.request.context.js.map