cha0sg0d
cha0sg0d9mo ago

How to get the type of context after middleware context extension?

I have a middleware where I validate and set a userId. My router knows that userId is no longer undefined, but I need to pass the route context to another function. How do I set the input type for that function such that userId is no longer undefined?
4 Replies
NeoBean
NeoBean8mo ago
I have the same question.
Nick
Nick8mo ago
Any reason that function needs coupling to your context types at all? It may seem like the lazy and easy way, but it’s usually best to define your interfaces properly Service classes etc shouldn’t be aware of tRPC
NeoBean
NeoBean8mo ago
I dont see a valid reason or requirement. but the lazy way may sometime feel intuitive to tell myself this function belongs strictly to these contexts. I agree with you 👍
Nick
Nick8mo ago
Yeah, the way I would do it if I wanted lazy, would be to break apart my context types into somewhere central, build my context types from there, and then at least have that stuff consumable/composable for service classes but for userId:string I suspect you don't need a UserId type exported from anywhere 😛