docs: add an example of injecting dependencies in module providers (#12663)

This commit is contained in:
Shahed Nasser
2025-06-03 11:46:49 +03:00
committed by GitHub
parent e078de1ab5
commit 7e1720279d
3 changed files with 15532 additions and 15460 deletions

View File

@@ -162,6 +162,9 @@ The method accepts an object having the following properties:
3. By default, the Event Module's service isn't injected into your module's container. To add it to the container, pass it in the module's registration object in `medusa-config.ts` in the `dependencies` property:
<CodeTabs group="module-deps">
<CodeTab label="Module Registration" value="module_registration">
export const depsHighlight = [
["8", "dependencies", "An array of module registration names to inject into the Module's container."],
]
@@ -182,9 +185,47 @@ module.exports = defineConfig({
})
```
</CodeTab>
<CodeTab label="Module Provider Registration" value="module_registration_factory">
export const providerHighlights = [
["8", "dependencies", "An array of module registration names to inject into the module and provider's container."],
]
```ts title="medusa-config.ts" highlights={depsHighlight}
import { Modules } from "@medusajs/framework/utils"
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/payment",
dependencies: [
Modules.EVENT_BUS,
],
options: {
providers: [
{
resolve: "./src/modules/my-provider",
id: "my-provider",
options: {
// ...
},
},
],
},
},
],
})
```
</CodeTab>
</CodeTabs>
The `dependencies` property accepts an array of module registration keys. The specified modules' main services are injected into the module's container.
That's how you can resolve it in your module's main service's constructor.
If a module has providers, the dependencies are also injected into the providers' containers.
### Test it Out

View File

@@ -26,7 +26,7 @@ export const generatedEditDates = {
"app/learn/fundamentals/workflows/add-workflow-hook/page.mdx": "2024-12-09T14:42:39.693Z",
"app/learn/fundamentals/events-and-subscribers/data-payload/page.mdx": "2025-05-01T15:30:08.421Z",
"app/learn/fundamentals/workflows/advanced-example/page.mdx": "2024-09-11T10:46:59.975Z",
"app/learn/fundamentals/events-and-subscribers/emit-event/page.mdx": "2025-03-18T15:09:40.243Z",
"app/learn/fundamentals/events-and-subscribers/emit-event/page.mdx": "2025-06-02T14:47:54.394Z",
"app/learn/fundamentals/workflows/conditions/page.mdx": "2025-01-27T08:45:19.027Z",
"app/learn/fundamentals/modules/module-link-directions/page.mdx": "2024-07-24T09:16:01+02:00",
"app/learn/fundamentals/admin/page.mdx": "2025-04-18T10:28:47.328Z",

File diff suppressed because it is too large Load Diff