Giter Site home page Giter Site logo

suhaotian / axios-enhance-adapter Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 404 KB

(Deprecated, use xior, link below)axios error retry and avoid repeat requests.

Home Page: https://www.npmjs.com/package/xior

License: MIT License

Shell 0.22% JavaScript 20.68% TypeScript 75.56% CSS 3.53%
adapter axios

axios-enhance-adapter's Introduction

Test and Release npm version min size install size license author

Depretated! axios enhance adapter

Depretated! Recommend use xior: https://www.npmjs.com/package/xior

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

axios-enhance-adapter's People

Contributors

suhaotian avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.