import React from 'react' ; import { getUserManager , signinSilent } from '@axa-fr/react-oidc-core' ; import { User } from 'oidc-client' ; type TrySigninSilent = ( args ? : any ) => Promise < User > ; const fetchWithSilentAuthenticateAndRetryOn401 = ( fetch , trySilentAuthenticateInjected ) => async ( url , options = { method : 'GET' } , isRetry = true ) : Promise < Response > => { const response = await fetch ( url , options ) ; if ( isRetry ) { if ( response . status === 401 ) { await trySilentAuthenticateInjected ( ) ; return fetchWithSilentAuthenticateAndRetryOn401 ( fetch , trySilentAuthenticateInjected ) ( url , options , false ) ; } } return response ; } ; export const wrapAuthenticating = ( fetch , trySilentAuthenticateInjected ) => ( WrappedComponent ) => ( props ) => { const previousFetch = fetch || props . fetch ; const newFetch = fetchWithSilentAuthenticateAndRetryOn401 ( previousFetch , trySilentAuthenticateInjected ) ; return < WrappedComponent { ... props } fetch = { newFetch } / > ; } ; const withFetchSilentAuthenticateAndRetryOn401 = ( fetch = undefined ) => wrapAuthenticating ( fetch , signinSilent ( getUserManager ) ) ; export default withFetchSilentAuthenticateAndRetryOn401 ;