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 PerettiandOli Juhl authored and GitHub committed 2024-02-22 11:59:53 +01:00
1 parent 72a17d6cd7
commit f611865553
6 files changed
+42 -47

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix(workflows=sdk): Fix StepFunction typings and custom step name
@@ -1,9 +1,9 @@
import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types" import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types"
import { import {
WorkflowData,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
WorkflowData,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { import {
createCartsStep, createCartsStep,
@@ -53,6 +53,7 @@ export const createCartWorkflow = createWorkflow(
// TODO: Add line items // TODO: Add line items
// @ts-ignore
const cart = createCartsStep([cartInput]) const cart = createCartsStep([cartInput])
return cart[0] return cart[0]
+1 -1
View File
@@ -40,6 +40,6 @@
"build": "rimraf dist && tsc --build", "build": "rimraf dist && tsc --build",
"watch": "tsc --build --watch", "watch": "tsc --build --watch",
"test": "jest --runInBand --bail --forceExit", "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"
} }
} }
+25 -14
View File
@@ -1,12 +1,12 @@
import { createStep, createWorkflow, StepResponse } from "./composer" import { createStep, createWorkflow, StepResponse } from "./composer"
const step1 = createStep("step1", async (input: {}, context) => { const step1 = createStep("step1", async (input: {}, context) => {
return new StepResponse({ step1: "step1" }) return new StepResponse({ step1: ["step1"] })
}) })
const step2 = createStep( const step2 = createStep(
"step2", "step2",
async (input: { test: string; test2: string }, context) => { async (input: { filters: { id: string[] } }, context) => {
return new StepResponse({ step2: input }) return new StepResponse({ step2: input })
} }
) )
@@ -18,24 +18,35 @@ const step3 = createStep("step3", async () => {
const workflow = createWorkflow("workflow", function () { const workflow = createWorkflow("workflow", function () {
const step1Res = step1() const step1Res = step1()
step3() step3()
return step2({ test: "test", test2: step1Res.step1 }) return step2({ filters: { id: step1Res.step1 } })
}) })
workflow() workflow()
.run({}) .run({})
.then((res) => { .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> /*const step1 = createStep("step1", async (input: {}, context) => {
? T return new StepResponse({ step1: ["step1"] })
: never })
function run< const step2 = createStep("step2", async (input: string[], context) => {
TWorkflow extends ReturnWorkflow<any, any, any>, return new StepResponse({ step2: input })
TData = TWorkflow extends ReturnWorkflow<infer T, infer R, infer X> })
? T
: never
>(name: string, options: FlowRunOptions<TData>) {}
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)
})*/
@@ -2,9 +2,9 @@ import {
TransactionStepsDefinition, TransactionStepsDefinition,
WorkflowManager, WorkflowManager,
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { OrchestrationUtils, isString } from "@medusajs/utils" import { isString, OrchestrationUtils } from "@medusajs/utils"
import { ulid } from "ulid" import { ulid } from "ulid"
import { StepResponse, resolveValue } from "./helpers" import { resolveValue, StepResponse } from "./helpers"
import { proxify } from "./helpers/proxy" import { proxify } from "./helpers/proxy"
import { import {
CreateWorkflowComposerContext, CreateWorkflowComposerContext,
@@ -199,6 +199,7 @@ function applyStep<
...localConfig, ...localConfig,
}) })
ret.__step__ = newStepName
WorkflowManager.update(this.workflowId, this.flow, this.handlers) WorkflowManager.update(this.workflowId, this.flow, this.handlers)
return proxify(ret) return proxify(ret)
@@ -8,9 +8,7 @@ import {
import { Context, MedusaContainer } from "@medusajs/types" import { Context, MedusaContainer } from "@medusajs/types"
export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> = export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> =
( (this: CreateWorkflowComposerContext) => WorkflowData<TOutput>
this: CreateWorkflowComposerContext
) => WorkflowData<{ [K in keyof TOutput]: TOutput[K] }>
/** /**
* A step function to be used in a workflow. * 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 [] export type StepFunction<TInput, TOutput = unknown> = (keyof TInput extends []
? // Function that doesn't expect any input ? // Function that doesn't expect any input
{ {
(): WorkflowData<{ (): WorkflowData<TOutput>
[K in keyof TOutput]: TOutput[K]
}>
} }
: // function that expects an input object : // function that expects an input object
{ {
( (input: WorkflowData<TInput> | TInput): WorkflowData<TOutput>
input: TInput extends object
? { [K in keyof TInput]: WorkflowData<TInput[K]> | TInput[K] }
: WorkflowData<TInput> | TInput
): WorkflowData<{
[K in keyof TOutput]: TOutput[K]
}>
}) & }) &
WorkflowDataProperties<{ WorkflowDataProperties<TOutput>
[K in keyof TOutput]: TOutput[K]
}> &
WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}>
export type WorkflowDataProperties<T = unknown> = { export type WorkflowDataProperties<T = unknown> = {
__type: Symbol __type: Symbol
@@ -56,22 +41,14 @@ export type WorkflowData<T = unknown> = (T extends object
? { ? {
[Key in keyof T]: WorkflowData<T[Key]> [Key in keyof T]: WorkflowData<T[Key]>
} }
: WorkflowDataProperties<T>) & : T & WorkflowDataProperties<T>) &
WorkflowDataProperties<T> & { WorkflowDataProperties<T> & {
config( config(
config: { name?: string } & Omit< config: { name?: string } & Omit<
TransactionStepsDefinition, TransactionStepsDefinition,
"next" | "uuid" | "action" "next" | "uuid" | "action"
> >
): T extends object ): WorkflowData<T>
? WorkflowData<
T extends object
? {
[K in keyof T]: T[K]
}
: T
>
: T
} }
export type CreateWorkflowComposerContext = { export type CreateWorkflowComposerContext = {