You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
816 B

2 years ago
import { AxiosError, AxiosInstance } from 'axios'
/**
*
*/
export class AxiosRetry {
/**
*
*/
retry(axiosInstance: AxiosInstance, error: AxiosError) {
// @ts-ignore
const { config } = error.response
const { waitTime, count } = config?.requestOptions?.retryRequest
config.__retryCount = config.__retryCount || 0
if (config.__retryCount >= count) {
return Promise.reject(error)
}
config.__retryCount += 1
//请求返回后config的header不正确造成重试请求失败,删除返回headers采用默认headers
delete config.headers
return this.delay(waitTime).then(() => axiosInstance(config))
}
/**
*
*/
private delay(waitTime: number) {
return new Promise((resolve) => setTimeout(resolve, waitTime))
}
}