import { randomUUID } from "node:crypto";
async function postIdempotent(path: string, body: unknown) {
const key = randomUUID();
for (let attempt = 0; attempt < 5; attempt++) {
const res = await fetch(`https://app.autousers.ai${path}`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.AUTOUSERS_API_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": key,
},
body: JSON.stringify(body),
});
if (res.ok) return res.json();
if (res.status >= 500 || res.status === 429) {
await new Promise((r) => setTimeout(r, 1000 * 2 ** attempt));
continue;
}
throw new Error(`HTTP ${res.status}`);
}
}