-
Notifications
You must be signed in to change notification settings - Fork 195
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
Add data property to the response object #49
Comments
Found it: server.ext('onPreResponse', function (request, reply) {
var response = request.response;
if (!response.isBoom) {
return reply.continue();
}
if (response.data){
response.output.payload.data = response.data;
}
return reply(response);
}); This code will run for all responses, and if the response is a boom object, and the data property was set on the response, it puts it on the final response object. |
This is not support in native way? |
More recent solution: server.ext('onPreResponse', (request, h) => {
const response = request.response;
if (!response.isBoom)
h.continue;
const is4xx = response.output.statusCode >= 400 && response.output.statusCode < 500;
if (is4xx && response.data)
response.output.payload.data = response.data;
return h.continue;
}); |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Hello!
Most of the Boom methods get an additional (optional) data property, what Hapi doesn't send forward in the response.
I tried using the Hapi response/tail events, but couldn't find the given data in the inputs of those events.
To add the data property:
Which is a bit more code than it is needed to write:
The solution proposed in #47 works for me as well (as well as the data property could be used instead of the solution proposed there).
The use case is the following:
I want to send error messages, which messages should be translated on the frontend. So the message would be like an error code, and the parameters for that code would be in the data object
So a real example could be the following:
And the response for this would be preferred:
Thx
Adam
ps.: I might be not aware of some functionality in hapi/boom, if this is the case, pls point the existing functionality out for me
The text was updated successfully, but these errors were encountered: