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

feat(workflows-sdk): Allow a step to not define an expected input #5775

Merged
merged 6 commits into from
Dec 21, 2023
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/hip-turtles-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---

feat(workflows-sdk): Allow a step to not define an expected input
3 changes: 2 additions & 1 deletion packages/workflows-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"build": "rimraf dist && tsc --build",
"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"
}
}
27 changes: 27 additions & 0 deletions packages/workflows-sdk/src/utils/_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createStep, createWorkflow, StepResponse } from "./composer"

const step1 = createStep("step1", async (input: {}, context) => {
console.log("step1", input) // Here the input will be `{}` and the createWorkflow does not have to provide an input
return new StepResponse({ step1: "step1" })
})

const step2 = createStep("step2", async (input: string, context) => {
console.log("step2", input) // Here the input will be the string value `step1`
return new StepResponse({ step2: "step2" })
})

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) // result: { step2: "step2" }
})
32 changes: 18 additions & 14 deletions packages/workflows-sdk/src/utils/composer/create-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { isString, OrchestrationUtils } from "@medusajs/utils"
*
* @returns The expected output based on the type parameter `TOutput`.
*/
type InvokeFn<TInput extends object, TOutput, TCompensateInput> = (
type InvokeFn<TInput, TOutput, TCompensateInput> = (
/**
* The input of the step.
*/
input: {
[Key in keyof TInput]: TInput[Key]
},
input: TInput,
/**
* The step's context.
*/
Expand Down Expand Up @@ -64,13 +62,13 @@ interface ApplyStepOptions<
TStepInputs extends {
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>
},
TInvokeInput extends object,
TInvokeInput,
TInvokeResultOutput,
TInvokeResultCompensateInput
> {
stepName: string
stepConfig?: TransactionStepsDefinition
input: TStepInputs
input?: TStepInputs
invokeFn: InvokeFn<
TInvokeInput,
TInvokeResultOutput,
Expand All @@ -92,7 +90,7 @@ interface ApplyStepOptions<
* @param compensateFn
*/
function applyStep<
TInvokeInput extends object,
TInvokeInput,
TStepInput extends {
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>
},
Expand Down Expand Up @@ -125,7 +123,9 @@ function applyStep<
context: transactionContext.context,
}

const argInput = await resolveValue(input, transactionContext)
const argInput = input
? await resolveValue(input, transactionContext)
: {}
const stepResponse: StepResponse<any, any> = await invokeFn.apply(
this,
[argInput, executionContext]
Expand Down Expand Up @@ -236,7 +236,7 @@ function applyStep<
* )
*/
export function createStep<
TInvokeInput extends object,
TInvokeInput,
TInvokeResultOutput,
TInvokeResultCompensateInput
>(
Expand Down Expand Up @@ -267,9 +267,13 @@ export function createStep<
(isString(nameOrConfig) ? nameOrConfig : nameOrConfig.name) ?? invokeFn.name
const config = isString(nameOrConfig) ? {} : nameOrConfig

const returnFn = function (input: {
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>
}): WorkflowData<TInvokeResultOutput> {
const returnFn = function (
input:
| {
[K in keyof TInvokeInput]: WorkflowData<TInvokeInput[K]>
}
| undefined
): WorkflowData<TInvokeResultOutput> {
if (!global[OrchestrationUtils.SymbolMedusaWorkflowComposerContext]) {
throw new Error(
"createStep must be used inside a createWorkflow definition"
Expand All @@ -296,10 +300,10 @@ export function createStep<
compensateFn,
})
)
}
} as StepFunction<TInvokeInput, TInvokeResultOutput>

returnFn.__type = OrchestrationUtils.SymbolWorkflowStepBind
returnFn.__step__ = stepName

return returnFn as unknown as StepFunction<TInvokeInput, TInvokeResultOutput>
return returnFn
}
27 changes: 21 additions & 6 deletions packages/workflows-sdk/src/utils/composer/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,34 @@ export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> =
* @typeParam TInput - The type of the input of the step.
* @typeParam TOutput - The type of the output of the step.
*/
export type StepFunction<TInput extends object = object, TOutput = unknown> = {
(input: { [K in keyof TInput]: WorkflowData<TInput[K]> }): WorkflowData<{
export type StepFunction<TInput, TOutput = unknown> = (keyof TInput extends []
? // Function that doesn't expect any input
{
(): WorkflowData<{
[K in keyof TOutput]: TOutput[K]
}>
}
: // 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]
}>
}) &
WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}> & {
config(
config: Pick<TransactionStepsDefinition, "maxRetries">
): WorkflowData<{
[K in keyof TOutput]: TOutput[K]
}>
}
} & WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}>
} & WorkflowDataProperties<{
[K in keyof TOutput]: TOutput[K]
}>

export type WorkflowDataProperties<T = unknown> = {
__type: Symbol
Expand Down