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

推荐订阅源

AI
AI
Cloudbric
Cloudbric
Schneier on Security
Schneier on Security
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
博客园 - 司徒正美
Jina AI
Jina AI
S
Secure Thoughts
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
S
SegmentFault 最新的问题
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
H
Heimdal Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
雷峰网
雷峰网
Last Week in AI
Last Week in AI
IT之家
IT之家
Project Zero
Project Zero
O
OpenAI News
腾讯CDC
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
T
Tenable Blog
博客园_首页
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Schneier on Security
Scott Helme
Scott Helme
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 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拦截器核心源码