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

* fix(workflows=sdk): Fix StepFunction typings and custom step name

* Create smart-nails-switch.md

* fixes

* fixes

* fixes

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-02-22 11:59:53 +01:00
committed by GitHub
parent 72a17d6cd7
commit f611865553
6 changed files with 42 additions and 47 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix(workflows=sdk): Fix StepFunction typings and custom step name

View File

@@ -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]

View File

@@ -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"
}
}

View File

@@ -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<infer T, infer R, infer X>
? T
: never
/*const step1 = createStep("step1", async (input: {}, context) => {
return new StepResponse({ step1: ["step1"] })
})
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 step2 = createStep("step2", async (input: string[], context) => {
return new StepResponse({ step2: input })
})
const test = run<typeof workflow>("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)
})*/

View File

@@ -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)

View File

@@ -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.
@@ -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
@@ -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 = {