chore: move ModuleRegistrationName to utils (#7911)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-03 06:30:56 -03:00
committed by GitHub
parent 46f15b4909
commit a7844efd09
339 changed files with 7203 additions and 7620 deletions
@@ -23,19 +23,14 @@ Similarly to your custom module, a commerce module's main service is registered
For example, you saw this code snippet in the [Medusa container chapter](../medusa-container/page.mdx):
```ts highlights={[["13"]]}
import type {
MedusaRequest,
MedusaResponse,
} from "@medusajs/medusa"
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ModuleRegistrationName } from "@medusajs/utils"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
const productModuleService: IProductModuleService =
req.scope.resolve(ModuleRegistrationName.PRODUCT)
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const productModuleService: IProductModuleService = req.scope.resolve(
ModuleRegistrationName.PRODUCT
)
const [, count] = await productModuleService.listAndCount()
@@ -51,4 +46,4 @@ When you resolve the `ModuleRegistrationName.PRODUCT` (or `productModuleService`
To resolve the main service of any commerce module, use the registration name defined in the `ModuleRegistrationName` enum imported from `@medusajs/modules-sdk`.
</Note>
</Note>
@@ -43,7 +43,7 @@ The above subscriber listens to the `product.created` event. Whenever the event
{/* TODO add when we have the admin dashboard to use with V2 for easy testing. */}
{/* ### Test Subscriber
{/\* ### Test Subscriber
To test the subscriber, start the Medusa application:
@@ -58,7 +58,7 @@ info: Processing product.created which has 1 subscribers
A product is created
```
The first message indicates that the `product.created` event was emitted, and the second one is the message logged from the subscriber. */}
The first message indicates that the `product.created` event was emitted, and the second one is the message logged from the subscriber. \*/}
---
@@ -83,23 +83,25 @@ For example:
export const highlights = [
["10", "container", "Recieve the Medusa Container in the object parameter."],
["13", "resolve", "Resolve the Product Module's main service."],
["13", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
[
"13",
"ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.",
],
]
```ts title="src/subscribers/product-created.ts" highlights={highlights}
import {
SubscriberArgs,
type SubscriberConfig,
} from "@medusajs/medusa"
import { SubscriberArgs, type SubscriberConfig } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ModuleRegistrationName } from "@medusajs/utils"
export default async function productCreateHandler({
data,
container,
}: SubscriberArgs<Record<string, string>>) {
const productModuleService: IProductModuleService =
container.resolve(ModuleRegistrationName.PRODUCT)
const productModuleService: IProductModuleService = container.resolve(
ModuleRegistrationName.PRODUCT
)
const product = await productModuleService.retrieve(data.id)
@@ -16,23 +16,22 @@ For example, in a custom API route you can resolve any service registered in the
export const highlights = [
["13", "resolve", "Resolve the Product Module's main service."],
["13", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
[
"13",
"ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.",
],
]
```ts highlights={highlights}
import type {
MedusaRequest,
MedusaResponse,
} from "@medusajs/medusa"
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ModuleRegistrationName } from "@medusajs/utils"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
const productModuleService: IProductModuleService =
req.scope.resolve(ModuleRegistrationName.PRODUCT)
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const productModuleService: IProductModuleService = req.scope.resolve(
ModuleRegistrationName.PRODUCT
)
const [, count] = await productModuleService.listAndCount()
+24 -28
View File
@@ -25,10 +25,7 @@ A workflow is made of a series of steps. A step is created using the `createStep
Create the file `src/workflows/hello-world.ts` with the following content:
```ts title="src/workflows/hello-world.ts"
import {
createStep,
StepResponse,
} from "@medusajs/workflows-sdk"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
const step1 = createStep("step-1", async () => {
return new StepResponse(`Hello from step one!`)
@@ -44,12 +41,9 @@ type WorkflowInput = {
name: string
}
const step2 = createStep(
"step-2",
async ({ name }: WorkflowInput) => {
return new StepResponse(`Hello ${name} from step two!`)
}
)
const step2 = createStep("step-2", async ({ name }: WorkflowInput) => {
return new StepResponse(`Hello ${name} from step two!`)
})
```
### 2. Create a Workflow
@@ -68,21 +62,20 @@ type WorkflowOutput = {
message: string
}
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
const str1 = step1()
// to pass input
const str2 = step2(input)
const myWorkflow = createWorkflow<WorkflowInput, WorkflowOutput>(
"hello-world",
function (input) {
const str1 = step1()
// to pass input
const str2 = step2(input)
return {
message: str1,
return {
message: str1,
}
}
})
)
export default myWorkflow
```
This creates a `hello-world` workflow. When you create a workflow, its constructed but not executed yet.
@@ -226,7 +219,11 @@ For example:
export const highlights = [
["15", "resolve", "Resolve the Product Module's main service."],
["15", "ModuleRegistrationName.PRODUCT", "The resource registration name imported from `@medusajs/modules-sdk`."]
[
"15",
"ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.",
],
]
```ts title="src/workflows/product-count.ts" highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports"
@@ -236,18 +233,17 @@ import {
createWorkflow,
} from "@medusajs/workflows-sdk"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ModuleRegistrationName } from "@medusajs/utils"
type WorkflowOutput = {
count: number
}
const step1 = createStep("step-1", async (_, context) => {
const productModuleService: IProductModuleService =
context.container.resolve(ModuleRegistrationName.PRODUCT)
return new StepResponse(
(await productModuleService.listAndCount())[1]
const productModuleService: IProductModuleService = context.container.resolve(
ModuleRegistrationName.PRODUCT
)
return new StepResponse((await productModuleService.listAndCount())[1])
})
const myWorkflow = createWorkflow<unknown, WorkflowOutput>(
@@ -264,4 +260,4 @@ const myWorkflow = createWorkflow<unknown, WorkflowOutput>(
export default myWorkflow
```
In the step, you resolve the Product Module's main service and use it to retrieve the product count.
In the step, you resolve the Product Module's main service and use it to retrieve the product count.