| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | import utils from '../utils.js';
|
|---|
| 4 | import AxiosHeaders from './AxiosHeaders.js';
|
|---|
| 5 |
|
|---|
| 6 | const headersToObject = (thing) => (thing instanceof AxiosHeaders ? { ...thing } : thing);
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * Config-specific merge-function which creates a new config-object
|
|---|
| 10 | * by merging two configuration objects together.
|
|---|
| 11 | *
|
|---|
| 12 | * @param {Object} config1
|
|---|
| 13 | * @param {Object} config2
|
|---|
| 14 | *
|
|---|
| 15 | * @returns {Object} New object resulting from merging config2 to config1
|
|---|
| 16 | */
|
|---|
| 17 | export default function mergeConfig(config1, config2) {
|
|---|
| 18 | // eslint-disable-next-line no-param-reassign
|
|---|
| 19 | config2 = config2 || {};
|
|---|
| 20 |
|
|---|
| 21 | // Use a null-prototype object so that downstream reads such as `config.auth`
|
|---|
| 22 | // or `config.baseURL` cannot inherit polluted values from Object.prototype.
|
|---|
| 23 | // `hasOwnProperty` is restored as a non-enumerable own slot to preserve
|
|---|
| 24 | // ergonomics for user code that relies on it.
|
|---|
| 25 | const config = Object.create(null);
|
|---|
| 26 | Object.defineProperty(config, 'hasOwnProperty', {
|
|---|
| 27 | // Null-proto descriptor so a polluted Object.prototype.get cannot turn
|
|---|
| 28 | // this data descriptor into an accessor descriptor on the way in.
|
|---|
| 29 | __proto__: null,
|
|---|
| 30 | value: Object.prototype.hasOwnProperty,
|
|---|
| 31 | enumerable: false,
|
|---|
| 32 | writable: true,
|
|---|
| 33 | configurable: true,
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | function getMergedValue(target, source, prop, caseless) {
|
|---|
| 37 | if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
|
|---|
| 38 | return utils.merge.call({ caseless }, target, source);
|
|---|
| 39 | } else if (utils.isPlainObject(source)) {
|
|---|
| 40 | return utils.merge({}, source);
|
|---|
| 41 | } else if (utils.isArray(source)) {
|
|---|
| 42 | return source.slice();
|
|---|
| 43 | }
|
|---|
| 44 | return source;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | function mergeDeepProperties(a, b, prop, caseless) {
|
|---|
| 48 | if (!utils.isUndefined(b)) {
|
|---|
| 49 | return getMergedValue(a, b, prop, caseless);
|
|---|
| 50 | } else if (!utils.isUndefined(a)) {
|
|---|
| 51 | return getMergedValue(undefined, a, prop, caseless);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | // eslint-disable-next-line consistent-return
|
|---|
| 56 | function valueFromConfig2(a, b) {
|
|---|
| 57 | if (!utils.isUndefined(b)) {
|
|---|
| 58 | return getMergedValue(undefined, b);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // eslint-disable-next-line consistent-return
|
|---|
| 63 | function defaultToConfig2(a, b) {
|
|---|
| 64 | if (!utils.isUndefined(b)) {
|
|---|
| 65 | return getMergedValue(undefined, b);
|
|---|
| 66 | } else if (!utils.isUndefined(a)) {
|
|---|
| 67 | return getMergedValue(undefined, a);
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // eslint-disable-next-line consistent-return
|
|---|
| 72 | function mergeDirectKeys(a, b, prop) {
|
|---|
| 73 | if (utils.hasOwnProp(config2, prop)) {
|
|---|
| 74 | return getMergedValue(a, b);
|
|---|
| 75 | } else if (utils.hasOwnProp(config1, prop)) {
|
|---|
| 76 | return getMergedValue(undefined, a);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | const mergeMap = {
|
|---|
| 81 | url: valueFromConfig2,
|
|---|
| 82 | method: valueFromConfig2,
|
|---|
| 83 | data: valueFromConfig2,
|
|---|
| 84 | baseURL: defaultToConfig2,
|
|---|
| 85 | transformRequest: defaultToConfig2,
|
|---|
| 86 | transformResponse: defaultToConfig2,
|
|---|
| 87 | paramsSerializer: defaultToConfig2,
|
|---|
| 88 | timeout: defaultToConfig2,
|
|---|
| 89 | timeoutMessage: defaultToConfig2,
|
|---|
| 90 | withCredentials: defaultToConfig2,
|
|---|
| 91 | withXSRFToken: defaultToConfig2,
|
|---|
| 92 | adapter: defaultToConfig2,
|
|---|
| 93 | responseType: defaultToConfig2,
|
|---|
| 94 | xsrfCookieName: defaultToConfig2,
|
|---|
| 95 | xsrfHeaderName: defaultToConfig2,
|
|---|
| 96 | onUploadProgress: defaultToConfig2,
|
|---|
| 97 | onDownloadProgress: defaultToConfig2,
|
|---|
| 98 | decompress: defaultToConfig2,
|
|---|
| 99 | maxContentLength: defaultToConfig2,
|
|---|
| 100 | maxBodyLength: defaultToConfig2,
|
|---|
| 101 | beforeRedirect: defaultToConfig2,
|
|---|
| 102 | transport: defaultToConfig2,
|
|---|
| 103 | httpAgent: defaultToConfig2,
|
|---|
| 104 | httpsAgent: defaultToConfig2,
|
|---|
| 105 | cancelToken: defaultToConfig2,
|
|---|
| 106 | socketPath: defaultToConfig2,
|
|---|
| 107 | allowedSocketPaths: defaultToConfig2,
|
|---|
| 108 | responseEncoding: defaultToConfig2,
|
|---|
| 109 | validateStatus: mergeDirectKeys,
|
|---|
| 110 | headers: (a, b, prop) =>
|
|---|
| 111 | mergeDeepProperties(headersToObject(a), headersToObject(b), prop, true),
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | utils.forEach(Object.keys({ ...config1, ...config2 }), function computeConfigValue(prop) {
|
|---|
| 115 | if (prop === '__proto__' || prop === 'constructor' || prop === 'prototype') return;
|
|---|
| 116 | const merge = utils.hasOwnProp(mergeMap, prop) ? mergeMap[prop] : mergeDeepProperties;
|
|---|
| 117 | const a = utils.hasOwnProp(config1, prop) ? config1[prop] : undefined;
|
|---|
| 118 | const b = utils.hasOwnProp(config2, prop) ? config2[prop] : undefined;
|
|---|
| 119 | const configValue = merge(a, b, prop);
|
|---|
| 120 | (utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
|
|---|
| 121 | });
|
|---|
| 122 |
|
|---|
| 123 | return config;
|
|---|
| 124 | }
|
|---|