Getting Started with Typed Fetch
Typed Fetch is a type-safe HTTP client for TypeScript that never throws exceptions. Inspired by Go's error handling pattern, it's built on top of the native Fetch API.
What the Project Exports
typedFetch
: The main function for making HTTP requests. It has the same signature as the nativefetch
API (accepting a URL and optional RequestInit object), but returns a structured object with either a response or an error, never throwing exceptions.- Individual error classes: Specific error classes exported individually for optimal tree-shaking, covering different HTTP status codes and network errors.
Advantages of the Approach
- Never Throws: Errors are returned as values, making handling explicit and predictable.
- Type-Safe: Full TypeScript support for responses and errors.
- Explicit Error Handling: Follows Go's philosophy where errors are values, not exceptions.
- Built on Native Fetch: Minimal overhead with familiar API.
- Specific Error Classes: Easy to handle different HTTP errors distinctly.
For installation and basic usage, see the next sections.