import { HttpRequestOptions, HttpResponse, withRetry } from "./imports"; export async function fetchRequest( method: string, url: string, body?: any, options: HttpRequestOptions = {} ): Promise> { const exec = async () => { const controller = new AbortController(); const timeout = options.timeout ?? 8000; const timer = setTimeout(() => controller.abort(), timeout); const res = await fetch(url, { method, headers: { "Content-Type": "application/json", ...(options.headers || {}) }, body: body ? JSON.stringify(body) : undefined, signal: controller.signal }); clearTimeout(timer); // consistent with axios const data = options.json === false ? await res.text() : await res.json().catch(() => null); if (!res.ok) { throw new Error(`Fetch error: ${res.status} ${res.statusText}`); } return { status: res.status, data, headers: {} }; }; return withRetry(exec, options.retry); }