import type { AxiosError, AxiosInstance } from 'axios'

/**
 *  请求重试机制
 */

export class AxiosRetry {
    /**
     * 重试
     */
    retry(AxiosInstance: AxiosInstance, error: AxiosError) {
        const { config } = error.response
        // @ts-expect-error
        const { waitTime, count } = config?.requestOptions?.retryRequest
        // @ts-expect-error
        config.__retryCount = config.__retryCount || 0
        // @ts-expect-error
        if (config.__retryCount >= count) {
            return Promise.reject(error)
        }
        // @ts-expect-error
        config.__retryCount += 1
        return this.delay(waitTime).then(() => AxiosInstance(config))
    }

    /**
     * 延迟
     */
    private delay(waitTime: number) {
        return new Promise((resolve) => setTimeout(resolve, waitTime))
    }
}
