docs: change event payload handling in subscribers (#8078)

This commit is contained in:
Shahed Nasser
2024-07-11 13:34:24 +03:00
committed by GitHub
parent 143847ace4
commit 02add3f0f5
8 changed files with 33 additions and 11 deletions

View File

@@ -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, theyre 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:
<TypeList types={[
{
name: "data",
type: "`object`",
description: "The data payload of the event. Its properties depend on the event."
},
{
name: "eventName",
type: "string",
description: "The name of the triggered event."
},
{
name: "metadata",
type: "`object`",
description: "Additional data and context of the emitted event.",
optional: true
},
]} sectionTitle="Access Event's Data Payload" />
This logs the product ID received in the `product.created` events data payload to the console.
---

View File

@@ -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

View File

@@ -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
)

View File

@@ -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,
})
}

View File

@@ -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,
})
}

View File

@@ -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)

View File

@@ -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"],

View File

@@ -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(() => {