|
| 1 | +export class FetchError extends Error { |
| 2 | + statusCode?: number |
| 3 | + code?: string |
| 4 | + |
| 5 | + constructor(message: string, statusCode?: number, code?: string) { |
| 6 | + super(message) |
| 7 | + this.name = 'FetchError' |
| 8 | + this.statusCode = statusCode |
| 9 | + this.code = code |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +interface ApiError { |
| 14 | + error?: string |
| 15 | + code?: string |
| 16 | + message?: string |
| 17 | +} |
| 18 | + |
| 19 | +interface FetchWrapperOptions extends RequestInit { |
| 20 | + timeout?: number |
| 21 | + params?: Record<string, string | number | boolean | undefined | unknown> |
| 22 | +} |
| 23 | + |
| 24 | +async function handleResponse(response: Response): Promise<never> { |
| 25 | + const data: ApiError = await response.json().catch(() => ({ error: 'An error occurred' })) |
| 26 | + throw new FetchError(data.error || data.message || 'Request failed', response.status, data.code) |
| 27 | +} |
| 28 | + |
| 29 | +async function fetchWrapper<T = unknown>( |
| 30 | + url: string, |
| 31 | + options: FetchWrapperOptions = {} |
| 32 | +): Promise<T> { |
| 33 | + const { timeout = 30000, params, ...fetchOptions } = options |
| 34 | + |
| 35 | + const urlObj = new URL(url, window.location.origin) |
| 36 | + if (params) { |
| 37 | + Object.entries(params).forEach(([key, value]) => { |
| 38 | + if (value !== undefined) { |
| 39 | + urlObj.searchParams.append(key, String(value)) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + const controller = new AbortController() |
| 45 | + const timeoutId = setTimeout(() => controller.abort(), timeout) |
| 46 | + |
| 47 | + try { |
| 48 | + const response = await fetch(urlObj.toString(), { |
| 49 | + ...fetchOptions, |
| 50 | + signal: controller.signal, |
| 51 | + }) |
| 52 | + |
| 53 | + clearTimeout(timeoutId) |
| 54 | + |
| 55 | + if (!response.ok) { |
| 56 | + await handleResponse(response) |
| 57 | + } |
| 58 | + |
| 59 | + return response.json() |
| 60 | + } catch (error) { |
| 61 | + clearTimeout(timeoutId) |
| 62 | + if (error instanceof Error && error.name === 'AbortError') { |
| 63 | + throw new FetchError('Request timeout', 408, 'TIMEOUT') |
| 64 | + } |
| 65 | + throw error |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +async function fetchWrapperBlob( |
| 70 | + url: string, |
| 71 | + options: FetchWrapperOptions = {} |
| 72 | +): Promise<Blob> { |
| 73 | + const { timeout = 30000, params, ...fetchOptions } = options |
| 74 | + |
| 75 | + const urlObj = new URL(url, window.location.origin) |
| 76 | + if (params) { |
| 77 | + Object.entries(params).forEach(([key, value]) => { |
| 78 | + if (value !== undefined) { |
| 79 | + urlObj.searchParams.append(key, String(value)) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + const controller = new AbortController() |
| 85 | + const timeoutId = setTimeout(() => controller.abort(), timeout) |
| 86 | + |
| 87 | + try { |
| 88 | + const response = await fetch(urlObj.toString(), { |
| 89 | + ...fetchOptions, |
| 90 | + signal: controller.signal, |
| 91 | + }) |
| 92 | + |
| 93 | + clearTimeout(timeoutId) |
| 94 | + |
| 95 | + if (!response.ok) { |
| 96 | + await handleResponse(response) |
| 97 | + } |
| 98 | + |
| 99 | + return response.blob() |
| 100 | + } catch (error) { |
| 101 | + clearTimeout(timeoutId) |
| 102 | + if (error instanceof Error && error.name === 'AbortError') { |
| 103 | + throw new FetchError('Request timeout', 408, 'TIMEOUT') |
| 104 | + } |
| 105 | + throw error |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export { fetchWrapper, fetchWrapperBlob } |
0 commit comments