axios-enhance-adapter
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

Test and Release npm version min size install size license author

axios enhance adapter

Note: Currently, only support axios <= v0.27.2

Features:

  • Avoid repeat requests at same time
  • Error retry

Usage

import { getEnhanceAdapter } from 'axios-enhance-adapter';
import axios, { AxiosRequestConfig } from 'axios';

const axiosInstance = axios.create({
  baseURL: `http://127.0.0.1:${port}`,
  adapter: getEnhanceAdapter(defaultOptions),
});
const defaultOptions = {
  shouldRetryOnError: (err) => {
    return true;
  },
  errorRetryInterval: 3000,
  errorRetryCount: 3,
  checkEnable(config: AxiosRequestConfig) {
    const method = config.method?.toLowerCase();
    const isGet = method === 'get';
    return isGet;
  },
  getKey(config: AxiosRequestConfig) {
    const { method, data, params, url } = config;
    const arr = [method, url];
    if (data) {
      arr.push(JSON.stringify(data));
    }
    if (params) {
      arr.push(JSON.stringify(params));
    }
    return arr.join(',');
  },
};

// only one will send
await Promise.all([1, 2, 3, 4, 5].map((item) => axiosInstance.get('/')));

// disable repeat requests filter and error retry
await Promise.all(
  [1, 2, 3, 4, 5].map((item) =>
    axiosInstance.get('/', {
      checkEnable: () => false,
    })
  )
);

// only disable error retry
await Promise.all(
  [1, 2, 3, 4, 5].map((item) =>
    axiosInstance.get('/', {
      shouldRetryOnError: (err) => {
        if (err.status === 401 || err.status === 403) {
          return false;
        }
        return true;
      },
    })
  )
);

Readme

Keywords

Package Sidebar

Install

npm i axios-enhance-adapter

Weekly Downloads

14

Version

0.1.6

License

MIT

Unpacked Size

19.8 kB

Total Files

6

Last publish

Collaborators

  • suhaotian