npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

axios-request-tool

v0.1.2

Published

对 axios 的封装,拦截请求、响应,取消请求

Downloads

13

Readme

axios-request-tool

对 axios 的封装,拦截请求、响应,取消请求

安装

Using npm:

npm install axios-request-tool

配置

使用前配置 new requestTool(options)

- options参数

- 参数一: axios对象(必须)

- 参数二: apiList 请求地址列表(必须)

- 参数三: interceptors 拦截对象配置(非必须)

- 参数四: axiosConfig axios配置(非必须)

import axios from 'axios'
import RequestTool from 'axios-request-tool'
const requestTool = new RequestTool({
  // 需要将axios传入
  axios,
  // 请求接口地址列表
  apiList: {
    login: '/login',
	other: '/other',
	...
  },
  // 拦截对象
  interceptors: {
    // 请求拦截 AxiosRequestConfig
    requestInterceptor(config: AxiosRequestConfig) {
      return config
    },
    // 请求错误拦截
    requestError(err) {},
    // 响应拦截
    responseInterceptor(res: AxiosResponse) {
      return res
    },
    // 响应错误拦截
    responseError(err) {},
  },
  // axios请求配置 这一点没有变动,可以参考axios官网
  axiosConfig: {
    baseURL: 'http://**********',
    timeout: 1000,
  },
})

实例参数解读

const requestTool =  new RequestTool({....})

| key | type | description | | -------------------- | ------------------------------------------: | :----------: | | axiosInstance | AxiosInstance | axios实例 | CancelToken | CancelTokenStatic |CancelToken实例| | Apis | { key:() => {},... } | 请求方法集合 | | cancelQueue | {key:() => {} | 取消方法集合 |

使用

配置好 RequestTool 可以在html或 js 文件中 直接 import 使用。

requestTool.Apis
  .login(data, "GET", { config })
  .then((res) => {})
  .catch((err) => {});

axios-request-tool 将会根据 requestTool.options 中配置的 apiList 对象中的 key 生成请求方法,返回 Promise 对象。 方法中第三个参数可以设置本次请求的 其他 参数配置, 也可以设置自定义其他的对象, 在拦截器中 config 参数获取,做出处理。

- 方法参数描述 —— 对应示例中的 login

| key | type | description | | ------ | -------------------------------------------------------------------: | :---------: | | 参数一 | {} | 请求参数 | | 参数二 | 可选: "GET"/"POST"/"PUT"/"DELETE"/"TRACE"/"CONNECT"等常见请求方式 | 请求方式 | | 参数三 | {url:string, params:object, urlParam:string} | 自定义选项 |

- 参数三解读: url 若传递url参数,将直接替换掉apiList中配置的url,采用传递的url

- 参数三解读: parmas 可用于POST等支持请求时传递query参数, 下面案例中会有提到

- 参数三解读:urlParam 可用于请求中将一些url请求参数拼接到当前url上面 下面案例中会有提到

案例使用

// params 使用
requestTool.Apis.Login(
  {
    name: 'lll',
    password: '123456',
  },
  'POST',
  {
    params: {
      name: 'lll',
      password: '123456',
    },
  }
)
  .then((er) => {
    console.log(er)
  })
  .catch((error) => console.log(error))
  
/**
 * 实际发出请求参数
 * query参数 /login?name=lll&password=123456
 * body 参数 {name: 'lll', password: '123456'}
 */
 
// urlParam 使用
requestTool.Apis.Login(
  {
    name: 'lll',
    password: '123456',
  },
  'POST',
  {
    urlParam: /12323
  }
)
  .then((er) => {
    console.log(er)
  })
  .catch((error) => console.log(error))
  
/**
 * 实际发出请求参数
 * query参数 /login/12323
 * body 参数 {name: 'lll', password: '123456'}
 */

requestTool.cancelQueue 取消请求

requestTool.cancelQueue 可以获取当前请求的取消请求方法强制中断某个请求

// requestTool.cancelQueue[apiKey]();
requestTool.cancelQueue.login();

执行 cancelQueue 中对应 apikey 的函数强行中断某个请求 catch 可以捕捉中断信息

TS的支持

如果想在ts中获得更好的Apis集合提示,需传入范型

import apiList from '@/apiList'
const requestTool = new RequestTool<typeof apiList>({
  axios,
  apiList,
})
requestTool.Apis.Login({}, 'POST').then((res) => {
  console.log(res)
})