You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building an internal ecommerce software for my company using Nuxt and Nitro. I'm planning on adding a plugin system to it that allows extending the behavior of the core.
I would like to add certain hooks for most core processes such as order:before-create and order:after-create to which plugins can hook into and either perform an action, or provide new data to override the core functionality.
Example
I install a GiftPlugin which will add a gift to every order above $100. To achieve this, I would have a POST /api/orders endpoint at the core of the system, and before saving the order to the database I would call the order:before-create hook with the core order data as an argument.
The GiftPlugin will add the gift product to the line items and return the new order data. The order returned by the plugin is the one that should be saved to the database.
Also, the core should allow multiple plugins to edit the order sequentially and each hook would receive the result from the previous plugin.
What I tried
I added a hook to my Nitro app called order:before-create and called it like this:
/** POST /api/orders */exportdefaultdefineEventHandler(async(event)=>{constnitro=useNitroApp();// Create core orderconstcoreOrder: Order={/* Order fields... */};/** Before create hook */constorderData=awaitnitro.hooks.callHookWith(async(hooks,args)=>{lethookResult: Order=args[0];for(consthookofhooks){hookResult=awaithook(hookResult);}returnhookResult;},'orders:before-create',coreOrder,);// Save the order after hooksawaitorderData.save();// Other core functionality...return{id: coreOrder.id};});
I would love to know if this is an adequate implementation of what I'm trying to achieve, if there's something I'm doing wrong or if hookable is even the right choice for something like this.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi!
I'm building an internal ecommerce software for my company using Nuxt and Nitro. I'm planning on adding a plugin system to it that allows extending the behavior of the core.
I would like to add certain hooks for most core processes such as
order:before-create
andorder:after-create
to which plugins can hook into and either perform an action, or provide new data to override the core functionality.Example
I install a
GiftPlugin
which will add a gift to every order above $100. To achieve this, I would have aPOST /api/orders
endpoint at the core of the system, and before saving the order to the database I would call theorder:before-create
hook with the core order data as an argument.The
GiftPlugin
will add the gift product to the line items and return the new order data. The order returned by the plugin is the one that should be saved to the database.Also, the core should allow multiple plugins to edit the order sequentially and each hook would receive the result from the previous plugin.
What I tried
I added a hook to my Nitro app called
order:before-create
and called it like this:I would love to know if this is an adequate implementation of what I'm trying to achieve, if there's something I'm doing wrong or if
hookable
is even the right choice for something like this.Thank you!
Beta Was this translation helpful? Give feedback.
All reactions