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-interceptor

v3.0.19

Published

axios interceptor can implement features such as response data caching, deduplication of identical requests, adding timestamps to get requests, re-initiating failed requests, and listening for changes in the queue of pending requests.

Downloads

950

Readme

axios-interceptor

Project setup

npm install axios-interceptor

Example

useInterceptor

const selfAxios = axios.create()
const {
  // A method for re-initiating failed requests.
  doRetry
} = useInterceptor({
  axios: selfAxios,
  // A method for obtaining the unique value of the request, used to determine whether it is the same request in the request interceptor and the response interceptor. useCache relies on it to get the data in the cache, useDebounce relies on it to determine whether it is the same request, whether it needs to be merged.
  // It provides an [internal getKey](https://github.com/czb3279338858/axios-interceptor/blob/3.0/lib/getKey.ts).
  getKey: (config: InternalAxiosRequestConfig) => JSON.stringify(config),
  // Using the data caching feature, data is only cached when isSuccess returns true.
  useCache: {
    isSuccess: (value: AxiosResponse) => value.status >= 200 && value.status < 300
  },
  // Using the request deduplication feature, when a request has not returned, but is initiated again elsewhere, they will be merged into one.
  // But be aware that some interfaces cannot be deduplicated, such as the interface for obtaining the unique id of the uploaded file.
  useDebounce: true,
  // Add a timestamp parameter to the get request.
  useTimestamp: {
    timestampKey: 'timestamp'
  },
  // When a request fails, it is allowed to re-initiate the request through the doRetry method. The return of isRetry determines whether the request needs to be added to the re-initiation queue.
  useRetry: {
    isRetry: (err: AxiosError) => !!err.response?.status && err.response.status >= 500 && err.response.status < 600
  },
  // The requestListChange method is called when the queue of pending requests changes, and the queue is passed as a parameter. For example, after a request is responded, the number of requests in the queue decreases by 1, and the requestListChange method is triggered.
  useChange: {
    requestListChange: (configs: AxiosRequestConfig[]) => console.log(configs)
  }
})

useCache

async function getUserInfo() {
  const { 
    data,
    // Requests with the _cache parameter will additionally return a _delCacheFun in the response to delete this cache.
    // This is useful for limiting the lifetime of the cache. For example, a cache for an interface is only valid on the current page, and when the page is destroyed, the cache should also be destroyed.
    _delCacheFun
  } = await selfAxios.get('https://.../getUserInfo', {
    // This request needs to cache the response data.
    _cache: true
  })
}

const userInfo = await getUserInfo()
// It additionally returns the _delCache method, which is used to delete the data cached by the getUserInfo request, commonly used for manually deleting data that has a certain lifespan.
userInfo._delCache()
async function postUserInfo() {
  const res = await selfAxios.get('https://.../postUserInfo', {
    // Updated the user data, and deleted the cache of the possible user data retrieval interface in the cacheMap.
    _delCache(cacheMap: CacheMap){
      for (const [key, value] of cacheMap) {
        if (value.config.url?.includes('https://.../getUserInfo')) {
          cacheMap.delete(key)
          break
        }
      }
    }
  })
}

useDebounce

async function getUserInfo() {
  const res = await selfAxios.get('https://.../getUserInfo', {
    // This request does not need to be deduplicated, such as the interface for obtaining a unique id.
    _noDebounce: true,
  })
}