Overview
ThebaseFetcher function provides a type-safe abstraction layer over the native Fetch API, handling common patterns like error management, JSON parsing, and network failures automatically.
Function Signature
Parameters
The URL to fetch from
Extended RequestInit options with typed headers
Return Value
Returns a promise that resolves to the typed response data. The function automatically parses JSON responses and returns
null for empty responses (204 No Content).Error Handling
The fetcher throws two types of errors:Thrown for HTTP errors (4xx, 5xx status codes). See Error Handling for details.
Thrown for network failures, CORS issues, or timeouts. See Error Handling for details.
Implementation Details
Response Handling
The fetcher implements intelligent response parsing:HTTP Error Processing
For failed requests (status 4xx/5xx), the fetcher attempts to extract error messages from the response body:The fetcher attempts to parse error messages from
message or error fields in the response body, falling back to the HTTP status text if parsing fails.Network Error Detection
Network-level failures are caught and wrapped:Usage Examples
Basic GET Request
POST Request with Headers
Error Handling Pattern
Related
- Error Handling - Error types and patterns
- Caching - Server-side cache integration