// case file 11 · Open source

nanofetch

A ~5KB, zero-dependency, type-safe HTTP client with an Axios-like API, built entirely on native fetch.

RoleAuthorTimelinev0.5.0StackTypeScript + native fetch0runtime dependencies
$ npm install @hnrie/nanofetch

import { api } from '@hnrie/nanofetch';
const res = await api.get('/users');
// 01 the problem

A colleague had dropped Axios after one of its supply-chain incidents and gone back to plain fetch, losing interceptors, retries and typed errors. Could you keep Axios’s ergonomics with zero dependencies?

// 02 how it works
  1. Step 01

    An Axios-compatible API (instances, interceptors, one call for GET/POST), so switching costs almost nothing.

  2. Step 02

    Works in browsers, Node.js and React Native, shipped as ESM and CJS.

  3. Step 03

    Request and response interceptor chains; a rejected handler that resolves short-circuits the chain.

  4. Step 04

    Retries with a fresh AbortController per attempt, cleaned up in each attempt’s finally block.

  5. Step 05

    Every error is a structured ApiError carrying method, URL and config.

// 03 decisions that mattered
01

Timeout and cancel are the same mechanism, not the same thing

Both abort through AbortController and look identical in a catch block. A Symbol as the timeout’s abort reason lets the handler tell them apart.

02

A consumed abort signal can’t be reused

Retrying on the previous attempt’s controller fails instantly. Each attempt gets its own controller.

03

Parse errors should be typed, not raw

The body is read as text and parsed in a try/catch, so a malformed JSON body becomes a client error with isParseError instead of a raw SyntaxError.

04

Token refresh the caller never sees

A 401 handler refreshes the token and calls replay(), rebuilt from the metadata on the error, so the original request just succeeds.

// 04 results
0runtime dependencies
~5KBpublished to npm
5typed errors: network, timeout, cancel, HTTP, parse
// 05 takeaway

A fetch wrapper looks like fifty lines until you list what Axios actually handles for you.

TypeScriptnative fetchESM + CJSNode 16+
// artefacts
// contact

Let’s talk

Open to software engineering and AI automation roles, plus selective contract work. I read every message.

psst, click it
● DRAFT

Show me the copy-paste.
I’ll tell you if it should be a system.

01
02
How often?
03
Which tools?

Hiring? Tell me about the team.
I’ll tell you where I’d help first.

01
02
03
Timeline
04

You get a reply within 24 hours: whether it should be automated, what I’d use, and a rough timeline.

You get a reply within 24 hours.