|
1
|
import { Context, EventBusTypes } from "@medusajs/types" |
|
2
|
|
|
3
|
/** |
|
4
|
* Helper function to compose and normalize a Message to be emitted by EventBus Module |
|
5
|
* @param eventName Name of the event to be emitted |
|
6
|
* @param data The content of the message |
|
7
|
* @param metadata Metadata of the message |
|
8
|
* @param context Context from the caller service |
|
9
|
* @param options Options to be passed to the event bus |
|
10
|
*/ |
|
11
|
export function composeMessage( |
|
12
|
eventName: string, |
|
13
|
{ |
|
14
|
data, |
|
15
|
source, |
|
16
|
object, |
|
17
|
action, |
|
18
|
context = {}, |
|
19
|
options, |
|
20
|
}: { |
|
21
|
data: any |
|
22
|
source: string |
|
23
|
object: string |
|
24
|
action?: string |
|
25
|
context?: Context |
|
26
|
options?: Record<string, any> |
|
27
|
} |
|
28
|
): EventBusTypes.Message { |
|
29
|
const act = action || eventName.split(".").pop() |
|
30
|
if ( |
|
31
|
!action /* && !Object.values(CommonEvents).includes(act as CommonEvents)*/ |
|
32
|
) { |
|
33
|
throw new Error("Action is required if eventName is not a CommonEvent") |
|
34
|
} |
|
35
|
|
|
36
|
const metadata: EventBusTypes.Event["metadata"] = { |
|
37
|
source, |
|
38
|
object, |
|
39
|
action: act!, |
|
40
|
} |
|
41
|
|
|
42
|
if (context.eventGroupId) { |
|
43
|
metadata.eventGroupId = context.eventGroupId |
|
44
|
} |
|
45
|
|
|
46
|
return { |
|
47
|
name: eventName, |
|
48
|
metadata, |
|
49
|
data, |
|
50
|
options, |
|
51
|
} |
|
52
|
} |