Day29 axios

简介: 一个 ajax 请求库。
拦截器的模拟实现
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <!-- 
        array.shift()该方法用于把数组的第一个元素从其中删除,并返回第一个元素的值
        思路为先将拦截器的响应回调与请求回调都压入一个数组中,之后进行遍历运行
        promise = promise.then(chains.shift(), chains.shift()); 通过循环使用promise的then链条得到最终的结果 -> 等式前面的promise将被最终的结果覆盖
     -->
    <script>
        //构造函数
        function Axios(config) {
            this.config = config;
            this.interceptors = {
                request: new InterceptorManager(),
                response: new InterceptorManager(),
            }
        }
        //发送请求  难点与重点
        Axios.prototype.request = function (config) {
            //创建一个 promise 对象
            let promise = Promise.resolve(config);
            //创建一个数组
            const chains = [dispatchRequest, undefined];
            //处理拦截器
            //请求拦截器 将请求拦截器的回调 压入到 chains 的前面  request.handles = []
            this.interceptors.request.handlers.forEach(item => {
                chains.unshift(item.fulfilled, item.rejected);
            });
            //响应拦截器
            this.interceptors.response.handlers.forEach(item => {
                chains.push(item.fulfilled, item.rejected);
            });

            // console.log(chains);
            //遍历
            while (chains.length > 0) {
                //array.shift()
                promise = promise.then(chains.shift(), chains.shift());
            }

            return promise;
        }

        //发送请求
        function dispatchRequest(config) {
            //返回一个promise 队形
            return new Promise((resolve, reject) => {
                resolve({
                    status: 200,
                    statusText: 'OK'
                });
            });
        }

        //创建实例
        let context = new Axios({});
        //创建axios函数
        let axios = Axios.prototype.request.bind(context);
        //将 context 属性 config interceptors 添加至 axios 函数对象身上
        Object.keys(context).forEach(key => {
            axios[key] = context[key];
        });

        //拦截器管理器构造函数
        function InterceptorManager() {
            this.handlers = [];
        }
        InterceptorManager.prototype.use = function (fulfilled, rejected) {
            this.handlers.push({
                fulfilled,
                rejected
            })
        }


        //以下为功能测试代码
        // 设置请求拦截器  config 配置对象
        axios.interceptors.request.use(function one(config) {
            console.log('请求拦截器 成功 - 1号');
            return config;
        }, function one(error) {
            console.log('请求拦截器 失败 - 1号');
            return Promise.reject(error);
        });

        axios.interceptors.request.use(function two(config) {
            console.log('请求拦截器 成功 - 2号');
            return config;
        }, function two(error) {
            console.log('请求拦截器 失败 - 2号');
            return Promise.reject(error);
        });

        // 设置响应拦截器
        axios.interceptors.response.use(function (response) {
            console.log('响应拦截器 成功 1号');
            return response;
        }, function (error) {
            console.log('响应拦截器 失败 1号')
            return Promise.reject(error);
        });

        axios.interceptors.response.use(function (response) {
            console.log('响应拦截器 成功 2号')
            return response;
        }, function (error) {
            console.log('响应拦截器 失败 2号')
            return Promise.reject(error);
        });


        //发送请求
        axios({
            method: 'GET',
            url: 'http://localhost:3000/posts'
        }).then(response => {
            console.log(response);
        });
    </script>

</head>

<body>

</body>

</html>
目录
相关文章
|
9月前
|
XML JSON 前端开发
Axios的特点
Axios的特点
44 0
|
13天前
|
JSON JavaScript 前端开发
axios浅析(一)
 axios 是一个轻量的 HTTP客户端 基于 XMLHttpRequest 服务来执行 HTTP 请求,支持丰富的配置,支持 Promise,支持浏览器端和 Node.js 端。自Vue2.0起,尤大宣布取消对 vue-resource 的官方推荐,转而推荐 axios。现在 axios 已经成为大部分 Vue 开发者的首选。
|
3月前
|
JSON 前端开发 JavaScript
axios的详细使用
axios的详细使用
84 1
|
3月前
|
XML 存储 JSON
Axios
Axios
29 0
|
3月前
|
JSON 前端开发 API
axios使用
axios使用
|
3月前
|
存储 设计模式 JSON
快速理解 Axios
快速理解 Axios
56 0
|
3月前
|
前端开发 JavaScript
Axios有哪些常用的方法?
Axios有哪些常用的方法?
49 0
|
10月前
axios详解
axios详解
67 0
|
11月前
|
JSON JavaScript 前端开发
axios的简单的使用
axios的简单的使用
47 0
|
JSON JavaScript 前端开发
axios是什么
axios是什么
149 1