( url: string, timeout: number, )
| 38 | } |
| 39 | |
| 40 | export async function fetchWithTimeout( |
| 41 | url: string, |
| 42 | timeout: number, |
| 43 | ): Promise<Response> { |
| 44 | const controller = new AbortController(); |
| 45 | const timeoutId = setTimeout(() => controller.abort(), timeout); |
| 46 | |
| 47 | try { |
| 48 | const response = await fetch(url, { signal: controller.signal }); |
| 49 | return response; |
| 50 | } catch (error) { |
| 51 | if (isNodeError(error) && error.code === 'ABORT_ERR') { |
| 52 | throw new FetchError(`Request timed out after ${timeout}ms`, 'ETIMEDOUT'); |
| 53 | } |
| 54 | throw new FetchError(getErrorMessage(error)); |
| 55 | } finally { |
| 56 | clearTimeout(timeoutId); |
| 57 | } |
| 58 | } |
no test coverage detected