完善sdk-nodejs-axios分支

pull/12/head
Changeden 4 years ago
parent f09e00ddd7
commit 2d6370ba1b
  1. 33
      sop-sdk/sdk-nodejs-axios/common/OpenClient.js
  2. 16
      sop-sdk/sdk-nodejs-axios/main.js

@ -6,7 +6,7 @@ const RequestType = require('./RequestType');
const SignUtil = require('./SignUtil');
const BaseRequest = require('./BaseRequest');
const IS_RUN_IN_BROWSER = this === window;
const IS_RUN_IN_BROWSER = typeof window !== 'undefined' && this === window;
const HEADERS = {'Accept-Encoding': 'identity'};
@ -47,10 +47,11 @@ const buildParams = (instance, request, token) => {
return allParams;
};
const executeRequest = async (instance = {}, request, token, callback, {headers}) => {
const executeRequest = async (instance = {}, request, token, callback, customOptions = {}) => {
const params = buildParams(instance, request, token);
const {url} = instance;
const options = {
let {headers} = customOptions;
const config = {
url,
method: 'POST',
params: undefined,
@ -60,28 +61,22 @@ const executeRequest = async (instance = {}, request, token, callback, {headers}
const requestType = request.getRealRequestType();
switch (requestType) {
case RequestType.GET: {
options.method = 'GET';
options.params = params;
config.method = 'GET';
config.params = params;
}
break;
case RequestType.POST_FORM: {
headers = Object.assign(headers, {
'Content-Type': 'application/x-www-form-urlencoded'
});
options.data = qs.stringify(params);
config.data = qs.stringify(params);
}
break;
case RequestType.POST_JSON: {
options.data = params;
config.data = params;
}
break;
case RequestType.POST_FILE: {
Object.keys(params).forEach(key => {
const value = params[key];
if (!(typeof key === 'undefined' || typeof value === 'undefined')) {
formData.append(key, params[key]);
}
});
let formData;
if (IS_RUN_IN_BROWSER) {
formData = new window.FormData()
@ -101,7 +96,13 @@ const executeRequest = async (instance = {}, request, token, callback, {headers}
});
headers = Object.assign(headers, formData.getHeaders());
}
options.data = formData;
Object.keys(params).forEach(key => {
const value = params[key];
if (!(typeof key === 'undefined' || typeof value === 'undefined')) {
formData.append(key, params[key]);
}
});
config.data = formData;
}
break;
default: {
@ -110,8 +111,8 @@ const executeRequest = async (instance = {}, request, token, callback, {headers}
}
}
try {
options['headers'] = headers;
const response = await axios.request(options);
config['headers'] = headers;
const response = await axios.request(config);
callback(parseResponse(undefined, response, request));
} catch (error) {
callback(parseResponse(error, undefined, request));

@ -25,14 +25,14 @@ const openClient = new OpenClient().setUrl(url).setAppId(appId).setPrivateKey(pr
// 添加上传文件
// 批量添加
const files = [
// name: 表单名称,path:文件全路径
{name: 'file1', path: `${__dirname}/aa.txt`},
{name: 'file2', path: `${__dirname}/bb.txt`}
];
request.setFiles(files);
// 单个添加
request.addFile('file3', `${__dirname}/package.json`);
// const files = [
// // name: 表单名称,path:文件全路径
// {name: 'file1', path: `${__dirname}/aa.txt`},
// {name: 'file2', path: `${__dirname}/bb.txt`}
// ];
// request.setFiles(files);
// // 单个添加
// request.addFile('file3', `${__dirname}/package.json`);
const data = await openClient.executeSync(request);
// 成功

Loading…
Cancel
Save