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 | const config = {};
|
---|
21 |
|
---|
22 | function getMergedValue(target, source, caseless) {
|
---|
23 | if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
|
---|
24 | return utils.merge.call({caseless}, target, source);
|
---|
25 | } else if (utils.isPlainObject(source)) {
|
---|
26 | return utils.merge({}, source);
|
---|
27 | } else if (utils.isArray(source)) {
|
---|
28 | return source.slice();
|
---|
29 | }
|
---|
30 | return source;
|
---|
31 | }
|
---|
32 |
|
---|
33 | // eslint-disable-next-line consistent-return
|
---|
34 | function mergeDeepProperties(a, b, caseless) {
|
---|
35 | if (!utils.isUndefined(b)) {
|
---|
36 | return getMergedValue(a, b, caseless);
|
---|
37 | } else if (!utils.isUndefined(a)) {
|
---|
38 | return getMergedValue(undefined, a, caseless);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // eslint-disable-next-line consistent-return
|
---|
43 | function valueFromConfig2(a, b) {
|
---|
44 | if (!utils.isUndefined(b)) {
|
---|
45 | return getMergedValue(undefined, b);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | // eslint-disable-next-line consistent-return
|
---|
50 | function defaultToConfig2(a, b) {
|
---|
51 | if (!utils.isUndefined(b)) {
|
---|
52 | return getMergedValue(undefined, b);
|
---|
53 | } else if (!utils.isUndefined(a)) {
|
---|
54 | return getMergedValue(undefined, a);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | // eslint-disable-next-line consistent-return
|
---|
59 | function mergeDirectKeys(a, b, prop) {
|
---|
60 | if (prop in config2) {
|
---|
61 | return getMergedValue(a, b);
|
---|
62 | } else if (prop in config1) {
|
---|
63 | return getMergedValue(undefined, a);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | const mergeMap = {
|
---|
68 | url: valueFromConfig2,
|
---|
69 | method: valueFromConfig2,
|
---|
70 | data: valueFromConfig2,
|
---|
71 | baseURL: defaultToConfig2,
|
---|
72 | transformRequest: defaultToConfig2,
|
---|
73 | transformResponse: defaultToConfig2,
|
---|
74 | paramsSerializer: defaultToConfig2,
|
---|
75 | timeout: defaultToConfig2,
|
---|
76 | timeoutMessage: defaultToConfig2,
|
---|
77 | withCredentials: defaultToConfig2,
|
---|
78 | withXSRFToken: defaultToConfig2,
|
---|
79 | adapter: defaultToConfig2,
|
---|
80 | responseType: defaultToConfig2,
|
---|
81 | xsrfCookieName: defaultToConfig2,
|
---|
82 | xsrfHeaderName: defaultToConfig2,
|
---|
83 | onUploadProgress: defaultToConfig2,
|
---|
84 | onDownloadProgress: defaultToConfig2,
|
---|
85 | decompress: defaultToConfig2,
|
---|
86 | maxContentLength: defaultToConfig2,
|
---|
87 | maxBodyLength: defaultToConfig2,
|
---|
88 | beforeRedirect: defaultToConfig2,
|
---|
89 | transport: defaultToConfig2,
|
---|
90 | httpAgent: defaultToConfig2,
|
---|
91 | httpsAgent: defaultToConfig2,
|
---|
92 | cancelToken: defaultToConfig2,
|
---|
93 | socketPath: defaultToConfig2,
|
---|
94 | responseEncoding: defaultToConfig2,
|
---|
95 | validateStatus: mergeDirectKeys,
|
---|
96 | headers: (a, b) => mergeDeepProperties(headersToObject(a), headersToObject(b), true)
|
---|
97 | };
|
---|
98 |
|
---|
99 | utils.forEach(Object.keys(Object.assign({}, config1, config2)), function computeConfigValue(prop) {
|
---|
100 | const merge = mergeMap[prop] || mergeDeepProperties;
|
---|
101 | const configValue = merge(config1[prop], config2[prop], prop);
|
---|
102 | (utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
|
---|
103 | });
|
---|
104 |
|
---|
105 | return config;
|
---|
106 | }
|
---|