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

fix(workflows-sdk): Fix StepFunction typings and custom step name #6468

Merged
merged 6 commits into from
Feb 22, 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
5 changes: 5 additions & 0 deletions .changeset/smart-nails-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---

fix(workflows=sdk): Fix StepFunction typings and custom step name
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
WorkflowData,
} from "@medusajs/workflows-sdk"
import {
createCartsStep,
Expand Down Expand Up @@ -53,6 +53,7 @@ export const createCartWorkflow = createWorkflow(

// TODO: Add line items

// @ts-ignore
const cart = createCartsStep([cartInput])

return cart[0]
Expand Down
2 changes: 1 addition & 1 deletion packages/workflows-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
adrien2p marked this conversation as resolved.
Show resolved Hide resolved
}
}
39 changes: 25 additions & 14 deletions packages/workflows-sdk/src/utils/_playground.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
)
Expand All @@ -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<infer T, infer R, infer X>
? T
: never
/*const step1 = createStep("step1", async (input: {}, context) => {
return new StepResponse({ step1: ["step1"] })
})

const step2 = createStep("step2", async (input: string[], context) => {
return new StepResponse({ step2: input })
})

const step3 = createStep("step3", async () => {
return new StepResponse({ step3: "step3" })
})

function run<
TWorkflow extends ReturnWorkflow<any, any, any>,
TData = TWorkflow extends ReturnWorkflow<infer T, infer R, infer X>
? T
: never
>(name: string, options: FlowRunOptions<TData>) {}
const workflow = createWorkflow("workflow", function () {
const step1Res = step1()
step3()
return step2(step1Res.step1)
})

const test = run<typeof workflow>("workflow", { input: "string" })*/
workflow()
.run({})
.then((res) => {
console.log(res.result)
})*/
5 changes: 3 additions & 2 deletions packages/workflows-sdk/src/utils/composer/create-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -199,6 +199,7 @@ function applyStep<
...localConfig,
})

ret.__step__ = newStepName
WorkflowManager.update(this.workflowId, this.flow, this.handlers)

return proxify(ret)
Expand Down
35 changes: 6 additions & 29 deletions packages/workflows-sdk/src/utils/composer/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
import { Context, MedusaContainer } from "@medusajs/types"

export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> =
(
this: CreateWorkflowComposerContext
) => WorkflowData<{ [K in keyof TOutput]: TOutput[K] }>
(this: CreateWorkflowComposerContext) => WorkflowData<TOutput>

/**
* A step function to be used in a workflow.
Expand All @@ -21,26 +19,13 @@ export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> =
export type StepFunction<TInput, TOutput = unknown> = (keyof TInput extends []
? // Function that doesn't expect any input
{
(): WorkflowData<{
[K in keyof TOutput]: TOutput[K]
}>
(): WorkflowData<TOutput>
}
: // function that expects an input object
{
(
input: TInput extends object
? { [K in keyof TInput]: WorkflowData<TInput[K]> | TInput[K] }
: WorkflowData<TInput> | TInput
): WorkflowData<{
[K in keyof TOutput]: TOutput[K]
}>
(input: WorkflowData<TInput> | TInput): WorkflowData<TOutput>
}) &
WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}> &
WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}>
WorkflowDataProperties<TOutput>

export type WorkflowDataProperties<T = unknown> = {
__type: Symbol
Expand All @@ -56,22 +41,14 @@ export type WorkflowData<T = unknown> = (T extends object
? {
[Key in keyof T]: WorkflowData<T[Key]>
}
: WorkflowDataProperties<T>) &
: T & WorkflowDataProperties<T>) &
WorkflowDataProperties<T> & {
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<T>
}

export type CreateWorkflowComposerContext = {
Expand Down
Loading