Files
medusa-store/www/apps/book/app/advanced-development/modules/options/page.mdx
Shahed Nasser 4fe28f5a95 chore: reorganize docs apps (#7228)
* reorganize docs apps

* add README

* fix directory

* add condition for old docs
2024-05-03 17:36:38 +03:00

148 lines
3.5 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.
A module can receive options when theyre added to the Medusa applications configuration.
---
## 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"
const modules = {
helloModuleService: {
resolve: "./dist/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 modules declaration as a second parameter. The module declaration is an object having an `options` property. Use it to access the options passed from the Modules configurations.
For example:
<CodeTabs groupId="service-type">
<CodeTab label="Using Service Factory" value="service-factory">
```ts highlights={[["24"], ["28"], ["29"], ["30"]]}
// other imports...
import { InternalModuleDeclaration } from "@medusajs/types"
// ...
// recommended to define type in another file
type HelloModuleOptions = {
capitalize?: boolean
}
class HelloModuleService extends ModulesSdkUtils
.abstractModuleServiceFactory<
// ...
>(
// ...
) {
// ...
protected options_: HelloModuleOptions
constructor(
{
// ...
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
//...
this.options_ = moduleDeclaration.options || {
capitalize: false,
}
}
}
```
</CodeTab>
<CodeTab label="Without Service Factory" value="no-service-factory">
```ts highlights={[["9"], ["13"], ["14"], ["16"], ["17"], ["18"]]}
import { InternalModuleDeclaration } from "@medusajs/types"
// recommended to define type in another file
type HelloModuleOptions = {
capitalize?: boolean
}
export default class HelloModuleService {
protected options_: HelloModuleOptions
constructor(
{},
protected readonly moduleDeclaration:
InternalModuleDeclaration
) {
this.options_ = moduleDeclaration.options || {
capitalize: false,
}
}
getMessage() {
return "Hello, world!"
}
}
```
</CodeTab>
</CodeTabs>
---
## Access Module Options in Loader
The object that a modules loaders receive as a parameter has an `options` property. Use it to access the options passed from the Modules configurations.
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 HelloModuleOptions = {
capitalize?: boolean
}
export default function helloWorldLoader({
options,
}: LoaderOptions<HelloModuleOptions>) {
console.log(
"[HELLO MODULE] Just started the Medusa application!",
options
)
}
```