Composable fetch instances with preset headers. Create configured fetch functions that automatically apply headers to every request.
npm install @pbzona/withfetchimport { 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.
Creates a new fetch function that applies the given middleware headers to every request.
const api = createFetch(
header("X-Custom", "value"),
bearerFromEnv("AUTH_TOKEN"),
);Static header applied to every request.
Header value read from an environment variable at request time.
headerFromEnv("X-Api-Key", "API_KEY");
headerFromEnv("X-Optional", "MAYBE_SET", { optional: true });Sets Authorization: Bearer <token>.
bearer("my-token");
bearerFromEnv("AUTH_TOKEN");
bearerFromEnv("AUTH_TOKEN", { optional: true });Sets Authorization: Basic <base64>.
basic("admin", "secret");
basicFromEnv("BASIC_USER", "BASIC_PASS");Shorthands for common headers.
createFetch(
accept("application/json"),
contentType("application/json"),
userAgent("my-app/1.0"),
);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() }),
);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.
When the same header is set by multiple sources, the last one wins:
- Middleware (in order — later middleware overrides earlier)
- Headers from a
Requestobject (if passed as the first argument) - Per-request
init.headers
MIT
This repo includes a GitHub Actions publish workflow at .github/workflows/publish.yml.
- Configure npm Trusted Publishing for this package and this GitHub workflow.
- Ensure workflow permissions include
id-token: write(already configured).
- Bump the version in
package.jsonand create a tag:
npm version patch
git push --follow-tags- The workflow runs on
v*tags, validates lint/typecheck/tests/build, verifies tag version matchespackage.json, then publishes to npm with provenance.
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 -yInstall from GitHub:
npx skills add <owner>/fetch-with --skill withfetch-consumer -g -y