|
|
@ -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)); |
|
|
|