(sourceId: string)
| 36 | } |
| 37 | |
| 38 | export async function fetchSource(sourceId: string): Promise<DataSource> { |
| 39 | try { |
| 40 | // Validate sourceId to prevent path traversal attacks |
| 41 | if (!sourceId || sourceId.trim() === '') { |
| 42 | throw new ApiError('Source ID cannot be empty', 400); |
| 43 | } |
| 44 | if (sourceId.includes('/') || sourceId.includes('..')) { |
| 45 | throw new ApiError('Invalid source ID format', 400); |
| 46 | } |
| 47 | |
| 48 | const response = await fetch(`${API_BASE}/sources/${encodeURIComponent(sourceId)}`); |
| 49 | |
| 50 | if (!response.ok) { |
| 51 | const errorMessage = await parseJsonResponse<{ error: string }>(response) |
| 52 | .then((data) => data.error) |
| 53 | .catch(() => response.statusText); |
| 54 | |
| 55 | throw new ApiError( |
| 56 | response.status === 404 ? `Source not found: ${sourceId}` : `Failed to fetch source: ${errorMessage}`, |
| 57 | response.status, |
| 58 | response.statusText |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | return response.json(); |
| 63 | } catch (err) { |
| 64 | // Ensure all errors are ApiError instances |
| 65 | if (err instanceof ApiError) { |
| 66 | throw err; |
| 67 | } |
| 68 | // Network errors, timeout, etc. |
| 69 | throw new ApiError(err instanceof Error ? err.message : 'Network error', 0); |
| 70 | } |
| 71 | } |
no outgoing calls
no test coverage detected