feat: restructure events payload (#8143)
* refactor: restructure events payload Breaking change: This PR changes the event payload accepted by the event listeners * refactor: fix failing tests and implement feedback * add integration tests * fix timeout --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
5813216c88
commit
f579f0b3be
@@ -28,7 +28,7 @@ export const emitEventStep = createStep(
|
||||
const data_ =
|
||||
typeof input.data === "function" ? await input.data(context) : input.data
|
||||
|
||||
const metadata: EventBusTypes.MessageBody["metadata"] = {
|
||||
const metadata: EventBusTypes.Event["metadata"] = {
|
||||
...input.metadata,
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ export const emitEventStep = createStep(
|
||||
metadata.eventGroupId = context.eventGroupId
|
||||
}
|
||||
|
||||
const message = {
|
||||
eventName: input.eventName,
|
||||
const message: EventBusTypes.Message = {
|
||||
name: input.eventName,
|
||||
data: data_,
|
||||
options: input.options,
|
||||
metadata,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Context } from "../shared-context"
|
||||
|
||||
export type Subscriber<TData = unknown> = (
|
||||
data: MessageBody<TData>
|
||||
) => Promise<void>
|
||||
export type Subscriber<TData = unknown> = (data: Event<TData>) => Promise<void>
|
||||
|
||||
export type SubscriberContext = {
|
||||
subscriberId: string
|
||||
@@ -17,13 +15,13 @@ export type EventMetadata = Record<string, unknown> & {
|
||||
eventGroupId?: string
|
||||
}
|
||||
|
||||
export type MessageBody<TData = unknown> = {
|
||||
eventName: string
|
||||
export type Event<TData = unknown> = {
|
||||
name: string
|
||||
metadata?: EventMetadata
|
||||
data: TData
|
||||
}
|
||||
|
||||
export type Message<TData = unknown> = MessageBody<TData> & {
|
||||
export type Message<TData = unknown> = Event<TData> & {
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ export function composeMessage(
|
||||
throw new Error("Action is required if eventName is not a CommonEvent")
|
||||
}
|
||||
|
||||
const metadata: EventBusTypes.MessageBody["metadata"] = {
|
||||
const metadata: EventBusTypes.Event["metadata"] = {
|
||||
source,
|
||||
object,
|
||||
action: act!,
|
||||
@@ -44,7 +44,7 @@ export function composeMessage(
|
||||
}
|
||||
|
||||
return {
|
||||
eventName,
|
||||
name: eventName,
|
||||
metadata,
|
||||
data,
|
||||
options,
|
||||
|
||||
@@ -261,7 +261,7 @@ export function MedusaService<
|
||||
|
||||
await this.eventBusModuleService_?.emit(
|
||||
primaryKeyValues_.map((primaryKeyValue) => ({
|
||||
eventName: `${kebabCase(modelName)}.deleted`,
|
||||
name: `${kebabCase(modelName)}.deleted`,
|
||||
data: isString(primaryKeyValue)
|
||||
? { id: primaryKeyValue }
|
||||
: primaryKeyValue,
|
||||
@@ -294,7 +294,7 @@ export function MedusaService<
|
||||
|
||||
await this.eventBusModuleService_?.emit(
|
||||
softDeletedModels.map(({ id }) => ({
|
||||
eventName: `${kebabCase(modelName)}.deleted`,
|
||||
name: `${kebabCase(modelName)}.deleted`,
|
||||
metadata: { source: "", action: "", object: "" },
|
||||
data: { id },
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user