npm.io
2.22.7 • Published 4d ago

@medusajs/workflows-sdk

Licence
MIT
Version
2.22.7
Deps
5
Size
178 kB
Vulns
0
Weekly
0
Stars
36.5K

Medusa logo

@medusajs/workflows-sdk

Documentation | Website

Building blocks for digital commerce

Medusa is released under the MIT license. PRs welcome! Discord Chat Follow @medusajs

Workflows SDK for Medusa: the tools to define durable, multi-step workflows with steps, compensation (rollback), hooks, and conditions. Use it to build custom commerce flows in a Medusa application, module, or plugin. Applications import it through @medusajs/framework/workflows-sdk, which re-exports this package; importing @medusajs/workflows-sdk directly is discouraged.

At a glance

Package @medusajs/workflows-sdk
Type Core library
Import from @medusajs/framework/workflows-sdk
Entry points ., ./composer (available as @medusajs/framework/workflows-sdk/composer)

Features

  • createStep and StepResponse to define steps with an optional compensation function.
  • createWorkflow and WorkflowResponse to compose steps into a workflow.
  • transform, when, and parallelize to manipulate data, run steps conditionally, and run steps in parallel inside a workflow.
  • createHook to expose hooks that other code can consume to extend a workflow.

Installation

No separate installation is needed in a Medusa application. It's installed with @medusajs/framework.

Usage

import {
  createStep,
  createWorkflow,
  StepResponse,
  WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"

type WorkflowInput = {
  name: string
}

const greetStep = createStep(
  "greet-step",
  async ({ name }: WorkflowInput) => {
    return new StepResponse(`Hello ${name}!`)
  }
)

const helloWorldWorkflow = createWorkflow(
  "hello-world",
  function (input: WorkflowInput) {
    const message = greetStep(input)

    return new WorkflowResponse({ message })
  }
)

export default helloWorldWorkflow

Execute the workflow by passing it the Medusa container, for example in an API route:

const { result } = await helloWorldWorkflow(req.scope).run({
  input: { name: "John" },
})

Resources