docs: added a services constraint document (#8999)
- Added a services constraint document mentioning that a service's methods must be async. - Modify the code in the services intro document to use `async` method.
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
export const metadata = {
|
||||||
|
title: `${pageNumber} Service Constraints`,
|
||||||
|
}
|
||||||
|
|
||||||
|
# {metadata.title}
|
||||||
|
|
||||||
|
This chapter lists constraints to keep in mind when creating a service.
|
||||||
|
|
||||||
|
## Use Async Methods
|
||||||
|
|
||||||
|
Medusa wraps adds wrappers around your service's methods and executes them as async methods.
|
||||||
|
|
||||||
|
So, make sure your service's methods are always async to avoid unexpected errors or behavior.
|
||||||
|
|
||||||
|
```ts highlights={[["8", "", "Method must be async."], ["13", "async", "Correct way of defining the method."]]}
|
||||||
|
import { MedusaService } from "@medusajs/utils"
|
||||||
|
import MyCustom from "./models/my-custom"
|
||||||
|
|
||||||
|
class HelloModuleService extends MedusaService({
|
||||||
|
MyCustom,
|
||||||
|
}){
|
||||||
|
// Don't
|
||||||
|
getMessage(): string {
|
||||||
|
return "Hello, World!"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do
|
||||||
|
async getMessage(): Promise<string> {
|
||||||
|
return "Hello, World!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default HelloModuleService
|
||||||
|
```
|
||||||
@@ -38,7 +38,7 @@ For example, create the file `src/modules/hello/service.ts` with the following c
|
|||||||
|
|
||||||
```ts title="src/modules/hello/service.ts"
|
```ts title="src/modules/hello/service.ts"
|
||||||
export default class HelloModuleService {
|
export default class HelloModuleService {
|
||||||
getMessage() {
|
async getMessage() {
|
||||||
return "Hello, world!"
|
return "Hello, world!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,7 +121,7 @@ export async function GET(
|
|||||||
)
|
)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
message: helloModuleService.getMessage(),
|
message: await helloModuleService.getMessage(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ export const generatedEditDates = {
|
|||||||
"app/basics/workflows/page.mdx": "2024-09-03T08:07:16.276Z",
|
"app/basics/workflows/page.mdx": "2024-09-03T08:07:16.276Z",
|
||||||
"app/deployment/page.mdx": "2024-08-05T07:24:05+00:00",
|
"app/deployment/page.mdx": "2024-08-05T07:24:05+00:00",
|
||||||
"app/page.mdx": "2024-09-03T07:09:09.034Z",
|
"app/page.mdx": "2024-09-03T07:09:09.034Z",
|
||||||
"app/basics/modules-and-services/page.mdx": "2024-09-03T07:45:28.079Z",
|
"app/basics/modules-and-services/page.mdx": "2024-09-04T15:37:27.695Z",
|
||||||
"app/basics/commerce-modules/page.mdx": "2024-09-03T07:48:48.148Z",
|
"app/basics/commerce-modules/page.mdx": "2024-09-03T07:48:48.148Z",
|
||||||
"app/advanced-development/workflows/retry-failed-steps/page.mdx": "2024-07-31T17:01:33+03:00",
|
"app/advanced-development/workflows/retry-failed-steps/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||||
"app/advanced-development/workflows/workflow-hooks/page.mdx": "2024-09-10T11:39:51.168Z",
|
"app/advanced-development/workflows/workflow-hooks/page.mdx": "2024-09-10T11:39:51.168Z",
|
||||||
@@ -72,6 +72,7 @@ export const generatedEditDates = {
|
|||||||
"app/debugging-and-testing/testing-tools/page.mdx": "2024-09-10T11:39:51.172Z",
|
"app/debugging-and-testing/testing-tools/page.mdx": "2024-09-10T11:39:51.172Z",
|
||||||
"app/debugging-and-testing/testing-tools/unit-tests/module-example/page.mdx": "2024-09-02T11:04:27.232Z",
|
"app/debugging-and-testing/testing-tools/unit-tests/module-example/page.mdx": "2024-09-02T11:04:27.232Z",
|
||||||
"app/debugging-and-testing/testing-tools/unit-tests/page.mdx": "2024-09-02T11:03:26.997Z",
|
"app/debugging-and-testing/testing-tools/unit-tests/page.mdx": "2024-09-02T11:03:26.997Z",
|
||||||
|
"app/advanced-development/modules/service-constraints/page.mdx": "2024-09-04T15:37:04.166Z",
|
||||||
"app/advanced-development/api-routes/page.mdx": "2024-09-04T09:36:33.961Z",
|
"app/advanced-development/api-routes/page.mdx": "2024-09-04T09:36:33.961Z",
|
||||||
"app/advanced-development/api-routes/responses/page.mdx": "2024-09-10T11:39:51.167Z",
|
"app/advanced-development/api-routes/responses/page.mdx": "2024-09-10T11:39:51.167Z",
|
||||||
"app/advanced-development/api-routes/validation/page.mdx": "2024-09-10T11:39:51.167Z",
|
"app/advanced-development/api-routes/validation/page.mdx": "2024-09-10T11:39:51.167Z",
|
||||||
|
|||||||
@@ -150,6 +150,11 @@ export const sidebar = numberSidebarItems(
|
|||||||
path: "/advanced-development/modules/service-factory",
|
path: "/advanced-development/modules/service-factory",
|
||||||
title: "Service Factory",
|
title: "Service Factory",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: "link",
|
||||||
|
path: "/advanced-development/modules/service-constraints",
|
||||||
|
title: "Service Constraints",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: "link",
|
type: "link",
|
||||||
path: "/advanced-development/modules/isolation",
|
path: "/advanced-development/modules/isolation",
|
||||||
|
|||||||
Reference in New Issue
Block a user