npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

idempotent-fetch

v0.1.0

Published

Platform-agnostic fetch wrapper for Idempotency-Key retries, processing polling, and replay detection.

Downloads

133

Readme

idempotent-fetch

idempotent-fetch is a platform-agnostic wrapper around fetch for Idempotency-Key workflows.

Pairs with idempotency_kit for Phoenix/Ecto on the server side.

Runtime support: browsers, React Native, Node 18+, Bun, Deno, and edge runtimes.

It provides:

  • automatic idempotency key generation
  • network retries with exponential backoff
  • processing-state polling
  • replay detection via x-idempotency-status: replayed

Usage

Basic request

import { idempotentFetch } from "idempotent-fetch";

const result = await idempotentFetch("https://api.example.com/workouts", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${token}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ name: "Leg day" }),
  idempotency: {
    keyPrefix: "workout-create",
    maxPollAttempts: 30,
    pollDelayMs: 2500,
    networkRetryAttempts: 3,
    timeoutMs: 30_000
  }
});

if (result.replayed) {
  console.log("response came from idempotency cache");
}

const payload = await result.response.json();

Fixed idempotency key

import { idempotentFetch } from "idempotent-fetch";

await idempotentFetch("https://api.example.com/workouts", {
  method: "POST",
  body: JSON.stringify({ name: "Leg day" }),
  idempotency: {
    key: "workout-create-123"
  }
});

Custom processing matcher

import { idempotentFetch } from "idempotent-fetch";

const result = await idempotentFetch("https://api.example.com/jobs", {
  method: "POST",
  idempotency: {
    isProcessingResponse: ({ response }) => response.status === 202
  }
});

Rebuild non-replayable bodies with requestFactory

import { idempotentFetch } from "idempotent-fetch";

await idempotentFetch("https://api.example.com/upload", {
  method: "POST",
  requestFactory: () => {
    const stream = createFreshReadableStreamSomehow();

    return {
      body: stream,
      headers: {
        "content-type": "application/octet-stream"
      }
    };
  },
  idempotency: {
    networkRetryAttempts: 2
  }
});

Installation

npm install idempotent-fetch

API

idempotentFetch(input, options)

Returns:

  • response: Response
  • idempotencyKey: string
  • replayed: boolean
  • pollAttempts: number
  • networkAttempts: number

Default processing detection expects:

  • HTTP status 409
  • JSON body with errors.code === "idempotency_request_in_progress"

Override via idempotency.isProcessingResponse when needed.

When idempotency.isProcessingResponse is provided, idempotentFetch currently parses a cloned JSON body eagerly for each response and passes it as responseBody to the matcher.

createIdempotencyKey(prefix?)

Creates a key like <prefix>-<random-id>. If prefix is missing/blank, uses request.

IdempotentFetchError

Errors thrown by retries/polling are instances of IdempotentFetchError with code:

  • "timeout"
  • "network"
  • "unknown"
  • "still_processing"
  • "non_replayable_body"

The original failure is available as error.cause.

Notes

  • Works in modern browsers, React Native, Node 18+, Bun, Deno, and edge runtimes with a standards-compatible fetch.
  • If globalThis.fetch is unavailable, pass a fetch implementation via options.fetch.
  • timeoutMs: 0 disables timeout.
  • Retries and polling resend the request. Use replayable bodies (for example JSON strings, blobs) or requestFactory.
  • Request inputs that include a body are treated as non-replayable for retries/polls; use requestFactory or set retries/polls to 0.
  • createIdempotencyKey uses crypto.randomUUID() when available; fallback randomness is non-cryptographic. Supply idempotency.key if you need strict key control.

License

MIT