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 🙏

© 2026 – Pkg Stats / Ryan Hefner

axios-deduplicator

v1.2.0

Published

axios deduplicator plugin (axios 请求去重插件)

Readme

English | 简体中文

AXIOS-DEDUPLICATOR

描述

这是一个简单的库,允许您去重 axios 请求。当您有多个相同的请求并且希望避免多次发出相同的请求时,它非常有用。它会在请求之前检查是否有相同的请求,如果有,则会返回相同的 Promise。

安装

npm install axios-deduplicator

pnpm add axios-deduplicator

使用

基本用法

import axios from 'axios';
import createAxiosDeduplicatorInstance from 'axios-deduplicator';

const axiosDeduplicator = createAxiosDeduplicatorInstance();

const axiosInstance = axios.create();
axiosInstance.interceptors.request.use(axiosDeduplicator.requestInterceptor);
axiosInstance.interceptors.response.use(
  axiosDeduplicator.responseInterceptorFulfilled,
  axiosDeduplicator.responseInterceptorRejected
);

自定义配置

import axios from 'axios';
import createAxiosDeduplicatorInstance from 'axios-deduplicator';

const axiosDeduplicator = createAxiosDeduplicatorInstance({
  repeatWindowMs: 2000,  // 两秒内不发起相同请求
  generateRequestKey(config) { // 自定义请求唯一标识
    return config.url;
  },
  skip(config, _res, error) {
    if (config?.isAllowRepeat === true) {
      return true
    }

    // http 状态码大于等于 400 时,跳过去重处理
    return error?.response?.status! >= 400
  },
  timeout: 5000
});

const axiosInstance = axios.create();
axiosInstance.interceptors.request.use(axiosDeduplicator.requestInterceptor);
axiosInstance.interceptors.response.use(
  axiosDeduplicator.responseInterceptorFulfilled,
  axiosDeduplicator.responseInterceptorRejected
);

使用 AxiosDeduplicator 类

import axios from 'axios';
import { AxiosDeduplicator } from 'axios-deduplicator';

const axiosDeduplicator = new AxiosDeduplicator();

const axiosInstance = axios.create();
axiosInstance.interceptors.request.use(axiosDeduplicator.requestInterceptor.bind(axiosDeduplicator));
axiosInstance.interceptors.response.use(
  axiosDeduplicator.responseInterceptorFulfilled.bind(axiosDeduplicator),
  axiosDeduplicator.responseInterceptorRejected.bind(axiosDeduplicator)
);

接口

createAxiosDeduplicatorInstance([options])

创建一个 AxiosDeduplicator 实例。

如果提供了 options,则它应该是一个对象,可以包含以下属性:

  • options.repeatWindowMs:一个数字,表示重复窗口的毫秒数。如果在 repeatWindowMs 毫秒内发出了相同的请求,则会直接返回相同的 Promise,不会向服务器发送实际请求。默认值为 0,这时只有在第一个请求还未响应之前,发送的相同请求才会被去重(当第一个请求响应时,直接分发响应结果给其他相同请求);

  • options.generateRequestKey(config: AxiosRequestConfig):一个函数,用于生成请求的唯一标识。默认情况下,它会使用请求的 URL、方法和数据作为唯一标识。如果您希望自定义唯一标识,可以提供此函数。它应该接受一个参数,该参数是请求的配置对象,返回一个字符串,表示请求的唯一标识;

  • options.skip(config?: AxiosRequestConfig, res?: AxiosResponse, err?: AxiosError): 一个函数,用于判断是否跳过去重。默认情况下,它会返回 false,表示不跳过去重。如果您希望自定义是否跳过去重,可以提供此函数。它应该接受三个参数,第一个参数是请求配置对象,第二个参数是请求错误对象,第三个参数是请求响应对象,返回一个布尔值,表示是否跳过去重;

  • options.timeout: 数字类型,单位毫秒,表示请求超时时间。默认值为 undefined,表示不设置超时时间。

AxiosDeduplicator

AxiosDeduplicator 类,用于去重 axios 请求。实例化时接受一个可选的 options 对象,该对象的属性与 createAxiosDeduplicatorInstanceoptions 属性相同。

许可证

MIT