From 1466ca73e0174a326e2ab672a526f220de715c8d Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Tue, 10 Sep 2024 21:00:23 +0300 Subject: [PATCH] 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. --- .../modules/service-constraints/page.mdx | 34 +++++++++++++++++++ .../app/basics/modules-and-services/page.mdx | 4 +-- www/apps/book/generated/edit-dates.mjs | 3 +- www/apps/book/sidebar.mjs | 5 +++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 www/apps/book/app/advanced-development/modules/service-constraints/page.mdx diff --git a/www/apps/book/app/advanced-development/modules/service-constraints/page.mdx b/www/apps/book/app/advanced-development/modules/service-constraints/page.mdx new file mode 100644 index 0000000000..a5414bd243 --- /dev/null +++ b/www/apps/book/app/advanced-development/modules/service-constraints/page.mdx @@ -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 { + return "Hello, World!" + } +} + +export default HelloModuleService +``` diff --git a/www/apps/book/app/basics/modules-and-services/page.mdx b/www/apps/book/app/basics/modules-and-services/page.mdx index 7e9a49db47..69627acb40 100644 --- a/www/apps/book/app/basics/modules-and-services/page.mdx +++ b/www/apps/book/app/basics/modules-and-services/page.mdx @@ -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" export default class HelloModuleService { - getMessage() { + async getMessage() { return "Hello, world!" } } @@ -121,7 +121,7 @@ export async function GET( ) res.json({ - message: helloModuleService.getMessage(), + message: await helloModuleService.getMessage(), }) } ``` diff --git a/www/apps/book/generated/edit-dates.mjs b/www/apps/book/generated/edit-dates.mjs index c987045e93..1c2bf0ae77 100644 --- a/www/apps/book/generated/edit-dates.mjs +++ b/www/apps/book/generated/edit-dates.mjs @@ -3,7 +3,7 @@ export const generatedEditDates = { "app/basics/workflows/page.mdx": "2024-09-03T08:07:16.276Z", "app/deployment/page.mdx": "2024-08-05T07:24:05+00:00", "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/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", @@ -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/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/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/responses/page.mdx": "2024-09-10T11:39:51.167Z", "app/advanced-development/api-routes/validation/page.mdx": "2024-09-10T11:39:51.167Z", diff --git a/www/apps/book/sidebar.mjs b/www/apps/book/sidebar.mjs index a5bb1f7e30..06726277f5 100644 --- a/www/apps/book/sidebar.mjs +++ b/www/apps/book/sidebar.mjs @@ -150,6 +150,11 @@ export const sidebar = numberSidebarItems( path: "/advanced-development/modules/service-factory", title: "Service Factory", }, + { + type: "link", + path: "/advanced-development/modules/service-constraints", + title: "Service Constraints", + }, { type: "link", path: "/advanced-development/modules/isolation",