You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

71 lines
2.1 KiB

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