import * as React from 'react' ; import { API_CUSTOM_ERROR_CODES } from '@proton/shared/lib/errors' ; import { getVerificationHeaders } from '@proton/shared/lib/fetch/headers' ; import { Api , HumanVerificationMethodType } from '@proton/shared/lib/interfaces' ; import { HumanVerificationModal } from '@proton/components' ; interface ExtraArguments { api : Api ; createModal : any ; onToken : ( token : string , tokenType : HumanVerificationMethodType ) => void ; verificationToken ? : string ; verificationTokenType ? : HumanVerificationMethodType ; } /**
 * Special human api handling for the signup since the human verification code needs to be triggered and included
 * in possibly multiple api requests.
 */ const humanApiHelper = < T > ( config , { api , createModal , verificationToken , verificationTokenType , onToken } ) : Promise < T > => { return api < T > ( { silence : [ API_CUSTOM_ERROR_CODES . HUMAN_VERIFICATION_REQUIRED ] , ... config , headers : { ... config . headers , ... getVerificationHeaders ( verificationToken , verificationTokenType ) , } , ignoreHandler : [ API_CUSTOM_ERROR_CODES . HUMAN_VERIFICATION_REQUIRED ] , } ) . catch ( ( error ) => { if ( error . data ?. Code !== API_CUSTOM_ERROR_CODES . HUMAN_VERIFICATION_REQUIRED || config . ignoreHandler ?. includes ( API_CUSTOM_ERROR_CODES . HUMAN_VERIFICATION_REQUIRED ) ) { throw error ; } const onVerify = ( token , tokenType ) : Promise < T > => { return api < T > ( { ... config , headers : { ... config . headers , ... getVerificationHeaders ( token , tokenType ) , } , ignoreHandler : [ API_CUSTOM_ERROR_CODES . HUMAN_VERIFICATION_REQUIRED ] , silence : [ API_CUSTOM_ERROR_CODES . TOKEN_INVALID ] , } ) . then ( ( result ) => { onToken ( token , tokenType ) ; return result ; } ) . catch ( ( error ) => { const Code = error ?. data ?. Code ; if ( Code && Code !== API_CUSTOM_ERROR_CODES . TOKEN_INVALID ) { onToken ( token , tokenType ) ; } throw error ; } ) ; } ; const handleVerification = ( { token , methods , onVerify } ) : Promise < T > => { return new Promise ( ( resolve , reject ) => { createModal ( < HumanVerificationModal < T > token = { token } methods = { methods } onVerify = { onVerify } onSuccess = { resolve } onError = { reject } onClose = { ( ) => reject ( error ) } />); } ) ; } ; const { Details : { HumanVerificationToken = '' , HumanVerificationMethods : methods = [ ] } = { } } = error . data || { } ; return handleVerification ( { token : HumanVerificationToken , methods , onVerify } ) ; } ) ; } ; const createHumanApi = ( { api , createModal } ) => { let verificationsTokens ; const clearToken = ( ) => { verificationsTokens = undefined ; } ; const setToken = ( token , tokenType ) => { verificationsTokens = { verificationToken : token , verificationTokenType : tokenType , } ; } ; const humanApiCaller = < T > ( config ) => humanApiHelper < T > ( config , { api , createModal , ... verificationsTokens , onToken : setToken , } ) ; return { api : humanApiCaller , setToken , clearToken , getTokenType : ( ) => verificationsTokens ?. verificationTokenType , hasToken : ( ) => verificationsTokens !== undefined , } ; } ; export type HumanApi = ReturnType < typeof createHumanApi > ; export default createHumanApi ;