Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: share execution context with hook handlers #8501

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/workflows-sdk/src/utils/_playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/core/workflows-sdk/src/utils/composer/create-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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],
* })
Expand Down
12 changes: 7 additions & 5 deletions packages/core/workflows-sdk/src/utils/composer/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
*/
Expand All @@ -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.
*/
Expand All @@ -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>
Expand Down
Loading