feature: share execution context with hook handlers (#8501)

This commit is contained in:
Harminder Virk
2024-08-08 17:18:07 +05:30
committed by GitHub
parent f831deaf74
commit 9cd66bc842
3 changed files with 17 additions and 14 deletions

View File

@@ -30,8 +30,9 @@ const workflow = createWorkflow(
}
)
workflow.hooks.something((input) => {
workflow.hooks.something((input, context) => {
console.log("input>", input)
console.log("context>", context)
})
workflow.run().then((res) => {

View File

@@ -19,12 +19,12 @@ export type Hook<Name extends string, Input> = {
}
/**
* Expose a hook in your workflow where you can inject custom functionality as a step function.
*
* Expose a hook in your workflow where you can inject custom functionality as a step function.
*
* A handler hook can later be registered to consume the hook and perform custom functionality.
*
*
* Learn more in [this documentation](https://docs.medusajs.com/v2/advanced-development/workflows/add-workflow-hook).
*
*
* @param name - The hook's name. This is used when the hook handler is registered to consume the workflow.
* @param input - The input to pass to the hook handler.
* @returns A workflow hook.
@@ -37,16 +37,16 @@ export type Hook<Name extends string, Input> = {
* WorkflowResponse,
* } from "@medusajs/workflows-sdk"
* import { createProductStep } from "./steps/create-product"
*
*
* export const myWorkflow = createWorkflow(
* "my-workflow",
* "my-workflow",
* function (input) {
* const product = createProductStep(input)
* const productCreatedHook = createHook(
* "productCreated",
* "productCreated",
* { productId: product.id }
* )
*
*
* return new WorkflowResponse(product, {
* hooks: [productCreatedHook],
* })

View File

@@ -32,7 +32,9 @@ export type HookHandler = (...args: any[]) => void | Promise<void>
type ConvertHooksToFunctions<THooks extends any[]> = {
[K in keyof THooks]: THooks[K] extends Hook<infer Name, infer Input>
? {
[Fn in Name]: (callback: (input: Input) => any) => void
[Fn in Name]: (
callback: (input: Input, context: StepExecutionContext) => any
) => void
}
: never
}[number]
@@ -218,9 +220,9 @@ export type ReturnWorkflow<TData, TResult, THooks extends any[]> = {
} & {
/**
* This method executes the workflow as a step. Useful when running a workflow within another.
*
*
* Learn more in [this documentation](https://docs.medusajs.com/v2/advanced-development/workflows/execute-another-workflow).
*
*
* @param param0 - The options to execute the workflow.
* @returns The workflow's result
*/
@@ -234,7 +236,7 @@ export type ReturnWorkflow<TData, TResult, THooks extends any[]> = {
}) => ReturnType<StepFunction<TData, TResult>>
/**
* This method executes a workflow.
*
*
* @param args - The options to execute the workflow.
* @returns Details of the workflow's execution, including its result.
*/
@@ -255,7 +257,7 @@ export type ReturnWorkflow<TData, TResult, THooks extends any[]> = {
config: (config: TransactionModelOptions) => void
/**
* The workflow's exposed hooks, used to register a handler to consume the hook.
*
*
* Learn more in [this documentation](https://docs.medusajs.com/v2/advanced-development/workflows/add-workflow-hook#how-to-consume-a-hook).
*/
hooks: ConvertHooksToFunctions<THooks>