diff --git a/www/apps/book/app/advanced-development/api-routes/additional-data/page.mdx b/www/apps/book/app/advanced-development/api-routes/additional-data/page.mdx new file mode 100644 index 0000000000..612597a01b --- /dev/null +++ b/www/apps/book/app/advanced-development/api-routes/additional-data/page.mdx @@ -0,0 +1,201 @@ +import { Details } from "docs-ui" + +export const metadata = { + title: `${pageNumber} Pass Additional Data to Medusa's API Route`, +} + +# {metadata.title} + +In this chapter, you'll learn how to pass additional data in requests to Medusa's API Route. + +## Why Pass Additional Data? + +Some of Medusa's API Routes accept an `additional_data` parameter whose type is an object. The API Route passes the `additional_data` to the workflow, which in turn passes it to its hooks. + +This is useful when you have a link from your custom module to a commerce module, and you want to perform an additional action when a request is sent to an existing API route. + +For example, the [Create Product API Route](!api!/admin#products_postproducts) accepts an `additional_data` parameter. If you have a data model linked to it, you consume the `productsCreated` hook to create a record of the data model using the custom data and link it to the product. + +### API Routes Accepting Additional Data + +
+ +- Campaigns + - [Create Campaign](!api!/admin#campaigns_postcampaigns) + - [Update Campaign](!api!/admin#campaigns_postcampaignsid) +- Cart + - [Create Cart](!api!/store#carts_postcarts) + - [Update Cart](!api!/store#carts_postcartsid) +- Customers + - [Create Customer](!api!/admin#customers_postcustomers) + - [Update Customer](!api!/admin#customers_postcustomersid) + - [Create Address](!api!/admin#customers_postcustomersidaddresses) + - [Update Address](!api!/admin#customers_postcustomersidaddressesaddress_id) +- Draft Orders + - [Create Draft Order](!api!/admin#draft-orders_postdraftorders) +- Orders + - [Complete Orders](!api!/admin#orders_postordersidcomplete) + - [Cancel Order's Fulfillment](!api!/admin#orders_postordersidfulfillmentsfulfillment_idcancel) + - [Create Shipment](!api!/admin#orders_postordersidfulfillmentsfulfillment_idshipments) + - [Create Fulfillment](!api!/admin#orders_postordersidfulfillments) +- Products + - [Create Product](!api!/admin#products_postproducts) + - [Update Product](!api!/admin#products_postproductsid) + - [Create Product Variant](!api!/admin#products_postproductsidvariants) + - [Update Product Variant](!api!/admin#products_postproductsidvariantsvariant_id) + - [Create Product Option](!api!/admin#products_postproductsidoptions) + - [Update Product Option](!api!/admin#products_postproductsidoptionsoption_id) +- Promotions + - [Create Promotion](!api!/admin#promotions_postpromotions) + - [Update Promotion](!api!/admin#promotions_postpromotionsid) + +
+ +--- + +## How to Pass Additional Data + +### 1. Specify Validation of Additional Data + +Before passing custom data in the `additional_data` object parameter, you must specify validation rules for the allowed properties in the object. + +To do that, use the middleware route object defined in `src/api/middlewares.ts`. + +For example, create the file `src/api/middlewares.ts` with the following content: + +```ts title="src/api/middlewares.ts" +import { defineMiddlewares } from "@medusajs/medusa" +import { z } from "zod" + +export default defineMiddlewares({ + routes: [ + { + method: "POST", + matcher: "/admin/products", + additionalDataValidator: { + brand: z.string().optional() + } + } + ] +}) +``` + +The middleware route object accepts an optional parameter `additionalDataValidator` whose value is an object of key-value pairs. The keys indicate the name of accepted properties in the `additional_data` parameter, and the value is [Zod](https://zod.dev/) validation rules of the property. + +In this example, you indicate that the `additional_data` parameter accepts a `brand` property whose value is an optional string. + + + +Refer to [Zod's documentation](https://zod.dev) for all available validation rules. + + + +### 2. Pass the Additional Data in a Request + +You can now pass a `brand` property in the `additional_data` parameter of a request to the Create Product API Route. + +For example: + +```bash +curl -X POST 'http://localhost:9000/admin/products' \ +-H 'Content-Type: application/json' \ +-H 'Authorization: Bearer {token}' \ +--data '{ + "title": "Product 1", + "additional_data": { + "brand": "Acme" + } +}' +``` + + + +Make sure to replace the `{token}` in the authorization header with an admin user's authentication token. + + + +In this request, you pass in the `additional_data` parameter a `brand` property and set its value to `Acme`. + +The `additional_data` is then passed to hooks in the `createProductsWorkflow` used by the API route. + +--- + +## Use Additional Data in a Hook + + + +Learn about workflow hooks in [this guide](../../workflows/workflow-hooks/page.mdx). + + + +Step functions consuming the workflow hook can access the `additional_data` in the first parameter. + +For example, consider you want to store the data passed in `additional_data` in the product's `metadata` property. + +To do that, create the file `src/workflows/hooks/product-created.ts` with the following content: + +```ts title="src/workflows/hooks/product-created.ts" +import { StepResponse } from "@medusajs/workflows-sdk" +import { createProductsWorkflow } from "@medusajs/core-flows" +import { Modules } from "@medusajs/utils" + +createProductsWorkflow.hooks.productsCreated( + async ({ products, additional_data }, { container }) => { + if (!additional_data.brand) { + return + } + + const productModuleService = container.resolve( + Modules.PRODUCT + ) + + await productModuleService.upsertProducts( + products.map((product) => ({ + ...product, + metadata: { + ...product.metadata, + brand: additional_data.brand + } + })) + ) + + return new StepResponse(products, { + products, + additional_data + }) + } +) +``` + +This consumes the `productsCreated` hook, which runs after the products are created. + +If `brand` is passed in `additional_data`, you resolve the Product Module's main service and use its `upsertProducts` method to update the products, adding the brand to the `metadata` property. + +### Compensation Function + +Hooks also accept a compensation function as a second parameter to undo the actions made by the step function. + +For example, pass the following second parameter to the `productsCreated` hook: + +```ts title="src/workflows/hooks/product-created.ts" +createProductsWorkflow.hooks.productsCreated( + async ({ products, additional_data }, { container }) => { + // ... + }, + async ({ products, additional_data }, { container }) => { + if (!additional_data.brand) { + return + } + + const productModuleService = container.resolve( + Modules.PRODUCT + ) + + await productModuleService.upsertProducts( + products + ) + } +) +``` + +This updates the product to their original state before adding the brand to their `metadata` property. diff --git a/www/apps/book/app/advanced-development/workflows/access-workflow-errors/page.mdx b/www/apps/book/app/advanced-development/workflows/access-workflow-errors/page.mdx index e9d66806e1..31a9343215 100644 --- a/www/apps/book/app/advanced-development/workflows/access-workflow-errors/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/access-workflow-errors/page.mdx @@ -47,6 +47,6 @@ export async function GET( ``` -The object passed to the `run` method accepts a `throwOnError` property. When disabled, the errors are returned in the `errors` property of the `run`'s output. The value of `errors` is an array of error objects. +The object passed to the `run` method accepts a `throwOnError` property. When disabled, the errors are returned in the `errors` property of `run`'s output. -Then, you can check the items in the `errors` array and handle them accordingly. Each error object has an `error` property, which holds the name or the text of the thrown error. \ No newline at end of file +The value of `errors` is an array of error objects. Each object has an `error` property, whose value is the name or text of the thrown error. diff --git a/www/apps/book/app/advanced-development/workflows/add-workflow-hook/page.mdx b/www/apps/book/app/advanced-development/workflows/add-workflow-hook/page.mdx index f608435fef..2625de36e3 100644 --- a/www/apps/book/app/advanced-development/workflows/add-workflow-hook/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/add-workflow-hook/page.mdx @@ -6,6 +6,8 @@ export const metadata = { In this chapter, you'll learn how to expose a hook in your workflow. +## When to Expose a Hook + Your workflow is reusable in other applications, and you allow performing an external action at some point in your workflow. @@ -18,6 +20,8 @@ Your workflow isn't reusable by other applications. Use a step that performs wha +--- + ## How to Expose a Hook in a Workflow? To expose a hook in your workflow, use the `createHook` function imported from `@medusajs/workflows-sdk`. @@ -58,7 +62,7 @@ export const myWorkflow = createWorkflow( The `createHook` function accepts two parameters: -1. The first is a string indicating the hook's name. This is used to add a hook handler later. +1. The first is a string indicating the hook's name. You use this to consume the hook later. 2. The second is the input to pass to the hook handler. The workflow must also pass an object having a `hooks` property as a second parameter to the `WorkflowResponse` constructor. Its value is an array of the workflow's hooks. @@ -81,4 +85,6 @@ myWorkflow.hooks.productCreated( ) ``` -The hook is available on the workflow's `hooks` property using its name `productCreated`. You invoke the hook, passing the handler as a parameter, which is a step function. +The hook is available on the workflow's `hooks` property using its name `productCreated`. + +You invoke the hook, passing a step function (the hook handler) as a parameter. diff --git a/www/apps/book/app/advanced-development/workflows/advanced-example/page.mdx b/www/apps/book/app/advanced-development/workflows/advanced-example/page.mdx deleted file mode 100644 index 68e49b43d9..0000000000 --- a/www/apps/book/app/advanced-development/workflows/advanced-example/page.mdx +++ /dev/null @@ -1,272 +0,0 @@ -export const metadata = { - title: `${pageNumber} Advanced Workflow Example`, -} - -# {metadata.title} - -In this chapter, you’ll create an advanced workflow. - -Workflows are most useful when performing tasks across services and integrations: - -- You simplify complex flows or processes by splitting them into a series of steps. -- You avoid data inconsistency and loss through retry mechanisms and rollbacks. -- You execute the workflow from anywhere in your Medusa application. - -The workflow you’ll build is an update-product workflow. You'll use an example `ErpService` that has methods to manage the product in a third-party ERP system, and the `ProductService`. - ---- - -## 1. Create Workflow File - -Start by creating the file `src/workflows/update-product-erp/index.ts` that will hold the constructed workflow. - -In the file, add the type of the expected workflow input: - -```ts title="src/workflows/update-product-erp/index.ts" -import { UpsertProductDTO } from "@medusajs/types" - -export type UpdateProductAndErpWorkflowInput = UpsertProductDTO - -``` - -The expected input is the data to update in the product along with the product’s ID. - ---- - -## 2. Create Update Product Step - -The first step in the workflow receives the product’s ID and the data to update, then updates the product. - -Create the file `src/workflows/update-product-erp/steps/update-product.ts` with the following content: - -export const updateProductHighlights = [ - ["10", "resolve", "Resolve the `ProductService` from the Medusa container."], - ["13", "previousProductData", "Retrieve the `previousProductData` to pass it to the compensation function."], - ["16", "updateProducts", "Update the product."], - ["30", "updateProducts", "Revert the product’s data using the `previousProductData` passed from the step to the compensation function."] -] - -```ts title="src/workflows/update-product-erp/steps/update-product.ts" highlights={updateProductHighlights} collapsibleLines="1-9" expandButtonLabel="Show Imports" -import { createStep, StepResponse } from "@medusajs/workflows-sdk" -import { IProductModuleService } from "@medusajs/types" -import { Modules } from "@medusajs/utils" -import { UpdateProductAndErpWorkflowInput } from ".." - -const updateProduct = createStep( - "update-product", - async (input: UpdateProductAndErpWorkflowInput, context) => { - const productModuleService: IProductModuleService = - context.container.resolve(Modules.PRODUCT) - - const { id } = input - const previousProductData = - await productModuleService.retrieveProduct(id) - - const product = await productModuleService.updateProducts(id, input) - - return new StepResponse(product, { - // pass to compensation function - previousProductData, - }) - }, - // compensation function - async ({ previousProductData }, context) => { - const productModuleService: IProductModuleService = - context.container.resolve(Modules.PRODUCT) - - const { id, type, options, variants, ...previousData } = previousProductData - - await productModuleService.updateProducts( - id, - { - ...previousData, - variants: variants.map((variant) => { - const variantOptions = {} - - variant.options.forEach((option) => { - variantOptions[option.option.title] = option.value - }) - - return { - ...variant, - options: variantOptions, - } - }), - options: options.map((option) => ({ - ...option, - values: option.values.map((value) => value.value), - })), - type_id: type.id, - }) - } -) - -export default updateProduct -``` - -In the step: - -- You resolve the Product Module's main service from the Medusa container. -- You retrieve the `previousProductData` to pass it to the compensation function. -- You update and return the product. - -You also pass a compensation function as a second parameter to `createStep`. The compensation function runs if an error occurs during the workflow execution. - -In the compensation function, you revert the product’s data using the `previousProductData` passed from the step to the compensation function. - ---- - -## 3. Create Step 2: Update ERP - -The second step in the workflow receives the same input. It updates the product’s details in the ERP system. - - - -The `ErpModuleService` used is assumed to be created in a module. - - - -Create the file `src/workflows/update-product-erp/steps/update-erp.ts` with the following content: - -export const updateErpHighlights = [ - [ - "9", - "resolve", - "Resolve the `erpModuleService` from the Medusa container.", - ], - [ - "14", - "previousErpData", - "Retrieve the `previousErpData` to pass it to the compensation function.", - ], - [ - "16", - "updateProductErpData", - "Update the product’s ERP data and return the data from the ERP system.", - ], - [ - "31", - "updateProductErpData", - "Revert the product's data in the ERP system to its previous state using the `previousErpData`.", - ], -] - -```ts title="src/workflows/update-product-erp/steps/update-erp.ts" highlights={updateErpHighlights} collapsibleLines="1-8" expandButtonLabel="Show Imports" -import { createStep, StepResponse } from "@medusajs/workflows-sdk" -import { UpdateProductAndErpWorkflowInput } from ".." -import ErpModuleService from "../../../modules/erp/service" - -const updateErp = createStep( - "update-erp", - async (input: UpdateProductAndErpWorkflowInput, context) => { - const erpModuleService: ErpModuleService = - context.container.resolve("erpModuleService") - - const { id, ...updatedData } = input - - // get previous ERP data - const previousErpData = await erpModuleService.retrieveProductErpDetails(id) - - const updatedErpData = await erpModuleService.updateProductErpData( - id, - updatedData - ) - - return new StepResponse(updatedErpData, { - // pass to compensation function - previousErpData, - productId: id, - }) - }, - // compensation function - async ({ previousErpData, productId }, context) => { - const erpService: ErpModuleService = context.container.resolve("erpService") - - await erpService.updateProductErpData(productId, previousErpData) - } -) - -export default updateErp -``` - -In the step: - -- You resolve the `erpModuleService` from the Medusa container. -- You retrieve the `previousErpData` to pass it to the compensation function. -- You update the product’s ERP data and return the data from the ERP system. - -You also pass a compensation function as a second parameter to `createStep`. In the compensation function, you revert the product's data in the ERP system to its previous state. - ---- - -## 4. Create Workflow - -With the steps ready, you'll create the workflow that runs these steps to update the product’s data both in Medusa and the external ERP system. - -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-8" expandButtonLabel="Show Imports" -import { - createWorkflow, - WorkflowResponse, -} from "@medusajs/workflows-sdk" -import { UpsertProductDTO } from "@medusajs/types" -import updateProduct from "./steps/update-product" -import updateErp from "./steps/update-erp" - -export type UpdateProductAndErpWorkflowInput = UpsertProductDTO - -const updateProductAndErpWorkflow = createWorkflow( - "update-product-and-erp", - function (input: UpdateProductAndErpWorkflowInput) { - const product = updateProduct(input) - const erpData = updateErp(input) - - return new WorkflowResponse({ - product, - erpData, - }) -}) - -export default updateProductAndErpWorkflow -``` - -In the workflow construction function, you first run the `updateProduct` step, then the `updateErp` step. You return as the workflow’s result an object holding the updated product and ERP data. - ---- - -## 5. Execute Workflow - -You can now use and execute your workflow in your Medusa application. - -To execute the workflow in an API route, create the file `src/api/products/[id]/erp/route.ts` with the following content: - -```ts title="src/api/products/[id]/erp/route.ts" -import { MedusaRequest, MedusaResponse } from "@medusajs/medusa" -import updateProductAndErpWorkflow, { - UpdateProductAndErpWorkflowInput, -} from "../../../../../workflows/update-product-erp" - -type ProductErpReq = Omit - -export const POST = async ( - req: MedusaRequest, - res: MedusaResponse -) => { - // skipping validation for simplicity - const productData: UpdateProductAndErpWorkflowInput = { - id: req.params.id, - ...req.body, - } - - const { result } = await updateProductAndErpWorkflow(req.scope).run({ - input: productData, - }) - - res.json(result) -} -``` - -In this `POST` API route, you retrieve the product’s ID from the path parameter and the data to update from the request body. You then execute the workflow by passing it the retrieved data as an input. - -The route returns the result of the workflow, which is an object holding both the update product’s details and the ERP details. diff --git a/www/apps/book/app/advanced-development/workflows/compensation-function/page.mdx b/www/apps/book/app/advanced-development/workflows/compensation-function/page.mdx index 98d790bec5..27d900e2a7 100644 --- a/www/apps/book/app/advanced-development/workflows/compensation-function/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/compensation-function/page.mdx @@ -4,16 +4,25 @@ export const metadata = { # {metadata.title} -In this chapter, you'll learn how to add a compensation function to a step. +In this chapter, you'll learn what a compensation function is and how to add it to a step. -## Compensation Function +## What is a Compensation Function -To avoid data inconsistency when an error is thrown in a workflow, define a function (called a compensation function) and pass it as a second parameter to the `createStep` function. +A compensation function rolls back or undoes changes made by a step when an error occurs in the workflow. -For example: +For example, if a step creates a record, the compensation function deletes the record when an error occurs later in the workflow. -```ts title="src/workflows/hello-world.ts" highlights={[["16"], ["17"], ["18"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" -// other imports... +By using compensation functions, you provide a mechanism that guarantees data consistency in your application and across systems. + +--- + +## How to add a Compensation Function? + +A compensation function is passed as a second parameter to the `createStep` function. + +For example, create the file `src/workflows/hello-world.ts` with the following content: + +```ts title="src/workflows/hello-world.ts" highlights={[["15"], ["16"], ["17"]]} collapsibleLines="1-5" expandButtonLabel="Show Imports" import { createStep, StepResponse, @@ -34,16 +43,15 @@ const step1 = createStep( ) ``` -Each step can have a compensation function. The compensation function only runs if an error occurs throughout the workflow. It’s useful to undo or roll back actions you’ve performed in a step. +Each step can have a compensation function. The compensation function only runs if an error occurs throughout the workflow. --- ## Test the Compensation Function -1. Add another step that throws an error: +Create a step in the same `src/workflows/hello-world.ts` file that throws an error: ```ts title="src/workflows/hello-world.ts" -// ... const step2 = createStep( "step-2", async () => { @@ -52,16 +60,16 @@ const step2 = createStep( ) ``` -2. Use the steps in a workflow. For example: +Then, create a workflow that uses the steps: ```ts title="src/workflows/hello-world.ts" collapsibleLines="1-8" expandButtonLabel="Show Imports" import { - // other imports... createWorkflow, WorkflowResponse, } from "@medusajs/workflows-sdk" +// other imports... -// ... +// steps... const myWorkflow = createWorkflow( "hello-world", @@ -77,7 +85,7 @@ const myWorkflow = createWorkflow( export default myWorkflow ``` -3. Execute the workflow from a resource, such as an API route: +Finally, execute the workflow from an API route: ```ts title="src/api/workflow/route.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports" import type { @@ -97,13 +105,7 @@ export async function GET( } ``` -4. Run the Medusa application: - -```bash npm2yarn -npm run dev -``` - -5. Send a `GET` request to `/workflow`: +Run the Medusa application and send a `GET` request to `/workflow`: ```bash curl http://localhost:9000/workflow @@ -112,4 +114,85 @@ curl http://localhost:9000/workflow In the console, you'll see: - `Hello from step one!` logged in the terminal, indicating that the first step ran successfully. -- `Oops! Rolling back my changes...` logged in the terminal, indicating that the second step failed and the compensation function of the first step ran consequently. \ No newline at end of file +- `Oops! Rolling back my changes...` logged in the terminal, indicating that the second step failed and the compensation function of the first step ran consequently. + +--- + +## Pass Input to Compensation Function + +If a step creates a record, the compensation function must receive the ID of the record to remove it. + +To pass input to the compensation function, pass a second parameter in the `StepResponse` returned by the step. + +For example: + +export const inputHighlights = [ + ["11", "", "The data to pass as an input to the compensation function."], + ["14", "{ message }", "The data received as an input from `StepResponse`'s second parameter."] +] + +```ts highlights={inputHighlights} +import { + createStep, + StepResponse, +} from "@medusajs/workflows-sdk" + +const step1 = createStep( + "step-1", + async () => { + return new StepResponse( + `Hello from step one!`, + { message: "Oops! Rolling back my changes..."} + ) + }, + async ({ message }) => { + console.log(message) + } +) +``` + +In this example, the step passes an object as a second parameter to `StepResponse`. + +The compensation function receives the object and uses its `message` property to log a message. + +--- + +## Resolve Resources from the Medusa Container + +The compensation function receives an object second parameter. The object has a `container` property that you use to resolve resources from the Medusa container. + +For example: + +export const containerHighlights = [ + ["15", "container", "Access the container in the second parameter object."], + ["16", "resolve", "Use the container to resolve resources."] +] + +```ts +import { + createStep, + StepResponse, +} from "@medusajs/workflows-sdk" +import { ContainerRegistrationKeys } from "@medusajs/utils" + +const step1 = createStep( + "step-1", + async () => { + return new StepResponse( + `Hello from step one!`, + { message: "Oops! Rolling back my changes..."} + ) + }, + async ({ message }, { container }) => { + const logger = container.resolve( + ContainerRegistrationKeys.LOGGER + ) + + logger.info(message) + } +) +``` + +In this example, you use the `container` property in the second object parameter of the compensation function to resolve the logger. + +You then use the logger to log a message. diff --git a/www/apps/book/app/advanced-development/workflows/conditions/page.mdx b/www/apps/book/app/advanced-development/workflows/conditions/page.mdx index 9b4634ca66..fe7155101e 100644 --- a/www/apps/book/app/advanced-development/workflows/conditions/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/conditions/page.mdx @@ -1,27 +1,35 @@ export const metadata = { - title: `${pageNumber} Conditions in Workflow with When-Then`, + title: `${pageNumber} Conditions in Workflows with When-Then`, } # {metadata.title} In this chapter, you'll learn how to execute an action based on a condition in a workflow using the when-then utility. -## What is the When-Then Utility? +## Why If-Conditions Aren't Allowed in Workflows? -The when-then utility executes an action if a condition is satisfied. +Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store its steps. -Since if-conditions aren't allowed in the workflow constructor function, use the when-then utility if you want to execute a step based on a condition. +At that point, variables in the workflow don't have any values. They only do when you execute the workflow. + +So, you can't use an if-condition that checks a variable's value, as the condition will be evaluated when Medusa creates the internal representation of the workflow, rather than during execution. + +Instead, use the when-then utility. --- -## How to Use the When-Then Utility? +## What is the When-Then Utility? + +The when-then utility functions execute an action if a condition is satisfied. + +The `when` function accepts as a parameter a function that returns a boolean value, and the `then` function is chained to `when`. `then` accepts as a parameter a function that's executed if `when`'s parameter function returns a `true` value. For example: export const highlights = [ - ["16", "input", "The data to pass as a parameter to the function in the second parameter"], - ["18", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."], - ["20", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."] + ["15", "input", "The data to pass as a parameter to the function in the second parameter"], + ["17", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."], + ["19", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."] ] ```ts highlights={highlights} @@ -32,13 +40,12 @@ import { } from "@medusajs/workflows-sdk" // step imports... -type WorkflowInput = { - is_active: boolean -} - const workflow = createWorkflow( "workflow", - function (input: WorkflowInput) { + function (input: { + is_active: boolean + }) { + const result = when( input, (input) => { @@ -59,9 +66,17 @@ const workflow = createWorkflow( ) ``` -Then `when` utility is a function imported from `@medusajs/workflows-sdk`. It accepts the following parameters: +In this code snippet, you execute the `isActiveStep` only if the `input.is_active`'s value is `true`. -1. An object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter. -2. A function that returns a boolean indicating whether to execute the action. +### When Parameters -To specify the action to perform if the condition is satisfied, chain a `then` function to `when` and pass it a callback function. The callback function is only executed if `when`'s second parameter function returns a `true` value. \ No newline at end of file +`when` utility is a function imported from `@medusajs/workflows-sdk`. It accepts the following parameters: + +1. The first parameter is either an object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter. +2. The second parameter is a function that returns a boolean indicating whether to execute the action in `then`. + +### Then Parameters + +To specify the action to perform if the condition is satisfied, chain a `then` function to `when` and pass it a callback function. + +The callback function is only executed if `when`'s second parameter function returns a `true` value. \ No newline at end of file diff --git a/www/apps/book/app/advanced-development/workflows/constructor-constraints/page.mdx b/www/apps/book/app/advanced-development/workflows/constructor-constraints/page.mdx index 0592611de5..1bee88f1b8 100644 --- a/www/apps/book/app/advanced-development/workflows/constructor-constraints/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/constructor-constraints/page.mdx @@ -4,13 +4,13 @@ export const metadata = { # {metadata.title} -This chapter lists some constraints to keep in mind when defining a workflow or its steps. +This chapter lists constraints of defining a workflow or its steps. ## Workflow Constraints ### No Async Functions -The function passed to the `createWorkflow` can’t be an async function: +The function passed to `createWorkflow` can’t be an async function: ```ts highlights={[["4", "async", "Function can't be async."], ["11", "", "Correct way of defining the function."]]} // Don't @@ -28,13 +28,21 @@ const myWorkflow = createWorkflow( }) ``` -### No Direct Data Manipulation +### No Direct Variable Manipulation -Since the constructor function only defines how the workflow works, you can’t directly manipulate data within the function. Instead, use the `transform` function: +You can’t directly manipulate variables within the workflow's constructor function. + + + +Learn more about why you can't manipulate variables [in this chapter](../conditions/page.mdx#why-if-conditions-arent-allowed-in-workflows) + + + +Instead, use the `transform` utility function imported from `@medusajs/workflows-sdk`: export const highlights = [ - ["9", "", "Don't manipulate data directly."], - ["20", "transform", "Use the `transform` function to manipulate data."] + ["9", "", "Don't manipulate variables directly."], + ["20", "transform", "Use the `transform` function to manipulate variables."] ] ```ts highlights={highlights} @@ -45,9 +53,9 @@ const myWorkflow = createWorkflow( const str1 = step1(input) const str2 = step2(input) - return { + return new WorkflowResponse({ message: `${str1}${str2}`, - } + }) }) // Do @@ -67,13 +75,21 @@ const myWorkflow = createWorkflow( }) ) - return result + return new WorkflowResponse(result) }) ``` ### No If Conditions -You can't use if-conditions in a workflow. Instead, use the when-then utility function explained in the next chapter: +You can't use if-conditions in a workflow. + + + +Learn more about why you can't use if-conditions [in this chapter](../conditions/page.mdx#why-if-conditions-arent-allowed-in-workflows) + + + +Instead, use the when-then utility function imported from `@medusajs/workflows-sdk`: ```ts // Don't @@ -104,7 +120,9 @@ const myWorkflow = createWorkflow( ### Returned Values -A step must only return [primitive values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) or an object. Values of other types, such as Maps, aren't allowed. +A step must only return serializable values, such as [primitive values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) or an object. + +Values of other types, such as Maps, aren't allowed. ```ts // Don't diff --git a/www/apps/book/app/advanced-development/workflows/execute-another-workflow/page.mdx b/www/apps/book/app/advanced-development/workflows/execute-another-workflow/page.mdx index f652d7e5a3..0994482f61 100644 --- a/www/apps/book/app/advanced-development/workflows/execute-another-workflow/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/execute-another-workflow/page.mdx @@ -41,7 +41,9 @@ const workflow = createWorkflow( ) ``` -Instead of invoking the workflow, passing it the container, you instead use its `runAsStep` method and pass it an object as a parameter. The object has an `input` property to pass input to the workflow. +Instead of invoking the workflow, passing it the container, you use its `runAsStep` method and pass it an object as a parameter. + +The object has an `input` property to pass input to the workflow. --- @@ -49,6 +51,12 @@ Instead of invoking the workflow, passing it the container, you instead use its If you need to perform some data manipulation to prepare the other workflow's input data, use the `transform` utility function imported from `@medusajs/workflows-sdk`. + + +Learn about the transform utility in [this chapter](../variable-manipulation/page.mdx). + + + For example: export const transformHighlights = [ @@ -91,13 +99,19 @@ const workflow = createWorkflow( ) ``` -In this example, you use the `transform` function to prepend `Hello` to the title of the product. Then, you pass the variable created through `transform` as an input to the `createProductsWorkflow`. +In this example, you use the `transform` function to prepend `Hello` to the title of the product. Then, you pass the result as an input to the `createProductsWorkflow`. --- ## Run Workflow Conditionally -To run a workflow in another based on a condition, use the `when` utility function imported from `@medusajs/workflows-sdk`. +To run a workflow in another based on a condition, use the when-then utility functions imported from `@medusajs/workflows-sdk`. + + + +Learn about the when-then utility in [this chapter](../conditions/page.mdx). + + For example: diff --git a/www/apps/book/app/advanced-development/workflows/long-running-workflow/page.mdx b/www/apps/book/app/advanced-development/workflows/long-running-workflow/page.mdx index 567a841f01..2e81b6c9ce 100644 --- a/www/apps/book/app/advanced-development/workflows/long-running-workflow/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/long-running-workflow/page.mdx @@ -10,10 +10,17 @@ In this chapter, you’ll learn what a long-running workflow is and how to confi ## What is a Long-Running Workflow? -By default, when you execute a workflow, you wait until the workflow finishes execution. Once you receive the workflow’s output, the rest of the code is executed. +When you execute a workflow, you wait until the workflow finishes execution to receive the output. A long-running workflow is a workflow that continues its execution in the background. You don’t receive its output immediately. Instead, you subscribe to the workflow execution to listen to status changes and receive its result once the execution is finished. +### Why use Long-Running Workflows? + +Long-running workflows are useful if: + +- A task takes too long. For example, you're importing data from a CSV file. +- The workflow's steps wait for an external action to finish before resuming execution. For example, before you import the data from the CSV file, you wait until the import is confirmed by the user. + --- ## Configure Long-Running Workflows @@ -71,9 +78,9 @@ So, when you execute the `hello-world` workflow, it continues its execution in t ## Change Step Status -Once the workflow's execution reaches an async step, it'll wait in the background for the step to succeed or fail before it moves to the rest of the steps. +Once the workflow's execution reaches an async step, it'll wait in the background for the step to succeed or fail before it moves to the next step. -To change fail or succeed a step, use the workflow engine's main service that is registered in the Medusa Container under the `Modules.WORKFLOW_ENGINE` (or `workflowsModuleService`) key. +To fail or succeed a step, use the Workflow Engine Module's main service that is registered in the Medusa Container under the `Modules.WORKFLOW_ENGINE` (or `workflowsModuleService`) key. ### Retrieve Transaction ID @@ -90,7 +97,9 @@ const { transaction } = await myWorkflow(req.scope) ### Change Step Status to Successful -For example, the following workflow step (used in a different workflow) resolves the workflow engine's main service and sets `step-2` of the previous workflow as successful: +The Workflow Engine Module's main service has a `setStepSuccess` method to set a step's status to successful. If you use it on a workflow execution's async step, the workflow continues execution to the next step. + +For example, consider the following step: export const successStatusHighlights = [ ["17", "transactionId", "Receive the workflow execution's transaction ID as an input to the step."], @@ -141,9 +150,7 @@ export const setStepSuccessStep = createStep( ); ``` -You use this step in another workflow that changes the long-running workflow's status. - -After change the async step's status to successful, the workflow execution continues to the next step. +In this step (which you use in a workflow other than the long-running workflow), you resolve the Workflow Engine Module's main service and set `step-2` of the previous workflow as successful. The `setStepSuccess` method of the workflow engine's main service accepts as a parameter an object having the following properties: @@ -206,7 +213,9 @@ The `setStepSuccess` method of the workflow engine's main service accepts as a p ### Change Step Status to Failed -The workflow engine's main service also has a `setStepFailure` method that changes a step's status to failed. It accepts the same parameter as `setStepSuccess`. +The Workflow Engine Module's main service also has a `setStepFailure` method that changes a step's status to failed. It accepts the same parameter as `setStepSuccess`. + +After changing the async step's status to failed, the workflow execution fails and the compensation functions of previous steps are executed. For example: @@ -259,15 +268,13 @@ export const setStepFailureStep = createStep( ); ``` -You use this step in another workflow that changes the long-running workflow's status. - -After change the async step's status to failed, the workflow execution fails and the compensation functions of previous steps are executed. +You use this step in another workflow that changes the status of an async step in a long-running workflow's execution to failed. --- ## Access Long-Running Workflow Status and Result -To access the status and result of a long-running workflow, use the workflow engine's main service' `subscribe` and `unsubscribe` methods. +To access the status and result of a long-running workflow execution, use the `subscribe` and `unsubscribe` methods of the Workflow Engine Module's main service. For example: @@ -319,9 +326,11 @@ export async function GET(req: MedusaRequest, res: MedusaResponse) { } ``` -In the above example, you execute the long-running workflow `hello-world`. You then resolve the workflow engine from the Medusa container and use its `subscribe` method to listen to changes in the workflow execution’s status. +In the above example, you execute the long-running workflow `hello-world` and resolve the Workflow Engine Module's main service from the Medusa container. -The `subscribe` method accepts an object having three properties: +### subscribe Method + +The main service's `subscribe` method allows you to listen to changes in the workflow execution’s status. It accepts an object having three properties: Promise`", + description: + "The function executed when the workflow execution's status changes. The function receives a data object. It has an `eventType` property, which you use to check the status of the workflow execution.", }, ]} sectionTitle="Access Long-Running Workflow Status and Result" /> -Once the workflow execution finishes, the subscriber function is executed with the `eventType` of the received parameter set to `onFinish`. The workflow’s output is set in the `result` property of the parameter. +If the value of `eventType` in the `subscriber` function's first parameter is `onFinish`, the workflow finished executing. The first parameter then also has a `result` property holding the workflow's output. + +### unsubscribe Method You can unsubscribe from the workflow using the workflow engine's `unsubscribe` method, which requires the same object parameter as the `subscribe` method. + +However, instead of the `subscriber` property, it requires a `subscriberOrId` property whose value is the same `subscriberId` passed to the `subscribe` method. + +--- + +## Example: Restaurant-Delivery Recipe + +To find a full example of a long-running workflow, refer to the [restaurant-delivery recipe](!resources!/recipes/marketplace/examples/restaurant-delivery). + +In the recipe, you use a long-running workflow that moves an order from placed to completed. The workflow waits for the restaurant to accept the order, the driver to pick up the order, and other external actions. \ No newline at end of file diff --git a/www/apps/book/app/advanced-development/workflows/page.mdx b/www/apps/book/app/advanced-development/workflows/page.mdx new file mode 100644 index 0000000000..8b73c9a2e0 --- /dev/null +++ b/www/apps/book/app/advanced-development/workflows/page.mdx @@ -0,0 +1,14 @@ +export const metadata = { + title: `${pageNumber} Workflows Advanced Development`, +} + +# {metadata.title} + +In the next chapters, you'll learn about workflows in-depth and how to use them in your custom development. + +By the end of these chapters, you'll learn about: + +- Constructing a workflow and its constraints. +- Using a compensation function to undo a step's action when errors occur. +- Hooks and how to consume and expose them. +- Configurations to retry workflows or run them in the background. diff --git a/www/apps/book/app/advanced-development/workflows/parallel-steps/page.mdx b/www/apps/book/app/advanced-development/workflows/parallel-steps/page.mdx index a50f845d91..31941584f1 100644 --- a/www/apps/book/app/advanced-development/workflows/parallel-steps/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/parallel-steps/page.mdx @@ -10,7 +10,7 @@ In this chapter, you’ll learn how to run workflow steps in parallel. If your workflow has steps that don’t rely on one another’s results, run them in parallel using the `parallelize` utility function imported from the `@medusajs/workflows-sdk`. -The workflow waits until all steps passed to the `parallelize` function finish executing before continuing with the rest of its implementation. +The workflow waits until all steps passed to the `parallelize` function finish executing before continuing to the next step. For example: @@ -56,4 +56,6 @@ const myWorkflow = createWorkflow( The `parallelize` function accepts the steps to run in parallel as a parameter. -It returns an array of the steps' results. The results are ordered based on the `parallelize` parameters' order. \ No newline at end of file +It returns an array of the steps' results in the same order they're passed to the `parallelize` function. + +So, `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`. \ No newline at end of file diff --git a/www/apps/book/app/advanced-development/workflows/variable-manipulation/page.mdx b/www/apps/book/app/advanced-development/workflows/variable-manipulation/page.mdx new file mode 100644 index 0000000000..b2cd4f66fe --- /dev/null +++ b/www/apps/book/app/advanced-development/workflows/variable-manipulation/page.mdx @@ -0,0 +1,116 @@ +export const metadata = { + title: `${pageNumber} Variable Manipulation in Workflows with transform`, +} + +# {metadata.title} + +In this chapter, you'll learn how to manipulate variables in a workflow using the transform utility. + +## Why Variable Manipulation isn't Allowed in Worflows? + +Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store its steps. + +At that point, variables in the workflow don't have any values. They only do when you execute the workflow. + +So, you can only pass variables as parameters to steps. But, in a workflow, you can't change a variable's value or, if the variable is an array, loop over its items. + +Instead, use the transform utility. + +--- + +## What is the transform Utility? + +The `transform` utility function creates a new variable as the result of manipulating other variables. + +For example, consider you have two strings as the output of two steps: + +```ts +const str1 = step1() +const str2 = step2() +``` + +To concatinate the strings, you create a new variable `str3` using the `transform` function: + +export const highlights = [ + ["14", "str3", "Holds the result returned by `transform`'s second parameter function."], + ["15", "", "Specify the data to pass as a parameter to the function in the second parameter."], + ["16", "data", "The data passed in the first parameter of `transform`."], + ["16", "`${data.str1}${data.str2}`", "Return the concatenated strings."] +] + +```ts highlights={highlights} +import { + createWorkflow, + WorkflowResponse, + transform, +} from "@medusajs/workflows-sdk" +// step imports... + +const myWorkflow = createWorkflow( + "hello-world", + function (input) { + const str1 = step1(input) + const str2 = step2(input) + + const str3 = transform( + { str1, str2 }, + (data) => `${data.str1}${data.str2}` + ) + + return new WorkflowResponse(str3) + } +) +``` + +The `transform` utility function is imported from `@medusajs/workflows-sdk`. It accepts two parameters: + +1. The first parameter is an object of variables to manipulate. The object is passed as a parameter to `transform`'s second parameter function. +2. The second parameter is the function performing the variable manipulation. + +The value returned by the second parameter function is returned by `transform`. So, the `str3` variable holds the concatenated string. + +You can use the returned value in the rest of the workflow, either to pass it as an input to other steps or to return it in the workflow's response. + +--- + +## Example: Looping Over Array + +Use `transform` to loop over arrays to create another variable from the array's items. + +For example: + +```ts collapsibleLines="1-7" expandButtonLabel="Show Imports" +import { + createWorkflow, + WorkflowResponse, + transform, +} from "@medusajs/workflows-sdk" +// step imports... + +type WorkflowInput = { + items: { + id: string + name: string + }[] +} + +const myWorkflow = createWorkflow( + "hello-world", + function ({ items }: WorkflowInput) { + const ids = transform( + { items }, + (data) => data.items.map((item) => item.id) + ) + + doSomethingStep(ids) + + // ... + } +) +``` + +This workflow receives an `items` array in its input. + +You use the `transform` utility to create an `ids` variable, which is an array of strings holding the `id` of each item in the `items` array. + +You then pass the `ids` variable as a parameter to the `doSomethingStep`. \ No newline at end of file diff --git a/www/apps/book/app/advanced-development/workflows/workflow-hooks/page.mdx b/www/apps/book/app/advanced-development/workflows/workflow-hooks/page.mdx index 1e06ecde71..a90e7be0ca 100644 --- a/www/apps/book/app/advanced-development/workflows/workflow-hooks/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/workflow-hooks/page.mdx @@ -4,7 +4,7 @@ export const metadata = { # {metadata.title} -In this chapter, you'll learn what a workflow hook is, and an example of how to consume a workflow hook defined in Medusa. +In this chapter, you'll learn what a workflow hook is and how to consume them. ## What is a Workflow Hook? @@ -12,6 +12,12 @@ A workflow hook is a point in a workflow where you can inject custom functionali Medusa exposes hooks in many of its workflows that are used in its API routes. You can consume those hooks to add your custom logic. + + +Refer to the [Workflows Reference](!resources!/medusa-workflows-reference) to view all workflows and their hooks. + + + You want to perform a custom action during a workflow's execution, such as when a product is created. @@ -22,17 +28,21 @@ You want to perform a custom action during a workflow's execution, such as when ## How to Consume a Hook? -You consume a hook by registering a hook handler on the workflow. A hook handler is registered in a TypeScript or JavaScript file created in the `src/workflows/hooks` directory. +A workflow has a special `hooks` property which is an object that holds its hooks. -You'll find a workflow's exposed hooks in its `hooks` property. +So, in a TypeScript or JavaScript file created under the `src/workflows/hooks` directory: -For example, to consume the `productsCreated` hook of Medusa's `createProductsWorkflow`, create the file `src/workflows/hooks/my-workflow.ts` with the following content: +- Import the workflow. +- Access its hook using the `hooks` property. +- Pass the hook a step function as a parameter to consume it. + +For example, to consume the `productsCreated` hook of Medusa's `createProductsWorkflow`, create the file `src/workflows/hooks/product-created.ts` with the following content: export const handlerHighlights = [ ["3", "productsCreated", "Invoke the hook, passing it a step function as a parameter."], ] -```ts title="src/workflows/hooks/my-workflow.ts" highlights={handlerHighlights} +```ts title="src/workflows/hooks/product-created.ts" highlights={handlerHighlights} import { createProductsWorkflow } from "@medusajs/core-flows" createProductsWorkflow.hooks.productsCreated( @@ -42,9 +52,11 @@ createProductsWorkflow.hooks.productsCreated( ) ``` -The hook is available on the workflow's `hooks` property using its name `productsCreated`. You invoke the hook, passing the handler as a parameter, which is a step function. +The `productsCreated` hook is available on the workflow's `hooks` property by its name. -Now, when a product is created using the Create Product API route, your hooks handler is executed. +You invoke the hook, passing a step function (the hook handler) as a parameter. + +Now, when a product is created using the [Create Product API route](!api!/admin#products_postproducts), your hook handler is executed after the product is created. @@ -52,20 +64,34 @@ A hook can have only one handler. -Similar to a step, the handler receives the hook's input as a first parameter, and the container in the object as a second parameter. + + +Refer to the [createProductsWorkflow reference](!resources!/references/medusa-workflows/createProductsWorkflow) to see at which point the hook handler is executed. + + + +### Hook Handler Parameter + +Since a hook handler is essentially a step function, it receives the hook's input as a first parameter, and an object holding a `container` property as a second parameter. + +Each hook has different input. For example, the `productsCreated` hook receives an object having a `products` property holding the created product. ### Hook Handler Compensation -You can also pass a compensation function as a second parameter: +Since the hook handler is a step function, you can set its compensation function as a second parameter of the hook. -```ts -import { myWorkflow } from "../my-workflow" +For example: -myWorkflow.hooks.productCreated( +```ts title="src/workflows/hooks/product-created.ts" +import { createProductsWorkflow } from "@medusajs/core-flows" + +createProductsWorkflow.productCreated( async ({ productId }, { container }) => { // TODO perform an action + + return new StepResponse(undefined, { ids }) }, - async () => { + async ({ ids }, { container }) => { // undo the performed action } ) @@ -73,11 +99,15 @@ myWorkflow.hooks.productCreated( The compensation function is executed if an error occurs in the workflow to undo the actions performed by the hook handler. -### Additional Data +The compensation function receives as an input the second parameter passed to the `StepResponse` returned by the step function. -Medusa's workflow hooks pass to their handlers in the first parameter object an `additional_data` property: +It also accepts as a second parameter an object holding a `container` property to resolve resources from the Medusa container. -```ts highlights={[["4", "additional_data"]]} +### Additional Data Property + +Medusa's workflows pass in the hook's input an `additional_data` property: + +```ts title="src/workflows/hooks/product-created.ts" highlights={[["4", "additional_data"]]} import { createProductsWorkflow } from "@medusajs/core-flows" createProductsWorkflow.hooks.productsCreated( @@ -87,13 +117,19 @@ createProductsWorkflow.hooks.productsCreated( ) ``` -This property is an object that holds additional data passed to the workflow. +This property is an object that holds additional data passed to the workflow through the request sent to the API route using the workflow. -{/* TODO change to talk about validators once we can document them. */} + -To pass that additional data when executing the workflow, pass it as a parameter to the `.run` method of the workflow: +Learn how to pass `additional_data` in requests to API routes in [this chapter](../../api-routes/additional-data/page.mdx). -```ts highlights={[["10", "additional_data"]]} + + +### Pass Additional Data to Workflow + +You can also pass that additional data when executing the workflow. Pass it as a parameter to the `.run` method of the workflow: + +```ts title="src/workflows/hooks/product-created.ts" highlights={[["10", "additional_data"]]} import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa" import { createProductsWorkflow } from "@medusajs/core-flows" @@ -112,5 +148,3 @@ export async function POST(req: MedusaRequest, res: MedusaResponse) { ``` Your hook handler then receives that passed data in the `additional_data` object. - -{/* TODO add a link to the workflows reference once available */} \ No newline at end of file diff --git a/www/apps/book/app/advanced-development/workflows/workflow-timeout/page.mdx b/www/apps/book/app/advanced-development/workflows/workflow-timeout/page.mdx index 9e38a37992..e7488827b9 100644 --- a/www/apps/book/app/advanced-development/workflows/workflow-timeout/page.mdx +++ b/www/apps/book/app/advanced-development/workflows/workflow-timeout/page.mdx @@ -6,17 +6,23 @@ export const metadata = { In this chapter, you’ll learn how to set a timeout for workflows and steps. -## Configure Workflow Timeout +## What is a Workflow Timeout? By default, a workflow doesn’t have a timeout. It continues execution until it’s finished or an error occurs. -You can configure a workflow’s timeout to indicate how long the workflow can run. Once the specified time is passed and the workflow is still running, the workflow is considered failed and an error is thrown. +You can configure a workflow’s timeout to indicate how long the workflow can execute. If a workflow's execution time passes the configured timeout, it is failed and an error is thrown. - +### Timeout Doesn't Stop Step Execution -Timeout doesn't stop the execution of a running step. The timeout only affects the status of the workflow and its result. +Configuring a timeout doesn't stop the execution of a step in progress. The timeout only affects the status of the workflow and its result. - +--- + +## Configure Workflow Timeout + +The `createWorkflow` function can accept a configuration object instead of the workflow’s name. + +In the configuration object, you pass a `timeout` property, whose value is a number indicating the timeout in seconds. For example: @@ -49,7 +55,7 @@ export default myWorkflow ``` -The `createWorkflow` function can accept a configuration object instead of the workflow’s name. In the configuration object, you pass a `timeout` property, whose value is a number indicating the timeout in seconds. +This workflow's executions fail if they run longer than two seconds. @@ -61,7 +67,7 @@ A workflow’s timeout error is returned in the `errors` property of the workflo ## Configure Step Timeout -Alternatively, you can configure timeout for a step rather than the entire workflow. +Alternatively, you can configure the timeout for a step rather than the entire workflow. @@ -69,6 +75,8 @@ As mentioned in the previous section, the timeout doesn't stop the execution of +The step’s configuration object accepts a `timeout` property, whose value is a number indicating the timeout in seconds. + For example: ```tsx @@ -83,7 +91,7 @@ const step1 = createStep( ) ``` -The step’s configuration object accepts a `timeout` property, whose value is a number indicating the timeout in seconds. +This step's executions fail if they run longer than two seconds. diff --git a/www/apps/book/app/basics/workflows/page.mdx b/www/apps/book/app/basics/workflows/page.mdx index 7a04688933..f34a6279d8 100644 --- a/www/apps/book/app/basics/workflows/page.mdx +++ b/www/apps/book/app/basics/workflows/page.mdx @@ -213,9 +213,7 @@ You’ll receive the following response: -- You're defining a flow with interactions across multiple systems and services. -- You're defining flows to be used across different resources. For example, if you want to invoke the flow manually through an API Router, but also want to automate its running through a scheduled job. -- You want to maintain data consistency and handle errors gracefully by rolling-back steps. This is explained more in later chapters. +You're implementing a custom feature exposed by an API route, or used in subscribers or scheduled jobs. diff --git a/www/apps/book/generated/edit-dates.mjs b/www/apps/book/generated/edit-dates.mjs index 688a15069b..9b3b3365d6 100644 --- a/www/apps/book/generated/edit-dates.mjs +++ b/www/apps/book/generated/edit-dates.mjs @@ -1,20 +1,21 @@ export const generatedEditDates = { "app/basics/scheduled-jobs/page.mdx": "2024-08-05T07:24:27+00:00", - "app/basics/workflows/page.mdx": "2024-09-11T10:48:38.853Z", + "app/basics/workflows/page.mdx": "2024-09-18T08:01:24.328Z", "app/deployment/page.mdx": "2024-08-05T07:24:05+00:00", "app/page.mdx": "2024-09-03T07:09:09.034Z", "app/basics/modules-and-services/page.mdx": "2024-09-11T10:48:35.195Z", "app/basics/commerce-modules/page.mdx": "2024-09-03T07:48:48.148Z", "app/advanced-development/workflows/retry-failed-steps/page.mdx": "2024-07-31T17:01:33+03:00", - "app/advanced-development/workflows/workflow-hooks/page.mdx": "2024-09-10T11:39:51.168Z", + "app/advanced-development/workflows/workflow-hooks/page.mdx": "2024-09-18T12:27:15.321Z", + "app/cheatsheet/page.mdx": "2024-07-11T13:53:40+03:00", "app/debugging-and-testing/logging/page.mdx": "2024-07-04T17:26:03+03:00", "app/more-resources/page.mdx": "2024-07-04T17:26:03+03:00", "app/storefront-development/page.mdx": "2024-09-11T10:58:59.290Z", "app/storefront-development/nextjs-starter/page.mdx": "2024-07-04T17:26:03+03:00", "app/basics/page.mdx": "2024-09-03T07:11:06.879Z", "app/basics/admin-customizations/page.mdx": "2024-09-03T08:07:35.584Z", - "app/advanced-development/workflows/workflow-timeout/page.mdx": "2024-07-31T17:01:33+03:00", - "app/advanced-development/workflows/parallel-steps/page.mdx": "2024-07-31T17:01:33+03:00", + "app/advanced-development/workflows/workflow-timeout/page.mdx": "2024-09-18T13:03:13.095Z", + "app/advanced-development/workflows/parallel-steps/page.mdx": "2024-09-18T12:56:50.436Z", "app/advanced-development/page.mdx": "2024-07-04T17:26:03+03:00", "app/first-customizations/page.mdx": "2024-09-11T10:48:42.374Z", "app/debugging-and-testing/page.mdx": "2024-05-03T17:36:38+03:00", @@ -23,32 +24,32 @@ export const generatedEditDates = { "app/basics/project-directories-files/page.mdx": "2024-07-04T17:26:03+03:00", "app/basics/api-routes/page.mdx": "2024-09-11T10:48:31.777Z", "app/basics/modules-directory-structure/page.mdx": "2024-05-07T18:00:28+02:00", - "app/advanced-development/workflows/access-workflow-errors/page.mdx": "2024-09-11T10:46:54.913Z", + "app/advanced-development/workflows/access-workflow-errors/page.mdx": "2024-09-18T12:54:04.695Z", "app/basics/events-and-subscribers/page.mdx": "2024-09-03T08:01:30.986Z", "app/advanced-development/modules/container/page.mdx": "2024-08-05T07:23:49+00:00", "app/basics/data-models/page.mdx": "2024-09-19T07:24:38.584Z", - "app/advanced-development/workflows/execute-another-workflow/page.mdx": "2024-07-21T21:19:23+02:00", + "app/advanced-development/workflows/execute-another-workflow/page.mdx": "2024-09-18T13:29:11.644Z", "app/basics/loaders/page.mdx": "2024-09-03T08:00:45.993Z", "app/advanced-development/admin/widgets/page.mdx": "2024-08-06T09:44:22+02:00", "app/advanced-development/data-models/page.mdx": "2024-09-19T07:26:43.535Z", "app/advanced-development/modules/remote-link/page.mdx": "2024-07-24T09:16:01+02:00", "app/advanced-development/api-routes/protected-routes/page.mdx": "2024-09-11T10:45:44.293Z", - "app/advanced-development/workflows/add-workflow-hook/page.mdx": "2024-08-13T09:55:37+03:00", + "app/advanced-development/workflows/add-workflow-hook/page.mdx": "2024-09-18T12:52:24.511Z", "app/advanced-development/events-and-subscribers/data-payload/page.mdx": "2024-07-16T17:12:05+01:00", "app/advanced-development/data-models/default-properties/page.mdx": "2024-09-19T07:32:06.118Z", "app/advanced-development/workflows/advanced-example/page.mdx": "2024-09-11T10:46:59.975Z", "app/advanced-development/events-and-subscribers/emit-event/page.mdx": "2024-09-10T11:39:51.168Z", - "app/advanced-development/workflows/conditions/page.mdx": "2024-07-31T17:01:33+03:00", + "app/advanced-development/workflows/conditions/page.mdx": "2024-09-18T08:52:40.755Z", "app/advanced-development/modules/module-link-directions/page.mdx": "2024-07-24T09:16:01+02:00", "app/advanced-development/admin/page.mdx": "2024-05-29T13:50:19+03:00", - "app/advanced-development/workflows/long-running-workflow/page.mdx": "2024-09-11T11:29:58.203Z", - "app/advanced-development/workflows/constructor-constraints/page.mdx": "2024-07-17T13:19:51+01:00", - "app/advanced-development/data-models/write-migration/page.mdx": "2024-09-19T08:50:51.503Z", - "app/advanced-development/data-models/manage-relationships/page.mdx": "2024-09-19T12:13:08.980Z", + "app/advanced-development/workflows/long-running-workflow/page.mdx": "2024-09-18T13:26:19.706Z", + "app/advanced-development/workflows/constructor-constraints/page.mdx": "2024-09-18T08:58:08.705Z", + "app/advanced-development/data-models/write-migration/page.mdx": "2024-07-15T17:46:10+02:00", + "app/advanced-development/data-models/manage-relationships/page.mdx": "2024-09-10T11:39:51.167Z", "app/advanced-development/modules/remote-query/page.mdx": "2024-07-21T21:20:24+02:00", "app/advanced-development/modules/options/page.mdx": "2024-08-05T07:23:49+00:00", - "app/advanced-development/data-models/relationships/page.mdx": "2024-09-19T11:26:16.387Z", - "app/advanced-development/workflows/compensation-function/page.mdx": "2024-07-31T17:01:33+03:00", + "app/advanced-development/data-models/relationships/page.mdx": "2024-09-11T11:28:55.494Z", + "app/advanced-development/workflows/compensation-function/page.mdx": "2024-09-18T09:13:11.941Z", "app/advanced-development/modules/service-factory/page.mdx": "2024-07-26T14:40:56+00:00", "app/advanced-development/data-models/primary-key/page.mdx": "2024-07-02T12:34:44+03:00", "app/advanced-development/modules/module-links/page.mdx": "2024-07-24T09:16:01+02:00", @@ -80,6 +81,10 @@ export const generatedEditDates = { "app/advanced-development/modules/query/page.mdx": "2024-09-11T10:46:49.512Z", "app/debugging-and-testing/testing-tools/modules-tests/module-example/page.mdx": "2024-09-10T11:39:51.171Z", "app/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2024-09-10T11:39:51.171Z", + "app/debugging-and-testing/instrumentation/page.mdx": "2024-09-17T08:53:15.910Z", + "app/advanced-development/api-routes/additional-data/page.mdx": "2024-09-18T12:22:26.063Z", + "app/advanced-development/workflows/page.mdx": "2024-09-18T08:00:57.364Z", + "app/advanced-development/workflows/variable-manipulation/page.mdx": "2024-09-18T09:03:20.805Z", "app/customization/custom-features/api-route/page.mdx": "2024-09-12T12:42:34.201Z", "app/customization/custom-features/module/page.mdx": "2024-09-12T12:39:37.928Z", "app/customization/custom-features/workflow/page.mdx": "2024-09-12T12:40:39.582Z", diff --git a/www/apps/book/sidebar.mjs b/www/apps/book/sidebar.mjs index c0c1a504ad..daaa143921 100644 --- a/www/apps/book/sidebar.mjs +++ b/www/apps/book/sidebar.mjs @@ -235,6 +235,11 @@ export const sidebar = numberSidebarItems( path: "/advanced-development/api-routes/cors", title: "Handling CORS", }, + { + type: "link", + path: "/advanced-development/api-routes/additional-data", + title: "Additional Data", + }, ], }, { @@ -367,18 +372,24 @@ export const sidebar = numberSidebarItems( ], }, { - type: "sub-category", + type: "link", + path: "/advanced-development/workflows", title: "Workflows", children: [ { type: "link", - path: "/advanced-development/workflows/constructor-constraints", - title: "Constraints", + path: "/advanced-development/workflows/variable-manipulation", + title: "Variable Manipulation", }, { type: "link", path: "/advanced-development/workflows/conditions", - title: "Conditions in Workflows", + title: "Using Conditions", + }, + { + type: "link", + path: "/advanced-development/workflows/constructor-constraints", + title: "Constructor Constraints", }, { type: "link", @@ -425,11 +436,6 @@ export const sidebar = numberSidebarItems( path: "/advanced-development/workflows/execute-another-workflow", title: "Execute Another Workflow", }, - { - type: "link", - path: "/advanced-development/workflows/advanced-example", - title: "Example: Advanced Workflow", - }, ], }, { diff --git a/www/apps/resources/app/recipes/marketplace/examples/restaurant-delivery/page.mdx b/www/apps/resources/app/recipes/marketplace/examples/restaurant-delivery/page.mdx index dabfca141f..8f8cfad814 100644 --- a/www/apps/resources/app/recipes/marketplace/examples/restaurant-delivery/page.mdx +++ b/www/apps/resources/app/recipes/marketplace/examples/restaurant-delivery/page.mdx @@ -24,9 +24,6 @@ By following this example, you’ll have a restaurant-delivery platform with the 3. Delivery handling, from the restaurant accepting the order to the driver delivering the order to the customer. 4. Real-time tracking of the order’s delivery status. -- Example Repository -- OpenAPI Spec for Postman -