docs: change event payload handling in subscribers (#8078)
This commit is contained in:
1 parent
143847ace4
commit
02add3f0f5
8 files changed
+33
-11
No files matched your search
+25
-3
@@ -1,3 +1,5 @@
|
|||||||
|
import { TypeList } from "docs-ui"
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
title: `${pageNumber} Event Data Payload`,
|
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.
|
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:
|
For example:
|
||||||
|
|
||||||
export const highlights = [
|
export const highlights = [
|
||||||
["7", "", "The event's data payload."],
|
["7", "", "The event's data payload."],
|
||||||
["8", "{ id: string }", "The type of expected data payloads."],
|
["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"
|
```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({
|
export default async function productCreateHandler({
|
||||||
data,
|
data,
|
||||||
}: SubscriberArgs<{ id: string }>) {
|
}: 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`)
|
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` event’s data payload to the console.
|
This logs the product ID received in the `product.created` event’s data payload to the console.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ export default async function productCreateHandler({
|
|||||||
const productModuleService: IProductModuleService =
|
const productModuleService: IProductModuleService =
|
||||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||||
|
|
||||||
const productId = "data" in data ? data.data.id : data.id
|
const productId = data.data.id
|
||||||
|
|
||||||
const product = await productModuleService.retrieveProduct(
|
const product = await productModuleService.retrieveProduct(
|
||||||
productId
|
productId
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ To execute the workflow, invoke it passing the Medusa container as a parameter.
|
|||||||
data,
|
data,
|
||||||
container,
|
container,
|
||||||
}: SubscriberArgs<{ id: string }>) {
|
}: SubscriberArgs<{ id: string }>) {
|
||||||
const userId = "data" in data ? data.data.id : data.id
|
const userId = data.data.id
|
||||||
const userModuleService: IUserModuleService = container.resolve(
|
const userModuleService: IUserModuleService = container.resolve(
|
||||||
ModuleRegistrationName.USER
|
ModuleRegistrationName.USER
|
||||||
)
|
)
|
||||||
|
|||||||
+1
-1
@@ -51,7 +51,7 @@ export default async function productCreateHandler({
|
|||||||
to: "shahednasser@gmail.com",
|
to: "shahednasser@gmail.com",
|
||||||
channel: "email",
|
channel: "email",
|
||||||
template: "product-created",
|
template: "product-created",
|
||||||
data: "data" in data ? data.data : data,
|
data: data.data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ export default async function productCreateHandler({
|
|||||||
to: "test@gmail.com",
|
to: "test@gmail.com",
|
||||||
channel: "email",
|
channel: "email",
|
||||||
template: "product-created",
|
template: "product-created",
|
||||||
data: "data" in data ? data.data : data,
|
data: data.data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ export const subscriberHighlights = [
|
|||||||
Modules.NOTIFICATION
|
Modules.NOTIFICATION
|
||||||
)
|
)
|
||||||
|
|
||||||
const inventoryItemId = "data" in data ? data.data.id : data.id
|
const inventoryItemId = data.data.id
|
||||||
|
|
||||||
const inventoryVariantLinkService = remoteLink.getLinkModule(
|
const inventoryVariantLinkService = remoteLink.getLinkModule(
|
||||||
Modules.PRODUCT,
|
Modules.PRODUCT,
|
||||||
@@ -798,7 +798,7 @@ The `order.placed` event is currently not emitted.
|
|||||||
data,
|
data,
|
||||||
container,
|
container,
|
||||||
}: SubscriberArgs<{ id: string }>) {
|
}: SubscriberArgs<{ id: string }>) {
|
||||||
const orderId = "data" in data ? data.data.id : data.id
|
const orderId = data.data.id
|
||||||
|
|
||||||
const orderModuleService: IOrderModuleService = container
|
const orderModuleService: IOrderModuleService = container
|
||||||
.resolve(ModuleRegistrationName.ORDER)
|
.resolve(ModuleRegistrationName.ORDER)
|
||||||
|
|||||||
@@ -1126,7 +1126,7 @@ In the subscriber, you can send a notification, such as an email, to the custome
|
|||||||
"digitalProductModuleService"
|
"digitalProductModuleService"
|
||||||
)
|
)
|
||||||
|
|
||||||
const orderId = "data" in data ? data.data.id : data.id
|
const orderId = data.data.id
|
||||||
|
|
||||||
const order = await orderModuleService.retrieveOrder(orderId, {
|
const order = await orderModuleService.retrieveOrder(orderId, {
|
||||||
relations: ["items"],
|
relations: ["items"],
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ export const workflowHighlights = [
|
|||||||
createProductWorkflow(container)
|
createProductWorkflow(container)
|
||||||
.run({
|
.run({
|
||||||
input: {
|
input: {
|
||||||
productId: "data" in data ? data.data.id : data.id
|
productId: data.data.id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|||||||
Reference in new issue
Block a user