docs: remove type arguments passed to createWorkflow (#8161)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-07-17 13:19:51 +01:00
1 parent e5c66fa670
commit b1964a0384
11 files changed
+62 -132

No files matched your search

@@ -205,23 +205,17 @@ With the steps ready, you'll create the workflow that runs these steps to update
Change the content of `src/workflows/update-product-erp/index.ts` to the following: Change the content of `src/workflows/update-product-erp/index.ts` to the following:
```ts title="src/workflows/update-product-erp/index.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports" ```ts title="src/workflows/update-product-erp/index.ts" collapsibleLines="1-5" expandButtonLabel="Show Imports"
import { createWorkflow } from "@medusajs/workflows-sdk" import { createWorkflow } from "@medusajs/workflows-sdk"
import { UpsertProductDTO, ProductDTO } from "@medusajs/types" import { UpsertProductDTO } from "@medusajs/types"
import updateProduct from "./steps/update-product" import updateProduct from "./steps/update-product"
import updateErp from "./steps/update-erp" import updateErp from "./steps/update-erp"
export type UpdateProductAndErpWorkflowInput = UpsertProductDTO export type UpdateProductAndErpWorkflowInput = UpsertProductDTO
type WorkflowOutput = { const updateProductAndErpWorkflow = createWorkflow(
product: ProductDTO "update-product-and-erp",
erpData: Record<string, unknown> function (input: UpdateProductAndErpWorkflowInput) {
}
const updateProductAndErpWorkflow = createWorkflow<
UpdateProductAndErpWorkflowInput,
WorkflowOutput
>("update-product-and-erp", function (input) {
const product = updateProduct(input) const product = updateProduct(input)
const erpData = updateErp(input) const erpData = updateErp(input)
@@ -62,14 +62,9 @@ import {
// ... // ...
type WorkflowOutput = { const myWorkflow = createWorkflow(
message: string "hello-world",
} function (input) {
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function (input) {
const str1 = step1() const str1 = step1()
step2() step2()
@@ -35,9 +35,9 @@ type WorkflowInput = {
is_active: boolean is_active: boolean
} }
const workflow = createWorkflow const workflow = createWorkflow(
<WorkflowInput, any> "workflow",
("workflow", function (input) { function (input: WorkflowInput) {
const result = when( const result = when(
input, input,
(input) => { (input) => {
@@ -12,20 +12,18 @@ This chapter lists some constraints to keep in mind when defining a workflow or
The function passed to the `createWorkflow` can’t be an async function: The function passed to the `createWorkflow` can’t be an async function:
```ts highlights={[["5", "async", "Function can't be async."], ["13", "", "Correct way of defining the function."]]} ```ts highlights={[["4", "async", "Function can't be async."], ["11", "", "Correct way of defining the function."]]}
// Don't // Don't
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput async function (input: WorkflowInput) {
>("hello-world", async function (input) {
// ... // ...
}) })
// Do // Do
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput function (input: WorkflowInput) {
>("hello-world", function (input) {
// ... // ...
}) })
``` ```
@@ -35,16 +33,15 @@ const myWorkflow = createWorkflow<
Since the constructor function only defines how the workflow works, you can’t directly manipulate data within the function. Instead, use the `transform` function: Since the constructor function only defines how the workflow works, you can’t directly manipulate data within the function. Instead, use the `transform` function:
export const highlights = [ export const highlights = [
["10", "", "Don't manipulate data directly."], ["9", "", "Don't manipulate data directly."],
["22", "transform", "Use the `transform` function to manipulate data."] ["20", "transform", "Use the `transform` function to manipulate data."]
] ]
```ts highlights={highlights} ```ts highlights={highlights}
// Don't // Don't
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput function (input: WorkflowInput) {
>("hello-world", function (input) {
const str1 = step1(input) const str1 = step1(input)
const str2 = step2(input) const str2 = step2(input)
@@ -54,10 +51,9 @@ const myWorkflow = createWorkflow<
}) })
// Do // Do
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput function (input: WorkflowInput) {
>("hello-world", function (input) {
const str1 = step1(input) const str1 = step1(input)
const str2 = step2(input) const str2 = step2(input)
@@ -81,20 +77,18 @@ You can't use if-conditions in a workflow. Instead, use the when-then utility fu
```ts ```ts
// Don't // Don't
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput function (input: WorkflowInput) {
>("hello-world", function (input) {
if (input.is_active) { if (input.is_active) {
// perform an action // perform an action
} }
}) })
// Do (explained in the next chapter) // Do (explained in the next chapter)
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "hello-world",
WorkflowOutput function (input: WorkflowInput) {
>("hello-world", function (input) {
when(input, (input) => { when(input, (input) => {
return input.is_active return input.is_active
}) })
@@ -47,14 +47,9 @@ const step3 = createStep("step-3", async () => {
return new StepResponse("Finished three steps") return new StepResponse("Finished three steps")
}) })
type WorkflowOutput = { const myWorkflow = createWorkflow(
message: string "hello-world",
} function () {
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function () {
step1() step1()
step2() step2()
const message = step3() const message = step3()
@@ -15,8 +15,8 @@ The workflow waits until all steps passed to the `parallelize` function finish e
For example: For example:
export const highlights = [ export const highlights = [
["23", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."], ["21", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."],
["23", "parallelize", "Run the steps passed as parameters in parallel."], ["21", "parallelize", "Run the steps passed as parameters in parallel."],
] ]
```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports" ```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
@@ -24,7 +24,6 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { ProductDTO } from "@medusajs/types"
import { import {
createProductStep, createProductStep,
getProductStep, getProductStep,
@@ -36,10 +35,9 @@ interface WorkflowInput {
title: string title: string
} }
const myWorkflow = createWorkflow< const myWorkflow = createWorkflow(
WorkflowInput, "my-workflow",
ProductDTO (input: WorkflowInput) => {
>("my-workflow", (input) => {
const product = createProductStep(input) const product = createProductStep(input)
const [prices, productSalesChannel] = parallelize( const [prices, productSalesChannel] = parallelize(
@@ -32,14 +32,9 @@ const step1 = createStep(
} }
) )
type WorkflowOutput = { const myWorkflow = createWorkflow(
message: string "hello-world",
} function () {
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function () {
const str1 = step1() const str1 = step1()
return { return {
@@ -20,7 +20,7 @@ Timeout doesn't stop the execution of a running step. The timeout only affects t
For example: For example:
```ts title="src/workflows/hello-world.ts" highlights={[["22"]]} collapsibleLines="1-16" expandButtonLabel="Show More" ```ts title="src/workflows/hello-world.ts" highlights={[["15"]]} collapsibleLines="1-12" expandButtonLabel="Show More"
import { import {
createStep, createStep,
createWorkflow, createWorkflow,
@@ -33,14 +33,7 @@ const step1 = createStep(
} }
) )
type WorkflowOutput = { const myWorkflow = createWorkflow({
message: string
}
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>({
name: "hello-world", name: "hello-world",
timeout: 2, // 2 seconds timeout: 2, // 2 seconds
}, function () { }, function () {
+5 -13
View File
@@ -60,13 +60,9 @@ import {
// ... // ...
type WorkflowOutput = { const myWorkflow = createWorkflow(
message: string
}
const myWorkflow = createWorkflow<WorkflowInput, WorkflowOutput>(
"hello-world", "hello-world",
function (input) { function (input: WorkflowInput) {
const str1 = step1() const str1 = step1()
// to pass input // to pass input
const str2 = step2(input) const str2 = step2(input)
@@ -227,9 +223,9 @@ Each step in the workflow receives as a second parameter a `context` object. The
For example: For example:
export const highlights = [ export const highlights = [
["15", "resolve", "Resolve the Product Module's main service."], ["11", "resolve", "Resolve the Product Module's main service."],
[ [
"15", "11",
"ModuleRegistrationName.PRODUCT", "ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.", "The resource registration name imported from `@medusajs/modules-sdk`.",
], ],
@@ -244,10 +240,6 @@ import {
import { IProductModuleService } from "@medusajs/types" import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils" import { ModuleRegistrationName } from "@medusajs/utils"
type WorkflowOutput = {
count: number
}
const step1 = createStep("step-1", async (_, context) => { const step1 = createStep("step-1", async (_, context) => {
const productModuleService: IProductModuleService = const productModuleService: IProductModuleService =
context.container.resolve(ModuleRegistrationName.PRODUCT) context.container.resolve(ModuleRegistrationName.PRODUCT)
@@ -257,7 +249,7 @@ const step1 = createStep("step-1", async (_, context) => {
return new StepResponse(count) return new StepResponse(count)
}) })
const myWorkflow = createWorkflow<unknown, WorkflowOutput>( const myWorkflow = createWorkflow(
"product-count", "product-count",
function () { function () {
const count = step1() const count = step1()
@@ -37,10 +37,10 @@ Start by creating a workflow that does two things:
For example, create the file `src/workflows/create-manager.ts`. with the following content: For example, create the file `src/workflows/create-manager.ts`. with the following content:
export const workflowHighlights = [ export const workflowHighlights = [
["27", "createManagerStep", "Thi step creates a manager."], ["20", "createManagerStep", "Thi step creates a manager."],
["44", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."], ["37", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."],
["53", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."], ["44", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."],
["55", '"manager"', "The actor type of the manager."] ["46", '"manager"', "The actor type of the manager."]
] ]
```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights} ```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights}
@@ -63,13 +63,6 @@ type CreateManagerWorkflowInput = {
authIdentityId: string authIdentityId: string
} }
type CreateManagerWorkflowOutput = {
id: string
first_name: string
last_name: string
email: string
}
const createManagerStep = createStep( const createManagerStep = createStep(
"create-manager-step", "create-manager-step",
async ({ async ({
@@ -87,11 +80,9 @@ const createManagerStep = createStep(
} }
) )
const createManagerWorkflow = createWorkflow< const createManagerWorkflow = createWorkflow(
CreateManagerWorkflowInput, CreateManagerWorkflowOutput
>(
"create-manager", "create-manager",
function (input) { function (input: CreateManagerWorkflowInput) {
const manager = createManagerStep({ const manager = createManagerStep({
manager: input.manager, manager: input.manager,
}) })
@@ -274,7 +274,7 @@ This is the first step that creates the vendor admin and returns it.
Then, create the workflow at `src/workflows/marketplace/create-vendor-admin/index.ts` with the following content: Then, create the workflow at `src/workflows/marketplace/create-vendor-admin/index.ts` with the following content:
export const vendorAdminWorkflowHighlights = [ export const vendorAdminWorkflowHighlights = [
["33", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."] ["24", "setAuthAppMetadataStep", "Step is imported from `@medusajs/core-flows`."]
] ]
```ts title="src/workflows/marketplace/create-vendor-admin/index.ts" highlights={vendorAdminWorkflowHighlights} ```ts title="src/workflows/marketplace/create-vendor-admin/index.ts" highlights={vendorAdminWorkflowHighlights}
@@ -294,18 +294,9 @@ export type CreateVendorAdminWorkflowInput = {
authIdentityId: string authIdentityId: string
} }
type CreateVendorAdminWorkflowOutput = { const createVendorAdminWorkflow = createWorkflow(
id: string
first_name: string
last_name: string
email: string
}
const createVendorAdminWorkflow = createWorkflow<
CreateVendorAdminWorkflowInput, CreateVendorAdminWorkflowOutput
>(
"create-vendor-admin", "create-vendor-admin",
function (input) { function (input: CreateVendorAdminWorkflowInput) {
const vendorAdmin = createVendorAdminStep({ const vendorAdmin = createVendorAdminStep({
admin: input.admin, admin: input.admin,
}) })
@@ -1100,26 +1091,18 @@ Finally, create the workflow at the file `src/workflows/marketplace/create-vendo
```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-7" expandMoreLabel="Show Imports" ```ts title="src/workflows/marketplace/create-vendor-orders/index.ts" collapsibleLines="1-7" expandMoreLabel="Show Imports"
import { createWorkflow, transform } from "@medusajs/workflows-sdk" import { createWorkflow, transform } from "@medusajs/workflows-sdk"
import { OrderDTO } from "@medusajs/types"
import retrieveCartStep from "./steps/retrieve-cart" import retrieveCartStep from "./steps/retrieve-cart"
import groupVendorItemsStep from "./steps/group-vendor-items" import groupVendorItemsStep from "./steps/group-vendor-items"
import createParentOrderStep from "./steps/create-parent-order" import createParentOrderStep from "./steps/create-parent-order"
import createVendorOrdersStep, { VendorOrder } from "./steps/create-vendor-orders" import createVendorOrdersStep from "./steps/create-vendor-orders"
type WorkflowInput = { type WorkflowInput = {
cart_id: string cart_id: string
} }
type WorkflowOutput = { const createVendorOrdersWorkflow = createWorkflow(
parent_order: OrderDTO
vendor_orders: VendorOrder[]
}
const createVendorOrdersWorkflow = createWorkflow<
WorkflowInput, WorkflowOutput
>(
"create-vendor-order", "create-vendor-order",
(input) => { (input: WorkflowInput) => {
const { cart } = retrieveCartStep(input) const { cart } = retrieveCartStep(input)
const { order } = createParentOrderStep(input) const { order } = createParentOrderStep(input)