|
1
|
import { |
|
2
|
Event, |
|
3
|
IEventBusModuleService, |
|
4
|
MedusaContainer, |
|
5
|
Subscriber, |
|
6
|
} from "@medusajs/types" |
|
7
|
import { |
|
8
|
isFileSkipped, |
|
9
|
kebabCase, |
|
10
|
Modules, |
|
11
|
registerDevServerResource, |
|
12
|
} from "@medusajs/utils" |
|
13
|
import { parse } from "path" |
|
14
|
import { configManager } from "../config" |
|
15
|
import { container } from "../container" |
|
16
|
import { ResourceLoader } from "../utils/resource-loader" |
|
17
|
import { SubscriberArgs, SubscriberConfig } from "./types" |
|
18
|
|
|
19
|
type SubscriberHandler<T> = (args: SubscriberArgs<T>) => Promise<void> |
|
20
|
|
|
21
|
type SubscriberModule<T> = { |
|
22
|
config: SubscriberConfig |
|
23
|
handler: SubscriberHandler<T> |
|
24
|
} |
|
25
|
|
|
26
|
export class SubscriberLoader extends ResourceLoader { |
|
27
|
protected resourceName = "subscriber" |
|
28
|
|
|
29
|
/** |
|
30
|
* The options of the plugin from which the subscribers are being loaded |
|
31
|
* @private |
|
32
|
*/ |
|
33
|
#pluginOptions: Record<string, unknown> |
|
34
|
|
|
35
|
/** |
|
36
|
* Map of subscribers descriptors to consume in the loader |
|
37
|
* @private |
|
38
|
*/ |
|
39
|
#subscriberDescriptors: Map<string, SubscriberModule<any>> = new Map() |
|
40
|
|
|
41
|
constructor( |
|
42
|
sourceDir: string | string[], |
|
43
|
options: Record<string, unknown> = {}, |
|
44
|
container: MedusaContainer |
|
45
|
) { |
|
46
|
super(sourceDir, container) |
|
47
|
this.#pluginOptions = options |
|
48
|
} |
|
49
|
|
|
50
|
protected async onFileLoaded( |
|
51
|
path: string, |
|
52
|
fileExports: Record<string, unknown> |
|
53
|
) { |
|
54
|
if (isFileSkipped(fileExports)) { |
|
55
|
return |
|
56
|
} |
|
57
|
|
|
58
|
const isValid = this.validateSubscriber(fileExports, path) |
|
59
|
|
|
60
|
this.logger.debug(`Registering subscribers from ${path}.`) |
|
61
|
|
|
62
|
if (!isValid) { |
|
63
|
return |
|
64
|
} |
|
65
|
|
|
66
|
this.#subscriberDescriptors.set(path, { |
|
67
|
config: fileExports.config, |
|
68
|
handler: fileExports.default, |
|
69
|
}) |
|
70
|
} |
|
71
|
|
|
72
|
private validateSubscriber( |
|
73
|
subscriber: any, |
|
74
|
path: string |
|
75
|
): subscriber is { |
|
76
|
default: SubscriberHandler<unknown> |
|
77
|
config: SubscriberConfig |
|
78
|
} { |
|
79
|
const handler = subscriber.default |
|
80
|
|
|
81
|
if (!handler || typeof handler !== "function") { |
|
82
|
/** |
|
83
|
* If the handler is not a function, we can't use it |
|
84
|
*/ |
|
85
|
this.logger.warn(`The subscriber in ${path} is not a function. skipped.`) |
|
86
|
return false |
|
87
|
} |
|
88
|
|
|
89
|
const config = subscriber.config |
|
90
|
|
|
91
|
if (!config) { |
|
92
|
/** |
|
93
|
* If the subscriber is missing a config, we can't use it |
|
94
|
*/ |
|
95
|
this.logger.warn( |
|
96
|
`The subscriber in ${path} is missing a config. skipped.` |
|
97
|
) |
|
98
|
return false |
|
99
|
} |
|
100
|
|
|
101
|
if (!config.event) { |
|
102
|
/** |
|
103
|
* If the subscriber is missing an event, we can't use it. |
|
104
|
* In production we throw an error, else we log a warning |
|
105
|
*/ |
|
106
|
if (configManager.isProduction) { |
|
107
|
throw new Error( |
|
108
|
`The subscriber in ${path} is missing an event in the config.` |
|
109
|
) |
|
110
|
} else { |
|
111
|
this.logger.warn( |
|
112
|
`The subscriber in ${path} is missing an event in the config. skipped.` |
|
113
|
) |
|
114
|
} |
|
115
|
|
|
116
|
return false |
|
117
|
} |
|
118
|
|
|
119
|
const events = Array.isArray(config.event) ? config.event : [config.event] |
|
120
|
|
|
121
|
if (events.some((e: unknown) => !(typeof e === "string"))) { |
|
122
|
/** |
|
123
|
* If the subscribers event is not a string or an array of strings, we can't use it |
|
124
|
*/ |
|
125
|
this.logger.warn( |
|
126
|
`The subscriber in ${path} has an invalid event config. The event must be a string or an array of strings. skipped.` |
|
127
|
) |
|
128
|
return false |
|
129
|
} |
|
130
|
|
|
131
|
return true |
|
132
|
} |
|
133
|
|
|
134
|
private inferIdentifier<T>( |
|
135
|
fileName: string, |
|
136
|
{ context }: SubscriberConfig, |
|
137
|
handler: SubscriberHandler<T> |
|
138
|
) { |
|
139
|
/** |
|
140
|
* If subscriberId is provided, use that |
|
141
|
*/ |
|
142
|
if (context?.subscriberId) { |
|
143
|
return context.subscriberId |
|
144
|
} |
|
145
|
|
|
146
|
const handlerName = handler.name |
|
147
|
|
|
148
|
/** |
|
149
|
* If the handler is not anonymous, use the name |
|
150
|
*/ |
|
151
|
if (handlerName && !handlerName.startsWith("_default")) { |
|
152
|
return kebabCase(handlerName) |
|
153
|
} |
|
154
|
|
|
155
|
/** |
|
156
|
* If the handler is anonymous, use the file name |
|
157
|
*/ |
|
158
|
const idFromFile = parse(fileName).name |
|
159
|
return kebabCase(idFromFile) |
|
160
|
} |
|
161
|
|
|
162
|
createSubscriber<T = unknown>({ |
|
163
|
fileName, |
|
164
|
config, |
|
165
|
handler, |
|
166
|
}: { |
|
167
|
fileName: string |
|
168
|
config: SubscriberConfig |
|
169
|
handler: SubscriberHandler<T> |
|
170
|
}) { |
|
171
|
const eventBusService: IEventBusModuleService = container.resolve( |
|
172
|
Modules.EVENT_BUS |
|
173
|
) |
|
174
|
|
|
175
|
const { event } = config |
|
176
|
|
|
177
|
const events = Array.isArray(event) ? event : [event] |
|
178
|
|
|
179
|
const subscriberId = this.inferIdentifier(fileName, config, handler) |
|
180
|
|
|
181
|
for (const e of events) { |
|
182
|
const subscriber = async (data: T) => { |
|
183
|
return await handler({ |
|
184
|
event: { name: e, ...data } as unknown as Event<T>, |
|
185
|
container, |
|
186
|
pluginOptions: this.#pluginOptions, |
|
187
|
}) |
|
188
|
} |
|
189
|
|
|
190
|
eventBusService.subscribe(e, subscriber as Subscriber, { |
|
191
|
...config.context, |
|
192
|
subscriberId, |
|
193
|
}) |
|
194
|
|
|
195
|
registerDevServerResource({ |
|
196
|
type: "subscriber", |
|
197
|
id: subscriberId, |
|
198
|
sourcePath: fileName, |
|
199
|
subscriberId, |
|
200
|
events, |
|
201
|
}) |
|
202
|
} |
|
203
|
} |
|
204
|
|
|
205
|
async load() { |
|
206
|
await super.discoverResources() |
|
207
|
|
|
208
|
for (const [ |
|
209
|
fileName, |
|
210
|
{ config, handler }, |
|
211
|
] of this.#subscriberDescriptors.entries()) { |
|
212
|
this.createSubscriber({ |
|
213
|
fileName, |
|
214
|
config, |
|
215
|
handler, |
|
216
|
}) |
|
217
|
} |
|
218
|
|
|
219
|
this.logger.debug(`Subscribers registered.`) |
|
220
|
|
|
221
|
/** |
|
222
|
* Return the file paths of the registered subscribers, to prevent the |
|
223
|
* backwards compatible loader from trying to register them. |
|
224
|
*/ |
|
225
|
return [...this.#subscriberDescriptors.keys()] |
|
226
|
} |
|
227
|
} |