mergeConfig.js 3.1 KB

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