-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defineEventHandler not accepting multiple handlers #127
Comments
I have a naive approach to doing this, if we can somehow merge all functions in defineEventHandler's argument
|
@pi0 I have also written a small function to merge all the handlers
|
Curious about what you are trying to do 🤔 Anyway, I just put handlers in an array const handlers = [
defineEventHandler((event) => {
event.context.count = 1
}),
defineEventHandler((event) => {
event.context.count++
}),
defineEventHandler((event) => {
console.log(event.context.count)
})
]
const app = createApp()
app.use(handlers) You can also export an array of event handlers in Nuxt // ~/server/middleware/something.ts
export default [
defineEventHandler((event) => {
event.context.count = 1
}),
defineEventHandler((event) => {
event.context.count++
}),
defineEventHandler((event) => {
console.log(event.context.count)
})
] |
No its not working in nuxt |
oh the array export works only on |
@wobsoriano No, I want middleware on a single endpoint, and |
That's why you have to conditionally check it 😉 Use server middleware > check url > continue to api url |
yeah currently I am checking the URL but I might add more endpoints and every time I will add a new endpoint will have to add it in middleware condition, so I do want something to add middleware on individual endpoints like express. |
Yeah that's currently the only way, or, you can create an interceptor function and use that for every api route you need |
@wobsoriano can we connect, what we can do is we can also implement this in nitro. |
@wobsoriano ?? |
H3 event handlers, unlike express, are not middleware runners. You probably want to split your logic into reusable composable functions that accept But if you really want to use this pattern, nesting apps is possible. as @wobsoriano suggested you can create a nested app: import { eventHandler, createApp } from 'h3';
const app = createApp();
app.use([
eventHandler((event) => {
event.context.counter = 1;
}),
eventHandler((event) => {
event.context.counter++;
}),
eventHandler((event) => 'Counter:' + event.context.counter),
]);
export default app; Example sandbox: https://stackblitz.com/edit/github-rqbwda?file=routes%2Findex.ts |
@pi0 now how do I do this it is not working anymore and I have used it in my project, please help |
No description provided.
The text was updated successfully, but these errors were encountered: