新增nodejs-axios版本分支

This commit is contained in:
Changeden
2021-04-14 00:30:43 +08:00
parent aaef05525e
commit 764d492449
5 changed files with 376 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
const isArray = require('isarray');
/**
* 请求类父类
*/
exports.BaseRequest = class BaseRequest {
constructor() {
this.bizModel = {};
this.files = undefined;
// 用于文件上传时强制转换成POST_FILE请求
this.__forceRequestType__ = undefined;
}
setBizModel(biz = {}) {
this.bizModel = biz;
return this;
}
setFiles(files) {
this.files = files;
return this;
}
addFile({name, path}) {
if (name && path) {
if (!isArray(this.files)) {
this.files = [];
}
this.files.push({name, path});
}
return this;
}
/**
* 返回接口名称
*/
getMethod() {
throw `未实现BaseRequest类getMethod()方法`;
}
/**
* 返回版本号
*/
getVersion() {
throw '未实现BaseRequest类getVersion()方法';
}
/**
* 返回请求类型使用RequestType.js
*/
getRequestType() {
throw '未实现BaseRequest类getRequestType()方法';
}
setForceRequestType(type) {
this.__forceRequestType__ = type;
return this;
}
getRealRequestType() {
return this.__forceRequestType__ || this.getRequestType();
}
/**
* 解析返回结果,子类可以覆盖实现
* @param responseData 服务器返回内容
* @returns 返回结果
*/
parseResponse(responseData) {
let data = responseData['error_response'];
if (!data) {
const dataNodeName = this.getMethod().replace(/\./g, '_') + '_response';
data = responseData[dataNodeName];
}
return data;
}
};