Files
medusa-store/www/apps/book/app/advanced-development/modules/options/page.mdx
Shahed Nasser 0462cc5acf docs: updates to use DML and other changes (#7834)
- Change existing data model guides and add new ones for DML
- Change module's docs around service factory + remove guides that are now necessary
- Hide/remove all mentions of module relationships, or label them as coming soon.
- Change all data model creation snippets to use DML
- use `property` instead of `field` when referring to a data model's properties.
- Fix all snippets in commerce module guides to use new method suffix (no more main model methods)
- Rework recipes, removing/hiding a lot of sections as a lot of recipes are incomplete with the current state of DML.


### Other changes

- Highlight fixes in some guides
- Remove feature flags guide
- Fix code block styles when there are no line numbers.

### Upcoming changes in other PRs

- Re-generate commerce module references (for the updates in the method names)
- Ensure that the data model references are generated correctly for models using DML.
- (probably at a very later point) revisit recipes
2024-06-26 07:55:59 +00:00

103 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `${pageNumber} Module Options`,
}
# {metadata.title}
In this chapter, youll learn about passing options to your module from the Medusa applications configurations and using them in the modules resources.
## What are Module Options?
A module can receive options to customize or configure its functionality.
For example, if youre creating a module that integrates a third-party service, youll want to receive the integration credentials in the options rather than adding them directly in your code.
---
## How to Pass Options to a Module?
To pass options to a module, add an `options` property to the modules configuration in `medusa-config.js`.
For example:
```js title="medusa-config.js"
module.exports = defineConfig({
// ...
modules: {
helloModuleService: {
resolve: "./modules/hello",
options: {
capitalize: true,
},
},
},
})
```
The `options` propertys value is an object. You can pass any properties you want.
---
## Access Module Options in Main Service
The modules main service receives the module options as a second parameter.
For example:
```ts title="src/modules/hello/service.ts" highlights={[["12"], ["14", "options?: ModuleOptions"], ["17"], ["18"], ["19"]]}
import { MedusaService } from "@medusajs/utils"
import MyCustom from "./models/my-custom"
// recommended to define type in another file
type ModuleOptions = {
capitalize?: boolean
}
export default class HelloModuleService extends MedusaService({
MyCustom,
}){
protected options_: ModuleOptions
constructor({}, options?: ModuleOptions) {
super(...arguments)
this.options_ = options || {
capitalize: false,
}
}
// ...
}
```
---
## Access Module Options in Loader
The object that a modules loaders receive as a parameter has an `options` property holding the module's options.
For example:
```ts title="src/modules/hello/loaders/hello-world.ts" highlights={[["11"], ["16"]]}
import {
LoaderOptions,
} from "@medusajs/modules-sdk"
// recommended to define type in another file
type ModuleOptions = {
capitalize?: boolean
}
export default function helloWorldLoader({
options,
}: LoaderOptions<ModuleOptions>) {
console.log(
"[HELLO MODULE] Just started the Medusa application!",
options
)
}
```