fix(modules-sdk): create workflow returning step transformer (#6102)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/workflows-sdk": patch
|
||||
---
|
||||
|
||||
create workflow can return destructured properties of a step
|
||||
@@ -1980,4 +1980,110 @@ describe("Workflow composer", function () {
|
||||
expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput)
|
||||
expect(mockCompensateSte1.mock.calls[0][0]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should compose a workflow that returns destructured properties", async () => {
|
||||
const step = function () {
|
||||
return new StepResponse({
|
||||
propertyNotReturned: 1234,
|
||||
property: {
|
||||
complex: {
|
||||
nested: 123,
|
||||
},
|
||||
a: "bc",
|
||||
},
|
||||
obj: "return from 2",
|
||||
})
|
||||
}
|
||||
|
||||
const step1 = createStep("step1", step)
|
||||
|
||||
const workflow = createWorkflow("workflow1", function () {
|
||||
const { property, obj } = step1()
|
||||
|
||||
return { someOtherName: property, obj }
|
||||
})
|
||||
|
||||
const { result } = await workflow().run({
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
someOtherName: {
|
||||
complex: {
|
||||
nested: 123,
|
||||
},
|
||||
a: "bc",
|
||||
},
|
||||
obj: "return from 2",
|
||||
})
|
||||
})
|
||||
|
||||
it("should compose a workflow that returns an array of steps", async () => {
|
||||
const step1 = createStep("step1", () => {
|
||||
return new StepResponse({
|
||||
obj: "return from 1",
|
||||
})
|
||||
})
|
||||
const step2 = createStep("step2", () => {
|
||||
return new StepResponse({
|
||||
obj: "returned from 2**",
|
||||
})
|
||||
})
|
||||
|
||||
const workflow = createWorkflow("workflow1", function () {
|
||||
const s1 = step1()
|
||||
const s2 = step2()
|
||||
|
||||
return [s1, s2]
|
||||
})
|
||||
|
||||
const { result } = await workflow().run({
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
obj: "return from 1",
|
||||
},
|
||||
{
|
||||
obj: "returned from 2**",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should compose a workflow that returns an object mixed of steps and properties", async () => {
|
||||
const step1 = createStep("step1", () => {
|
||||
return new StepResponse({
|
||||
obj: {
|
||||
nested: "nested",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
const step2 = createStep("step2", () => {
|
||||
return new StepResponse({
|
||||
obj: "returned from 2**",
|
||||
})
|
||||
})
|
||||
|
||||
const workflow = createWorkflow("workflow1", function () {
|
||||
const { obj } = step1()
|
||||
const s2 = step2()
|
||||
|
||||
return [{ step1_nested_obj: obj.nested }, s2]
|
||||
})
|
||||
|
||||
const { result } = await workflow().run({
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
step1_nested_obj: "nested",
|
||||
},
|
||||
{
|
||||
obj: "returned from 2**",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
|
||||
|
||||
import { MedusaModule } from "@medusajs/modules-sdk"
|
||||
import { OrchestrationUtils } from "@medusajs/utils"
|
||||
import { EOL } from "os"
|
||||
import { ulid } from "ulid"
|
||||
import { MedusaWorkflow } from "../medusa-workflow"
|
||||
@@ -129,33 +128,11 @@ export const exportWorkflow = <TData = unknown, TResult = unknown>(
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
|
||||
let result: any = undefined
|
||||
|
||||
const resFrom =
|
||||
resultFrom?.__type === OrchestrationUtils.SymbolWorkflowStep
|
||||
? resultFrom.__step__
|
||||
: resultFrom
|
||||
|
||||
if (resFrom) {
|
||||
if (Array.isArray(resFrom)) {
|
||||
result = resFrom.map((from) => {
|
||||
const res = transaction.getContext().invoke?.[from]
|
||||
return res?.__type === OrchestrationUtils.SymbolWorkflowWorkflowData
|
||||
? res.output
|
||||
: res
|
||||
})
|
||||
} else {
|
||||
const res = transaction.getContext().invoke?.[resFrom]
|
||||
result =
|
||||
res?.__type === OrchestrationUtils.SymbolWorkflowWorkflowData
|
||||
? res.output
|
||||
: res
|
||||
}
|
||||
|
||||
const ret = result || resFrom
|
||||
result = options?.wrappedInput
|
||||
? await resolveValue(ret, transaction.getContext())
|
||||
: ret
|
||||
let result
|
||||
if (options?.wrappedInput) {
|
||||
result = await resolveValue(resultFrom, transaction.getContext())
|
||||
} else {
|
||||
result = transaction.getContext().invoke?.[resultFrom]
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user