import fetch from 'node-fetch'
async function postWithRetry(url, body, { maxAttempts = 5 } = {}) {
let attempt = 0
while (attempt < maxAttempts) {
attempt++
const idempotencyKey = `retry-${Date.now()}-${attempt}`
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.BLOOIO_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey
},
body: JSON.stringify(body)
})
if (res.ok) return res.json()
if (res.status < 500) throw new Error(`Non-retryable: ${res.status}`)
await new Promise(r => setTimeout(r, Math.min(1000 * 2 ** attempt, 10000)))
}
throw new Error('Max retry attempts reached')
}