@@ -42,6 +42,7 @@ import {
4242 addPayloadsToDependencies ,
4343 getMessageTypeAndModule
4444} from './utils' ;
45+ import * as KafkaRenderer from './protocols/kafka' ;
4546export {
4647 renderedFunctionType ,
4748 TypeScriptChannelRenderType ,
@@ -367,7 +368,111 @@ export async function generateTypeScriptChannels(
367368 dependencies . push ( ...( new Set ( renderedDependencies ) as any ) ) ;
368369 break ;
369370 }
371+ case 'kafka' : {
372+ // AsyncAPI v2 explicitly say to use RFC 6570 URI template, Kafka does not support '/' subject separation, so lets replace it
373+ let topic = simpleContext . topic ;
374+ if ( topic . startsWith ( '/' ) ) {
375+ topic = topic . slice ( 1 ) ;
376+ }
377+ topic = topic . replace ( / \/ / g, generator . kafkaTopicSeparator ) ;
378+ let kafkaContext : RenderRegularParameters = {
379+ ...simpleContext ,
380+ topic,
381+ messageType : ''
382+ } ;
383+ const renders = [ ] ;
384+ const payload = payloads . channelModels [ channel . id ( ) ] ;
385+ if ( payload === undefined ) {
386+ throw new Error (
387+ `Could not find payload for ${ channel . id ( ) } for channel typescript generator`
388+ ) ;
389+ }
390+ const { messageModule, messageType} =
391+ getMessageTypeAndModule ( payload ) ;
392+ kafkaContext = { ...kafkaContext , messageType, messageModule} ;
393+ const operations = channel . operations ( ) . all ( ) ;
394+ if ( operations . length > 0 && ! ignoreOperation ) {
395+ for ( const operation of operations ) {
396+ const payloadId = findOperationId ( operation , channel ) ;
397+ const payload = payloads . operationModels [ payloadId ] ;
398+ if ( payload === undefined ) {
399+ throw new Error (
400+ `Could not find payload for ${ payloadId } for channel typescript generator ${ JSON . stringify ( payloads . operationModels , null , 4 ) } `
401+ ) ;
402+ }
403+ const { messageModule, messageType} =
404+ getMessageTypeAndModule ( payload ) ;
405+ kafkaContext = {
406+ ...kafkaContext ,
407+ messageType,
408+ messageModule,
409+ subName : findNameFromOperation ( operation , channel )
410+ } ;
411+ const action = operation . action ( ) ;
412+ if (
413+ shouldRenderFunctionType (
414+ functionTypeMapping ,
415+ ChannelFunctionTypes . KAFKA_PUBLISH ,
416+ action ,
417+ generator . asyncapiReverseOperations
418+ )
419+ ) {
420+ renders . push ( KafkaRenderer . renderPublish ( kafkaContext ) ) ;
421+ }
422+ if (
423+ shouldRenderFunctionType (
424+ functionTypeMapping ,
425+ ChannelFunctionTypes . KAFKA_SUBSCRIBE ,
426+ action ,
427+ generator . asyncapiReverseOperations
428+ )
429+ ) {
430+ renders . push ( KafkaRenderer . renderSubscribe ( kafkaContext ) ) ;
431+ }
432+ }
433+ } else {
434+ if (
435+ shouldRenderFunctionType (
436+ functionTypeMapping ,
437+ ChannelFunctionTypes . KAFKA_PUBLISH ,
438+ 'send' ,
439+ generator . asyncapiReverseOperations
440+ )
441+ ) {
442+ renders . push ( KafkaRenderer . renderPublish ( kafkaContext ) ) ;
443+ }
444+ if (
445+ shouldRenderFunctionType (
446+ functionTypeMapping ,
447+ ChannelFunctionTypes . KAFKA_SUBSCRIBE ,
448+ 'receive' ,
449+ generator . asyncapiReverseOperations
450+ )
451+ ) {
452+ renders . push ( KafkaRenderer . renderSubscribe ( kafkaContext ) ) ;
453+ }
454+ }
455+ protocolCodeFunctions [ protocol ] . push (
456+ ...renders . map ( ( value ) => value . code )
457+ ) ;
370458
459+ externalProtocolFunctionInformation [ protocol ] . push (
460+ ...renders . map ( ( value ) => {
461+ return {
462+ functionType : value . functionType ,
463+ functionName : value . functionName ,
464+ messageType : value . messageType ,
465+ replyType : value . replyType ,
466+ parameterType : parameter ?. model ?. type
467+ } ;
468+ } )
469+ ) ;
470+ const renderedDependencies = renders
471+ . map ( ( value ) => value . dependencies )
472+ . flat ( Infinity ) ;
473+ dependencies . push ( ...( new Set ( renderedDependencies ) as any ) ) ;
474+ break ;
475+ }
371476 case 'mqtt' : {
372477 const topic = simpleContext . topic ;
373478 let natsContext : RenderRegularParameters = {
@@ -440,7 +545,7 @@ export async function generateTypeScriptChannels(
440545 messageType : value . messageType ,
441546 replyType : value . replyType ,
442547 parameterType : parameter ?. model ?. type
443- } as renderedFunctionType ;
548+ } ;
444549 } )
445550 ) ;
446551 const renderedDependencies = renders
0 commit comments