Skip to main content

Channels

export default {
...,
generators: [
{
preset: 'channels',
outputPath: './src/__gen__/',
language: 'typescript',
protocols: ['nats']
}
]
};

channels preset with asyncapi input generates support functions for each operation based on the selected protocol.

This generator uses payloads, headers and parameters generators, in case you dont have any defined, it will automatically include them with default values.

This is supported through the following inputs: asyncapi

It supports the following languages; typescript

It supports the following protocols; nats, kafka, mqtt, amqp, event_source, http_client, websocket

Options​

These are the available options for the channels generator;

OptionDefaultTypeDescriptionΒ 
asyncapiReverseOperationsfalseBooleanUsed in conjunction with AsyncAPI input, and reverses the operation actions i.e. send becomes receive and receive becomes send. Often used in testing scenarios to act as the reverse API.
asyncapiGenerateForOperationstrueBooleanUsed in conjunction with AsyncAPI input, which if true generate the functions upholding how operations are defined. If false the functions are generated regardless of what operations define. I.e. send and receive does not matter.
functionTypeMapping{}Record<String, ChannelFunctionTypes[]>Used in conjunction with AsyncAPI input, can define channel ID along side the type of functions that should be rendered.
kafkaTopicSeparator'.'StringUsed with AsyncAPI to ensure the right character separate topics, example if address is my/resource/path it will be converted to my.resource.path
eventSourceDependency'@microsoft/fetch-event-source'StringBecause @microsoft/fetch-event-source is out-dated in some areas we allow you to change the fork/variant that can be used instead

TypeScript​

Regardless of protocol, these are the dependencies:

  • If validation enabled, ajv: ^8.17.1

Depending on which protocol, these are the dependencies:

For TypeScript, the generator creates one file per protocol plus an index file that re-exports all protocols as namespaces. For example;

// Import specific functions from a protocol file
import {
jetStreamPublishToSendUserSignedup,
subscribeToReceiveUserSignedup,
publishToSendUserSignedup
} from 'src/__gen__/nats';

// Or import the entire protocol namespace
import * as nats from 'src/__gen__/nats';

// Or import all protocols from the index
import { nats, kafka, mqtt, amqp, event_source } from 'src/__gen__/index';

The generated file structure is:

outputPath/
β”œβ”€β”€ index.ts # Re-exports all protocol namespaces
β”œβ”€β”€ nats.ts # NATS-specific functions
β”œβ”€β”€ kafka.ts # Kafka-specific functions
β”œβ”€β”€ mqtt.ts # MQTT-specific functions
β”œβ”€β”€ amqp.ts # AMQP-specific functions
β”œβ”€β”€ event_source.ts # EventSource-specific functions
β”œβ”€β”€ http_client.ts # HTTP client-specific functions
└── websocket.ts # WebSocket-specific functions

Each protocol file contains standalone exported functions for interacting with channels defined in your AsyncAPI document.