Describe the change
Not really a documentation issue, but I'm having a hard time understanding something.
I'm building a reusable middleware library with H3 v2 and want to support two usage patterns from a single function:
// Pattern A: app-wide via app.use()
app.use(withAuth({ role: 'user' }))
// Pattern B: per-route handler wrapping (e.g. Nitro/Nuxt file routes)
export default withAuth({ role: 'user' }, async (event) => {
return event.context.authCtx
})
My current implementation returns defineMiddleware(...) for pattern A and defineEventHandler({ middleware: [m], handler }) for pattern B.
Two questions:
-
For pattern B, is defineEventHandler({ middleware, handler }) the right approach, or is there a more idiomatic way to compose a pre-handler middleware with a route handler?
-
For Nuxt's server/middleware/ files, the convention is export default defineEventHandler(...) but pattern A returns a Middleware type. Is it safe/correct to export a Middleware directly from a Nitro server middleware file, or should it always be wrapped in defineEventHandler?
URLs
No response
Additional information
Describe the change
Not really a documentation issue, but I'm having a hard time understanding something.
I'm building a reusable middleware library with H3 v2 and want to support two usage patterns from a single function:
My current implementation returns
defineMiddleware(...)for pattern A anddefineEventHandler({ middleware: [m], handler })for pattern B.Two questions:
For pattern B, is
defineEventHandler({ middleware, handler })the right approach, or is there a more idiomatic way to compose a pre-handler middleware with a route handler?For Nuxt's
server/middleware/files, the convention isexport default defineEventHandler(...)but pattern A returns aMiddlewaretype. Is it safe/correct to export aMiddlewaredirectly from a Nitro server middleware file, or should it always be wrapped indefineEventHandler?URLs
No response
Additional information