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

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

No files matched your search

@@ -30,8 +30,9 @@ const workflow = createWorkflow(
} }
) )
workflow.hooks.something((input) => { workflow.hooks.something((input, context) => {
console.log("input>", input) console.log("input>", input)
console.log("context>", context)
}) })
workflow.run().then((res) => { workflow.run().then((res) => {
@@ -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. * 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). * 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 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. * @param input - The input to pass to the hook handler.
* @returns A workflow hook. * @returns A workflow hook.
@@ -37,16 +37,16 @@ export type Hook<Name extends string, Input> = {
* WorkflowResponse, * WorkflowResponse,
* } from "@medusajs/workflows-sdk" * } from "@medusajs/workflows-sdk"
* import { createProductStep } from "./steps/create-product" * import { createProductStep } from "./steps/create-product"
* *
* export const myWorkflow = createWorkflow( * export const myWorkflow = createWorkflow(
* "my-workflow", * "my-workflow",
* function (input) { * function (input) {
* const product = createProductStep(input) * const product = createProductStep(input)
* const productCreatedHook = createHook( * const productCreatedHook = createHook(
* "productCreated", * "productCreated",
* { productId: product.id } * { productId: product.id }
* ) * )
* *
* return new WorkflowResponse(product, { * return new WorkflowResponse(product, {
* hooks: [productCreatedHook], * hooks: [productCreatedHook],
* }) * })
@@ -32,7 +32,9 @@ export type HookHandler = (...args: any[]) => void | Promise<void>
type ConvertHooksToFunctions<THooks extends any[]> = { type ConvertHooksToFunctions<THooks extends any[]> = {
[K in keyof THooks]: THooks[K] extends Hook<infer Name, infer Input> [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 : never
}[number] }[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. * 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). * Learn more in [this documentation](https://docs.medusajs.com/v2/advanced-development/workflows/execute-another-workflow).
* *
* @param param0 - The options to execute the workflow. * @param param0 - The options to execute the workflow.
* @returns The workflow's result * @returns The workflow's result
*/ */
@@ -234,7 +236,7 @@ export type ReturnWorkflow<TData, TResult, THooks extends any[]> = {
}) => ReturnType<StepFunction<TData, TResult>> }) => ReturnType<StepFunction<TData, TResult>>
/** /**
* This method executes a workflow. * This method executes a workflow.
* *
* @param args - The options to execute the workflow. * @param args - The options to execute the workflow.
* @returns Details of the workflow's execution, including its result. * @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 config: (config: TransactionModelOptions) => void
/** /**
* The workflow's exposed hooks, used to register a handler to consume the hook. * 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). * Learn more in [this documentation](https://docs.medusajs.com/v2/advanced-development/workflows/add-workflow-hook#how-to-consume-a-hook).
*/ */
hooks: ConvertHooksToFunctions<THooks> hooks: ConvertHooksToFunctions<THooks>