Fetch Extras
Fetch Extras

Useful utilities for working with Fetch.

fetch-extras is a collection of utilities designed to enhance the standard Fetch API by allowing developers to build focused, composable HTTP clients without introducing new interfaces or wrapper objects.

The principal functionalities of this tool include:

1. Composable "with" Wrapper System*

The library uses a modular approach where each functionality is added via a with* function. These can be stacked using a pipeline utility to build a custom client tailored to specific needs. Key wrappers include:

  • Request Configuration: Utilities for setting a Base URL, default headers, and search parameters.

  • Automatic JSON Handling: Features for auto-stringifying request bodies and parsing JSON responses.

  • Timeouts and Retries: Support for aborting requests that take too long and automatically retrying failed requests with exponential backoff.

  • Authentication: A withTokenRefresh utility to handle automatic auth token refreshing on 401 errors.

2. Performance and Resource Management

  • Control Mechanisms: Includes client-side rate limiting, concurrency capping, and request deduplication (collapsing identical GET requests into one).

  • Caching: Built-in in-memory caching for unconditional GET responses with a configurable Time-To-Live (TTL).

  • Progress Tracking: Specialized hooks to track both download and upload progress.

3. Advanced Data Validation and Utilities

  • Schema Validation: Allows for validating responses against Standard Schemas like Zod, Valibot, or ArkType.

  • Pagination: A paginate utility for easily async-iterating over paginated API endpoints.

  • Lifecycle Hooks: Provides beforeRequest and afterResponse hooks for custom logic at different stages of the request.

4. Developer Experience and Compatibility

  • Zero Dependencies: A lightweight tool with no external dependencies that is fully tree-shakeable, meaning only the utilities you import are included in your final bundle.

  • Cross-Platform: Works in all modern environments, including browsers, Node.js, Deno, Bun, and Cloudflare Workers.

  • TypeScript Support: Offers full type definitions with strong generics for better type safety.