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 hooking into responses #447

Closed
pi0 opened this issue Jul 17, 2023 · 5 comments · Fixed by #482
Closed

Allow hooking into responses #447

pi0 opened this issue Jul 17, 2023 · 5 comments · Fixed by #482
Labels
discussion enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Jul 17, 2023

Related: unjs/nitro#1396

Currently, it is not possible to hook into responses (that are handled by h3 not directly with res.end). In order to do this, we might refactor app.ts and allow hooking into directly returned responses and allow to hook into using custom callback onBeforeResponse(event, { body }).

This can be later used by higher order frameworks to support custom response rewrite handling and logging.

At this point, i also like to gether some common usecase ideas before moving forward (mainly because it also adds some overhead even if callback is not used)

@Hebilicious
Copy link
Member

I think the most common use cases for this type of feature would be custom logging/metrics. I could also see for applications that are not super performant sensitive having some kind of db read/http request to extend the response after it is handled by the framework.
However there might be some overlap with scoped middlewares.

@emdede
Copy link

emdede commented Jul 18, 2023

Just had this today and my use-case would be custom error messages per "middleware" which afaik is currently not possible. Please correct me if I'm wrong.

router.post('/', (event) => somethingThatPotentiallyErrors())

router.onBeforeResponse((event, { body }) => {
  if (body instanceof Error) return { status: 'error', message: body.message }
  return { status: 'ok', ...body }
}

Catching the error in this scenario is not clear to me at the moment. I assume H3 would have already transformed the error at this point.

@tobychidi
Copy link

It is needed for stuff like treblle-utils by treblle.com . Then I feel like we are missing out? Cuz there is a solution for this in Express and Fastify.

@Hebilicious
Copy link
Member

Hebilicious commented Jul 22, 2023

It is needed for stuff like treblle-utils by treblle.com . Then I feel like we are missing out? Cuz there is a solution for this in Express and Fastify.

I agree that this feature is needed, however looking at Trebble docs (for the first time); based on the Koa integration looks, I think its possible to integrate it with h3 without hooking into the response.

@pi0
Copy link
Member Author

pi0 commented Aug 2, 2023

Thanks for the feedbacks @Hebilicious, @emdede and @tobychidi ❤️ 3 hooks will be supported in next release (see #482)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants