@ -1,49 +1,58 @@
import axios from 'axios' ;
import axios from "axios" ;
interface PlanResponse {
interface PlanResponse {
data? : any ;
data? : unknown ;
status? : number ;
status? : number ;
error? : string ;
error? : string ;
}
}
export const loadPlanWithRetry = async ( handle : string , retries = 3 ) : Promise < PlanResponse > = > {
export const loadPlanWithRetry = async (
handle : string ,
retries = 3 ,
) : Promise < PlanResponse > = > {
try {
try {
console . log ( ` [Plan Service] Loading plan ${ handle } , attempt 1/ ${ retries } ` ) ;
console . log ( ` [Plan Service] Loading plan ${ handle } , attempt 1/ ${ retries } ` ) ;
console . log ( ` [Plan Service] Context: Deep link handle= ${ handle } , isClaimFlow= ${ handle . includes ( 'claim' ) } ` ) ;
console . log (
` [Plan Service] Context: Deep link handle= ${ handle } , isClaimFlow= ${ handle . includes ( "claim" ) } ` ,
) ;
// Different endpoint if this is a claim flow
// Different endpoint if this is a claim flow
const response = await loadPlan ( handle ) ;
const response = await loadPlan ( handle ) ;
console . log ( ` [Plan Service] Plan ${ handle } loaded successfully: ` , {
console . log ( ` [Plan Service] Plan ${ handle } loaded successfully: ` , {
status : response?.status ,
status : response?.status ,
headers : response?.headers ,
headers : response?.headers ,
data : response?.data
data : response?.data ,
} ) ;
} ) ;
return response ;
return response ;
} catch ( error : any ) {
} catch ( error : unknown ) {
console . error ( ` [Plan Service] Error loading plan ${ handle } : ` , {
console . error ( ` [Plan Service] Error loading plan ${ handle } : ` , {
message : error.message ,
message : ( error as Error ) . message ,
status : error.response?.status ,
status : ( error as { response ? : { status? : number } } ) ? . response ? . status ,
statusText : error.response?.statusText ,
statusText : ( error as { response ? : { statusText? : string } } ) ? . response
data : error.response?.data ,
? . statusText ,
headers : error.response?.headers ,
data : ( error as { response ? : { data? : unknown } } ) ? . response ? . data ,
headers : ( error as { response ? : { headers? : unknown } } ) ? . response
? . headers ,
config : {
config : {
url : error.config?.url ,
url : ( error as { config ? : { url? : string } } ) ? . config ? . url ,
method : error.config?. method ,
method : ( error as { config ? : { method? : string } } ) ? . config ? . method ,
baseURL : error.config?. baseURL ,
baseURL : ( error as { config ? : { baseURL? : string } } ) ? . config ? . baseURL ,
headers : error.config?.headers
headers : ( error as { config ? : { headers? : unknown } } ) ? . config ? . headers ,
}
} ,
} ) ;
} ) ;
if ( retries > 1 ) {
if ( retries > 1 ) {
console . log ( ` [Plan Service] Retrying plan ${ handle } , ${ retries - 1 } attempts remaining ` ) ;
console . log (
await new Promise ( resolve = > setTimeout ( resolve , 1000 ) ) ;
` [Plan Service] Retrying plan ${ handle } , ${ retries - 1 } attempts remaining ` ,
) ;
await new Promise ( ( resolve ) = > setTimeout ( resolve , 1000 ) ) ;
return loadPlanWithRetry ( handle , retries - 1 ) ;
return loadPlanWithRetry ( handle , retries - 1 ) ;
}
}
return {
return {
error : ` Failed to load plan ${ handle } after ${ 4 - retries } attempts: ${ error . message } ` ,
error : ` Failed to load plan ${ handle } after ${ 4 - retries } attempts: ${ ( error as Error ) . message } ` ,
status : error.response?.status
status : ( error as { response ? : { status? : number } } ) ? . response ? . status ,
} ;
} ;
}
}
} ;
} ;
@ -51,7 +60,7 @@ export const loadPlanWithRetry = async (handle: string, retries = 3): Promise<Pl
export const loadPlan = async ( handle : string ) : Promise < PlanResponse > = > {
export const loadPlan = async ( handle : string ) : Promise < PlanResponse > = > {
console . log ( ` [Plan Service] Making API request for plan ${ handle } ` ) ;
console . log ( ` [Plan Service] Making API request for plan ${ handle } ` ) ;
const endpoint = handle . includes ( 'claim' )
const endpoint = handle . includes ( "claim" )
? ` /api/claims/ ${ handle } `
? ` /api/claims/ ${ handle } `
: ` /api/plans/ ${ handle } ` ;
: ` /api/plans/ ${ handle } ` ;
@ -60,11 +69,11 @@ export const loadPlan = async (handle: string): Promise<PlanResponse> => {
try {
try {
const response = await axios . get ( endpoint ) ;
const response = await axios . get ( endpoint ) ;
return response ;
return response ;
} catch ( error : any ) {
} catch ( error : unknown ) {
console . error ( ` [Plan Service] API request failed for ${ handle } : ` , {
console . error ( ` [Plan Service] API request failed for ${ handle } : ` , {
endpoint ,
endpoint ,
error : error. message ,
error : ( error as Error ) . message ,
response : error.response?.data
response : ( error as { response ? : { data? : unknown } } ) ? . response ? . data ,
} ) ;
} ) ;
throw error ;
throw error ;
}
}