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-simple-wrapper

v1.0.5

Published

provide various axios interceptors to help you to build your project

Downloads

17

Readme

axios-simple-wrapper

基于axios, 提供各种常用的拦截器以便于快速开发

安装

安装

npm i axios-simple-wrapper --save

拦截器

防抖拦截器

添加axios拦截器用于防抖(防止同一时间内重复发送某个请求)

import {applyDebounceInterceptor} from 'axios-simple-wrapper'
import axios from 'axios'

applyDebounceInterceptor(axios, {/* 其它配置 */})

一共有3种防抖策略: 不允许重复的请求, 若重复则取消之前的请求, 无策略

默认会使用'不允许重复的请求', 若要重复指定, 可以在url后添加一个 # 号, 然后利用url传参的方式设置, 如下面样例所示:

import axios from "axios";

// 应用拦截器后默认使用'不允许重复的请求'策略
axios.method('https://localhost')

// 或者可以手动指定
// 使用'不允许重复的请求'策略
axios.method('https://localhost#rejectPolicy=rejectIfExist')
// 使用'若重复则取消之前的请求'策略
axios.method('https://localhost#rejectPolicy=cancelOldTask')
// 使用'无策略'
axios.method('https://localhost#rejectPolicy=noPolicy')

可用配置:

| 名称 | 类型 | 说明 | |:------------------------:|:------------------:|:---------------------------------------------------:| | disableOnDevelopmentMode | boolean | 在开发模式下关闭拦截; 例如在React18+中, 生命周期函数(钩子)可能会被重复调用而造成请求重复 | | rejectErrorClass | RequestRejectError | 当使用'不允许重复的请求'策略时, 抛出的异常类型 |

判断任务是否被取消

如果使用的是'不允许重复的请求'策略, 可以将请求catch的异常对象使用instanceofRequestRejectError进行比较

如果是'不允许重复的请求'策略, 可以与CanceledError进行比较 详细请见: rejectErrorClass用法

import {RequestRejectError} from 'axios-simple-wrapper'
import axios from "axios";

class RejectError extends RequestRejectError {
  constructor(config: AxiosRequestConfig, message?: string) {
    super(config, message)
  }
}

applyDebounceInterceptor(axios, {
  rejectErrorClass: RejectError
})

axios.get('url').catch(err => {
  if (err instanceof RequestRejectError) {
    // 任务被拒绝了
  }
})

额外工具

快速发送防抖请求

import axiosWrapper, {applyDebounceInterceptor, cancelOldAjax, noRepeatAjax} from 'axios-simple-wrapper'
import axios from 'axios'

// 将axios传过去! 不然会报错
axiosWrapper.default.axios = axios
applyDebounceInterceptor(axios)

// 这种只支持GET和POST请求,对象会被自动解析为application/x-www-form-urlencoded格式
noRepeatAjax('url', {msg: 'HelloWorld'}, 'POST')
cancelOldAjax('url', {msg: 'HelloWorld'}, 'GET')

// 若要使用其它方法可以将AxiosConfig直接传入(这里是直接将参数全丢给了Axios,请求数据需要自己处理,但拦截器仍然有效)
noRepeatAjax({
  url: 'url',
  method: 'PUT'
})
cancelOldAjax({
  url: 'url',
  method: 'DELETE'
})

在发送GET请求时,参数会自动添加到url上; 发送POST请求则添加到请求体上, Content-Typeapplication/x-www-form-urlencoded,

高级用法

Typescript下发送请求,一般需要声明返回值的类型,而axios请求的返回值是AxiosPromise, 其本质是Promise<AxiosResponse>, 返回类型默认被AxiosResponse包装, 其data属性就是响应体的数据:

import axios, {AxiosResponse} from "axios";

// 泛型指定响应体的类型为number
axios.get<number>('url').then((response: AxiosResponse) => {
  const responseBody = response.data
  // true
  console.log(responseBody === 'number')
})

但有些时候,我们可能设置了一些拦截器或者过滤器,导致返回的结果并不是AxiosResponse对象,而且无法直接通过axios自带泛型来声明返回值的类型

因此在上面两个方法中额外声明了一个泛型Clean, 将其设置为true, 返回值的类型将不会再被AxiosResponse包装:

import axios from "axios";

type Person = {/*...*/}

// 类型为: AxiosPromise<Person> 也可以理解为 Promise<AxiosResponse<Person>>
noRepeatAjax<Person>('url')

// 类型为: Promise<Person>
noRepeatAjax<Person, true>('url')