1.1.5 • Published 2 years ago

axios-encapsulation v1.1.5

Weekly downloads
4
License
ISC
Repository
github
Last release
2 years ago

axios-encapsulation

Axios plugin that intercepts failed requests and retries them whenever possible.

Installation

npm install axios-encapsulation

Usage

import Encapsulation from "axios-encapsulation";
import { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios/index.d";

const transformData = function (res: AxiosResponse) {
  if (res.data.code === 200) {
    return res.data.data !== undefined ? res.data.data : res.data.result;
  }
  return Promise.reject(res.data);
};

const addStampToken = function (config: AxiosRequestConfig) {
  const token = getToken();
  if (token) {
    set(config, "headers.common", { token });
  }
  if (config.method === "get") {
    config.params = {
      ...config.params,
      stamp: Math.random(),
    };
  }
  return config;
};

const encapsulationInstance = new Encapsulation({
  retry: {
    retryDelay: (retryCount: number) => {
      return retryCount * 1000;
    },
    shouldResetTimeout: true,
    retryCondition: (error: AxiosError) => {
      return error.config.method === "get" || error.config.method === "post";
    },
  },
  request: [addStampToken],
  response: [transformData],
});
const { Axios } = encapsulationInstance;
const { get, post } = Axios;

1.1.5

修复 detectDuplicateRequests

1.1.5

2 years ago

1.1.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago