docs: add support for ignoring hooks (#12599)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2025-05-23 18:48:08 +03:00
1 parent 117fc25aea
commit 8303aed701
2 files changed
+33 -70

No files matched your search

@@ -29,7 +29,7 @@ You can use this workflow within your own customizations or custom workflows, al
For example, in the [Subscriptions recipe](https://docs.medusajs.com/resources/recipes/subscriptions/examples/standard#create-workflow),
this workflow is used within another workflow that creates a subscription order.
<SourceCodeLink link="https://github.com/medusajs/medusa/blob/4936315b2dc777fbaaf2869b16bf48602fc7e69b/packages/core/core-flows/src/cart/workflows/complete-cart.ts#L90" />
<SourceCodeLink link="https://github.com/medusajs/medusa/blob/117fc25aeaae3e4c8708b03c79c37e876c603cd6/packages/core/core-flows/src/cart/workflows/complete-cart.ts#L90" />
## Examples
@@ -138,7 +138,7 @@ const myWorkflow = createWorkflow(
## Steps
<WorkflowDiagram workflow={{"name":"completeCartWorkflow","steps":[{"type":"step","name":"useQueryGraphStep","description":"This step fetches data across modules using the Query.\n\nLearn more in the [Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx","depth":1},{"type":"step","name":"useRemoteQueryStep","description":"This step fetches data across modules using the remote query.\n\nLearn more in the [Remote Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).\n\n:::note\n\nThis step is deprecated. Use [useQueryGraphStep](../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx) instead.\n\n:::","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useRemoteQueryStep/page.mdx","depth":2},{"type":"step","name":"validateCartPaymentsStep","description":"This step validates a cart's payment sessions. Their status must\nbe `pending` or `requires_more`. If not valid, the step throws an error.\n\n:::tip\n\nYou can use the [retrieveCartStep](../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.retrieveCartStep/page.mdx) to retrieve a cart's details.\n\n:::","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.validateCartPaymentsStep/page.mdx","depth":3},{"type":"hook","name":"validate","description":"This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.","link":"#validate","depth":4},{"type":"when","condition":"{\n return !orderId\n }","depth":"5","steps":[{"type":"step","name":"useRemoteQueryStep","description":"This step fetches data across modules using the remote query.\n\nLearn more in the [Remote Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).\n\n:::note\n\nThis step is deprecated. Use [useQueryGraphStep](../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx) instead.\n\n:::","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useRemoteQueryStep/page.mdx","depth":5},{"type":"step","name":"validateShippingStep","description":"This step validates shipping data when cart is completed.\n\nIt ensures that a shipping method is selected if there is an item in the cart that requires shipping.\nIt also ensures that product's shipping profile mathes the selected shipping options. If the\nconditions are not met, an error is thrown.\n\n:::note\n\nYou can retrieve cart or shipping option's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),\nor [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).\n\n:::","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.validateShippingStep/page.mdx","depth":5},{"type":"step","name":"createOrdersStep","description":"This step creates one or more orders.","link":"../../../../Order/Steps_Order/functions/core_flows.Order.Steps_Order.createOrdersStep/page.mdx","depth":5},{"type":"step","name":"createRemoteLinkStep","description":"This step creates remote links between two records of linked data models.\n\nLearn more in the [Remote Link documentation.](https://docs.medusajs.com/learn/fundamentals/module-links/remote-link#create-link).","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.createRemoteLinkStep/page.mdx","depth":5},{"type":"step","name":"updateCartsStep","description":"This step updates a cart.","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.updateCartsStep/page.mdx","depth":5},{"type":"step","name":"reserveInventoryStep","description":"This step reserves the quantity of line items from the associated\nvariant's inventory.","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.reserveInventoryStep/page.mdx","depth":5},{"type":"step","name":"emitEventStep","description":"Emit an event.","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.emitEventStep/page.mdx","depth":5},{"type":"hook","name":"beforePaymentAuthorization","description":"This step is a hook that you can inject custom functionality into.","link":"#beforePaymentAuthorization","depth":5},{"type":"step","name":"authorizePaymentSessionStep","description":"This step authorizes a payment session.","link":"../../../../Payment/Steps_Payment/functions/core_flows.Payment.Steps_Payment.authorizePaymentSessionStep/page.mdx","depth":5},{"type":"step","name":"addOrderTransactionStep","description":"This step creates order transactions.","link":"../../../../Order/Steps_Order/functions/core_flows.Order.Steps_Order.addOrderTransactionStep/page.mdx","depth":5},{"type":"hook","name":"orderCreated","description":"This step is a hook that Line truncated
<WorkflowDiagram workflow={{"name":"completeCartWorkflow","steps":[{"type":"step","name":"useQueryGraphStep","description":"This step fetches data across modules using the Query.\n\nLearn more in the [Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx","depth":1},{"type":"step","name":"useRemoteQueryStep","description":"This step fetches data across modules using the remote query.\n\nLearn more in the [Remote Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).\n\n:::note\n\nThis step is deprecated. Use [useQueryGraphStep](../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx) instead.\n\n:::","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useRemoteQueryStep/page.mdx","depth":2},{"type":"step","name":"validateCartPaymentsStep","description":"This step validates a cart's payment sessions. Their status must\nbe `pending` or `requires_more`. If not valid, the step throws an error.\n\n:::tip\n\nYou can use the [retrieveCartStep](../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.retrieveCartStep/page.mdx) to retrieve a cart's details.\n\n:::","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.validateCartPaymentsStep/page.mdx","depth":3},{"type":"hook","name":"validate","description":"This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.","link":"#validate","depth":4},{"type":"when","condition":"{\n return !orderId\n }","depth":"5","steps":[{"type":"step","name":"useRemoteQueryStep","description":"This step fetches data across modules using the remote query.\n\nLearn more in the [Remote Query documentation](https://docs.medusajs.com/learn/fundamentals/module-links/query).\n\n:::note\n\nThis step is deprecated. Use [useQueryGraphStep](../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useQueryGraphStep/page.mdx) instead.\n\n:::","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.useRemoteQueryStep/page.mdx","depth":5},{"type":"step","name":"validateShippingStep","description":"This step validates shipping data when cart is completed.\n\nIt ensures that a shipping method is selected if there is an item in the cart that requires shipping.\nIt also ensures that product's shipping profile mathes the selected shipping options. If the\nconditions are not met, an error is thrown.\n\n:::note\n\nYou can retrieve cart or shipping option's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),\nor [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).\n\n:::","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.validateShippingStep/page.mdx","depth":5},{"type":"step","name":"createOrdersStep","description":"This step creates one or more orders.","link":"../../../../Order/Steps_Order/functions/core_flows.Order.Steps_Order.createOrdersStep/page.mdx","depth":5},{"type":"step","name":"createRemoteLinkStep","description":"This step creates remote links between two records of linked data models.\n\nLearn more in the [Remote Link documentation.](https://docs.medusajs.com/learn/fundamentals/module-links/remote-link#create-link).","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.createRemoteLinkStep/page.mdx","depth":5},{"type":"step","name":"updateCartsStep","description":"This step updates a cart.","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.updateCartsStep/page.mdx","depth":5},{"type":"step","name":"reserveInventoryStep","description":"This step reserves the quantity of line items from the associated\nvariant's inventory.","link":"../../../Steps_Cart/functions/core_flows.Cart.Steps_Cart.reserveInventoryStep/page.mdx","depth":5},{"type":"step","name":"emitEventStep","description":"Emit an event.","link":"../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.emitEventStep/page.mdx","depth":5},{"type":"step","name":"authorizePaymentSessionStep","description":"This step authorizes a payment session.","link":"../../../../Payment/Steps_Payment/functions/core_flows.Payment.Steps_Payment.authorizePaymentSessionStep/page.mdx","depth":5},{"type":"step","name":"addOrderTransactionStep","description":"This step creates order transactions.","link":"../../../../Order/Steps_Order/functions/core_flows.Order.Steps_Order.addOrderTransactionStep/page.mdx","depth":5}]}]}} />
## Input
@@ -176,62 +176,6 @@ Handlers consuming this hook accept the following input.
<TypeList types={[{"name":"input","type":"[input](../../../core_flows.Cart.Workflows_Cart/page.mdx#__type-3)","description":"The input data for the hook.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"input","type":"[CompleteCartWorkflowInput](../../../../types/core_flows.CompleteCartWorkflowInput/page.mdx)","description":"The data to complete a cart and place an order.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the cart to complete.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"cart","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" openedLevel={1} sectionTitle="validate"/>
### beforePaymentAuthorization
This step is a hook that you can inject custom functionality into.
:::note
This hook is nested within a [when](https://docs.medusajs.com/learn/fundamentals/workflows/conditions) condition, so it may not be executed if the when condition isn't satisfied.
:::
#### Example
```ts
import { completeCartWorkflow } from "@medusajs/medusa/core-flows"
completeCartWorkflow.hooks.beforePaymentAuthorization(
(async ({}, { container }) => {
//TODO
})
)
```
#### Input
Handlers consuming this hook accept the following input.
<TypeList types={[{"name":"input","type":"[input](../../../core_flows.Cart.Workflows_Cart/page.mdx#__type-3)","description":"The input data for the hook.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"input","type":"[CompleteCartWorkflowInput](../../../../types/core_flows.CompleteCartWorkflowInput/page.mdx)","description":"The data to complete a cart and place an order.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the cart to complete.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"cart","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" openedLevel={1} sectionTitle="beforePaymentAuthorization"/>
### orderCreated
This step is a hook that you can inject custom functionality into.
:::note
This hook is nested within a [when](https://docs.medusajs.com/learn/fundamentals/workflows/conditions) condition, so it may not be executed if the when condition isn't satisfied.
:::
#### Example
```ts
import { completeCartWorkflow } from "@medusajs/medusa/core-flows"
completeCartWorkflow.hooks.orderCreated(
(async ({}, { container }) => {
//TODO
})
)
```
#### Input
Handlers consuming this hook accept the following input.
<TypeList types={[{"name":"input","type":"[input](../../../core_flows.Cart.Workflows_Cart/page.mdx#__type-3)","description":"The input data for the hook.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"input","type":"[CompleteCartWorkflowInput](../../../../types/core_flows.CompleteCartWorkflowInput/page.mdx)","description":"The data to complete a cart and place an order.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the cart to complete.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"cart","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" openedLevel={1} sectionTitle="orderCreated"/>
## Emitted Events
This section lists the events that are either triggered by the `emitEventStep` in the workflow, or by another workflow executed within this workflow.
@@ -303,20 +303,39 @@ class WorkflowsPlugin {
if (stepType === "hook" && "symbol" in initializer.arguments[1]) {
// get the hook's name from the first argument
stepId = this.helper.normalizeName(initializer.arguments[0].getText())
const hookArgumetSymbol = this.getHookArgumentSymbol({
argument: initializer.arguments[1],
constructorFn,
})
const shouldIgnore = ts
.getJSDocCommentsAndTags(initializer.parent)
.some((comment) => {
if (!("tags" in comment)) {
return false
}
if (hookArgumetSymbol) {
stepReflection = this.assembleHookReflection({
stepId,
context,
inputSymbol: hookArgumetSymbol,
workflowName: workflowVarName,
workflowComments,
workflowReflection: workflowReflection.parent,
return comment.tags?.some(
(tag) => tag.tagName.getText() === "ignore"
)
})
if (stepId === "beforePaymentAuthorization") {
console.log(
shouldIgnore,
ts.getJSDocCommentsAndTags(initializer.parent)
)
}
if (!shouldIgnore) {
const hookArgumetSymbol = this.getHookArgumentSymbol({
argument: initializer.arguments[1],
constructorFn,
})
if (hookArgumetSymbol) {
stepReflection = this.assembleHookReflection({
stepId,
context,
inputSymbol: hookArgumetSymbol,
workflowName: workflowVarName,
workflowComments,
workflowReflection: workflowReflection.parent,
})
}
}
} else {
const initializerReflection = findReflectionInNamespaces(