async function submitWithRetry(formData, maxAttempts = 3) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
const response = await fetch('https://api.omnifence.ai/api/v1/moderate/image', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.OMNIFENCE_API_KEY}` },
body: formData,
});
if (response.ok) return response.json();
const error = await response.json();
// Don't retry permanent errors
if (response.status < 500) throw new Error(error.message);
// Exponential backoff for server errors
if (attempt < maxAttempts) {
await new Promise((resolve) => setTimeout(resolve, 1000 * 2 ** attempt));
}
}
throw new Error('Max retry attempts reached');
}