123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import { deepMerge, isUndefined } from '../utils'
- const mergeKeys = (keys, globalsConfig, config2) => {
- const config = {}
- keys.forEach((prop) => {
- if (!isUndefined(config2[prop])) {
- config[prop] = config2[prop]
- } else if (!isUndefined(globalsConfig[prop])) {
- config[prop] = globalsConfig[prop]
- }
- })
- return config
- }
- export default (globalsConfig, config2 = {}) => {
- const method = config2.method || globalsConfig.method || 'GET'
- let config = {
- baseURL: globalsConfig.baseURL || '',
- method,
- url: config2.url || '',
- params: config2.params || {},
- custom: { ...(globalsConfig.custom || {}), ...(config2.custom || {}) },
- header: deepMerge(globalsConfig.header || {}, config2.header || {})
- }
- const defaultToConfig2Keys = ['getTask', 'validateStatus']
- config = { ...config, ...mergeKeys(defaultToConfig2Keys, globalsConfig, config2) }
-
- if (method === 'DOWNLOAD') {
-
- if (!isUndefined(config2.timeout)) {
- config.timeout = config2.timeout
- } else if (!isUndefined(globalsConfig.timeout)) {
- config.timeout = globalsConfig.timeout
- }
-
- } else if (method === 'UPLOAD') {
- delete config.header['content-type']
- delete config.header['Content-Type']
- const uploadKeys = [
-
- 'files',
-
-
- 'fileType',
-
-
- 'file',
-
- 'filePath',
- 'name',
-
- 'timeout',
-
- 'formData'
- ]
- uploadKeys.forEach((prop) => {
- if (!isUndefined(config2[prop])) {
- config[prop] = config2[prop]
- }
- })
-
- if (isUndefined(config.timeout) && !isUndefined(globalsConfig.timeout)) {
- config.timeout = globalsConfig.timeout
- }
-
- } else {
- const defaultsKeys = [
- 'data',
-
- 'timeout',
-
- 'dataType',
-
- 'responseType',
-
-
- 'sslVerify',
-
-
- 'withCredentials',
-
-
- 'firstIpv4'
-
- ]
- config = { ...config, ...mergeKeys(defaultsKeys, globalsConfig, config2) }
- }
- return config
- }
|