惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
量子位
V
Visual Studio Blog
博客园 - Franky
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
罗磊的独立博客
小众软件
小众软件
V
V2EX
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
月光博客
月光博客
Recent Announcements
Recent Announcements
雷峰网
雷峰网
F
Fortinet All Blogs
M
MIT News - Artificial intelligence

博客园 - herryLo

LangChainJs之基础模型(一) 什么是生成式AI 白嫖党体验Claude Code 关于Web GIS基础知识 直播流IOS无法播放问题排查 如何全方位提升博客站点? web前端pdf.js预览pdf实例创建报错:Array. prototype` contains unexpected enumerable properties Windows 10 专业版下推送docker镜像到harbor报错:x509: certificate relies on legacy Common Name field, use SANs instead A Philosophy of Software Design 软件设计哲学 浅读-《深入浅出Nodejs》 腾讯云对象存储 COS搭建个人网站 你知道,前端工程部署有哪些方式嘛? 2022前端开发知识总结归纳(前端掌握知识) 如何使用Docker构建前端项目 Rancher前端服务发布 知识扩展-SQL查询基础 AntV G2可视化引擎, 有用过嘛? 弄懂!ES6中的Iterator迭代器 🔥[译] 正交设计组件的好处 GitHub Action一键部署配置,值得拥有 小程序开发指南之性能优化
Axios源码解析:请求响应拦截器
herryLo · 2021-12-02 · via 博客园 - herryLo

原文链接

在Axios中拦截器是如何注册和调用的呢?下面我们一起来看看

浏览器端Axios调用流程如下:

初始化Axios——> 注册拦截器 ——> 请求拦截——> ajax请求 ——> 响应拦截 ——> 请求响应回调

Axios初始化

第一步:当然调用Axios请求时初始化了

// 初始化Axios
function Axios(instanceConfig) {
  this.defaults = instanceConfig;
  this.interceptors = {
    request: new InterceptorManager(),
    response: new InterceptorManager()
  };
}

module.exports = Axios;

InterceptorManager和InterceptorManager函数分别是是request拦截器和response拦截器,注册拦截器回调函数,这里只会讲/请求拦截器/,因为响应拦截器基本也是这个流程;这样我们使用axios.request.use来添加拦截器函数时,实际就是InterceptorManager实例对象方法,那么这里InterceptorManager函数做了什么呢?

InterceptorManager注册拦截函数

function InterceptorManager() {
  this.handlers = [];
}

//负责将拦截回调函数保存在handlers中
InterceptorManager.prototype.use = function use(fulfilled, rejected) {
  this.handlers.push({
    fulfilled: fulfilled,
    rejected: rejected
  });
  return this.handlers.length - 1;
};

通过axios.request.useaxios.response.use请求、回调拦截器添加拦截器,实际就是调用用InterceptorManager下的use方法,将回调函数保存在this.handlers数组中。

调用拦截器函数和请求函数

一下就是调用请求的实际代码,调用Axios下的request方法,同时也会先进行一次参数合并

this.request(mergeConfig(config || {}, {
  method: method,
  url: url,
  data: (config || {}).data
}));

下面就是request方法的实际代码,如下:

Axios.prototype.request = function request(config) {
  // dispatchRequest函数即ajax请求函数
  var chain = [dispatchRequest, undefined];
  var promise = Promise.resolve(config);

  this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
      chain.unshift(interceptor.fulfilled, interceptor.rejected);
  });

  this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
      chain.push(interceptor.fulfilled, interceptor.rejected);
  });

  // 组装 Promise 调用链,完成链式调用
  while (chain.length) {
      promise = promise.then(chain.shift(), chain.shift());
  }

  return promise;
};

在上面的代码中chain变量的实际值是这样的:

chain = [请求回调拦截函数, 请求异常回调拦截函数, dispatchRequest, undefined, 响应回调拦截函数, 响应异常回调拦截函数 ]

然后通过循环,使用Promise链式调用,来完成请求拦截——> ajax请求 ——> 响应拦截的功能,最后将结果return出来,非常的巧妙!!

看下面的就理解了?

(图来源自转载)

参考:

77.9K Star 的 Axios 项目有哪些值得借鉴的地方

Axios拦截器核心源码