import type { FetchLike } from "./search-provider.js"; export const DEFAULT_FETCH_TIMEOUT_MS = 15_000; export async function fetchWithTimeout( fetchFn: FetchLike, url: string | URL, init: RequestInit = {}, timeoutMs: number = DEFAULT_FETCH_TIMEOUT_MS, ): Promise { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), timeoutMs); try { return await fetchFn(url, { ...init, signal: controller.signal }); } catch (error) { if (error instanceof Error && error.name === "AbortError") { throw new Error(`Request timed out after ${timeoutMs}ms: ${String(url)}`); } throw error; } finally { clearTimeout(timeout); } }