| 4 | const API_BASE = '/api'; |
| 5 | |
| 6 | export async function fetchRequests(sourceId?: string): Promise<RequestsResponse> { |
| 7 | try { |
| 8 | const url = sourceId |
| 9 | ? `${API_BASE}/requests?source_id=${encodeURIComponent(sourceId)}` |
| 10 | : `${API_BASE}/requests`; |
| 11 | |
| 12 | const response = await fetch(url); |
| 13 | |
| 14 | if (!response.ok) { |
| 15 | const errorMessage = await response |
| 16 | .json() |
| 17 | .then((data) => data.error) |
| 18 | .catch(() => response.statusText); |
| 19 | throw new ApiError(`Failed to fetch requests: ${errorMessage}`, response.status, response.statusText); |
| 20 | } |
| 21 | |
| 22 | return response.json(); |
| 23 | } catch (err) { |
| 24 | if (err instanceof ApiError) { |
| 25 | throw err; |
| 26 | } |
| 27 | throw new ApiError(err instanceof Error ? err.message : 'Network error', 0); |
| 28 | } |
| 29 | } |