Replies: 7 comments 10 replies
-
Hmmm, I see. So the idea would be to define some kind of I think that would suffice, please ignore. =) |
Beta Was this translation helpful? Give feedback.
-
So if we have 100 procedures split across different files and merged onto the main router, we should import and use the middleware'd procedure and use it in all those instances? I can't see how this is sustainable. Sure, it's nice to have granular procedure-level middleware. Again, that's not the general point of middleware and having it on a router-level is essential for any Node server framework. |
Beta Was this translation helpful? Give feedback.
-
In the case of chaining middleware, we want to define each middleware separately without const isAuthed = trpc.middleware(({ next, ctx }) => {
...
return next({
ctx: {
user: ctx.session.user,
},
})
}) const withAuthMW = trpc.procedure.use(isAuthed).middleware(...) instead of const authedProcedure = trpc.procedure.use(isAuthed).use
type WithAuth = Parameters<typeof authedProcedure>[0]
const withAuthMW: WithAuth = ... It's better if middleware can update other props. const isAnyWithInputValidation = trpc.procedure.input(z.object({foo: z.string()})).middleware(({input, next}) => {
const { foo } = input
...
return next({...})
})
const anyProcedure = trpc.procedure.use(isAnyWithInputValidation).input(
z.object({
foo: z.string(),
bar: z.number()
})
).query(({input}) => {
const { foo, bar } = input
...
}) |
Beta Was this translation helpful? Give feedback.
-
I totally agree with @huzaifahj that
is very much needed and I think it's a huge regression. tRPC examples are very primitive and there it's not a big problem that can only add middleware per procedure. You have router and procedure visually in the same place. In real life bigger projects you would put queries and mutations to separate files and then you would have to make sure that you use correct procedure in these files since it's not clear to which router they belong to. It's easy to make a mistake of putting a public procedure under admin router. |
Beta Was this translation helpful? Give feedback.
-
Sorry to gravedig, but just wanted to add: not being able to apply middleware to a namespace is a huge miss, IMO. This will prevent me from migrating to V10 just from the sheer amount of present and future maintenance this change creates. My project is designed around namespaces with certain rules, such that routes added to particular routers automatically inherit all those rules. Very easy to move fast and not break anything by just putting a route in the right place. I understand that separating routing from middleware logic is probably the by-the-books Separation of Concerns way to do this, but imo is a big step back in actual DX. It results in a ton of duplicate code, and now every added route has a fresh chance to break auth rules by missing a middleware/pipe wrapper. |
Beta Was this translation helpful? Give feedback.
-
I'm just adopting tsrpc and just discovered that the ability to make an entire router password protected existed in 9 and not 10. This was pretty surprising to me and feels like a miss - I get that it is possible to use protectedProcedure when declaring each procedure but like AdventureBeard said, it feels like there's increased risk of accidentally using publicProcedure in one place and leaving an API exposed |
Beta Was this translation helpful? Give feedback.
-
Since the discussion is still open, In my case I have a router subtree for a specific feature that now needs to check for a feature flag for the entire route. Just adding that at the router level would make it much more predictable. It is definitely doable with a base proc as @KATT mentioned. But in my case:
It's like adding a node in the middle of a linked list and then removing it. Whereas with a router level middleware, we're just putting a wall/door in between. No need to move pieces anymore. Depending on the size of the tree and the complexity of the procedures it can make a big difference. |
Beta Was this translation helpful? Give feedback.
-
In tRPC 9, we get the ability to apply middleware to every query/mutation in a router as a result of chaining queries/mutations. However it seems like in v10 we'd have to individually apply certain middleware to each query/mutation individually.
Let's say I wanted to add a logger to all the queries/mutations, it would be nice to be able to apply the middleware to the main router instead of having to import it into all the individual routers.
So in the example below (modified from the alpha middleware example), it would be nice to place middleware on to the main router so that it applies to any routes and merged routes, etc.
Beta Was this translation helpful? Give feedback.
All reactions