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

Allow after request middleware #1301

Open
1 task
Barbapapazes opened this issue Jun 11, 2023 · 5 comments
Open
1 task

Allow after request middleware #1301

Barbapapazes opened this issue Jun 11, 2023 · 5 comments
Labels
enhancement New feature or request h3 upstream

Comments

@Barbapapazes
Copy link
Member

Describe the feature

Hello,

I was thinking about after middleware like in express.

For example, if I've a middleware like this:

export default eventHandler(() => {
	console.log('before')
	next()
	console.log('after')
})

And a route like this:

export default eventHandler(() => {
	console.log('route')
	return
})

I expect:

before
route
after

This is not an API to implement but juste an example of what I've in mind

nuxt/nuxt#21522

Additional information

  • Would you be willing to help implement this feature?
@gnoeley
Copy link
Contributor

gnoeley commented Aug 2, 2023

I recently came across this issue and ended up with something like (StackBlitz),

// middleware/afterware.ts
export default eventHandler((event) => {
  const start = Date.now();
  event.res.once('close', () => {
    const ms = Date.now() - start;
    console.log(`Request took ${ms}ms`);
  });
});

This was based on the node middleware helper function approach here in the h3 library.
I'm not sure on the correctness of this but it would be nice if this was somehow supported.

Just noticed a recent change unjs/h3#482 that has added the ability to add before/after hooks to h3 though. Could this be used to implement this functionality in Nitro using unjs/hookable?

@pi0
Copy link
Member

pi0 commented Aug 2, 2023

@gnoeley yes that's the plan! You would be able to use nitro app hooks to leverage them.

@gnoeley
Copy link
Contributor

gnoeley commented Aug 2, 2023

If more hooks are to be added to Nitro, is there a plan to document them more clearly either as a main-section in docs? To date, I've been grepping the code to find our what was supported. Looks like they are all listed under Plugins->Examples currently, but perhaps calling them out directly could be useful. It was a point of confusion for me personally at least 😅 Happy to raise a PR if that would be welcome?

@pi0
Copy link
Member

pi0 commented Aug 2, 2023

Certainly! I will make sure to at least write clear usage in PR description. You are always more than welcome to contribute and improve docs and examples ❤️

@gnoeley
Copy link
Contributor

gnoeley commented Aug 2, 2023

@pi0 , I've opened a draft PR with some proposed documentation changes. I've had a guess at what the final API for the addition of the new hooks could be, happy to accept comments once that is finalized and I can update. Can have any further discussion in the PR.

@pi0 pi0 removed the good first issue Good for newcomers label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request h3 upstream
Projects
None yet
Development

No branches or pull requests

4 participants