import { C as ConcurrencyGuard, j as UnifiedAdmitter } from './unified-BouIz5EX.js'; import { D as Decision } from './types-DKirIBQt.js'; import { L as LimiterOrStrategy, a as CommonAdapterOptions } from './core-DcpxT2lH.js'; import './store-CQjuAFM_.js'; /** * tRPC adapter. Builds a middleware function (pass it to `t.middleware(...)`) that rate-limits the * procedures it guards: under the limit it calls `next()`; over it **throws** so tRPC turns it into * an error response; a fail-closed store outage rethrows the store error. Because a tRPC `ctx` is * app-defined, you supply the `key` derivation. Dependency-free: the denial error defaults to * {@link RateLimitExceededError}; pass `errorFactory` to throw a `TRPCError` so tRPC reports * `TOO_MANY_REQUESTS`. * * Also exposes {@link trpcUnifiedAdmission} and {@link trpcAdaptiveConcurrency} (0.9.2, TK-1326) * using the try/finally wrap pattern. */ /** Metadata about the call being limited, passed to {@link TrpcRateLimitOptions.key}/`cost`. */ interface TrpcCallMeta { ctx: Ctx; /** The procedure path (e.g. `"post.create"`), when tRPC provides it. */ path?: string; /** The procedure type (`"query"`/`"mutation"`/`"subscription"`), when tRPC provides it. */ type?: string; } /** The slice of tRPC's middleware argument the adapter uses. */ interface TrpcMiddlewareParams extends TrpcCallMeta { /** Continue to the next middleware / the procedure resolver. */ next: () => Promise; } type TrpcRateLimitOptions = LimiterOrStrategy & Pick & { /** Derive the limit key from the call (required — a tRPC `ctx` is app-defined). */ key: (meta: TrpcCallMeta) => string; /** Cost of a call in limiter units. A function computes it per call. Default 1. */ cost?: number | ((meta: TrpcCallMeta) => number); /** Observability hook fired on every denial. */ onLimited?: (ctx: Ctx, decision: Decision) => void; /** Observability hook fired when the store throws (before the fail policy is applied). */ onError?: (ctx: Ctx, err: unknown) => void; /** * Build the error thrown on a denial. Default: {@link RateLimitExceededError}. Pass a TRPCError * factory to surface the proper code: `(d) => new TRPCError({ code: "TOO_MANY_REQUESTS" })`. */ errorFactory?: (decision: Decision) => unknown; }; /** A tRPC middleware function; pass it to `t.middleware(...)`. */ type TrpcRateLimitMiddleware = (params: TrpcMiddlewareParams) => Promise; /** * Build a tRPC rate-limit middleware. * * @example * ```ts * import { TRPCError } from "@trpc/server"; * const rl = trpcRateLimit({ * strategy: gcra({ limit: 100, periodMs: 60_000 }), * key: ({ ctx }) => ctx.user?.id ?? ctx.ip, * errorFactory: (d) => new TRPCError({ code: "TOO_MANY_REQUESTS", message: `retry in ${d.retryAfterMs}ms` }), * }); * export const limited = t.procedure.use(t.middleware(rl)); * ``` */ declare function trpcRateLimit(options: TrpcRateLimitOptions): TrpcRateLimitMiddleware; /** Options for {@link trpcUnifiedAdmission}. */ type TrpcUnifiedAdmissionOptions = Pick & { admitter: UnifiedAdmitter; /** Derive the limit key from the call. Required — tRPC `ctx` is app-defined. */ key: (meta: TrpcCallMeta) => string; /** Cost of a call. Default 1. */ cost?: number | ((meta: TrpcCallMeta) => number); /** Fired on deny. */ onLimited?: (ctx: Ctx, decision: Decision) => void; /** Fired when admit() throws. */ onError?: (ctx: Ctx, err: unknown) => void; /** Build the thrown error. Default {@link RateLimitExceededError}. */ errorFactory?: (decision: Decision) => unknown; }; /** * Build a tRPC middleware enforcing a {@link UnifiedAdmitter}. On admit it forwards to next() * in a try/finally; on a thrown procedure, release({dropped: true}); on a clean return, * release({dropped: false}). */ declare function trpcUnifiedAdmission(options: TrpcUnifiedAdmissionOptions): TrpcRateLimitMiddleware; /** Options for {@link trpcAdaptiveConcurrency}. */ type TrpcAdaptiveConcurrencyOptions = { guard: ConcurrencyGuard; /** Fired on deny. */ onLimited?: (ctx: Ctx, decision: Decision) => void; /** Build the thrown error. */ errorFactory?: (decision: Decision) => unknown; }; /** Build a tRPC middleware enforcing an adaptive {@link ConcurrencyGuard}. */ declare function trpcAdaptiveConcurrency(options: TrpcAdaptiveConcurrencyOptions): TrpcRateLimitMiddleware; export { LimiterOrStrategy, type TrpcAdaptiveConcurrencyOptions, type TrpcCallMeta, type TrpcMiddlewareParams, type TrpcRateLimitMiddleware, type TrpcRateLimitOptions, type TrpcUnifiedAdmissionOptions, trpcAdaptiveConcurrency, trpcRateLimit, trpcUnifiedAdmission };