From 02add3f0f58823dabf9c85242ef76263f7d52056 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 11 Jul 2024 13:34:24 +0300 Subject: [PATCH] docs: change event payload handling in subscribers (#8078) --- .../data-payload/page.mdx | 28 +++++++++++++++++-- .../basics/events-and-subscribers/page.mdx | 2 +- www/apps/book/app/basics/workflows/page.mdx | 2 +- .../notification/send-notification/page.mdx | 2 +- .../notification/sendgrid/page.mdx | 2 +- .../app/recipes/commerce-automation/page.mdx | 4 +-- .../app/recipes/digital-products/page.mdx | 2 +- .../integrate-ecommerce-stack/page.mdx | 2 +- 8 files changed, 33 insertions(+), 11 deletions(-) diff --git a/www/apps/book/app/advanced-development/events-and-subscribers/data-payload/page.mdx b/www/apps/book/app/advanced-development/events-and-subscribers/data-payload/page.mdx index d54acd1fc5..34caeb54e5 100644 --- a/www/apps/book/app/advanced-development/events-and-subscribers/data-payload/page.mdx +++ b/www/apps/book/app/advanced-development/events-and-subscribers/data-payload/page.mdx @@ -1,3 +1,5 @@ +import { TypeList } from "docs-ui" + export const metadata = { title: `${pageNumber} Event Data Payload`, } @@ -10,14 +12,13 @@ In this chapter, you'll learn how subscribers receive an event's data payload. When events are emitted, they’re emitted with a data payload. -The object that the subscriber function receives as a parameter has a `data` property, which is the event's data payload. +The object that the subscriber function receives as a parameter has a `data` property, which is an object holding the event payload with additional context. For example: export const highlights = [ ["7", "", "The event's data payload."], ["8", "{ id: string }", "The type of expected data payloads."], - ["9", '"data" in data ? data.data.id : data.id', "The payload data is either in `data.data` or directly in `data`."] ] ```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-5" expandButtonLabel="Show Imports" @@ -29,7 +30,7 @@ import type { export default async function productCreateHandler({ data, }: SubscriberArgs<{ id: string }>) { - const productId = "data" in data ? data.data.id : data.id + const productId = data.data.id console.log(`The product ${productId} was created`) } @@ -38,6 +39,27 @@ export const config: SubscriberConfig = { } ``` +The `data` object has the following properties: + + + This logs the product ID received in the `product.created` event’s data payload to the console. --- diff --git a/www/apps/book/app/basics/events-and-subscribers/page.mdx b/www/apps/book/app/basics/events-and-subscribers/page.mdx index cdbb0ca209..3a641af9ff 100644 --- a/www/apps/book/app/basics/events-and-subscribers/page.mdx +++ b/www/apps/book/app/basics/events-and-subscribers/page.mdx @@ -98,7 +98,7 @@ export default async function productCreateHandler({ const productModuleService: IProductModuleService = container.resolve(ModuleRegistrationName.PRODUCT) - const productId = "data" in data ? data.data.id : data.id + const productId = data.data.id const product = await productModuleService.retrieveProduct( productId diff --git a/www/apps/book/app/basics/workflows/page.mdx b/www/apps/book/app/basics/workflows/page.mdx index 2909d276af..3b1a1610b5 100644 --- a/www/apps/book/app/basics/workflows/page.mdx +++ b/www/apps/book/app/basics/workflows/page.mdx @@ -133,7 +133,7 @@ To execute the workflow, invoke it passing the Medusa container as a parameter. data, container, }: SubscriberArgs<{ id: string }>) { - const userId = "data" in data ? data.data.id : data.id + const userId = data.data.id const userModuleService: IUserModuleService = container.resolve( ModuleRegistrationName.USER ) diff --git a/www/apps/resources/app/architectural-modules/notification/send-notification/page.mdx b/www/apps/resources/app/architectural-modules/notification/send-notification/page.mdx index 22979153f2..52b6c6cd74 100644 --- a/www/apps/resources/app/architectural-modules/notification/send-notification/page.mdx +++ b/www/apps/resources/app/architectural-modules/notification/send-notification/page.mdx @@ -51,7 +51,7 @@ export default async function productCreateHandler({ to: "shahednasser@gmail.com", channel: "email", template: "product-created", - data: "data" in data ? data.data : data, + data: data.data, }) } diff --git a/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx b/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx index 4f48719b08..c864b1d1bf 100644 --- a/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx +++ b/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx @@ -145,7 +145,7 @@ export default async function productCreateHandler({ to: "test@gmail.com", channel: "email", template: "product-created", - data: "data" in data ? data.data : data, + data: data.data, }) } diff --git a/www/apps/resources/app/recipes/commerce-automation/page.mdx b/www/apps/resources/app/recipes/commerce-automation/page.mdx index e97761245f..551f48d098 100644 --- a/www/apps/resources/app/recipes/commerce-automation/page.mdx +++ b/www/apps/resources/app/recipes/commerce-automation/page.mdx @@ -291,7 +291,7 @@ export const subscriberHighlights = [ Modules.NOTIFICATION ) - const inventoryItemId = "data" in data ? data.data.id : data.id + const inventoryItemId = data.data.id const inventoryVariantLinkService = remoteLink.getLinkModule( Modules.PRODUCT, @@ -798,7 +798,7 @@ The `order.placed` event is currently not emitted. data, container, }: SubscriberArgs<{ id: string }>) { - const orderId = "data" in data ? data.data.id : data.id + const orderId = data.data.id const orderModuleService: IOrderModuleService = container .resolve(ModuleRegistrationName.ORDER) diff --git a/www/apps/resources/app/recipes/digital-products/page.mdx b/www/apps/resources/app/recipes/digital-products/page.mdx index f0055d932a..926c000e1a 100644 --- a/www/apps/resources/app/recipes/digital-products/page.mdx +++ b/www/apps/resources/app/recipes/digital-products/page.mdx @@ -1126,7 +1126,7 @@ In the subscriber, you can send a notification, such as an email, to the custome "digitalProductModuleService" ) - const orderId = "data" in data ? data.data.id : data.id + const orderId = data.data.id const order = await orderModuleService.retrieveOrder(orderId, { relations: ["items"], diff --git a/www/apps/resources/app/recipes/integrate-ecommerce-stack/page.mdx b/www/apps/resources/app/recipes/integrate-ecommerce-stack/page.mdx index 623a62eb53..ee66a31d74 100644 --- a/www/apps/resources/app/recipes/integrate-ecommerce-stack/page.mdx +++ b/www/apps/resources/app/recipes/integrate-ecommerce-stack/page.mdx @@ -269,7 +269,7 @@ export const workflowHighlights = [ createProductWorkflow(container) .run({ input: { - productId: "data" in data ? data.data.id : data.id + productId: data.data.id } }) .then(() => {