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

This commit is contained in:
Shahed Nasser
2024-07-17 15:19:51 +03:00
committed by GitHub
parent e5c66fa670
commit b1964a0384
11 changed files with 62 additions and 132 deletions

View File

@@ -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:
```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 { UpsertProductDTO, ProductDTO } from "@medusajs/types"
import { UpsertProductDTO } from "@medusajs/types"
import updateProduct from "./steps/update-product"
import updateErp from "./steps/update-erp"
export type UpdateProductAndErpWorkflowInput = UpsertProductDTO
type WorkflowOutput = {
product: ProductDTO
erpData: Record<string, unknown>
}
const updateProductAndErpWorkflow = createWorkflow<
UpdateProductAndErpWorkflowInput,
WorkflowOutput
>("update-product-and-erp", function (input) {
const updateProductAndErpWorkflow = createWorkflow(
"update-product-and-erp",
function (input: UpdateProductAndErpWorkflowInput) {
const product = updateProduct(input)
const erpData = updateErp(input)

View File

@@ -62,14 +62,9 @@ import {
// ...
type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input) {
const str1 = step1()
step2()

View File

@@ -35,9 +35,9 @@ type WorkflowInput = {
is_active: boolean
}
const workflow = createWorkflow
<WorkflowInput, any>
("workflow", function (input) {
const workflow = createWorkflow(
"workflow",
function (input: WorkflowInput) {
const result = when(
input,
(input) => {

View File

@@ -12,20 +12,18 @@ This chapter lists some constraints to keep in mind when defining a workflow or
The function passed to the `createWorkflow` cant 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
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", async function (input) {
const myWorkflow = createWorkflow(
"hello-world",
async function (input: WorkflowInput) {
// ...
})
// Do
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
// ...
})
```
@@ -35,16 +33,15 @@ const myWorkflow = createWorkflow<
Since the constructor function only defines how the workflow works, you cant directly manipulate data within the function. Instead, use the `transform` function:
export const highlights = [
["10", "", "Don't manipulate data directly."],
["22", "transform", "Use the `transform` function to manipulate data."]
["9", "", "Don't manipulate data directly."],
["20", "transform", "Use the `transform` function to manipulate data."]
]
```ts highlights={highlights}
// Don't
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const str1 = step1(input)
const str2 = step2(input)
@@ -54,10 +51,9 @@ const myWorkflow = createWorkflow<
})
// Do
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const str1 = step1(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
// Don't
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
if (input.is_active) {
// perform an action
}
})
// Do (explained in the next chapter)
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
when(input, (input) => {
return input.is_active
})

View File

@@ -47,14 +47,9 @@ const step3 = createStep("step-3", async () => {
return new StepResponse("Finished three steps")
})
type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function () {
const myWorkflow = createWorkflow(
"hello-world",
function () {
step1()
step2()
const message = step3()

View File

@@ -15,8 +15,8 @@ The workflow waits until all steps passed to the `parallelize` function finish e
For example:
export const highlights = [
["23", "[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", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."],
["21", "parallelize", "Run the steps passed as parameters in parallel."],
]
```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
@@ -24,7 +24,6 @@ import {
createWorkflow,
parallelize,
} from "@medusajs/workflows-sdk"
import { ProductDTO } from "@medusajs/types"
import {
createProductStep,
getProductStep,
@@ -36,10 +35,9 @@ interface WorkflowInput {
title: string
}
const myWorkflow = createWorkflow<
WorkflowInput,
ProductDTO
>("my-workflow", (input) => {
const myWorkflow = createWorkflow(
"my-workflow",
(input: WorkflowInput) => {
const product = createProductStep(input)
const [prices, productSalesChannel] = parallelize(

View File

@@ -32,14 +32,9 @@ const step1 = createStep(
}
)
type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>("hello-world", function () {
const myWorkflow = createWorkflow(
"hello-world",
function () {
const str1 = step1()
return {

View File

@@ -20,7 +20,7 @@ Timeout doesn't stop the execution of a running step. The timeout only affects t
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 {
createStep,
createWorkflow,
@@ -33,14 +33,7 @@ const step1 = createStep(
}
)
type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<
{},
WorkflowOutput
>({
const myWorkflow = createWorkflow({
name: "hello-world",
timeout: 2, // 2 seconds
}, function () {

View File

@@ -60,13 +60,9 @@ import {
// ...
type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<WorkflowInput, WorkflowOutput>(
const myWorkflow = createWorkflow(
"hello-world",
function (input) {
function (input: WorkflowInput) {
const str1 = step1()
// to pass 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:
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",
"The resource registration name imported from `@medusajs/modules-sdk`.",
],
@@ -244,10 +240,6 @@ import {
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
type WorkflowOutput = {
count: number
}
const step1 = createStep("step-1", async (_, context) => {
const productModuleService: IProductModuleService =
context.container.resolve(ModuleRegistrationName.PRODUCT)
@@ -257,7 +249,7 @@ const step1 = createStep("step-1", async (_, context) => {
return new StepResponse(count)
})
const myWorkflow = createWorkflow<unknown, WorkflowOutput>(
const myWorkflow = createWorkflow(
"product-count",
function () {
const count = step1()

View File

@@ -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:
export const workflowHighlights = [
["27", "createManagerStep", "Thi step creates a manager."],
["44", "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."],
["55", '"manager"', "The actor type of the manager."]
["20", "createManagerStep", "Thi step creates a manager."],
["37", "createManagerWorkflow", "The workflow used to create a manager and set its auth identity's actor type."],
["44", "setAuthAppMetadataStep", "Set the actor type of the manager's associated auth identity."],
["46", '"manager"', "The actor type of the manager."]
]
```ts title="src/workflows/create-manager.ts" highlights={workflowHighlights}
@@ -63,13 +63,6 @@ type CreateManagerWorkflowInput = {
authIdentityId: string
}
type CreateManagerWorkflowOutput = {
id: string
first_name: string
last_name: string
email: string
}
const createManagerStep = createStep(
"create-manager-step",
async ({
@@ -87,11 +80,9 @@ const createManagerStep = createStep(
}
)
const createManagerWorkflow = createWorkflow<
CreateManagerWorkflowInput, CreateManagerWorkflowOutput
>(
const createManagerWorkflow = createWorkflow(
"create-manager",
function (input) {
function (input: CreateManagerWorkflowInput) {
const manager = createManagerStep({
manager: input.manager,
})

View File

@@ -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:
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}
@@ -294,18 +294,9 @@ export type CreateVendorAdminWorkflowInput = {
authIdentityId: string
}
type CreateVendorAdminWorkflowOutput = {
id: string
first_name: string
last_name: string
email: string
}
const createVendorAdminWorkflow = createWorkflow<
CreateVendorAdminWorkflowInput, CreateVendorAdminWorkflowOutput
>(
const createVendorAdminWorkflow = createWorkflow(
"create-vendor-admin",
function (input) {
function (input: CreateVendorAdminWorkflowInput) {
const vendorAdmin = createVendorAdminStep({
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"
import { createWorkflow, transform } from "@medusajs/workflows-sdk"
import { OrderDTO } from "@medusajs/types"
import retrieveCartStep from "./steps/retrieve-cart"
import groupVendorItemsStep from "./steps/group-vendor-items"
import createParentOrderStep from "./steps/create-parent-order"
import createVendorOrdersStep, { VendorOrder } from "./steps/create-vendor-orders"
import createVendorOrdersStep from "./steps/create-vendor-orders"
type WorkflowInput = {
cart_id: string
}
type WorkflowOutput = {
parent_order: OrderDTO
vendor_orders: VendorOrder[]
}
const createVendorOrdersWorkflow = createWorkflow<
WorkflowInput, WorkflowOutput
>(
const createVendorOrdersWorkflow = createWorkflow(
"create-vendor-order",
(input) => {
(input: WorkflowInput) => {
const { cart } = retrieveCartStep(input)
const { order } = createParentOrderStep(input)