ts-api-core
Version:
Nodejs api framework core
158 lines • 6.18 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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.HttpAccess = void 0;
/* eslint-disable no-console */
const axios = require("axios");
const app_1 = require("../../base/app");
const error_1 = require("../../base/error");
const ioc_decorator_1 = require("../../mvc/ioc.decorator");
/**
* http请求类 用于get/post请求
*/
let HttpAccess = class HttpAccess {
/**
* Http Access
* @param endPoint 接口地址
* @param timeOut 接口超时 单位毫秒
*/
constructor(endPoint, timeOut = 60000) {
this.timeOut = timeOut;
this.SetEndPoint(endPoint);
}
SetEndPoint(value) {
if (!value) {
this.endPoint = value !== null && value !== void 0 ? value : "";
return;
}
this.endPoint = value.endsWith("/")
? value.substring(0, value.length - 1)
: value;
if (this.endPoint.includes("?")) {
throw new error_1.SysError("endpoint里面包含路径参数分隔符? 法再次指定actionpath", "HttpAccess.SetEndPoint");
}
}
TransParams(params) {
if (!params) {
return undefined;
}
if (params instanceof Map) {
const result = {};
params.forEach((value, key, map) => {
result[key] = value;
});
return result;
}
return params;
}
Get(actionPath, params, headers, respType) {
return __awaiter(this, void 0, void 0, function* () {
const p1 = this.TransParams(params);
const head = this.TransParams(headers);
const url = this.GetFullUrl(actionPath, p1);
if (app_1.App.deubgHttpRequest) {
console.log("Get " + url);
}
const result = yield axios.default.request({
url,
method: "get",
timeout: this.timeOut,
headers: head,
responseType: respType
});
return result;
});
}
Post(actionPath, body, params, headers, respType) {
return __awaiter(this, void 0, void 0, function* () {
const p1 = this.TransParams(params);
const head = this.TransParams(headers);
const url = this.GetFullUrl(actionPath, p1);
if (app_1.App.deubgHttpRequest) {
console.log("Post " + url);
console.info(JSON.stringify(body));
}
const result = yield axios.default.request({
url,
method: "post",
timeout: this.timeOut,
headers: head,
data: body,
responseType: respType
});
return result;
});
}
/**
* 获取完整请求路径
* @param actionPath
* @param params
*/
GetFullUrl(actionPath, params) {
let result = this.endPoint;
if (result && actionPath) {
if (actionPath.indexOf("://") > 1) {
result = actionPath;
}
else {
result = actionPath.startsWith("/")
? result + actionPath
: result + "/" + actionPath;
}
}
else if (actionPath) {
if (actionPath.indexOf("://") < 1) {
throw new error_1.SysError("URL Invalid", "HttpAccess.GetFullUrl");
}
result = actionPath;
}
if (params) {
result = this.FormatUrl(result);
for (const key in params) {
const value = params[key];
if (value === undefined) {
continue;
}
result += key + "=" + encodeURIComponent(value) + "&";
}
result = result.substring(0, result.length - 1);
}
return result;
}
FormatUrl(url) {
const index = url.lastIndexOf("?");
let result = url;
if (index === -1) {
// 如果没有问号分隔路径与参数,则加一个问号
result = result + "?";
}
else if (index < result.length - 1) {
// 如果问号不是最后一个字符,则加一个参数连接符
result += "&";
}
return result;
}
};
HttpAccess = __decorate([
(0, ioc_decorator_1.Provide)(),
__metadata("design:paramtypes", [String, Number])
], HttpAccess);
exports.HttpAccess = HttpAccess;
//# sourceMappingURL=http.access.js.map