chargeRateLimit (middleware/rate-limit.ts)

cloudflare-workers-nextjs-saas-template · src/api/middleware/rate-limit.ts · typescript · MIT
auth, Auth / Identity

Middleware and utilities for enforcing API rate limits with KV storage.

// Source: https://github.com/LubomirGeorgiev/cloudflare-workers-nextjs-saas-template | License: MIT | Copyright 2025 Lubomir Georgiev (Любомир Георгиев)
// Provided via justclaw.it.com; keep this notice with the code.
import "server-only";

import type { Context, MiddlewareHandler } from "hono";

import type { ApiEnv } from "@/api/types";
import { actionErrorToProblem, toProblemResponse } from "@/lib/api/errors";
import type { ApiPrincipal } from "@/lib/api/principal";
import { applyRateLimitHeaders } from "@/lib/api/rate-limit-headers";
import { __INTERNAL_TRUSTED_CLIENT_IP_HEADER } from "@/utils/trusted-client-ip";
import {
  consumeRateLimit,
  RATE_LIMITS,
  RateLimitError,
  type RateLimitSnapshot,
} from "@/utils/with-rate-limit";

/** The buckets an unauthenticated edge caller can 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key