docs: show hooks in when condition in workflows reference (#12552)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2025-05-21 13:00:15 +03:00
1 parent 13f29e5ca3
commit bda143673d
2 files changed
+90 -5

No files matched your search

@@ -176,6 +176,62 @@ 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"/> <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 ## 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. This section lists the events that are either triggered by the `emitEventStep` in the workflow, or by another workflow executed within this workflow.
@@ -1,6 +1,10 @@
import { MarkdownTheme } from "../../theme.js" import { MarkdownTheme } from "../../theme.js"
import Handlebars from "handlebars" import Handlebars from "handlebars"
import { DeclarationReflection, SignatureReflection } from "typedoc" import {
DeclarationReflection,
DocumentReflection,
SignatureReflection,
} from "typedoc"
import { cleanUpHookInput, getHookChildren, getProjectChild } from "utils" import { cleanUpHookInput, getHookChildren, getProjectChild } from "utils"
import beautifyCode from "../../utils/beautify-code.js" import beautifyCode from "../../utils/beautify-code.js"
@@ -12,9 +16,30 @@ export default function (theme: MarkdownTheme) {
return "" return ""
} }
const hooks = this.parent.documents.filter( const hooks: {
(document) => document.comment?.modifierTags.has("@hook") hook: DocumentReflection
) isNested: boolean
}[] = []
this.parent.documents.forEach((document) => {
if (document.comment?.modifierTags.has("@hook")) {
hooks.push({
hook: document,
isNested: false,
})
return
}
if (!document.comment?.modifierTags.has("@when")) {
return
}
const nestedHooks =
document.children?.filter(
(child) => child.comment?.modifierTags.has("@hook")
) || []
hooks.push(...nestedHooks.map((hook) => ({ hook, isNested: true })))
})
if (!hooks.length) { if (!hooks.length) {
return "" return ""
@@ -27,7 +52,7 @@ export default function (theme: MarkdownTheme) {
const hooksTitleLevel = Handlebars.helpers.titleLevel() const hooksTitleLevel = Handlebars.helpers.titleLevel()
const hookChildren = getHookChildren(this.parent) const hookChildren = getHookChildren(this.parent)
hooks.forEach((hook) => { hooks.forEach(({ hook, isNested }) => {
const hookReflection = const hookReflection =
hookChildren.find((child) => { hookChildren.find((child) => {
if (child.name !== hook.name || !child.comment) { if (child.name !== hook.name || !child.comment) {
@@ -70,6 +95,10 @@ export default function (theme: MarkdownTheme) {
)}\n\n` )}\n\n`
} }
if (isNested) {
str += `\n\n:::note\n\nThis 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.\n\n:::\n\n`
}
const hookExample = hookReflection.comment?.getTag(`@example`) const hookExample = hookReflection.comment?.getTag(`@example`)
if (hookExample) { if (hookExample) {