Custom handler implementation #3
Labels
No Label
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: thereadme/hapi-sequelize-crud#3
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
So I was thinking on using this in one of the projects I am working on. Given our requirements, I would have to add some extra features.
The first of it is related with custom serializers. We plan to port the project in the future to jsonapi and I am also aware that in some situations people want to format the endpoints in a way that makes easier the frontend implementation.
The first idea I had was to make use of some functional composition. Allowing the developers to add to the options a map function that would let them change the format of the reply.
Let me know if is something of interest.
Good idea for sure, what do you think about the modifier implementation described here?
This way the developer can modify the input and output of a function, along with an optional middleware.
I'm not sure what action we're going to modify using middlewares here though, ideas?
I think we can use the before and after create, read, update and delete actions.
The before hooks would allow for parsing payloads and url parameters from the request.
The after actions would allow for mapping the sequelize responses.
Maybe is a premature optimization but allowing the developer to specify the models in which those middleware should run when defining them will improve the runtime performance and the overall implementation. (As they won't need to check which model triggered the action)
The options object could receive an array of objects defining the middleware like:
If some of the fields are not specified the library should provide default implementations. At the same the ones that appear first in the array should override the behaviour of the remaining.
This is just a draft idea, so don't hesitate to throw it away and propose something that would fit this better.😃
One thing I left in the previous one:
the pre and post function should be maybe like this:
So the user can interrupt the flow or do async tasks.
Yeah, the plan looks good.
I would suggest using Promises instead of
next
, or maybe we can have both.The new ES version are praising Promises by introducing
async/await
, so I think that's a good way to go.Yep, that was my idea. If the developer returns a promise we use it. Note that
async/await
are es7 features not widely used at the moment.Once you give green light I'll start working on a first PR ;)
@Istar-Eldritch I'm asking my colleagues to review the idea to see if there are any flaws / any room for improvement, will notify you about it. Thank you Ruben! 🙏
@mdibaiee Sure! Take the time you need, there's no rush. 😉
Sorry for the long delay! 🙏
I think this is okay and we can proceed! Thank you Ruben!
@Istar-Eldritch FYI: I recently encountered a similar problem and solved it using standard Hapi settings. I added what I learned to the docs: https://github.com/mdibaiee/hapi-sequelize-crud/tree/master#modify-the-response-format
@Istar-Eldritch I just ran into the problem where I needed a "pre" hook and sorted it out with built-in hapi methods. I've added what I learned to the docs and closed this issue. Feel free to re-open if needed!
@joeybaker Looks good to me.
Sorry for not replying before.
You may be interested as well in the default event lifecycle builtin hapi. Adding a
onPostHandler
oronPreResponse
may provider extra functionality than the schema. You can see the whole hapi lifecycle hereIf you set a tag in the route settings you are able to identify this route from all the handlers checking if the tag exists in
req.route.settings.tags
. This is the method I was using to achieve this. So was able to transform the data in ways the schema does not allow like changing the structure or decorating it with extra information.Ok, just found you have an entry as well for this in the main README.
Yup! Sorry for not linking to it before!