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

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

Loading…
Cancel
Save