From c2fe3f152059b5c0323190e57ffed8f762efd4b7 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 22 Apr 2025 16:27:15 +0530 Subject: [PATCH] fix: steps to return undefined and still chain the config method (#12255) Fixes: FRMW-2946 --- .changeset/popular-windows-agree.md | 5 +++ .../workflows-sdk/src/utils/_playground.ts | 14 ++++++ .../src/utils/composer/__tests__/compose.ts | 44 +++++-------------- .../workflows-sdk/src/utils/composer/type.ts | 6 ++- 4 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 .changeset/popular-windows-agree.md diff --git a/.changeset/popular-windows-agree.md b/.changeset/popular-windows-agree.md new file mode 100644 index 0000000000..d3657ba0d4 --- /dev/null +++ b/.changeset/popular-windows-agree.md @@ -0,0 +1,5 @@ +--- +"@medusajs/workflows-sdk": patch +--- + +fix: steps to return undefined and still chain the config method diff --git a/packages/core/workflows-sdk/src/utils/_playground.ts b/packages/core/workflows-sdk/src/utils/_playground.ts index fdca4027ce..70ee2ecca9 100644 --- a/packages/core/workflows-sdk/src/utils/_playground.ts +++ b/packages/core/workflows-sdk/src/utils/_playground.ts @@ -21,6 +21,14 @@ const step3 = createStep("step3", async function (_, context) { return new StepResponse({ step3: "step3" }) }) +const step4 = createStep("step4", async function (_, context) { + return new StepResponse({ id: 1 }) +}) + +const step5 = createStep("step5", async function (_, context) { + return new StepResponse(void 0) +}) + const workflow = createWorkflow( "sub-workflow", function (input: WorkflowData<{ outsideWorkflowData: string }>) { @@ -37,6 +45,12 @@ const workflow = createWorkflow( } ) + const step4Result = step4().config({ name: "foo" }) + step4Result + + const step5Result = step5().config({ name: "foo" }) + step5Result + return new WorkflowResponse( { r: somethingHook.getResult(), step3: step3() }, { hooks: [somethingHook] } diff --git a/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts b/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts index 276071f2d4..21a5f3ce75 100644 --- a/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts +++ b/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts @@ -852,7 +852,7 @@ describe("Workflow composer", function () { ) const step4 = createStep( "step4", - mockStep4Fn as unknown as StepFunction, + mockStep4Fn as unknown as StepFunction>, step4CompensationFn ) @@ -923,7 +923,7 @@ describe("Workflow composer", function () { ) const step4 = createStep( "step4", - mockStep4Fn as unknown as StepFunction, + mockStep4Fn as unknown as StepFunction>, step4CompensationFn ) @@ -2002,14 +2002,10 @@ describe("Workflow composer", function () { mockStep3Fn as unknown as StepFunction>, step3CompensationFn ) - const step4 = createStep( - "step4", - mockStep4Fn as unknown as StepFunction, - step4CompensationFn - ) + const step4 = createStep("step4", mockStep4Fn, step4CompensationFn) const workflow = createWorkflow("workflow1", function (input) { - const [step1Res] = parallelize(step1(), step2(), step3(), step4()) + const [step1Res] = parallelize(step1(), step2(), step3(), step4({})) return new WorkflowResponse(step1Res) }) @@ -2058,33 +2054,17 @@ describe("Workflow composer", function () { throw new Error("An error occured in step 4.") }) - const step1 = createStep( - "step1", - mockStep1Fn as unknown as StepFunction>, - step1CompensationFn - ) - const step2 = createStep( - "step2", - mockStep2Fn as unknown as StepFunction>, - step2CompensationFn - ) - const step3 = createStep( - "step3", - mockStep3Fn as unknown as StepFunction>, - step3CompensationFn - ) - const step4 = createStep( - "step4", - mockStep4Fn as unknown as StepFunction, - step4CompensationFn - ) + const step1 = createStep("step1", mockStep1Fn, step1CompensationFn) + const step2 = createStep("step2", mockStep2Fn, step2CompensationFn) + const step3 = createStep("step3", mockStep3Fn, step3CompensationFn) + const step4 = createStep("step4", mockStep4Fn, step4CompensationFn) const workflow = createWorkflow("workflow1", function (input) { const [step1Res] = parallelize( - step1().config({ name: "newStep1Name" }), - step2(), - step3(), - step4() + step1({}).config({ name: "newStep1Name" }), + step2({}), + step3({}), + step4({}) ) return new WorkflowResponse(step1Res) }) diff --git a/packages/core/workflows-sdk/src/utils/composer/type.ts b/packages/core/workflows-sdk/src/utils/composer/type.ts index 9c7ef06b1e..843418ac74 100644 --- a/packages/core/workflows-sdk/src/utils/composer/type.ts +++ b/packages/core/workflows-sdk/src/utils/composer/type.ts @@ -50,6 +50,8 @@ type ConvertHooksToFunctions = THooks extends [ ? ConvertHookToObject & ConvertHooksToFunctions : {} +export type Void = { " $$type": "void" } + /** * A step function to be used in a workflow. * @@ -62,7 +64,9 @@ export type StepFunction< > = (KeysOfUnion extends [] ? // Function that doesn't expect any input { - (): WorkflowData & StepFunctionReturnConfig + (): TOutput & {} extends never + ? WorkflowData & StepFunctionReturnConfig + : WorkflowData & StepFunctionReturnConfig } : // function that expects an input object {