axios 0.18升级至0.19.0后,自定义参数被过滤掉
在项目里面有类似于这样的请求,在axios请求上添加了一个type属性,比如type: 'download'
用以统一处理下载相关的请求,在0.18版本上可以正常使用,代码像如下这样
export function downloadFile(params) {
/**
* 下载文件
*/
return request({ url: 'download_url', method: 'get', params, type: 'download' })
}
升级至0.19.0后,发现在请求拦截器里面,参数type被竟然弄丢了!
于是怀着一颗好奇的心,打开了axios源码,对比了一下,发现axios对拦截的请求参数进行了过滤,具体代码在这个文件里面axios/lib/core/mergeConfig.js
,代码如下
utils.forEach([
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength',
'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken',
'socketPath'
], function defaultToConfig2(prop) {
if (typeof config2[prop] !== 'undefined') {
config[prop] = config2[prop];
} else if (typeof config1[prop] !== 'undefined') {
config[prop] = config1[prop];
}
});
于是给源码打了个patch包,添加了允许接收type属性,算是个临时解决方案吧