import { AxiosRequestConfig } from '../types' export default function mergeConfig( config1: AxiosRequestConfig, config2: AxiosRequestConfig ): AxiosRequestConfig { const config = Object.create(null) if (!config2) { config2 = {} } for (let key in config2) { mergeField(key) } function mergeField(key: string): void { const strat = strats[key] || defaultStatus config[key] = strat(config1[key], config2![key]) } return config } // 合并策略函数 // 默认 function defaultStatus(val1: any, val2: any) { return typeof val2 !== 'undefined' ? val2 : val1 } // 只接受自定义配置 function fromVal2Strat(val1: any, val2: any) { if (typeof val2 !== 'undefined') { return val2 } } const strats = Object.create(null) const stratKeysFromVal2 = ['url', 'params', 'data'] stratKeysFromVal2.forEach(key => { strats[key] = fromVal2Strat })