diff --git a/.changeset/smart-nails-switch.md b/.changeset/smart-nails-switch.md new file mode 100644 index 0000000000..c20f62b66b --- /dev/null +++ b/.changeset/smart-nails-switch.md @@ -0,0 +1,5 @@ +--- +"@medusajs/workflows-sdk": patch +--- + +fix(workflows=sdk): Fix StepFunction typings and custom step name diff --git a/packages/core-flows/src/definition/cart/workflows/create-carts.ts b/packages/core-flows/src/definition/cart/workflows/create-carts.ts index 6900ee85a6..d3b3ac5354 100644 --- a/packages/core-flows/src/definition/cart/workflows/create-carts.ts +++ b/packages/core-flows/src/definition/cart/workflows/create-carts.ts @@ -1,9 +1,9 @@ import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types" import { - WorkflowData, createWorkflow, parallelize, transform, + WorkflowData, } from "@medusajs/workflows-sdk" import { createCartsStep, @@ -53,6 +53,7 @@ export const createCartWorkflow = createWorkflow( // TODO: Add line items + // @ts-ignore const cart = createCartsStep([cartInput]) return cart[0] diff --git a/packages/workflows-sdk/package.json b/packages/workflows-sdk/package.json index 84ab7d68ca..5fb689f09f 100644 --- a/packages/workflows-sdk/package.json +++ b/packages/workflows-sdk/package.json @@ -40,6 +40,6 @@ "build": "rimraf dist && tsc --build", "watch": "tsc --build --watch", "test": "jest --runInBand --bail --forceExit", - "test:run": "../../node_modules/.bin/ts-node ./src/utils/_test.ts" + "test:run": "../../node_modules/.bin/ts-node ./src/utils/_playground.ts" } } diff --git a/packages/workflows-sdk/src/utils/_playground.ts b/packages/workflows-sdk/src/utils/_playground.ts index 8224af5046..f2129c1973 100644 --- a/packages/workflows-sdk/src/utils/_playground.ts +++ b/packages/workflows-sdk/src/utils/_playground.ts @@ -1,12 +1,12 @@ import { createStep, createWorkflow, StepResponse } from "./composer" const step1 = createStep("step1", async (input: {}, context) => { - return new StepResponse({ step1: "step1" }) + return new StepResponse({ step1: ["step1"] }) }) const step2 = createStep( "step2", - async (input: { test: string; test2: string }, context) => { + async (input: { filters: { id: string[] } }, context) => { return new StepResponse({ step2: input }) } ) @@ -18,24 +18,35 @@ const step3 = createStep("step3", async () => { const workflow = createWorkflow("workflow", function () { const step1Res = step1() step3() - return step2({ test: "test", test2: step1Res.step1 }) + return step2({ filters: { id: step1Res.step1 } }) }) workflow() .run({}) .then((res) => { - console.log(res.result) // result: { step2: { test: "test", test2: "step1" } } + console.log(res.result) }) -/*type type0 = typeof workflow extends ReturnWorkflow - ? T - : never +/*const step1 = createStep("step1", async (input: {}, context) => { + return new StepResponse({ step1: ["step1"] }) +}) -function run< - TWorkflow extends ReturnWorkflow, - TData = TWorkflow extends ReturnWorkflow - ? T - : never ->(name: string, options: FlowRunOptions) {} +const step2 = createStep("step2", async (input: string[], context) => { + return new StepResponse({ step2: input }) +}) -const test = run("workflow", { input: "string" })*/ +const step3 = createStep("step3", async () => { + return new StepResponse({ step3: "step3" }) +}) + +const workflow = createWorkflow("workflow", function () { + const step1Res = step1() + step3() + return step2(step1Res.step1) +}) + +workflow() + .run({}) + .then((res) => { + console.log(res.result) + })*/ diff --git a/packages/workflows-sdk/src/utils/composer/create-step.ts b/packages/workflows-sdk/src/utils/composer/create-step.ts index 0cd3142352..54e438135f 100644 --- a/packages/workflows-sdk/src/utils/composer/create-step.ts +++ b/packages/workflows-sdk/src/utils/composer/create-step.ts @@ -2,9 +2,9 @@ import { TransactionStepsDefinition, WorkflowManager, } from "@medusajs/orchestration" -import { OrchestrationUtils, isString } from "@medusajs/utils" +import { isString, OrchestrationUtils } from "@medusajs/utils" import { ulid } from "ulid" -import { StepResponse, resolveValue } from "./helpers" +import { resolveValue, StepResponse } from "./helpers" import { proxify } from "./helpers/proxy" import { CreateWorkflowComposerContext, @@ -199,6 +199,7 @@ function applyStep< ...localConfig, }) + ret.__step__ = newStepName WorkflowManager.update(this.workflowId, this.flow, this.handlers) return proxify(ret) diff --git a/packages/workflows-sdk/src/utils/composer/type.ts b/packages/workflows-sdk/src/utils/composer/type.ts index 2ef18de247..ee7ea958fa 100644 --- a/packages/workflows-sdk/src/utils/composer/type.ts +++ b/packages/workflows-sdk/src/utils/composer/type.ts @@ -8,9 +8,7 @@ import { import { Context, MedusaContainer } from "@medusajs/types" export type StepFunctionResult = - ( - this: CreateWorkflowComposerContext - ) => WorkflowData<{ [K in keyof TOutput]: TOutput[K] }> + (this: CreateWorkflowComposerContext) => WorkflowData /** * A step function to be used in a workflow. @@ -21,26 +19,13 @@ export type StepFunctionResult = export type StepFunction = (keyof TInput extends [] ? // Function that doesn't expect any input { - (): WorkflowData<{ - [K in keyof TOutput]: TOutput[K] - }> + (): WorkflowData } : // function that expects an input object { - ( - input: TInput extends object - ? { [K in keyof TInput]: WorkflowData | TInput[K] } - : WorkflowData | TInput - ): WorkflowData<{ - [K in keyof TOutput]: TOutput[K] - }> + (input: WorkflowData | TInput): WorkflowData }) & - WorkflowDataProperties<{ - [K in keyof TOutput]: TOutput[K] - }> & - WorkflowDataProperties<{ - [K in keyof TOutput]: TOutput[K] - }> + WorkflowDataProperties export type WorkflowDataProperties = { __type: Symbol @@ -56,22 +41,14 @@ export type WorkflowData = (T extends object ? { [Key in keyof T]: WorkflowData } - : WorkflowDataProperties) & + : T & WorkflowDataProperties) & WorkflowDataProperties & { config( config: { name?: string } & Omit< TransactionStepsDefinition, "next" | "uuid" | "action" > - ): T extends object - ? WorkflowData< - T extends object - ? { - [K in keyof T]: T[K] - } - : T - > - : T + ): WorkflowData } export type CreateWorkflowComposerContext = {