Skip to content

Commit dc62285

Browse files
committed
MessageSchemaContainer making messageTypeField optional
1 parent 4837221 commit dc62285

2 files changed

Lines changed: 23 additions & 32 deletions

File tree

packages/core/lib/queues/AbstractQueueService.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
stringValueSerializer,
99
} from '@lokalise/node-core'
1010
import { resolveGlobalErrorLogObject } from '@lokalise/node-core'
11-
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
1211
import type { ZodSchema, ZodType } from 'zod/v3'
1312

1413
import {
@@ -162,13 +161,9 @@ export abstract class AbstractQueueService<
162161
messageTypeField: string
163162
}) {
164163
const messageSchemas = options.handlers.map((entry) => entry.schema)
165-
const messageDefinitions: CommonEventDefinition[] = options.handlers
166-
.map((entry) => entry.definition)
167-
.filter((entry) => entry !== undefined)
168164

169165
return new MessageSchemaContainer<MessagePayloadSchemas>({
170166
messageSchemas,
171-
messageDefinitions,
172167
messageTypeField: options.messageTypeField,
173168
})
174169
}
@@ -178,11 +173,9 @@ export abstract class AbstractQueueService<
178173
messageTypeField: string
179174
}) {
180175
const messageSchemas = options.messageSchemas
181-
const messageDefinitions: readonly CommonEventDefinition[] = []
182176

183177
return new MessageSchemaContainer<MessagePayloadSchemas>({
184178
messageSchemas,
185-
messageDefinitions,
186179
messageTypeField: options.messageTypeField,
187180
})
188181
}
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,53 @@
11
import type { Either } from '@lokalise/node-core'
2-
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
32
import type { ZodSchema } from 'zod/v3'
43

54
export 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+
1111
export 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

Comments
 (0)