feat: Allow to override ctx type when middleware is used
See original GitHub issueDescribe the feature you’d like to request
I have this middleware which refines types on my ctx:
const isCompanyMemberMiddleware = t.middleware(({ ctx, next }) => {
if (ctx.user?.system_role !== SystemRole.COMPANY_MEMBER) {
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
if (!ctx.user.company_id) {
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
return next({ ctx })
})
const companyProc = t.procedure.use(isCompanyMemberMiddleware)
but in my jobs query which uses this middleware typescript does not know that:

Describe the solution you’d like to see
It shure would be nice if we could override the ctx type with something like:
const companyProc = t.procedure.use(isCompanyMemberMiddleware).context<{user: {system_role: 'COMPANY_MEMBER', company_id: string}}>()
Desribe alternate solutions
for the moment, I am just writing this in every query/mutation:
companyProc.input(z.object({})).query(async ({ input, ctx }) => {
const user = ctx.user as ISpottedSessionCompanyMember
return ctx.prisma.job.findMany({
where: {
company_id: user.company_id
}
})
})
which works, but it’s kind of annoying having to do it in every query/mutation
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I’d be down to file a PR implementing this feature!
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
oakserver/oak: A middleware framework for handling ... - GitHub
use (async (ctx) => { const result = ctx.request.body({ type: "reader" }) ...
Read more >typescript - Override request type in next-connect middleware
I managed to implement as sample solution. Here is the code demo: stakc-blitz modified. Sample description of the approach.
Read more >Go Fiber by Examples: Delving into built-in functions
Type : string , default: "" (empty string). This will enable ctx.IP to return the value of the given header key. By default,...
Read more >API — Flask Documentation (2.2.x)
To register a function, use the after_request() decorator. This data structure is internal. It should not be modified directly and its format may...
Read more >koa | Yarn - Package Manager
js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to perform...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Try
Hmmmmmmmmmmm, if
ctx.user.system_roleis a disctiminated union i feel like it should workMaybe try