You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{createTRPCRateLimit}from"@fantasy/trpc-rate-limit";import{initTRPC}from"@trpc/server";constroot=initTRPC.create();const{
procedure
}=root;constrateLimiter=createTRPCRateLimiter({
root,windowMs: 15*60*1000,// 15 minutesmax: 100,// Limit each IP to 100 requests per `window` (here, per 15 minutes)headers: true,// Return rate limit info in the `RateLimit-*` headers});exportconstrateLimitedProcedure=procedure.use(rateLimiter.middleware());
Desribe alternate solutions
Alternative - do it in createContext()
/** * Inner function for `createContext` where we create the context. * This is useful for testing when we don't want to mock Next.js' request/response */exportasyncfunctioncreateContextInner(_opts: CreateContextOptions){return{};}exporttypeContext=trpc.inferAsyncReturnType<typeofcreateContextInner>;/** * Creates context for an incoming request * @link https://trpc.io/docs/context */exportasyncfunctioncreateContext(opts: trpcNext.CreateNextContextOptions,): Promise<Context>{// for API-response caching see https://trpc.io/docs/cachingawaitrateLimiter.validate({info: opts.info,})returnawaitcreateContextInner({});}
Additional information
👨👧👦 Contributing
🙋♂️ Yes, I'd be down to file a PR implementing this feature!
The text was updated successfully, but these errors were encountered:
Describe the feature you'd like to request
Goal: showcase that it is possible to create middlewares as OSS-packages.
We could do something like https://github.com/express-rate-limit/express-rate-limit but for tRPC
Describe the solution you'd like to see
Probably needs #3152 before.
Desribe alternate solutions
Alternative - do it in
createContext()
Additional information
👨👧👦 Contributing
The text was updated successfully, but these errors were encountered: