1- import z from 'zod/v3 '
1+ import z from 'zod/v4 '
22import type { TopicConfig } from '../types.ts'
33import { KafkaHandlerConfig } from './KafkaHandlerConfig.ts'
44import { KafkaHandlerContainer } from './KafkaHandlerContainer.ts'
5- import type { KafkaHandlerRouting } from './KafkaHandlerRoutingBuilder.ts'
5+ import { KafkaHandlerRoutingBuilder } from './KafkaHandlerRoutingBuilder.ts'
66
77const CREATE_SCHEMA = z . object ( {
88 type : z . literal ( 'create' ) ,
@@ -44,32 +44,33 @@ describe('KafkaHandlerContainer', () => {
4444
4545 // When & Then
4646 expect (
47- ( ) => new KafkaHandlerContainer ( topicHandlers1 , 'type' ) ,
47+ ( ) => new KafkaHandlerContainer ( topicHandlers1 as any , 'type' ) ,
4848 ) . toThrowErrorMatchingInlineSnapshot ( '[Error: Duplicate handler for topic create]' )
49- expect ( ( ) => new KafkaHandlerContainer ( topicHandlers2 ) ) . toThrowErrorMatchingInlineSnapshot (
50- '[Error: Duplicate handler for topic empty]' ,
51- )
49+ expect (
50+ ( ) => new KafkaHandlerContainer ( topicHandlers2 as any ) ,
51+ ) . toThrowErrorMatchingInlineSnapshot ( '[Error: Duplicate handler for topic empty]' )
5252 } )
5353
5454 it ( 'should resolve handler with message type' , ( ) => {
5555 // Given
56- const topicHandlers : KafkaHandlerRouting < TopicsConfig , any , any > = {
57- all : [
58- new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) ,
59- new KafkaHandlerConfig ( UPDATE_SCHEMA , ( ) => Promise . resolve ( ) ) ,
60- new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) ,
61- ] ,
62- create : [ new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
63- empty : [ new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
64- }
56+ const routing = new KafkaHandlerRoutingBuilder < TopicsConfig , any > ( )
57+ . addConfig ( 'all' , new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
58+ . addConfig ( 'all' , new KafkaHandlerConfig ( UPDATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
59+ . addConfig ( 'all' , new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) )
60+ . addConfig ( 'create' , new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
61+ . addConfig ( 'empty' , new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) )
6562
6663 // When
67- const container = new KafkaHandlerContainer ( topicHandlers , 'type' )
64+ const container = new KafkaHandlerContainer ( routing . build ( ) , 'type' )
6865
6966 // Then
70- expect ( container . resolveHandler ( 'all' , { type : 'create' } ) ?. schema ) . toBe ( CREATE_SCHEMA )
67+ expect ( container . resolveHandler ( 'all' , { type : 'create' , prop : 1 } ) ?. schema ) . toBe (
68+ CREATE_SCHEMA ,
69+ )
7170 expect ( container . resolveHandler ( 'all' , { type : 'update' } ) ?. schema ) . toBe ( UPDATE_SCHEMA )
72- expect ( container . resolveHandler ( 'all' , { type : 'non-existing' } ) ?. schema ) . toBe ( EMPTY_SCHEMA )
71+ expect ( container . resolveHandler ( 'all' , { type : 'non-existing' as any } ) ?. schema ) . toBe (
72+ EMPTY_SCHEMA ,
73+ )
7374 expect ( container . resolveHandler ( 'all' , { } ) ?. schema ) . toBe ( EMPTY_SCHEMA )
7475
7576 expect ( container . resolveHandler ( 'create' , { type : 'create' , prop : 1 } ) ?. schema ) . toBe (
@@ -81,18 +82,19 @@ describe('KafkaHandlerContainer', () => {
8182 expect ( container . resolveHandler ( 'create' , { } as any ) ?. schema ) . toBe ( undefined )
8283
8384 expect ( container . resolveHandler ( 'empty' , { } as any ) ?. schema ) . toBe ( EMPTY_SCHEMA )
84- expect ( container . resolveHandler ( 'empty' , { type : 'create' } ) ?. schema ) . toBe ( EMPTY_SCHEMA )
85+ expect ( container . resolveHandler ( 'empty' , { type : 'create' } as any ) ?. schema ) . toBe (
86+ EMPTY_SCHEMA ,
87+ )
8588 } )
8689
8790 it ( 'should resolve handler without message type' , ( ) => {
8891 // Given
89- const topicHandlers : KafkaHandlerRouting < TopicsConfig , any , any > = {
90- create : [ new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
91- empty : [ new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
92- }
92+ const routing = new KafkaHandlerRoutingBuilder < TopicsConfig , any > ( )
93+ . addConfig ( 'create' , new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
94+ . addConfig ( 'empty' , new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) )
9395
9496 // When
95- const container = new KafkaHandlerContainer ( topicHandlers )
97+ const container = new KafkaHandlerContainer ( routing . build ( ) )
9698
9799 // Then
98100 expect ( container . resolveHandler ( 'create' , { type : 'create' , prop : 1 } ) ?. schema ) . toBe (
@@ -104,7 +106,9 @@ describe('KafkaHandlerContainer', () => {
104106 expect ( container . resolveHandler ( 'create' , { } as any ) ?. schema ) . toBe ( CREATE_SCHEMA )
105107
106108 expect ( container . resolveHandler ( 'empty' , { } as any ) ?. schema ) . toBe ( EMPTY_SCHEMA )
107- expect ( container . resolveHandler ( 'empty' , { type : 'create' } ) ?. schema ) . toBe ( EMPTY_SCHEMA )
109+ expect ( container . resolveHandler ( 'empty' , { type : 'create' } as any ) ?. schema ) . toBe (
110+ EMPTY_SCHEMA ,
111+ )
108112 } )
109113 } )
110114
@@ -119,19 +123,15 @@ describe('KafkaHandlerContainer', () => {
119123
120124 it ( 'should return all topics' , ( ) => {
121125 // Given
122- const topicHandlers : KafkaHandlerRouting < TopicsConfig , any , any > = {
123- all : [
124- new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) ,
125- new KafkaHandlerConfig ( UPDATE_SCHEMA , ( ) => Promise . resolve ( ) ) ,
126- new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) ,
127- ] ,
128- create : [ new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
129- empty : [ new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) ] ,
130- another : [ ] ,
131- }
126+ const routing = new KafkaHandlerRoutingBuilder < TopicsConfig , any > ( )
127+ . addConfig ( 'all' , new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
128+ . addConfig ( 'all' , new KafkaHandlerConfig ( UPDATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
129+ . addConfig ( 'all' , new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) )
130+ . addConfig ( 'create' , new KafkaHandlerConfig ( CREATE_SCHEMA , ( ) => Promise . resolve ( ) ) )
131+ . addConfig ( 'empty' , new KafkaHandlerConfig ( EMPTY_SCHEMA , ( ) => Promise . resolve ( ) ) )
132132
133133 // When
134- const container = new KafkaHandlerContainer ( topicHandlers , 'type' )
134+ const container = new KafkaHandlerContainer ( { ... routing . build ( ) , another : [ ] } , 'type' )
135135
136136 // Then
137137 expect ( container . topics ) . toEqual ( [ 'all' , 'create' , 'empty' ] )
0 commit comments