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

@pbzona/withfetch

v0.0.9

Published

Composable fetch instances with preset headers

Readme

withfetch

Composable fetch instances with preset headers. Create configured fetch functions that automatically apply headers to every request.

Install

npm install @pbzona/withfetch

Usage

import { createFetch, header, headerFromEnv, bearer } from "@pbzona/withfetch";

const api = createFetch(
  headerFromEnv("X-Api-Key", "API_KEY"),
  bearer("my-token"),
);

// Headers are applied to every request
await api("https://api.example.com/data");
await api("https://api.example.com/data", { method: "POST", body: "..." });

The returned function has the same signature as fetch. Per-request headers override middleware headers.

API

createFetch(...middleware): fetch

Creates a new fetch function that applies the given middleware headers to every request.

const api = createFetch(
  header("X-Custom", "value"),
  bearerFromEnv("AUTH_TOKEN"),
);

Headers

header(name, value)

Static header applied to every request.

headerFromEnv(name, envVar, options?)

Header value read from an environment variable at request time.

headerFromEnv("X-Api-Key", "API_KEY");
headerFromEnv("X-Optional", "MAYBE_SET", { optional: true });

Authentication

bearer(token) / bearerFromEnv(envVar, options?)

Sets Authorization: Bearer <token>.

bearer("my-token");
bearerFromEnv("AUTH_TOKEN");
bearerFromEnv("AUTH_TOKEN", { optional: true });

basic(username, password) / basicFromEnv(usernameEnv, passwordEnv, options?)

Sets Authorization: Basic <base64>.

basic("admin", "secret");
basicFromEnv("BASIC_USER", "BASIC_PASS");

Shortcuts

accept(mimeType) / contentType(mimeType) / userAgent(value)

Shorthands for common headers.

createFetch(
  accept("application/json"),
  contentType("application/json"),
  userAgent("my-app/1.0"),
);

Vercel OIDC

vercelOidc(options?)

Reads the VERCEL_OIDC_TOKEN environment variable and sets it as a Bearer token. Useful for authenticating between Vercel-hosted services.

import { createFetch, vercelOidc } from "@pbzona/withfetch";

// Simple: reads VERCEL_OIDC_TOKEN env var
const api = createFetch(vercelOidc());

// Advanced: use @vercel/oidc for token retrieval in Vercel Functions
import { getVercelOidcToken } from "@vercel/oidc";

const api = createFetch(
  vercelOidc({ getToken: () => getVercelOidcToken() }),
);

Environment Variables

All *FromEnv functions resolve environment variables at request time, not when the middleware is created. This means:

  • Tokens that rotate are picked up automatically
  • Environment variables set after middleware creation are still read

By default, a missing environment variable throws an error. Empty strings are also treated as missing. Pass { optional: true } to silently skip the header instead.

Header Precedence

When the same header is set by multiple sources, the last one wins:

  1. Middleware (in order — later middleware overrides earlier)
  2. Headers from a Request object (if passed as the first argument)
  3. Per-request init.headers

License

MIT

Publishing

This repo includes a GitHub Actions publish workflow at .github/workflows/publish.yml.

One-time setup

  1. Configure npm Trusted Publishing for this package and this GitHub workflow.
  2. Ensure workflow permissions include id-token: write (already configured).

Release flow

  1. Bump the version in package.json and create a tag:
npm version patch
git push --follow-tags
  1. The workflow runs on v* tags, validates lint/typecheck/tests/build, verifies tag version matches package.json, then publishes to npm with provenance.

Agent Skill

This repository ships an installable skill for coding agents at skills/withfetch-consumer/SKILL.md.

Install from a local checkout:

npx skills add /path/to/fetch-with --skill withfetch-consumer -g -y

Install from GitHub:

npx skills add <owner>/fetch-with --skill withfetch-consumer -g -y