In the Nuxt 3 documentation, there's an example that shows using server middleware to annotate requests with additional information via event.context
:
export default defineEventHandler((event) => {
event.context.auth = { user: 123 }
})
However, there is no example of how to make the corresponding TypeScript typing for this. I'd prefer my event.context.auth
not to be an any
type.
The standard TypeScript library type extension method doesn't seem to work correctly here. I tried to make a shims.d.ts
containing the following:
declare module 'h3' {
interface H3EventContext {
auth: AuthSession
}
}
However, this breaks the type inference on the defineEventHandler
function, making all event
parameters into implicit any
s. How can I declare my event context type without breaking stuff?
defineEventHandler
functions to somehow lose their type inference entirely such that theevent
parameter becomes anany
. Not sure what's happening there! – Geoffrey