[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create OSS package middleware (e.g. rate limit) #3227

Closed
1 task
KATT opened this issue Nov 22, 2022 · 3 comments
Closed
1 task

feat: Create OSS package middleware (e.g. rate limit) #3227

KATT opened this issue Nov 22, 2022 · 3 comments
Labels
💸 Get paid! I'll send you money through your GitHub sponsors for addressing this @trpc/server

Comments

@KATT
Copy link
Member
KATT commented Nov 22, 2022

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.

import { createTRPCRateLimit } from "@fantasy/trpc-rate-limit";
import {initTRPC} from "@trpc/server";

const root = initTRPC.create();

const {
  procedure
} = root;

const rateLimiter = createTRPCRateLimiter({
  root,
	windowMs: 15 * 60 * 1000, // 15 minutes
	max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
	headers: true, // Return rate limit info in the `RateLimit-*` headers
});


export const rateLimitedProcedure = 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
 */
export async function createContextInner(_opts: CreateContextOptions) {
  return {};
}

export type Context = trpc.inferAsyncReturnType<typeof createContextInner>;

/**
 * Creates context for an incoming request
 * @link https://trpc.io/docs/context
 */
export async function createContext(
  opts: trpcNext.CreateNextContextOptions,
): Promise<Context> {
  // for API-response caching see https://trpc.io/docs/caching
  await rateLimiter.validate({
    info: opts.info,
  })

  return await createContextInner({});
}

Additional information

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!
@KATT KATT added ✨ enhancement @trpc/server 💸 Get paid! I'll send you money through your GitHub sponsors for addressing this labels Nov 22, 2022
@KATT KATT moved this to Ready to work on - docs in Roadmap and priorities Nov 27, 2022
@santosmarco-caribou
Copy link

I'm not being able to click on the Yes, I'd be down to file a PR implementing this feature! checkbox but I'd love to work on this.

@juliusmarminge
Copy link
Member

I've started on this here but my time is limited right now so it's not a priority on my end.

@OrJDev
Copy link
Contributor
OrJDev commented Jan 2, 2023

Done. I just took what @juliusmarminge made, and changed a few things to get it to work with different frameworks, seems to work fine.

Could be improved with the type safety tho

Edit: alright it is type safed now.

https://github.com/OrJDev/trpc-limiter

@KATT KATT closed this as completed May 24, 2023
@github-project-automation github-project-automation bot moved this from Ready to work on - docs to ✅ Done in Roadmap and priorities May 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💸 Get paid! I'll send you money through your GitHub sponsors for addressing this @trpc/server
Projects
Status: Done
Development

No branches or pull requests

4 participants