Useful utilities for working with Fetch
Great for creating tiny custom HTTP clients without a heavy dependency. Fully tree-shakeable.
For a full-featured HTTP client on top of Fetch, check out my ky package.
Install
npm install fetch-extras
Usage
import {
pipeline,
withTimeout,
withBaseUrl,
withHeaders,
withHttpError
} from 'fetch-extras';
const apiFetch = pipeline(
fetch,
f => withTimeout(f, 5000),
f => withBaseUrl(f, 'https://api.example.com'),
f => withHeaders(f, {Authorization: 'Bearer token'}),
withHttpError,
);
const response = await apiFetch('/users');
const data = await response.json();
API
The with* functions are listed in the recommended wrapping order for use with pipeline.
Related
- is-network-error - Check if a value is a Fetch network error
- ky - HTTP client based on Fetch
- parse-sse - Parse Server-Sent Events (SSE) from a Response