createStep - Workflows API Reference
This documentation provides a reference to the createStep
. It belongs to the @medusajs/workflows-sdk
package.
This function creates a StepFunction that can be used as a step in a workflow constructed by the createWorkflow function.
Example
import {
createStep,
StepResponse,
StepExecutionContext,
WorkflowData
} from "@medusajs/workflows-sdk"
interface CreateProductInput {
title: string
}
export const createProductStep = createStep(
"createProductStep",
async function (
input: CreateProductInput,
context
) {
const productService = context.container.resolve(
"productService"
)
const product = await productService.create(input)
return new StepResponse({
product
}, {
product_id: product.id
})
},
async function (
input,
context
) {
const productService = context.container.resolve(
"productService"
)
await productService.delete(input.product_id)
}
)
Type Parameters
TInvokeInput
objectRequiredThe type of the expected input parameter to the invocation function.
TInvokeResultOutput
objectRequiredThe type of the expected output parameter of the invocation function.
TInvokeResultCompensateInput
objectRequiredThe type of the expected input parameter to the compensation function.
Parameters
nameOrConfig
string | object & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">RequiredThe name of the step or its configuration.
invokeFn
InvokeFn<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>RequiredAn invocation function that will be executed when the workflow is executed. The function must return an instance of StepResponse. The constructor of StepResponse
accepts the output of the step as a first argument, and optionally as a second argument the data to be passed to the compensation function as a parameter.
compensateFn
CompensateFn<TInvokeResultCompensateInput>A compensation function that's executed if an error occurs in the workflow. It's used to roll-back actions when errors occur.
It accepts as a parameter the second argument passed to the constructor of the StepResponse instance returned by the invocation function. If the
invocation function doesn't pass the second argument to
StepResponse
constructor, the compensation function receives the first argument
passed to the StepResponse
constructor instead.Returns
A step function to be used in a workflow.
Was this section helpful?