fix: steps to return undefined and still chain the config method (#12255)

Fixes: FRMW-2946
This commit is contained in:
Harminder Virk
2025-04-22 16:27:15 +05:30
committed by GitHub
parent 38710c116e
commit c2fe3f1520
4 changed files with 36 additions and 33 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix: steps to return undefined and still chain the config method

View File

@@ -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<undefined | { id: number }>({ 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] }

View File

@@ -852,7 +852,7 @@ describe("Workflow composer", function () {
)
const step4 = createStep(
"step4",
mockStep4Fn as unknown as StepFunction<never, never>,
mockStep4Fn as unknown as StepFunction<never, StepResponse<void>>,
step4CompensationFn
)
@@ -923,7 +923,7 @@ describe("Workflow composer", function () {
)
const step4 = createStep(
"step4",
mockStep4Fn as unknown as StepFunction<never, never>,
mockStep4Fn as unknown as StepFunction<never, StepResponse<void>>,
step4CompensationFn
)
@@ -2002,14 +2002,10 @@ describe("Workflow composer", function () {
mockStep3Fn as unknown as StepFunction<never, StepResponse<string>>,
step3CompensationFn
)
const step4 = createStep(
"step4",
mockStep4Fn as unknown as StepFunction<never, never>,
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<never, StepResponse<string>>,
step1CompensationFn
)
const step2 = createStep(
"step2",
mockStep2Fn as unknown as StepFunction<never, StepResponse<string>>,
step2CompensationFn
)
const step3 = createStep(
"step3",
mockStep3Fn as unknown as StepFunction<never, StepResponse<string>>,
step3CompensationFn
)
const step4 = createStep(
"step4",
mockStep4Fn as unknown as StepFunction<never, never>,
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)
})

View File

@@ -50,6 +50,8 @@ type ConvertHooksToFunctions<THooks extends any[]> = THooks extends [
? ConvertHookToObject<A> & ConvertHooksToFunctions<R>
: {}
export type Void = { " $$type": "void" }
/**
* A step function to be used in a workflow.
*
@@ -62,7 +64,9 @@ export type StepFunction<
> = (KeysOfUnion<TInput> extends []
? // Function that doesn't expect any input
{
(): WorkflowData<TOutput> & StepFunctionReturnConfig<TOutput>
(): TOutput & {} extends never
? WorkflowData<Void> & StepFunctionReturnConfig<TOutput>
: WorkflowData<TOutput> & StepFunctionReturnConfig<TOutput>
}
: // function that expects an input object
{