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