2021-05-25 23:17:29 +08:00
|
|
|
import { isUndefined, isPlainObject } from '../helpers/is';
|
|
|
|
import { deepMerge } from '../helpers/utils';
|
2021-05-21 14:26:22 +08:00
|
|
|
import { AxiosRequestConfig } from './Axios';
|
2021-05-11 10:22:44 +08:00
|
|
|
|
2021-05-21 14:26:22 +08:00
|
|
|
type AxiosRequestConfigKey = keyof AxiosRequestConfig;
|
2021-05-11 10:22:44 +08:00
|
|
|
|
|
|
|
function onlyFromConfig2(
|
2021-05-21 14:26:22 +08:00
|
|
|
keys: AxiosRequestConfigKey[],
|
|
|
|
target: AxiosRequestConfig,
|
|
|
|
config2: AxiosRequestConfig,
|
2021-05-11 10:22:44 +08:00
|
|
|
) {
|
|
|
|
keys.forEach((key) => {
|
2021-05-21 14:26:22 +08:00
|
|
|
const value = config2[key];
|
|
|
|
|
|
|
|
if (!isUndefined(value)) {
|
|
|
|
target[key] = value as any;
|
2021-05-11 10:22:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function priorityFromConfig2(
|
2021-05-21 14:26:22 +08:00
|
|
|
keys: AxiosRequestConfigKey[],
|
|
|
|
target: AxiosRequestConfig,
|
2021-05-11 10:22:44 +08:00
|
|
|
config1: AxiosRequestConfig,
|
2021-05-21 14:26:22 +08:00
|
|
|
config2: AxiosRequestConfig,
|
2021-05-11 10:22:44 +08:00
|
|
|
) {
|
|
|
|
keys.forEach((key) => {
|
2021-05-21 14:26:22 +08:00
|
|
|
const value1 = config1[key];
|
|
|
|
const value2 = config2[key];
|
|
|
|
|
|
|
|
if (!isUndefined(value2)) {
|
|
|
|
target[key] = value2 as any;
|
|
|
|
} else if (!isUndefined(value1)) {
|
|
|
|
target[key] = value1 as any;
|
2021-05-11 10:22:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deepMergeConfig(
|
2021-05-21 14:26:22 +08:00
|
|
|
keys: AxiosRequestConfigKey[],
|
|
|
|
target: AxiosRequestConfig,
|
2021-05-11 10:22:44 +08:00
|
|
|
config1: AxiosRequestConfig,
|
2021-05-21 14:26:22 +08:00
|
|
|
config2: AxiosRequestConfig,
|
2021-05-11 10:22:44 +08:00
|
|
|
) {
|
|
|
|
keys.forEach((key) => {
|
2021-05-21 14:26:22 +08:00
|
|
|
const value1 = config1[key];
|
|
|
|
const value2 = config2[key];
|
|
|
|
|
|
|
|
if (isPlainObject(value2)) {
|
|
|
|
target[key] = deepMerge(value1 ?? {}, value2) as any;
|
|
|
|
} else if (isPlainObject(value1)) {
|
|
|
|
target[key] = deepMerge(value1) as any;
|
2021-05-11 10:22:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:04:11 +08:00
|
|
|
const onlyFromConfig2Keys: AxiosRequestConfigKey[] = [
|
|
|
|
'url',
|
|
|
|
'method',
|
|
|
|
'data',
|
|
|
|
'upload',
|
|
|
|
'download',
|
|
|
|
];
|
2021-05-21 14:26:22 +08:00
|
|
|
const priorityFromConfig2Keys: AxiosRequestConfigKey[] = [
|
|
|
|
'adapter',
|
|
|
|
'baseURL',
|
|
|
|
'paramsSerializer',
|
|
|
|
'transformRequest',
|
|
|
|
'transformResponse',
|
|
|
|
'errorHandler',
|
|
|
|
'cancelToken',
|
|
|
|
'dataType',
|
|
|
|
'responseType',
|
|
|
|
'timeout',
|
|
|
|
'enableHttp2',
|
|
|
|
'enableQuic',
|
|
|
|
'enableCache',
|
|
|
|
'sslVerify',
|
|
|
|
'validateStatus',
|
|
|
|
'onUploadProgress',
|
|
|
|
'onDownloadProgress',
|
|
|
|
];
|
|
|
|
const deepMergeConfigKeys: AxiosRequestConfigKey[] = ['headers', 'params'];
|
|
|
|
|
|
|
|
export function mergeConfig(
|
2021-05-11 10:22:44 +08:00
|
|
|
config1: AxiosRequestConfig = {},
|
2021-05-21 14:26:22 +08:00
|
|
|
config2: AxiosRequestConfig = {},
|
2021-05-11 10:22:44 +08:00
|
|
|
): AxiosRequestConfig {
|
|
|
|
const config: AxiosRequestConfig = {};
|
|
|
|
|
|
|
|
onlyFromConfig2(onlyFromConfig2Keys, config, config2);
|
|
|
|
priorityFromConfig2(priorityFromConfig2Keys, config, config1, config2);
|
|
|
|
deepMergeConfig(deepMergeConfigKeys, config, config1, config2);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|