mergeConfig.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {deepMerge, isUndefined} from '../utils'
  2. /**
  3. * 合并局部配置优先的配置,如果局部有该配置项则用局部,如果全局有该配置项则用全局
  4. * @param {Array} keys - 配置项
  5. * @param {Object} globalsConfig - 当前的全局配置
  6. * @param {Object} config2 - 局部配置
  7. * @return {{}}
  8. */
  9. const mergeKeys = (keys, globalsConfig, config2) => {
  10. let config = {}
  11. keys.forEach(prop => {
  12. if (!isUndefined(config2[prop])) {
  13. config[prop] = config2[prop]
  14. } else if (!isUndefined(globalsConfig[prop])) {
  15. config[prop] = globalsConfig[prop]
  16. }
  17. })
  18. return config
  19. }
  20. /**
  21. *
  22. * @param globalsConfig - 当前实例的全局配置
  23. * @param config2 - 当前的局部配置
  24. * @return - 合并后的配置
  25. */
  26. export default (globalsConfig, config2 = {}) => {
  27. const method = config2.method || globalsConfig.method || 'GET'
  28. let config = {
  29. baseURL: config2.baseURL || globalsConfig.baseURL || '',
  30. method: method,
  31. url: config2.url || '',
  32. params: config2.params || {},
  33. custom: {...(globalsConfig.custom || {}), ...(config2.custom || {})},
  34. header: deepMerge(globalsConfig.header || {}, config2.header || {})
  35. }
  36. const defaultToConfig2Keys = ['getTask', 'validateStatus', 'paramsSerializer', 'forcedJSONParsing']
  37. config = {...config, ...mergeKeys(defaultToConfig2Keys, globalsConfig, config2)}
  38. // eslint-disable-next-line no-empty
  39. if (method === 'DOWNLOAD') {
  40. const downloadKeys = [
  41. // #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
  42. 'timeout',
  43. // #endif
  44. // #ifdef MP
  45. 'filePath',
  46. // #endif
  47. ]
  48. config = {...config, ...mergeKeys(downloadKeys, globalsConfig, config2)}
  49. } else if (method === 'UPLOAD') {
  50. delete config.header['content-type']
  51. delete config.header['Content-Type']
  52. const uploadKeys = [
  53. // #ifdef APP-PLUS || H5
  54. 'files',
  55. // #endif
  56. // #ifdef MP-ALIPAY
  57. 'fileType',
  58. // #endif
  59. // #ifdef H5
  60. 'file',
  61. // #endif
  62. 'filePath',
  63. 'name',
  64. // #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
  65. 'timeout',
  66. // #endif
  67. 'formData',
  68. ]
  69. uploadKeys.forEach(prop => {
  70. if (!isUndefined(config2[prop])) {
  71. config[prop] = config2[prop]
  72. }
  73. })
  74. // #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
  75. if (isUndefined(config.timeout) && !isUndefined(globalsConfig.timeout)) {
  76. config['timeout'] = globalsConfig['timeout']
  77. }
  78. // #endif
  79. } else {
  80. const defaultsKeys = [
  81. 'data',
  82. // #ifdef H5 || APP-PLUS || MP-ALIPAY || MP-WEIXIN
  83. 'timeout',
  84. // #endif
  85. 'dataType',
  86. // #ifndef MP-ALIPAY
  87. 'responseType',
  88. // #endif
  89. // #ifdef APP-PLUS
  90. 'sslVerify',
  91. // #endif
  92. // #ifdef H5
  93. 'withCredentials',
  94. // #endif
  95. // #ifdef APP-PLUS
  96. 'firstIpv4',
  97. // #endif
  98. // #ifdef MP-WEIXIN
  99. 'enableHttp2',
  100. 'enableQuic',
  101. // #endif
  102. // #ifdef MP-TOUTIAO || MP-WEIXIN
  103. 'enableCache',
  104. // #endif
  105. // #ifdef MP-WEIXIN
  106. 'enableHttpDNS',
  107. 'httpDNSServiceId',
  108. 'enableChunked',
  109. 'forceCellularNetwork',
  110. // #endif
  111. // #ifdef MP-ALIPAY
  112. 'enableCookie',
  113. // #endif
  114. // #ifdef MP-BAIDU
  115. 'cloudCache',
  116. 'defer'
  117. // #endif
  118. ]
  119. config = {...config, ...mergeKeys(defaultsKeys, globalsConfig, config2)}
  120. }
  121. return config
  122. }