question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: Allow to override ctx type when middleware is used

See original GitHub issue

Describe 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: image

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:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KATTcommented, Sep 18, 2022

Try

return next({
    ctx: { user: { ...ctx.user } }
  })
1reaction
KATTcommented, Sep 18, 2022

Hmmmmmmmmmmm, if ctx.user.system_role is a disctiminated union i feel like it should work

Maybe try

return next({
    ctx: { prisma: ctx.prisma, user: { ...ctx.user } }
  })
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found