ts-api-core
Version:
Nodejs api framework core
77 lines • 3.44 kB
JavaScript
;
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.ApiBase = void 0;
const base_object_1 = require("../context/base.object");
const error_1 = require("../base/error");
const result_code_1 = require("../base/result.code");
/**
* API接口访问基类
*/
class ApiBase extends base_object_1.BaseObject {
get httpAccess() {
if (!this._httpAccess) {
this._httpAccess = this.context.http;
}
return this._httpAccess;
}
/**
* get请求
* @param actionPath 接口路径 斜杠开始
* @param params url参数 选填 支持Map对象/普通对象。
* @param headers 请求头 选填 支持Map对象/普通对象。
*/
Get(actionPath, params, pick, onCheckResult, headers, respType) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.httpAccess.Get(actionPath, params, headers, respType);
this.CheckResult(result.data, onCheckResult);
return pick === false ? result.data : (_a = result.data) === null || _a === void 0 ? void 0 : _a.RetObject;
});
}
/**
* post请求
* @param actionPath 接口路径
* @param body body参数
* @param params url参数 选填 支持Map对象/普通对象。
* @param headers 请求头 选填 支持Map对象/普通对象。
*/
// eslint-disable-next-line max-params
Post(actionPath, body, params, pick, onCheckResult, headers, respType) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.httpAccess.Post(actionPath, body, params, headers, respType);
this.CheckResult(result.data, onCheckResult);
return pick === false ? result.data : (_a = result.data) === null || _a === void 0 ? void 0 : _a.RetObject;
});
}
/**
* 对iShop的接口返回数据进行判断验证
* @param result
*/
CheckResult(result, onCheckResult) {
if (onCheckResult) {
if (!onCheckResult(result)) {
throw new error_1.ApiError(result.RetCode, result.RetMessage, result.ReqID);
}
}
else if (result.RetCode !== 0) {
if (result.RetCode === undefined) {
throw new error_1.ApiError(result_code_1.ResultCode.NO, "解析失败", result.ReqID);
}
else {
throw new error_1.ApiError(result.RetCode, result.RetMessage ? result.RetMessage : JSON.stringify(result.RetObject), result.ReqID);
}
}
}
}
exports.ApiBase = ApiBase;
//# sourceMappingURL=api.base.js.map