on GitHub" data-tooltip-id=":Rblcldtb:">v2.6·
In this chapter, you'll learn how to expose a hook in your workflow.
To expose a hook in your workflow, use createHook
from the Workflows SDK.
For example:
1import {2 createStep,3 createHook,4 createWorkflow,5 WorkflowResponse,6} from "@medusajs/framework/workflows-sdk"7import { createProductStep } from "./steps/create-product"8 9export const myWorkflow = createWorkflow(10 "my-workflow", 11 function (input) {12 const product = createProductStep(input)13 const productCreatedHook = createHook(14 "productCreated", 15 { productId: product.id }16 )17 18 return new WorkflowResponse(product, {19 hooks: [productCreatedHook],20 })21 }22)
The createHook
function accepts two parameters:
The workflow must also pass an object having a hooks
property as a second parameter to the WorkflowResponse
constructor. Its value is an array of the workflow's hooks.
To consume the hook of the workflow, create the file src/workflows/hooks/my-workflow.ts
with the following content:
The hook is available on the workflow's hooks
property using its name productCreated
.
You invoke the hook, passing a step function (the hook handler) as a parameter.