Repository navigation
Middleware: swap context - #915
Conversation
Tests to make sure context-swapping works and doesn't have unexpected side-effects.
Remove swapContext method for now - can be added later, but the use-case is supported via `return next({ ctx: ... })`.
|
This pull request is being automatically deployed with Vercel (learn more). www – ./www🔍 Inspect: https://vercel.com/trpc/www/8GNsheUDxLmAVmLNCkYtHd7BdBPg todomvc – ./examples/next-prisma-todomvc🔍 Inspect: https://vercel.com/trpc/todomvc/5Gx7d55jzgJEQ6bN8rXQGz42dcut |
|
Wow, awesome! 🤩 Will review properly soon, right now just responding to your text!
👌
💚 ✅
💯 - rip them out - I just had those there as a work in progress kinda thing.
Yay!!
👌
👌 👌
Agree with you - it's loose but it's kinda okay for now. The things we do in tRPC are possible thanks to TypeScript not being 100% pure and we are able to typecast stuff.
I'm impressed you worked this out on your own though, it was a bit of a bitch to make and I agree there should be a simpler approach.
YES. I love options objects and I don't know why I haven't thought about doing this with generics! |
# Conflicts: # packages/server/src/router.ts
|
@KATT good catch, hadn't tried merging routers. Hope it doesn't get crazy. I added c99e345 to expose |
KATT
left a comment
There was a problem hiding this comment.
Some last few comments then I think it's good to go
From #818
@KATT I created a new PR because I made a lot of (type-level) changes.
return next({ ctx: ... })MiddlewareFunctionKeepContextandMiddlewareFunctionSwapContextwith a singleMiddlewareFunctiontype with an overloadednextinputnext({ ctx: ... })a function which can pass anything toctx. The type passed is inferred as theTContextfor the middleware's return value - this gets theexpectTypeOftest added in the previous PR working= anyin type args. It was hard to keep track of what needed to be changed when defaults were used. I don't think any were internal so I've left them like that, but worth taking a lookNotes for the future:
procedure.tsare a bit loose. It's internal, so that's ok as long as it's covered by tests, but I think the crux is that it uses amiddlewaresarray. I haven't tried this at all, but I think we could get more type safety by requiring that a procedure has exactly one middleware, and adding some helpers which compose middlewares. That way we can properly keep track of input and output contexts, instead of procedures having a list of unknown -> unknown middlewares.ProcedureWithInputandProcedureWithoutInputmade this a bit harder than it feels like it should be. Maybe Procedure instances could always have inputs, where for convenience you can add queries/mutations/subscriptions to a router without, but the "input" defaults to() => undefined(which is a validProcedureInputParserCustomValidatorEsque, I believe)class Router<T extends TRouterOptions>{ ... }. Then to get the context you doT['context'], to get queries it'd beT['queries']. This can make it easier to write helper functions and refactor things without having to duplicate the ever-growing<TInputContext, TContext, TQueries, TMutations, TSubscriptions, TGodKnowsWhatElse>typearg lists, or fall back on potentially dangerousanydefaults.