docs: general fixes and improvements (#7918)

* docs improvements and changes

* updated module definition

* modules + dml changes

* fix build

* fix vale error

* fix lint errors

* fixes to stripe docs

* fix condition

* fix condition

* fix module defintion

* fix checkout

* disable UI action

* change oas preview action

* flatten provider module options

* fix lint errors

* add module link docs

* pr comments fixes

* fix vale error

* change node engine version

* links -> linkable

* add note about database name

* small fixes

* link fixes

* fix response code in api reference

* added migrations step
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-07-04 17:26:03 +03:00
1 parent 32982e708a
commit 964927b597
149 files changed
+1676 -3008

No files matched your search

@@ -14,7 +14,7 @@ Use modules to customize or develop commerce and architectural features in your
---
## How to Create a Module
## How to Create a Module?
<Note title="Steps Summary">
@@ -28,7 +28,7 @@ Modules are created in a sub-directory of `src/modules`.
For example, create the directory `src/modules/hello`.
### 1. Create a Service
### 1. Create Main Service
A module must define a service. A service is a TypeScript or JavaScript class holding methods related to a business logic or commerce functionality.
@@ -46,21 +46,27 @@ export default class HelloModuleService {
A module must have an `index.ts` file in its root directory that exports its definition. The definition specifies the main service of the module.
For example, create the file `src/modules/hello.index.ts` with the following content:
For example, create the file `src/modules/hello/index.ts` with the following content:
```ts title="src/modules/hello.index.ts" highlights={[["4", "", "The main service of the module."]]}
```ts title="src/modules/hello.index.ts" highlights={[["5", "", "The main service of the module."]]}
import HelloModuleService from "./service"
import { Module } from "@medusajs/utils"
export default {
export default Module("hello", {
service: HelloModuleService,
}
})
```
You use the `Module` function imported from `@medusajs/utils` to create the module definition. It requires two parameters:
1. The module's name.
2. An object with a required property `service` indicating the module's main service.
### 3. Add Module to Configurations
The last step is to add the module in Medusa’s configurations.
In `medusa-config.js`, add the module to the `modules` object:
In `medusa-config.js`, add a `modules` property and pass to it your custom module:
```js title="medusa-config.js" highlights={[["4", "helloModuleService", "The key of the main service to be registered in the Medusa container."]]}
module.exports = defineConfig({
@@ -75,23 +81,25 @@ module.exports = defineConfig({
Its key (`helloModuleService`) is the name of the module’s main service. It will be registered in the Medusa container with that name.
Its value is an object having the `resolve` property, whose value is either a path to module's directory (relative to `src`, so it shouldn't include `src` in the path) or an `npm` package’s name.
Its value is an object having the `resolve` property, whose value is either a path to module's directory relative to `src`(it shouldn't include `src` in the path), or an `npm` package’s name.
### 4. Test the Module
---
## Test the Module
Since the module's main service is registered in the Medusa container, you can resolve it in other resources to use its functionalities.
One way to test the module is to create the API route `src/api/store/custom/route.ts` with the following content:
For example, create the API route `src/api/store/custom/route.ts` with the following content:
```ts title="src/api/store/custom/route.ts"
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import HelloService from "../../../modules/hello/service"
import HelloModuleService from "../../../modules/hello/service"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const helloModuleService: HelloService = req.scope.resolve(
const helloModuleService: HelloModuleService = req.scope.resolve(
"helloModuleService"
)
@@ -128,7 +136,6 @@ You’ll receive the following response:
<Note title="Use modules when" type="success">
- You're implementing a custom commerce feature. For example, you're implementing digital products.
- You want to extend data models in other commerce modules, such as adding a field or a relation to the `Product` model.
- You want to re-use your custom commerce functionalities across Medusa applications or use them in other environments, such as Edge functions and Next.js apps.
</Note>