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

@plinth-dev/api-client

v0.1.0

Published

Server-only typed fetch wrapper — never throws on HTTP errors.

Downloads

446

Readme

@plinth-dev/api-client

Server-only typed fetch wrapper for Next.js. Named API registry, retries on 5xx/429 with exponential backoff, abort-signal propagation, RFC 7807 problem+json auto-parsing. Never throws on HTTP errors — returns ApiResponse<T> with success: boolean. The caller writes one branch.

Design rationale: https://plinth.run/sdk/ts/api-client/.

Install

pnpm add @plinth-dev/api-client

Minimum example

// app/api-clients.server.ts — registered once at module init
import "server-only";
import { register } from "@plinth-dev/api-client";
import { cookies } from "next/headers";

register("items-api", {
  baseUrl: process.env.ITEMS_API_URL!,
  authHeader: async () => {
    const session = (await cookies()).get("session")?.value;
    return session ? `Bearer ${session}` : null;
  },
  timeoutMs: 10_000,
});
// app/(module)/items/[id]/page.tsx
import { api } from "@plinth-dev/api-client";

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const res = await api("items-api").get<Item>(`/items/${id}`);

  if (!res.success) {
    if (res.error!.code === "not_found") notFound();
    throw new Error(res.error!.message);
  }
  return <ItemView item={res.data!} />;
}

Behaviour

  • Never throws on HTTP errors. A 404, 500, network failure, timeout — all return { success: false, error: {...}, data: null, meta: {...} }. One branch.
  • Auto-parses RFC 7807 problem+json. When the response Content-Type is application/problem+json (the shape sdk-go/errors's middleware emits), error.code, error.message, error.fields are populated from the body. Other error responses get { code: "unknown", message: <body text> }.
  • Retries 5xx + 429 with exponential backoff (default 2 retries, 100 ms initial, doubling). Retry list is configurable. Network errors and timeouts also retry.
  • Abort propagation. If init.signal is provided, it cascades through retries. Server-component cancellation (Next.js's request abort) thus actually cancels in-flight retries.
  • Trace propagation. Wire OTel via setTraceHeaderFunc once at app init; the function is called per request and its output is merged into the request headers. Compatible with @opentelemetry/api's propagation.inject.
  • Per-call header override. Pass init.headers; per-call wins over defaultHeaders.

API at a glance

| Symbol | Purpose | |---|---| | register(name, config) | Register a named API. Re-registering replaces. | | api(name) | Returns a typed ApiClient with get/post/put/patch/delete. Throws if name unregistered (programmer error). | | ApiResponse<T> | { data, success, error, meta }. The single shape every call returns. | | ApiError | { status, code, message, fields? }. | | setTraceHeaderFunc(fn) | Wire OTel propagation. | | setFetchImpl(impl) | Test-only override for the underlying fetch. | | clearRegistry() | Test-only reset. |

Compatibility

  • Server-only: uses Headers, Response, AbortController, fetch — all available in Node 20+ and Next.js Server Components / actions.
  • Node 20+ (native fetch).
  • TypeScript 5.9+ for verbatimModuleSyntax.
  • ESM-only.

License

MIT — see LICENSE.