浏览代码

Merge branch 'feat/v2' of github.com:medusajs/medusa-starter-default into feat/v2

olivermrbl 1 年之前
父节点
当前提交
14898c9f25
共有 2 个文件被更改,包括 85 次插入8 次删除
  1. 8 8
      src/subscribers/README.md
  2. 77 0
      src/workflows/README.md

+ 8 - 8
src/subscribers/README.md

@@ -8,7 +8,6 @@ For example, create the file `src/subscribers/product-created.ts` with the follo
 
 ```ts
 import {
-  ProductService,
   type SubscriberConfig,
 } from "@medusajs/medusa"
 
@@ -19,7 +18,7 @@ export default async function productCreateHandler() {
 
 // subscriber config
 export const config: SubscriberConfig = {
-  event: ProductService.Events.CREATED,
+  event: "product.created",
 }
 ```
 
@@ -36,10 +35,9 @@ A subscriber receives an object having the following properties:
 - `container`: The Medusa container. Use it to resolve modules' main services and other registered resources.
 
 ```ts
-import {
-  ProductService,
+import type {
   SubscriberArgs,
-  type SubscriberConfig,
+  SubscriberConfig,
 } from "@medusajs/medusa"
 import { IProductModuleService } from "@medusajs/types"
 import { ModuleRegistrationName } from "@medusajs/modules-sdk"
@@ -47,16 +45,18 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk"
 export default async function productCreateHandler({
   data,
   container,
-}: SubscriberArgs<Record<string, string>>) {
+}: SubscriberArgs<{ id: string }>) {
+  const productId = "data" in data ? data.data.id : data.id
+
   const productModuleService: IProductModuleService =
     container.resolve(ModuleRegistrationName.PRODUCT)
 
-  const product = await productModuleService.retrieve(data.id)
+  const product = await productModuleService.retrieve(productId)
 
   console.log(`The product ${product.title} was created`)
 }
 
 export const config: SubscriberConfig = {
-  event: ProductService.Events.CREATED,
+  event: "product.created",
 }
 ```

+ 77 - 0
src/workflows/README.md

@@ -0,0 +1,77 @@
+# Custom Workflows
+
+A workflow is a series of queries and actions that complete a task.
+
+The workflow is created in a TypeScript or JavaScript file under the `src/workflows` directory.
+
+For example:
+
+```ts
+import { 
+  createStep,
+  createWorkflow,
+  StepResponse,
+} from "@medusajs/workflows-sdk"
+
+const step1 = createStep("step-1", async () => {
+  return new StepResponse(`Hello from step one!`)
+})
+
+type WorkflowInput = {
+  name: string
+}
+
+const step2 = createStep(
+  "step-2",
+  async ({ name }: WorkflowInput) => {
+    return new StepResponse(`Hello ${name} from step two!`)
+  }
+)
+
+type WorkflowOutput = {
+  message: string
+}
+
+const myWorkflow = createWorkflow<
+  WorkflowInput,
+  WorkflowOutput
+>("hello-world", function (input) {
+  const str1 = step1()
+  // to pass input
+  step2(input)
+
+  return {
+    message: str1,
+  }
+})
+
+export default myWorkflow
+```
+
+## Execute Workflow
+
+You can execute the workflow from other resources, such as API routes, scheduled jobs, or subscribers.
+
+For example, to execute the workflow in an API route:
+
+```ts
+import type {
+  MedusaRequest,
+  MedusaResponse,
+} from "@medusajs/medusa"
+import myWorkflow from "../../../workflows/hello-world"
+
+export async function GET(
+  req: MedusaRequest,
+  res: MedusaResponse
+) {
+  const { result } = await myWorkflow(req.scope)
+    .run({
+      input: {
+        name: req.query.name as string,
+      },
+    })
+
+  res.send(result)
+}
+```