11import type { Either } from '@lokalise/node-core'
2- import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
32import type { ZodSchema } from 'zod/v3'
43
54export type MessageSchemaContainerOptions < MessagePayloadSchemas extends object > = {
6- messageDefinitions : readonly CommonEventDefinition [ ]
75 messageSchemas : readonly ZodSchema < MessagePayloadSchemas > [ ]
8- messageTypeField : string
6+ messageTypeField ? : string
97}
108
9+ const NO_MESSAGE_TYPE = 'NO_MESSAGE_TYPE'
10+
1111export class MessageSchemaContainer < MessagePayloadSchemas extends object > {
12- public readonly messageDefinitions : Record < string , CommonEventDefinition >
1312 private readonly messageSchemas : Record < string , ZodSchema < MessagePayloadSchemas > >
14- private readonly messageTypeField : string
13+ private readonly messageTypeField ? : string
1514
1615 constructor ( options : MessageSchemaContainerOptions < MessagePayloadSchemas > ) {
16+ if ( options . messageTypeField === undefined && options . messageSchemas . length === 1 ) {
17+ throw new Error (
18+ 'if messageTypeField is not provided, messageSchemas must have a single schema' ,
19+ )
20+ }
21+
1722 this . messageTypeField = options . messageTypeField
1823 this . messageSchemas = this . resolveSchemaMap ( options . messageSchemas )
19- this . messageDefinitions = this . resolveDefinitionsMap ( options . messageDefinitions ?? [ ] )
2024 }
2125
2226 public resolveSchema (
2327 // biome-ignore lint/suspicious/noExplicitAny: This is expected
2428 message : Record < string , any > ,
2529 ) : Either < Error , ZodSchema < MessagePayloadSchemas > > {
30+ if ( ! this . messageTypeField ) {
31+ return this . messageSchemas [ NO_MESSAGE_TYPE ]
32+ ? { result : this . messageSchemas [ NO_MESSAGE_TYPE ] }
33+ : { error : new Error ( 'Unsupported message' ) }
34+ }
35+
2636 const schema = this . messageSchemas [ message [ this . messageTypeField ] ]
2737 if ( ! schema ) {
28- return {
29- error : new Error ( `Unsupported message type: ${ message [ this . messageTypeField ] } ` ) ,
30- }
31- }
32- return {
33- result : schema ,
38+ return { error : new Error ( `Unsupported message type: ${ message [ this . messageTypeField ] } ` ) }
3439 }
40+ return { result : schema }
3541 }
3642
3743 private resolveSchemaMap (
3844 supportedSchemas : readonly ZodSchema < MessagePayloadSchemas > [ ] ,
3945 ) : Record < string , ZodSchema < MessagePayloadSchemas > > {
46+ if ( ! this . messageTypeField ) {
47+ if ( ! supportedSchemas [ 0 ] ) return { }
48+ return { [ NO_MESSAGE_TYPE ] : supportedSchemas [ 0 ] }
49+ }
50+
4051 return supportedSchemas . reduce (
4152 ( acc , schema ) => {
4253 // @ts -ignore
@@ -46,17 +57,4 @@ export class MessageSchemaContainer<MessagePayloadSchemas extends object> {
4657 { } as Record < string , ZodSchema < MessagePayloadSchemas > > ,
4758 )
4859 }
49-
50- private resolveDefinitionsMap (
51- supportedDefinitions : readonly CommonEventDefinition [ ] ,
52- ) : Record < string , CommonEventDefinition > {
53- return supportedDefinitions . reduce (
54- ( acc , definition ) => {
55- // @ts -ignore
56- acc [ definition . publisherSchema . shape [ this . messageTypeField ] . value ] = definition
57- return acc
58- } ,
59- { } as Record < string , CommonEventDefinition > ,
60- )
61- }
6260}
0 commit comments