Files
cosmos-explorer/src/Utils/FetchWithTimeout.ts
sunghyunkang1111 5b7d1a74af Added health metrics for application load and database load (#2257)
* Added health metrics for application load

* Added health metrics for application load

* Fix unit tests

* Added more metrics

* Added few comments

* Added DatabaseLoad Scenario and address comments

* Fix unit tests

* fix unit tests

* Fix unit tests

* fix unit tests

* fix the mock

* Fix unit tests
2025-12-09 14:14:35 -06:00

38 lines
1.1 KiB
TypeScript

/**
* Perform a fetch with an AbortController-based timeout. Returns the Response or throws (including AbortError on timeout).
*
* Usage: await fetchWithTimeout(url, { method: 'GET', headers: {...} }, 10000);
*
* A shared helper to remove duplicated inline implementations across the codebase.
*/
export async function fetchWithTimeout(
url: string,
init: RequestInit = {},
timeoutMs: number = 5000,
): Promise<Response> {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeoutMs);
try {
const response = await fetch(url, { ...init, signal: controller.signal });
return response;
} finally {
clearTimeout(id);
}
}
/**
* Convenience wrapper that returns null instead of throwing on timeout / network error.
* Useful for feature probing scenarios where failure should be treated as absence.
*/
export async function tryFetchWithTimeout(
url: string,
init: RequestInit = {},
timeoutMs: number = 5000,
): Promise<Response | null> {
try {
return await fetchWithTimeout(url, init, timeoutMs);
} catch {
return null;
}
}