The Principal Dev – Masterclass for Tech Leads

The Principal Dev – Masterclass for Tech Leads28-29 May

Join

fetch-extras logo

Useful utilities for working with Fetch

Build tiny, focused HTTP clients by composing only the features you need on top of the standard fetch API. No wrapper objects, no new interface to learn, no lock-in.

Highlights

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,
	withJsonResponse,
} from 'fetch-extras';

// Create a tiny reusable API client that:
// - Times out after 5 seconds
// - Uses a base URL so you only write paths
// - Sends auth headers on every request
// - Throws errors for non-2xx responses
// - Parses JSON responses automatically
const apiFetch = pipeline(
	fetch,
	withTimeout(5000),
	withBaseUrl('https://api.example.com'),
	withHeaders({Authorization: 'Bearer token'}),
	withHttpError(),
	withJsonResponse(),
);

const data = await apiFetch('/users');

pipeline() order is the documented order throughout this package. Runtime wrapper nesting is the inverse, so pipeline(fetch, withTimeout(5000), withHeaders(headers)) becomes withHeaders(headers)(withTimeout(5000)(fetch)).

API

Wrappers

Listed in the recommended pipeline order. Read the list top to bottom as the order you pass wrappers to pipeline().

Utilities

Errors

FAQ

How is this different from Ky?

Ky is a full-featured HTTP client with its own API (ky.get(), .json(), etc.). This package instead gives you individual utilities that wrap the standard fetch function. You pick only what you need and compose them together. If you want a batteries-included client, use Ky. If you want to stay close to the fetch API while adding specific capabilities, use this.

How do I use a proxy?

This package wraps the standard fetch API, so proxy support comes from the runtime. In Node.js, use the --use-env-proxy flag.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.