-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Add parser option #346
Comments
I don't understand what parser are you referring to. If you would like to add an option so you can plug in some trpc logic, I'm +1, bu I don't think it belongs to this module. |
I don’t get it either, can you please elaborate? Thanks! |
Hi @mcollina and @gurgunday, I just stumbled accross this issue having a similar one. I am also using Whenever a tRPC client calls a tRPC procedure "protected" by Usually, a rate limiter for tRPC would be implemented by using middlewares. This way, we could throw a trpc.middleware(async ({ ctx, next }) => {
const {
limit, // nice to have
remaining, // nice to have,
reset, // nice to have
isLimited, // not required, could be determined by e.g. if(remaining <= 0) { ... }
} = await ctx.fastify.checkRateLimit(ctx.req.ip) // of course, I would need to pass down this method as tRPC context as well as req.ip; see https://trpc.io/docs/server/context
if (isLimited) {
throw new TRPCError({
code: 'TOO_MANY_REQUESTS'
})
}
return next()
}) This way, it would be possible to throw proper TRPC errors when using You may ask "why don't you use a tRPC rate limiter like this instead?". However, I run a REST API and tRPC API in parrallel with fastify. It would be great to use the same rate limit plugin accross all APIs with the same library. I don't want to introduce additional dependencies doing the same for different scopes. I hope I was able to explain the issue. Sorry for the long message. If I can help you further or give more details, let me know. Thank you and cheers! |
@Charioteer would you like to send a PR? |
@mcollina Sure, I can implement my suggestion. I only have some spare time on weekends, so it might take a while. |
Prerequisites
🚀 Feature Proposal
If you use fastify with trpc you most likely use some kind of parser. We are using fastify + trpc + zod as a parser. There is an option for response
errorResponseBuilder
but it expect an object. So we can't fully specify what we return (in our case it should be either TrpcError or stringified json for zod), so that's the only reason we cant currently use this package. (Unless you can think of an easier solution for this, would be very helpful). It would be nice to have a parser option which would apply specified parser to the response (you can include just a couple, the most popular ones)Motivation
No response
Example
No response
The text was updated successfully, but these errors were encountered: