-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHandlerContainer.ts
More file actions
380 lines (348 loc) · 11.5 KB
/
HandlerContainer.ts
File metadata and controls
380 lines (348 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import type { Either } from '@lokalise/node-core'
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
import { isCommonEventDefinition } from '@message-queue-toolkit/schemas'
import type { ZodSchema } from 'zod/v4'
import type { DoNotProcessMessageError } from '../errors/DoNotProcessError.ts'
import type { RetryMessageLaterError } from '../errors/RetryMessageLaterError.ts'
import {
extractMessageTypeFromSchema,
isMessageTypeLiteralConfig,
isMessageTypePathConfig,
type MessageTypeResolverConfig,
type MessageTypeResolverContext,
resolveMessageType,
} from './MessageTypeResolver.ts'
export type PreHandlingOutputs<PrehandlerOutput = undefined, BarrierOutput = undefined> = {
preHandlerOutput: PrehandlerOutput
barrierOutput: BarrierOutput
}
export type LogFormatter<MessagePayloadSchema> = (message: MessagePayloadSchema) => unknown
export type BarrierResult<BarrierOutput> =
| BarrierResultPositive<BarrierOutput>
| BarrierResultNegative
export type BarrierResultPositive<BarrierOutput> = {
isPassing: true
output: BarrierOutput
}
export type BarrierResultNegative = {
isPassing: false
output?: never
}
export type PrehandlerResult = Either<DoNotProcessMessageError | RetryMessageLaterError, 'success'>
export type BarrierCallback<
MessagePayloadSchema extends object,
ExecutionContext,
PrehandlerOutput,
BarrierOutput,
> = (
message: MessagePayloadSchema,
context: ExecutionContext,
preHandlerOutput: PrehandlerOutput,
) => Promise<BarrierResult<BarrierOutput>>
export type Prehandler<MessagePayloadSchema extends object, ExecutionContext, PrehandlerOutput> = (
message: MessagePayloadSchema,
context: ExecutionContext,
preHandlerOutput: Partial<PrehandlerOutput>,
next: (result: PrehandlerResult) => void,
) => void
export type HandlerConfigOptions<
MessagePayloadSchema extends object,
ExecutionContext,
PrehandlerOutput,
BarrierOutput,
> = {
/**
* Explicit message type for this handler.
* Required when using a custom resolver function that cannot extract types from schemas.
* Optional when using messageTypePath or literal resolver modes.
*/
messageType?: string
messageLogFormatter?: LogFormatter<MessagePayloadSchema>
preHandlerBarrier?: BarrierCallback<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>
preHandlers?: Prehandler<MessagePayloadSchema, ExecutionContext, PrehandlerOutput>[]
}
export class MessageHandlerConfig<
const MessagePayloadSchema extends object,
const ExecutionContext,
const PrehandlerOutput = undefined,
const BarrierOutput = unknown,
> {
public readonly schema: ZodSchema<MessagePayloadSchema>
public readonly definition?: CommonEventDefinition
/**
* Explicit message type for this handler, if provided.
* Used for routing when type cannot be extracted from schema.
*/
public readonly messageType?: string
public readonly handler: Handler<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>
public readonly messageLogFormatter?: LogFormatter<MessagePayloadSchema>
public readonly preHandlerBarrier?: BarrierCallback<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>
public readonly preHandlers: Prehandler<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput
>[]
constructor(
schema: ZodSchema<MessagePayloadSchema>,
handler: Handler<MessagePayloadSchema, ExecutionContext, PrehandlerOutput, BarrierOutput>,
options?: HandlerConfigOptions<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>,
eventDefinition?: CommonEventDefinition,
) {
this.schema = schema
this.definition = eventDefinition
this.messageType = options?.messageType
this.handler = handler
this.messageLogFormatter = options?.messageLogFormatter
this.preHandlerBarrier = options?.preHandlerBarrier
this.preHandlers = options?.preHandlers ?? []
}
}
export class MessageHandlerConfigBuilder<
MessagePayloadSchemas extends object,
ExecutionContext,
PrehandlerOutput = undefined,
> {
private readonly configs: MessageHandlerConfig<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput,
// biome-ignore lint/suspicious/noExplicitAny: This is expected
any
>[]
constructor() {
this.configs = []
}
/**
* Add a handler configuration for a specific message type.
* The schema is used for both routing (to match the message type) and validation (for the handler).
*
* The message type field (e.g., 'type' or 'detail-type') must be at the root level of the message
* and must be a literal value in the schema for routing to work.
*
* Example:
* ```typescript
* const USER_CREATED_SCHEMA = z.object({
* type: z.literal('user.created'),
* userId: z.string(),
* email: z.string()
* })
*
* builder.addConfig(USER_CREATED_SCHEMA, async (message) => {
* // message has type 'user.created', userId, and email
* })
* ```
*
* EventBridge example:
* ```typescript
* const USER_PRESENCE_SCHEMA = z.object({
* 'detail-type': z.literal('v2.users.{id}.presence'),
* time: z.string(),
* detail: z.object({
* userId: z.string(),
* presenceStatus: z.string()
* })
* })
*
* builder.addConfig(USER_PRESENCE_SCHEMA, async (message) => {
* // message is the full EventBridge envelope
* const detail = message.detail // Access nested payload directly
* })
* ```
*/
addConfig<MessagePayloadSchema extends MessagePayloadSchemas, const BarrierOutput>(
schema: ZodSchema<MessagePayloadSchema> | CommonEventDefinition,
handler: Handler<MessagePayloadSchema, ExecutionContext, PrehandlerOutput, BarrierOutput>,
options?: HandlerConfigOptions<
MessagePayloadSchema,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>,
): this {
const payloadSchema = isCommonEventDefinition(schema)
? // @ts-ignore
(schema.consumerSchema as ZodSchema<MessagePayloadSchema>)
: schema
const definition = isCommonEventDefinition(schema) ? schema : undefined
this.configs.push(
new MessageHandlerConfig<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
>(
payloadSchema,
// @ts-expect-error
handler,
options,
definition,
),
)
return this
}
build() {
return this.configs
}
}
export type Handler<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput = undefined,
BarrierOutput = undefined,
> = (
message: MessagePayloadSchemas,
context: ExecutionContext,
preHandlingOutputs: PreHandlingOutputs<PrehandlerOutput, BarrierOutput>,
definition?: CommonEventDefinition,
) => Promise<Either<'retryLater', 'success'>>
export type HandlerContainerOptions<
MessagePayloadSchemas extends object,
ExecutionContext,
PrehandlerOutput = undefined,
> = {
messageHandlers: MessageHandlerConfig<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>[]
/**
* Configuration for resolving message types.
*/
messageTypeResolver?: MessageTypeResolverConfig
}
export class HandlerContainer<
MessagePayloadSchemas extends object,
ExecutionContext,
PrehandlerOutput = undefined,
> {
private readonly messageHandlers: Record<
string,
MessageHandlerConfig<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>
>
private readonly messageTypeResolver?: MessageTypeResolverConfig
constructor(
options: HandlerContainerOptions<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>,
) {
this.messageTypeResolver = options.messageTypeResolver
this.messageHandlers = this.resolveHandlerMap(options.messageHandlers)
}
/**
* Resolves a handler for the given message type.
*/
public resolveHandler<PrehandlerOutput = undefined, BarrierOutput = undefined>(
messageType: string,
): MessageHandlerConfig<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput,
BarrierOutput
> {
const handler = this.messageHandlers[messageType]
if (!handler) {
throw new Error(`Unsupported message type: ${messageType}`)
}
// @ts-expect-error
return handler
}
/**
* Resolves message type from message data and optional attributes using the configured resolver.
*
* @param messageData - The parsed message data
* @param messageAttributes - Optional message-level attributes (e.g., PubSub attributes)
* @returns The resolved message type
* @throws Error if message type cannot be resolved
*/
public resolveMessageType(
messageData: unknown,
messageAttributes?: Record<string, unknown>,
): string {
if (this.messageTypeResolver) {
const context: MessageTypeResolverContext = { messageData, messageAttributes }
return resolveMessageType(this.messageTypeResolver, context)
}
throw new Error('Unable to resolve message type: messageTypeResolver is not configured')
}
/**
* Gets the field path used for extracting message type from schemas during registration.
* Returns undefined for literal or custom resolver modes.
*/
private getMessageTypePathForSchema(): string | undefined {
if (this.messageTypeResolver && isMessageTypePathConfig(this.messageTypeResolver)) {
return this.messageTypeResolver.messageTypePath
}
// For literal or custom resolver, we don't extract type from schema
return undefined
}
/**
* Gets the literal message type if configured.
*/
private getLiteralMessageType(): string | undefined {
if (this.messageTypeResolver && isMessageTypeLiteralConfig(this.messageTypeResolver)) {
return this.messageTypeResolver.literal
}
return undefined
}
private resolveHandlerMap(
supportedHandlers: MessageHandlerConfig<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput
>[],
): Record<
string,
MessageHandlerConfig<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>
> {
const literalType = this.getLiteralMessageType()
const messageTypePath = this.getMessageTypePathForSchema()
return supportedHandlers.reduce(
(acc, entry) => {
let messageType: string | undefined
// Priority 1: Explicit messageType on the handler config
if (entry.messageType) {
messageType = entry.messageType
}
// Priority 2: Literal type from resolver config (same for all handlers)
else if (literalType) {
messageType = literalType
}
// Priority 3: Extract type from schema shape using the field path
else if (messageTypePath) {
// @ts-expect-error - ZodSchema has shape property at runtime
messageType = extractMessageTypeFromSchema(entry.schema, messageTypePath)
}
if (!messageType) {
throw new Error(
'Unable to determine message type for handler at registration time. ' +
'Either provide explicit messageType in handler options (required for custom resolver functions), ' +
'use a literal resolver, or ensure the schema has a literal type field matching messageTypePath.',
)
}
if (acc[messageType]) {
throw new Error(`Duplicate handler for message type: ${messageType}`)
}
acc[messageType] = entry
return acc
},
{} as Record<
string,
MessageHandlerConfig<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>
>,
)
}
}