UNPKG

ts-api-core

Version:

Nodejs api framework core

171 lines 7.69 kB
"use strict"; 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.WrapperResult = exports.HttpPost = exports.HttpGet = exports.HttpApi = exports.HttpApiCallback = exports.BaseWrapper = void 0; const base_controller_1 = require("../controller/base.controller"); const api_base_pc_1 = require("./api.base.pc"); /** * 数据加工基类 */ class BaseWrapper extends api_base_pc_1.ApiBasePc { constructor(context) { super(context, context.esn, context.token); } /** 获取指定 Service */ getService(target) { if (!this.services) { this.services = {}; } const result = this.services[target.name]; if (result) { return result; } const service = new target(this.context); this.services[target.name] = service; return service; } /** * 并行执行异步任务 * 某步出错不影响其他步骤执行 * 程序执行时间为最长的那个时间 * @param promises * @returns * @author yangyxd */ executeParaller(promises) { return __awaiter(this, void 0, void 0, function* () { return base_controller_1.BaseController.executeParaller(promises); }); } /** * 调用Http请求,如果传入一个列表,则并发请求 * @param requestList 请求列表或单个请求 * @returns [响应数据数组,ReqID] * @description * 需要拿 ReqId 建议使用此函数。 * 如果所有请求成功, * 如果请求是数组,则返回一个数组,返回的响应数据数组依次对应请求列表,ReqID为所有请求组合成的ActionName和ReqID */ requestApi(requestList) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; return __awaiter(this, void 0, void 0, function* () { if (requestList && Array.isArray(requestList)) { const promises = new Array(requestList.length); for (let i = 0; i < requestList.length; i++) { const e = requestList[i]; if (!e) { promises[i] = undefined; continue; } promises[i] = e.isGet ? this.Get(e.actionPath, e.params, false, (_a = e.callback) === null || _a === void 0 ? void 0 : _a.checkResult, e.respType) : this.Post(e.actionPath, e.body, e.params, false, (_b = e.callback) === null || _b === void 0 ? void 0 : _b.checkResult, e.respType); } const [err, data] = yield base_controller_1.BaseController.executeParaller(promises); if (err || !data) { if (err !== undefined) { throw err; } throw new Error("data is empty"); } let reqId; const result = new Array(data.length); for (let i = 0; i < data.length; i++) { const requestItem = requestList[i]; const e = data[i]; if (e === undefined || requestItem === undefined) { continue; } result[i] = ((_c = requestItem.callback) === null || _c === void 0 ? void 0 : _c.parser) ? requestItem.callback.parser(e) : e.RetObject; if (((_d = requestItem.callback) === null || _d === void 0 ? void 0 : _d.success) !== undefined) { result[i] = requestItem.callback.success(e, result[i]); } // 将多个请求的ReqID和ActionName连接起来 const _requestActionName = requestItem.actionName === "" ? this.getActionName(requestItem.actionPath) : requestItem.actionName; const _itemReqID = _requestActionName + ":" + ((_e = e.ReqID) !== null && _e !== void 0 ? _e : '_'); reqId = reqId ? (reqId += "," + _itemReqID) : _itemReqID; } return [result, reqId]; } else if (requestList) { const e = requestList; const data = e.isGet ? yield this.Get(e.actionPath, e.params, false, (_f = e.callback) === null || _f === void 0 ? void 0 : _f.checkResult, e.respType) : yield this.Post(e.actionPath, e.body, e.params, false, (_g = e.callback) === null || _g === void 0 ? void 0 : _g.checkResult, e.respType); const result = ((_h = e.callback) === null || _h === void 0 ? void 0 : _h.parser) ? e.callback.parser(data) : data === null || data === void 0 ? void 0 : data.RetObject; if (((_j = e.callback) === null || _j === void 0 ? void 0 : _j.success) !== undefined) { e.callback.success(data, result); } return [result, data === null || data === void 0 ? void 0 : data.ReqID]; } return [undefined, undefined]; }); } getActionName(actionPath) { // 取url中最后一段为name const i = actionPath ? actionPath.lastIndexOf("/") : -1; return i < 0 ? actionPath : actionPath.substr(i + 1); } /** 获取当前时间戳(毫秒) */ get currentTimeMillis() { return Date.now(); } /** 获取当前时间戳(秒) */ get curentTimestamp() { return Math.round(Date.now() / 1000); } } exports.BaseWrapper = BaseWrapper; class HttpApiCallback { } exports.HttpApiCallback = HttpApiCallback; class HttpApi { constructor(isGet, actionPath, actionName, params, body, callback) { this.isGet = isGet; this.actionPath = actionPath; this.actionName = actionName !== null && actionName !== void 0 ? actionName : ""; this.params = params; this.body = body; this.callback = callback; } } exports.HttpApi = HttpApi; class HttpGet extends HttpApi { constructor(actionPath, params, actionName, callback, respType) { super(true, actionPath, actionName, params, undefined, callback); this.respType = respType; } } exports.HttpGet = HttpGet; class HttpPost extends HttpApi { constructor(actionPath, body, params, actionName, callback, respType) { super(false, actionPath, actionName, params, body, callback); this.respType = respType; } } exports.HttpPost = HttpPost; /** * WrapperResult */ class WrapperResult { constructor(data, reqId) { this.data = data; this.reqId = reqId; } } exports.WrapperResult = WrapperResult; //# sourceMappingURL=base.wrapper.js.map