Typed fetch with timeout
TypeScript
export async function fetchJson<T>(
url: string,
{ timeout = 8000, ...init }: RequestInit & { timeout?: number } = {},
): Promise<T> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, { ...init, signal: controller.signal });
if (!response.ok) {
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
}
return (await response.json()) as T;
} finally {
clearTimeout(timer);
}
}