当前位置: 首页 > Web前端 > vue.js

axios源码(六)

时间:2023-03-31 20:20:51 vue.js

现在看axios的核心,构造器axios,它本身就是一个构造器,然后在它的原型prototype上绑定了一些方法,拦截器在this.interceptors上。为什么拦截器可以拦截?因为在发送请求之前,config会先传递给请求拦截器,处理后传递给dispatchRequest(即发送请求的方法),然后将返回的结果传递给响应拦截器,而response将在执行操作后返回结果。functionAxios(instanceConfig){这个.defaults=instanceConfig;this.interceptors={request:newInterceptorManager(),//InterceptorManager也是一个构造函数response:newInterceptorManager()};}prototype原型添加request方法(用于发送请求),getUri方法(用于建立路径request的),并且有一些方法的别名,比如axios.get(),就是在prototype上增加了get方法,所以在实例上可以访问的源码分析如下'usestrict';varutils=require('./../utils');varbuildURL=require('../helpers/buildURL');varInterceptorManager=require('./InterceptorManager');vardispatchRequest=require('./dispatchRequest');varmergeConfig=require('./mergeConfig');/***ConstructorAxios**@param{Object}instanceConfig默认配置*/functionAxios(instanceConfig){this.defaults=instanceConfig;this.interceptors={request:newInterceptorManager(),//拦截器rManager也是一个构造函数response:newInterceptorManager()};}/***分发一个请求**@param{Object}config请求配置(合并默认配置)*/Axios.prototype.request=functionrequest(config){//axios({})中的配置是{}/*eslintno-param-reassign:0*///允许axios('example/url'[,config])lafetchAPIif(typeofconfig==='字符串'){config=arguments[1]||{};配置.url=参数[0];}else{配置=配置||{};}config=mergeConfig(this.defaults,config);//设置config.methodif(config.method){config.method=config.method.toLowerCase();}elseif(this.defaults.method){config.method=this.defaults.method.toLowerCase();}else{config.method='get';}//过滤掉跳过的拦截器filterinterceptorvarrequestInterceptorChain=[];varsynchronousRequestInterceptors=true;//forEach方法在InterceptorManager文件中,其实质还是在utils文件中forEach方法,参考axios源码(3)this.interceptors.request.forEach(functionunshiftRequestInterceptors(interceptor){//拦截器就是this.handler的每个元素,参见InterceptorManager文件if(typeofinterceptor.runWhen==='function'&&interceptor.runWhen(config)===false){return;}synchronousRequestInterceptors=synchronousRequestInterceptors&&interceptor.synchronous;requestInterceptorChain.unshift(拦截器。实现,拦截器。拒绝);});varresponseInterceptorChain=[];this.interceptors.response.forEach(functionpushResponseInterceptors(interceptor){//跟上面一样responseInterceptorChain.push(interceptor.fulfilled,interceptor.rejected);});变种承诺;if(!synchronousRequestInterceptors){varchain=[dispatchRequest,undefined];Array.prototype.unshift.apply(chain,requestInterceptorChain);//添加到链chain.concat(responseInterceptorChain);//一个新的数组promise=Promise.resolve(config);while(chain.length){promise=promise.then(chain.shift(),chain.shift());}回报承诺;}varnewConfig=配置;while(requestInterceptorChain.length){varonFulfilled=requestInterceptorChain.shift();varonRejected=requestInterceptorChain.shift();尝试{newConfig=onFulfilled(newConfig);}赶上(错误){onRejected(错误);休息;}}尝试{promise=dispatchRequest(newConfig);}catch(error){returnPromise.reject(error);}while(responseInterceptorChain.length){promise=promise.then(responseInterceptorChain.shift(),responseInterceptorChain.shift());}returnpromise;};Axios.prototype.getUri=functiongetUri(config){config=mergeConfig(this.defaults,config);returnbuildURL(config.url,config.params,config.paramsSerializer).replace(/^\?/,'');};//为支持的请求方法提供别名给请求的方法提供一个别名utils.forEach(['delete','get','head','options'],functionforEachMethodNoData(method){/*eslintfunc-names:0*/Axios.prototype[方法od]=function(url,config){returnthis.request(mergeConfig(config||{},{method:method,url:url,data:(config||{}).data}));};});utils.forEach(['post','put','patch'],functionforEachMethodWithData(method){/*eslintfunc-names:0*/Axios.prototype[method]=function(url,data,config){returnthis.request(mergeConfig(config||{},{method:method,url:url,data:data}));};});module.exports=Axios;

最新推荐
猜你喜欢