import { GaxiosError } from './common' ; export async function getRetryConfig ( err ) { let config = getConfig ( err ) ; if ( ! err || ! err . config || ( ! config && ! err . config . retry ) ) { return { shouldRetry : false } ; } config = config || { } ; config . currentRetryAttempt = config . currentRetryAttempt || 0 ; config . retry = config . retry === undefined || config . retry === null ? 3 : config . retry ; config . httpMethodsToRetry = config . httpMethodsToRetry || [ 'GET' , 'HEAD' , 'PUT' , 'OPTIONS' , 'DELETE' , ] ; config . noResponseRetries = config . noResponseRetries === undefined || config . noResponseRetries === null ? 2 : config . noResponseRetries ; const retryRanges = [ [ 100 , 199 ] , [ 429 , 429 ] , [ 500 , 599 ] , ] ; config . statusCodesToRetry = config . statusCodesToRetry || retryRanges ; err . config . retryConfig = config ; const shouldRetryFn = config . shouldRetry || shouldRetryRequest ; if ( ! ( await shouldRetryFn ( err ) ) ) { return { shouldRetry : false , config : err . config } ; } const retryDelay = config . currentRetryAttempt ? 0 : config . retryDelay ?? 100 ; const delay = retryDelay + ( ( Math . pow ( 2 , config . currentRetryAttempt ) - 1 ) / 2 ) * 1000 ; err . config . retryConfig ! . currentRetryAttempt ! += 1 ; const backoff = new Promise ( resolve => { setTimeout ( resolve , delay ) ; } ) ; if ( config . onRetryAttempt ) { config . onRetryAttempt ( err ) ; } await backoff ; return { shouldRetry : true , config : err . config } ; } param err function shouldRetryRequest ( err ) { const config = getConfig ( err ) ; if ( err . name === 'AbortError' ) { return false ; } if ( ! config || config . retry === 0 ) { return false ; } if ( ! err . response && ( config . currentRetryAttempt || 0 ) >= config . noResponseRetries ! ) { return false ; } if ( ! err . config . method || config . httpMethodsToRetry ! . indexOf ( err . config . method . toUpperCase ( ) ) < 0 ) { return false ; } if ( err . response && err . response . status ) { let isInRange = false ; for ( const [ min , max ] of config . statusCodesToRetry ! ) { const status = err . response . status ; if ( status >= min && status <= max ) { isInRange = true ; break ; } } if ( ! isInRange ) { return false ; } } config . currentRetryAttempt = config . currentRetryAttempt || 0 ; if ( config . currentRetryAttempt >= config . retry ! ) { return false ; } return true ; } param err function getConfig ( err ) { if ( err && err . config && err . config . retryConfig ) { return err . config . retryConfig ; } return ; }