attaching a property on to nitroApp and accessing it else where server-side #1517
Replies: 2 comments
-
man I have this same question, I created a working demo but im trying to get verification if this is a proper way to handle it. it could be worth a try for you. look at nitro plugins, they get executed once after server is initialized. Can anyone confirm if this approach makes sense? |
Beta Was this translation helpful? Give feedback.
-
You can add context to each h3 event by wrapping eventHandler like: import type { H3Event, H3EventContext } from 'h3'
interface CustomH3Event extends H3Event {
context: H3EventContext & {
// your custom props
session: Record<string, any>
},
}
export function defineCustomEventHandler<T>(handler: (event: CustomEvent) => T) {
return defineEventHandler(async (event) => {
// example session loader
const session = await getUserSession(event)
if (!session) {
throw createError({
statusCode: 401,
statusMessage: 'Unauthorized',
})
}
// set your custom context here
event.context.session = session
return handler(event as CustomEvent)
})
} |
Beta Was this translation helpful? Give feedback.
-
So I'm curious how you would go by doing this. Let's say I have a discord client and I need to inject this else where through the app. How would I bind it on to the nitro instance?
Beta Was this translation helpful? Give feedback.
All reactions