Intercept H3 Response Body (Plus Types?) #1392
|
Now, the plugin I want to create requires I append something to the body of the response. Here is what was done in express. How do I replicate in H3/Nitro const originalSend = res.send; |
Answered by
Hebilicious
Jul 5, 2023
Replies: 2 comments 2 replies
|
Would the |
2 replies
|
You can try with nitro: export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("request", (event) => {
console.log("on request", event.path);
});
nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
send(
event,
JSON.stringify({
status: 200,
data: body,
})
);
});
}); |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think that your only option is a middleware, altough, they run globally before your logic. We do have some features coming that will enable the ability to run code after an event handler has returned, but in the meantime I also added a proposal that could allow you to do that #1396
However, if you need to do this now, you can do it with a custom nitro preset #1373