0.1.6 • Published 12 months ago

axios-enhance-adapter v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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;
      },
    })
  )
);
0.1.6

12 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.0.13

12 months ago

0.0.12

12 months ago

0.0.11

12 months ago

0.0.10

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago