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 Content hooks #2267
Add Content hooks #2267
Conversation
I think the potential of this PR is limited because most of the time I need to sanitize data, I need to do so in the context of a Request. |
Why does that make it of limited use? That's the exact use case this is for. Every request that I receive will now be automatically sanitized, and every time I create a DTO, to send back, it'll be sanitized. I don't have to manually call it any longer, and I can't forget to call it. |
The limitation is in the lack of context available within this function. Either it's part of the Content conformant type, or you don't have access. |
You could make the calls look like this: mutating func beforeEncode(into response: Response, for request: Request) throws
mutating func afterDecode(into response: Response, for request: Request) throws That way you can know what user is logged in, from the Request. Either through the headers (token) or through the associated |
One more note; I believe this to be a very subjective PR. So I can see it being delayed until after the Vapor 4 release. But it's such a common use case that I personally do feel that it deserves to be in the core framework in some form. |
Doesn't |
On the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 This looks good to me. Could you add a section to https://github.com/vapor/docs/blob/master/4.0/docs/content.md explaining how to use this new feature? I'd like to merge docs + new API at the same time so we can include a link in the release notes.
Docs PR: vapor/docs#471 |
These changes are now available in 4.2.0 |
These changes are now available in 4.3.0 |
* Adds automatic calls to beforeEncode() and afterDecode() on Content * Added doc comment
Adds optional
beforeEncode
andafterDecode
methods toContent
protocol (#2267).Docs: https://docs.vapor.codes/4.0/content/#hooks