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

next-charge

v0.1.0

Published

Token-bucket charge system for Next.js API routes, backed by Redis

Readme

next-charge

Token-bucket charge system for Next.js API routes backed by Redis. Prevents unbounded spend from public demos.

Install

pnpm add next-charge

Quick start

Define your charge system

// lib/charge.ts
import { createChargeSystem } from "next-charge";
import { Redis } from "@upstash/redis";

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

export const {
  withCharge,
  getChargeState,
  getAllChargeStates,
  topOff,
  checkCharges,
  chargeStatusHandler,
  checkHandler,
} = createChargeSystem({
  redis,
  pools: [
    { id: "generate-image", maxCharges: 5, rechargeIntervalHours: 2 },
    { id: "describe-image", maxCharges: 20, rechargeIntervalHours: 0.5 },
  ],
});

Wrap your API routes

// app/api/generate/route.ts
import { withCharge } from "@/lib/charge";

export const POST = withCharge("generate-image", async (req: Request) => {
  const body = await req.json();
  // ... your handler logic
  return Response.json({ success: true });
});

When charges are depleted, the wrapper returns a 429 response:

{
  "error": "out_of_charge",
  "retryAfterMs": 3600000,
  "poolId": "generate-image"
}

A Retry-After header (in seconds) is also set.

If Redis is unreachable, the wrapper returns a 503 with { "error": "service_unavailable" } (fail-closed).

Add status endpoints (optional)

// app/api/usage/route.ts
import { chargeStatusHandler } from "@/lib/charge";

export const GET = chargeStatusHandler();

// app/api/usage/check/route.ts
import { checkHandler } from "@/lib/charge";

export const GET = checkHandler();

GET /api/usage returns all pool states. GET /api/usage/check?pools=generate-image,describe-image returns a quick availability check:

{ "ok": true, "pools": [{ "id": "generate-image", "available": true }, ...] }

Client-side hook

import { useChargeFetch } from "next-charge/client";

const chargeFetch = useChargeFetch({
  onOutOfCharge: (body, retryStr) => {
    alert(`Out of charges for ${body.poolId}. Recharges in ${retryStr}.`);
  },
});

const res = await chargeFetch("/api/generate", { method: "POST", body: JSON.stringify(data) });

useChargeFetch wraps fetch() and intercepts 429 responses with error: "out_of_charge". It calls your onOutOfCharge callback with the parsed body and a human-readable retry string (e.g. "45m", "2h 15m"). The original Response is still returned so you can handle other errors normally.

Configuration

createChargeSystem(config)

| Field | Type | Default | Description | |-------|------|---------|-------------| | redis | ChargeRedis | required | Redis client instance | | pools | ChargePoolConfig[] | required | Array of charge pool definitions | | keyPrefix | string | "charge:" | Prefix for Redis keys | | devBypass | boolean | false | Skip consumption (reads still work if Redis is available) |

ChargePoolConfig

| Field | Type | Default | Description | |-------|------|---------|-------------| | id | string | required | Unique pool identifier | | maxCharges | number | required | Maximum accumulated charges | | rechargeIntervalHours | number | required | Hours to regenerate one charge | | label | string | id | Human-readable label for status endpoints | | group | string | "" | Grouping key for dashboard sections |

Redis compatibility

next-charge needs only three methods from your Redis client:

interface ChargeRedis {
  get(key: string): Promise<string | null>;
  set(key: string, value: string): Promise<unknown>;
  eval(script: string, keys: string[], args: string[]): Promise<unknown>;
}

This is directly compatible with @upstash/redis and @vercel/kv. For ioredis, wrap with a thin adapter:

import Redis from "ioredis";

const raw = new Redis(process.env.REDIS_URL!);
const redis = {
  get: (k: string) => raw.get(k),
  set: (k: string, v: string) => raw.set(k, v) as Promise<unknown>,
  eval: (script: string, keys: string[], args: string[]) =>
    raw.eval(script, keys.length, ...keys, ...args),
};

How it works

Each pool tracks { current, lastUpdatedAt } in a single Redis key. Recharge is passive and time-based -- no background jobs or cron required.

On consume, an atomic Lua script computes how many charges have accumulated since lastUpdatedAt, adds them to current (capped at maxCharges), then decrements by one. If no charges are available, it returns retryAfterMs without writing.

lastUpdatedAt advances by exact interval multiples (not wall-clock time) to preserve partial recharge progress. A missing key initializes as full. Redis failures fail-closed (503). When devBypass is true, consumption always succeeds.

License

MIT