2 Star 10 Fork 6

hokhyk / ajax_fetch_interceptor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ajax-fetch-interceptor

介绍

一个用于拦截ajaxfetch的请求和响应的小工具. 我自己已将它用在了我的chrome浏览器扩展插件项目中, 但你也可以将其用于其他很多地方,例如数据采集、API的mock,甚至是用于多个系统的单点登陆(SSO)。 请注意,ajax部分完全基于wendux的源代码: https://github.com/wendux/Ajax-hook,因为这个轮子已经很完美了。

使用方法

  1. 安装

    • 下载这个git仓库。

    • 运行 npm run build

    • 拦截ajax, 需要使用dist目录下的ajaxhook.ming.js.

      <script src="dist/ajaxhook.min.js"></script>
    • 拦截fetch需要使用dist下的fetchhook.ming.js.

      <script src="dist/ajaxhook.min.js"></script>
  2. 使用

  • proxy拦截ajax:

    
      ah.proxy({
        //请求发起前进入
        onRequest: (config, handler) => {
            console.log(config.url)
            handler.next(config);
        },
        //请求成功后进入
        onResponse: (response, handler) => {
            console.log(response.response)
            handler.next(response)
        },
        //请求发生错误时进入,比如超时;注意,不包括http状态码错误,如404仍然会认为请求成功
        onError: (err, handler) => {
            console.log(err.type)
            handler.next(err)
        }
    })
  • hookFetch拦截fetch:


   fh.hookFetch({
      requestInterceptors: {
           // input is usually url, init is of Request entity.
          itc1: function (input, init)
              {  .... do something
                      return init },
          itc2: function (input, init)
              {  .... do something
                return init },
      },
      responseInterceptors: {
          // input is usually url, interceptorRes is of Response body.
          itc3: function (input, interceptorRes)
              {  .... do something
                 return interceptorRes },
          itc4: function (input, interceptorRes)
              {  .... do something
                return interceptorRes },
      }
    })

从这里下载完整的仓库: https://gitee.com/hokhyk/ajax_fetch_proxy。 尽情使用吧,因为我让它以license 许可发行.

MIT License Copyright (c) 2021 hokhyk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A simple javascript tool used to intercept `ajax` as well as `fetch` requests and responses. I myself use it in my chrome plugin project, but you can use it in many other places like `data collection`, `api mocking`, and even `SSO` of multiple systems. Please note ajax part is completely based on `wendux`'s source code at `https://github... 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/hokhyk/ajax_fetch_interceptor.git
git@gitee.com:hokhyk/ajax_fetch_interceptor.git
hokhyk
ajax_fetch_interceptor
ajax_fetch_interceptor
master

搜索帮助