Skip to content
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

Closed
notshekhar opened this issue Jun 2, 2022 · 18 comments
Closed

defineEventHandler not accepting multiple handlers #127

notshekhar opened this issue Jun 2, 2022 · 18 comments

Comments

@notshekhar
Copy link

No description provided.

@notshekhar
Copy link
Author

notshekhar commented Jun 2, 2022

I have a naive approach to doing this, if we can somehow merge all functions in defineEventHandler's argument
defineEventHandler function takes handler and returns handlers
if we can do this defineEventHandler(one, two, three) and returns a handler like this we can do this

  function one(event){
     const a = 1
      console.log(a)
      return function b(){
          const a = 2
          return a
          return function c(){
              return 3
          }(event)
      }(event)
}

@notshekhar notshekhar changed the title I want defineEventHandler to take multiple handlers and if first handler returns something it wont run next function in the argument defineEventHandle not accepting multiple handlers Jun 2, 2022
@notshekhar
Copy link
Author

@pi0

@notshekhar
Copy link
Author

notshekhar commented Jun 2, 2022

@pi0 I have also written a small function to merge all the handlers

function mergeFunction() {
    let func = arguments[arguments.length - 1].toString()
    for (let i = arguments.length - 1; i > 0; i--) {
        const function2 = arguments[i - 1].toString()
        func = `${function2.substring(
            0,
            function2.length - 1
        )}\nreturn ${func}(event)}\n`
    }
    func = func.substring(func.indexOf("{") + 1, func.lastIndexOf("}"))
    console.log(func)
    return Function("event", func)
}

@wobsoriano
Copy link
Contributor

wobsoriano commented Jun 2, 2022

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)
  })
]

@notshekhar
Copy link
Author

No its not working in nuxt

@notshekhar
Copy link
Author

@notshekhar
Copy link
Author

@notshekhar
Copy link
Author

@wobsoriano

@wobsoriano
Copy link
Contributor

oh the array export works only on ~/server/middleware in nuxt

@notshekhar
Copy link
Author

notshekhar commented Jun 19, 2022

@wobsoriano No, I want middleware on a single endpoint, and /server/middleware automatically runs on every route change.

@wobsoriano
Copy link
Contributor

@wobsoriano No, I want middleware on a single endpoint, and /server/middleware automatically runs on every route change.

That's why you have to conditionally check it 😉

Use server middleware > check url > continue to api url

@notshekhar
Copy link
Author

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.

@notshekhar
Copy link
Author

@wobsoriano

@wobsoriano
Copy link
Contributor

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

@notshekhar
Copy link
Author

@wobsoriano can we connect, what we can do is we can also implement this in nitro.

@notshekhar notshekhar changed the title defineEventHandle not accepting multiple handlers defineEventHandler not accepting multiple handlers Jun 23, 2022
@notshekhar
Copy link
Author

@wobsoriano ??

@pi0
Copy link
Member

pi0 commented Jun 23, 2022

H3 event handlers, unlike express, are not middleware runners. You probably want to split your logic into reusable composable functions that accept event objects.

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

@notshekhar
Copy link
Author

notshekhar commented Nov 21, 2022

@pi0 now how do I do this it is not working anymore and I have used it in my project, please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants