UNPKG

elmer-ui-core

Version:

web app framework

410 lines (409 loc) 20.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElmerServiceRequest = void 0; var axios_1 = require("axios"); var elmer_common_1 = require("elmer-common"); var configuration_1 = require("../configuration"); var AppServiceConfig_1 = require("../configuration/AppServiceConfig"); var ElmerServiceRequest = (function (_super) { __extends(ElmerServiceRequest, _super); function ElmerServiceRequest() { var _this = _super.call(this) || this; _this.requestResult = {}; _this.env = "Prod"; _this.configData = AppServiceConfig_1.getServiceConfig(); _this.config = _this.configData.config; return _this; } ElmerServiceRequest.prototype.init = function (reload) { this.env = configuration_1.getGlobalConfiguration().env || "Prod"; this.configData = AppServiceConfig_1.getServiceConfig(); this.config = this.configData.config; }; ElmerServiceRequest.prototype.setConfig = function (configData) { if (configData) { this.configData = configData; this.config = this.configData.config; this.env = configuration_1.getGlobalConfiguration().env || "Prod"; } else { throw new Error("Can not set an empty object to service config"); } }; ElmerServiceRequest.prototype.send = function (data, success, fail, complete, beforeRequest) { var _this = this; this.success = success; this.fail = fail; this.complete = complete; if (beforeRequest !== undefined && this.isFunction(beforeRequest)) { beforeRequest(data); } if (data && this.isArray(data) && data.length > 0) { this.requestResult = {}; data.map(function (tmpOption) { if (_this.isEmpty(tmpOption.id)) { tmpOption.id === _this.guid(); } _this.requestResult[tmpOption.id] = { status: null, statusText: null }; _this.sendRequest(tmpOption); }); } else { typeof this.fail === "function" && this.fail({ status: 500, statusText: "请设置发送请求内容!" }); this.requestCompleteCheck(); console.error("请设置发送请求内容!"); } }; ElmerServiceRequest.prototype.getUrl = function (endPointID, nameSpace) { var endPoint = null; if (this.isEmpty(nameSpace)) { endPoint = this.getValue(this.config.endPoints, endPointID); } else { var namespaceData = this.getValue(this.config, nameSpace); endPoint = namespaceData ? this.getValue(namespaceData.endPoints, endPointID) : null; } if (endPoint) { return this.getRequestUrl(endPoint, { namespace: nameSpace, }); } return ""; }; ElmerServiceRequest.prototype.sendRequest = function (option) { var _this = this; var endPoint = this.getEndPoint(option); if (!this.config || Object.keys(this.config).length <= 0) { this.init(true); } return new Promise(function (resolve, reject) { var _a, _b, _c, _d, _e, _f, _g, _h; if (endPoint || !_this.isEmpty(option.url)) { var method = _this.config.dummy ? "GET" : _this.getRequestMethod(option, endPoint); var comBeforeResult = (_c = (_b = (_a = _this.configData) === null || _a === void 0 ? void 0 : _a.common) === null || _b === void 0 ? void 0 : _b.onBefore) === null || _c === void 0 ? void 0 : _c.call(_b, endPoint, option); endPoint && typeof endPoint.onBefore === "function" && endPoint.onBefore(option, endPoint); var reqUrl = _this.getRequestUrl(endPoint, option); var header = __assign({ "Content-Type": "application/json;charset=UTF-8" }, _this.getRequestHeader(endPoint, option)); var allData = void 0; var postData = void 0; var contentType = !_this.isEmpty(header["Content-Type"]) ? header["Content-Type"] : (header["content-Type"] || header["content-type"]); if (/multipart\/form-data/.test(contentType)) { postData = option.data; } else { allData = __assign(__assign({}, (endPoint && _this.isObject(endPoint.data) ? endPoint.data : {})), (option && _this.isObject(option.data) ? option.data : {})); postData = JSON.stringify(allData); } var timeout = option.timeout || 30000; method = method.toUpperCase(); if (!_this.isEmpty(option.type)) { method = option.type.toUpperCase(); } if (undefined === comBeforeResult || (undefined !== comBeforeResult && comBeforeResult)) { if (!option.skip) { axios_1.default(reqUrl, { data: postData, headers: header, method: method, onDownloadProgress: function (event) { var _a; (_a = option.downloadProgress) === null || _a === void 0 ? void 0 : _a.call(option, event, option); }, onUploadProgress: function (event) { var _a; (_a = option.uploadProgress) === null || _a === void 0 ? void 0 : _a.call(option, event, option); }, timeout: timeout }).then(function (data) { var _a, _b, _c; var comEvent = {}; var comResponse = (_c = (_b = (_a = _this.configData) === null || _a === void 0 ? void 0 : _a.common) === null || _b === void 0 ? void 0 : _b.onResponse) === null || _c === void 0 ? void 0 : _c.call(_b, data, comEvent); if (undefined === comResponse || (undefined !== comResponse && comResponse)) { _this.requestResult[option.id] = { status: data.status, statusText: data.statusText }; if (data.status === 200) { endPoint && typeof endPoint.onAfter === "function" && endPoint.onAfter(data); var result = data.data; _this.responseDataCheck(result); typeof option.success === "function" && option.success(result, { endPoint: endPoint, headers: data.headers, request: option, response: data }); typeof _this.success === "function" && _this.success(result, { endPoint: endPoint, headers: data.headers, request: option, response: data }); typeof option.complete === "function" && option.complete({ request: option, response: data }); _this.requestCompleteCheck(); resolve(result, comResponse, option); } else { reject({ status: data.status, statusText: data.statusText }); } } }).catch(function (err) { var _a, _b; var errResult = (_b = (_a = _this.configData.common) === null || _a === void 0 ? void 0 : _a.onError) === null || _b === void 0 ? void 0 : _b.call(_a, err); if (undefined === errResult || (undefined !== errResult && errResult)) { _this.requestResult[option.id] = { status: err.status || 500, statusText: err.statusText }; typeof option.fail === "function" && option.fail(err, option); typeof option.complete === "function" && option.complete(option); typeof _this.fail === "function" && _this.fail(option.endPoint, err); _this.requestCompleteCheck(); reject(err); } }); } else { var comResponse = (_f = (_e = (_d = _this.configData) === null || _d === void 0 ? void 0 : _d.common) === null || _e === void 0 ? void 0 : _e.onResponse) === null || _f === void 0 ? void 0 : _f.call(_e, { status: 200, statusText: "200 Found" }, {}); if (undefined === comResponse || (undefined !== comResponse && comResponse)) { _this.requestResult[option.id] = { status: 200, statusText: "success" }; typeof option.complete === "function" && option.complete(option); _this.requestCompleteCheck(); resolve({}); } } } } else { var errData = { status: 600, statusText: "请配置请求参数: " + option.endPoint }; var errResult = (_h = (_g = _this.configData.common) === null || _g === void 0 ? void 0 : _g.onError) === null || _h === void 0 ? void 0 : _h.call(_g, errData); if (undefined === errResult || (undefined !== errResult && errResult)) { typeof option.fail === "function" && option.fail(errData); typeof _this.fail === "function" && _this.fail(option.endPoint, "未配置请求参数!"); typeof option.complete === "function" && option.complete(option); _this.requestResult[option.id] = errData; _this.requestCompleteCheck(); console.error("获取请求配置参数错误【" + option.endPoint + "】!"); reject(errData); } } }); }; ElmerServiceRequest.prototype.sendRequestAsync = function (option) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.sendRequest(option)]; case 1: return [2, (_a.sent())]; } }); }); }; ElmerServiceRequest.prototype.getRequestMethod = function (option, endPoint) { if (option) { if (!this.isEmpty(option["type"])) { return option["type"]; } if (!this.isEmpty(option["method"])) { return option["method"]; } } if (endPoint) { if (!this.isEmpty(endPoint.type)) { return endPoint.type; } if (!this.isEmpty(endPoint.method)) { return endPoint.method; } } return "GET"; }; ElmerServiceRequest.prototype.getEndPoint = function (option) { if (this.isEmpty(this.config)) { this.init(); } if (this.isEmpty(option.namespace) && this.config) { return this.getValue(this.config.endPoints, option.endPoint); } else { var nameSpace = this.getValue(this.config, option.namespace); if (nameSpace) { return this.getValue(nameSpace.endPoints, option.endPoint); } } }; ElmerServiceRequest.prototype.requestCompleteCheck = function () { var _this = this; var isComplete = true; Object.keys(this.requestResult).map(function (tmpRequestID) { var tmpResult = _this.requestResult[tmpRequestID]; if (tmpResult.status == null) { isComplete = false; } }); isComplete && typeof this.complete === "function" && this.complete(); }; ElmerServiceRequest.prototype.getRequestHeader = function (endPoint, option) { var _this = this; var header = __assign(__assign({}, (endPoint && endPoint.header ? endPoint.header : {})), (option && option.header ? option.header : {})); var result = {}; if (header) { Object.keys(header).map(function (tmpKey) { var tmpHeaderKey = _this.humpToStr(tmpKey); tmpHeaderKey = tmpHeaderKey.substr(0, 1).toUpperCase() + tmpHeaderKey.substr(1); result[tmpHeaderKey] = header[tmpKey]; }); } return header; }; ElmerServiceRequest.prototype.getRequestUrl = function (endPoint, option) { var reqUrl = ""; var namespaceData = this.isEmpty(option.namespace) ? this.config : this.getValue(this.config, option.namespace); if (namespaceData.dummy) { reqUrl = namespaceData.dummyPath + (endPoint ? endPoint.dummy : "undefined"); } else { if (this.isEmpty(option.url)) { var env = this.env; var defineBaseUrl = typeof namespaceData.getDomain === "function" ? namespaceData.getDomain({ endPoint: endPoint, env: env, option: option }) : null; var baseUrl = namespaceData.baseUrl; if (this.isEmpty(defineBaseUrl)) { if (namespaceData.envUrls && !this.isEmpty(env)) { if (!this.isEmpty(namespaceData.envUrls[env])) { baseUrl = namespaceData.envUrls[env]; } } } else { baseUrl = defineBaseUrl; } reqUrl = baseUrl + (endPoint ? endPoint.url : "undefined"); } else { reqUrl = option.url; } } if ((option && option.uri) || (endPoint && endPoint.uri)) { var uriArr_1 = []; var uriData_1 = endPoint.uri || {}; this.extend(uriData_1, option.uri); Object.keys(uriData_1).map(function (urlKey) { var urlValue = uriData_1[urlKey]; if (urlValue) { var myValue = encodeURIComponent(urlValue); uriArr_1.push(urlKey + "=" + myValue); } }); var uriParam = uriArr_1.join("&"); if (/\?/.test(reqUrl)) { reqUrl += "&" + uriParam; } else { reqUrl += "?" + uriParam; } } namespaceData = null; var regMatch = reqUrl.match(/^http[s]{0,1}\:\/\//); if (regMatch) { reqUrl = reqUrl.replace(/^http[s]{0,1}\:\/\//, ""); reqUrl = reqUrl.replace(/\/\//g, "/"); reqUrl = regMatch[0] + reqUrl; } return reqUrl; }; ElmerServiceRequest.prototype.responseDataCheck = function (data) { if (!data.success && data.toLogin) { var toUrl = data.loginUrl; if (typeof toUrl === "string" && toUrl.length > 0) { window.location.href = toUrl; } } }; ElmerServiceRequest.className = "ElmerServiceRequest"; return ElmerServiceRequest; }(elmer_common_1.Common)); exports.ElmerServiceRequest = ElmerServiceRequest;