trying to use defineEventHandler with before[] for per-route middleware
Environment
I noticed that in this merge https://github.com/unjs/h3/pull/485 there is before: [] - i tried something like this:
const index = defineEventHandler({
handler: async () => {
return metapi().render(
await prisma.user.findMany({
}),
)
},
before: [
() => {
console.log('we are in before[]')
return auth.user() && auth.user().isAdmin
},
],
})
Reproduction
Just run the above code in a route
Describe the bug
not sure if i'm using before: [] incorrectly?
Additional context
No response
Logs
No response
Read https://h3.unjs.io/guide/event-handler#object-syntax
Read https://h3.unjs.io/guide/event-handler#object-syntax
Are you saying I should use onRequest: [] instead of before: [] ?
Read https://h3.unjs.io/guide/event-handler#object-syntax
I think the documentation for defineEventHandler is incomplete.
there is no example of how to apply defineEventHandler with object syntax.
what if i want to create middleware at a non-global route level. did i make defineEventHandler in another file that accepts the object event then I called it on the body defineEventHandler by inserting an event object in the route level?
Read https://h3.unjs.io/guide/event-handler#object-syntax
I think the documentation for
defineEventHandleris incomplete.there is no example of how to apply
defineEventHandlerwith object syntax.what if i want to create middleware at a non-global route level. did i make
defineEventHandlerin another file that accepts the objecteventthen I called it on the bodydefineEventHandlerby inserting aneventobject in the route level?
true. The docs are quite cryptic to say the least.
Registering middleware with h3 v2 is much clearer (I hope!) also possible to register generic middleware per route.
defineEventHandler({
middleware: [
// (you can add as many with any order)
onRequest(() => {}),
onResponse(() => {}),
onError(() => {}),
(event, next) => {
next();
},
],
handler: (event) => {}
});