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
796 B

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