import type { BaseQueryEnhancer } from './baseQueryTypes' ; import { HandledError } from './HandledError' ; remarks param attempt param maxRetries async function defaultBackoff ( attempt = 0 , maxRetries = 5 ) { const attempts = Math . min ( attempt , maxRetries ) ; const timeout = ~ ~ ( ( Math . random ( ) + 0.4 ) * ( 300 << attempts ) ) ; await new Promise ( ( resolve ) => setTimeout ( ( res ) => resolve ( res ) , timeout ) ) ; } export interface RetryOptions { /**
     * How many times the query will be retried (default: 5)
     */ maxRetries ? : number ; /**
     * Function used to determine delay between retries
     */ backoff ? : ( attempt : number , maxRetries : number ) => Promise < void > ; } function fail ( e ) { throw Object . assign ( new HandledError ( { error : e } ) , { throwImmediately : true , } ) ; } const retryWithBackoff = ( baseQuery , defaultOptions ) => async ( args , api , extraOptions ) => { const options = { maxRetries : 5 , backoff : defaultBackoff , ... defaultOptions , ... extraOptions , } ; let retry = 0 ; while ( true ) { try { const result = await baseQuery ( args , api , extraOptions ) ; if ( result . error ) { throw new HandledError ( result ) ; } return result ; } catch ( e ) { retry ++ ; if ( e . throwImmediately || retry > options . maxRetries ) { if ( e instanceof HandledError ) { return e . value ; } throw e ; } await options . backoff ( retry , options . maxRetries ) ; } } } ; example export const retry = Object . assign ( retryWithBackoff , { fail } ) ;