async function safeApiCall(requestData, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const response = await fetch("/api/ult", {
method: "POST",
headers: {
"x-api-key": "your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify(requestData),
});
if (response.ok) {
return await response.json();
}
if (response.status === 429 && attempt < maxRetries) {
// Exponential backoff for rate limits
await sleep(2 ** attempt * 1000);
continue;
}
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
} catch (error) {
if (attempt === maxRetries) throw error;
await sleep(1000 * attempt); // Simple backoff
}
}
}