-
Notifications
You must be signed in to change notification settings - Fork 213
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
Comments
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. |
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. |
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. |
Thanks for the feedbacks @Hebilicious, @emdede and @tobychidi ❤️ 3 hooks will be supported in next release (see #482) |
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 refactorapp.ts
and allow hooking into directly returned responses and allow to hook into using custom callbackonBeforeResponse(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)
The text was updated successfully, but these errors were encountered: