mergeConfig.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. import utils from '../utils.js';
  3. import AxiosHeaders from "./AxiosHeaders.js";
  4. const headersToObject = (thing) => thing instanceof AxiosHeaders ? { ...thing } : thing;
  5. /**
  6. * Config-specific merge-function which creates a new config-object
  7. * by merging two configuration objects together.
  8. *
  9. * @param {Object} config1
  10. * @param {Object} config2
  11. *
  12. * @returns {Object} New object resulting from merging config2 to config1
  13. */
  14. export default function mergeConfig(config1, config2) {
  15. // eslint-disable-next-line no-param-reassign
  16. config2 = config2 || {};
  17. const config = {};
  18. function getMergedValue(target, source, prop, caseless) {
  19. if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
  20. return utils.merge.call({caseless}, target, source);
  21. } else if (utils.isPlainObject(source)) {
  22. return utils.merge({}, source);
  23. } else if (utils.isArray(source)) {
  24. return source.slice();
  25. }
  26. return source;
  27. }
  28. // eslint-disable-next-line consistent-return
  29. function mergeDeepProperties(a, b, prop , caseless) {
  30. if (!utils.isUndefined(b)) {
  31. return getMergedValue(a, b, prop , caseless);
  32. } else if (!utils.isUndefined(a)) {
  33. return getMergedValue(undefined, a, prop , caseless);
  34. }
  35. }
  36. // eslint-disable-next-line consistent-return
  37. function valueFromConfig2(a, b) {
  38. if (!utils.isUndefined(b)) {
  39. return getMergedValue(undefined, b);
  40. }
  41. }
  42. // eslint-disable-next-line consistent-return
  43. function defaultToConfig2(a, b) {
  44. if (!utils.isUndefined(b)) {
  45. return getMergedValue(undefined, b);
  46. } else if (!utils.isUndefined(a)) {
  47. return getMergedValue(undefined, a);
  48. }
  49. }
  50. // eslint-disable-next-line consistent-return
  51. function mergeDirectKeys(a, b, prop) {
  52. if (prop in config2) {
  53. return getMergedValue(a, b);
  54. } else if (prop in config1) {
  55. return getMergedValue(undefined, a);
  56. }
  57. }
  58. const mergeMap = {
  59. url: valueFromConfig2,
  60. method: valueFromConfig2,
  61. data: valueFromConfig2,
  62. baseURL: defaultToConfig2,
  63. transformRequest: defaultToConfig2,
  64. transformResponse: defaultToConfig2,
  65. paramsSerializer: defaultToConfig2,
  66. timeout: defaultToConfig2,
  67. timeoutMessage: defaultToConfig2,
  68. withCredentials: defaultToConfig2,
  69. withXSRFToken: defaultToConfig2,
  70. adapter: defaultToConfig2,
  71. responseType: defaultToConfig2,
  72. xsrfCookieName: defaultToConfig2,
  73. xsrfHeaderName: defaultToConfig2,
  74. onUploadProgress: defaultToConfig2,
  75. onDownloadProgress: defaultToConfig2,
  76. decompress: defaultToConfig2,
  77. maxContentLength: defaultToConfig2,
  78. maxBodyLength: defaultToConfig2,
  79. beforeRedirect: defaultToConfig2,
  80. transport: defaultToConfig2,
  81. httpAgent: defaultToConfig2,
  82. httpsAgent: defaultToConfig2,
  83. cancelToken: defaultToConfig2,
  84. socketPath: defaultToConfig2,
  85. responseEncoding: defaultToConfig2,
  86. validateStatus: mergeDirectKeys,
  87. headers: (a, b , prop) => mergeDeepProperties(headersToObject(a), headersToObject(b),prop, true)
  88. };
  89. utils.forEach(Object.keys(Object.assign({}, config1, config2)), function computeConfigValue(prop) {
  90. const merge = mergeMap[prop] || mergeDeepProperties;
  91. const configValue = merge(config1[prop], config2[prop], prop);
  92. (utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
  93. });
  94. return config;
  95. }