AsyncAPI Generator: A Comprehensive Guide to  Generator Project

AsyncAPI Generator: A Comprehensive Guide to Generator Project

By following this comprehensive guide, developers can effectively utilize the Generator to create a wide range of outputs, from code and diagrams to markdown files and applications, based on a template and an AsyncAPI document. For a more in-depth understanding of the project, be sure to check out my YouTube video where I explain the entire process step-by-step.


Introduction

The Generator is a powerful tool that enables the creation of various outputs, including code, diagrams, markdownfiles, microservices and applications based on template and AsyncAPI Dcoument.

AsyncAPI Document

The AsyncAPI document can be considered like a blueprint for describing APIs and their functionality.A well-structured AsyncAPI document is essential for effective communication of the API's behavior.

It consists of several root elements, including:

  • asyncapi: specifies the AsyncAPI version

  • info: provides metadata about the API, such as title and version

  • channels: defines the channels (topics or queues) used for communication

  • operations: defines the operations that can be performed on the channels

Templates

Templates are independent Node.js projects that describe the generation result based on the AsyncAPI document's content. AsyncAPI provides a range of pre-built templates, including:

Template NameDescription
@asyncapi/nodejs-templateGenerates a Node.js service using the Hermes package
@asyncapi/nodejs-ws-templateGenerates a Node.js service supporting the WebSocket protocol
@asyncapi/java-templateGenerates a Java JMS application
@asyncapi/java-spring-templateGenerates a Java Spring service
@asyncapi/java-spring-cloud-stream-templateGenerates a Java Spring Cloud Stream service
@asyncapi/python-paho-templateGenerates a Python service using the Paho library
@asyncapi/html-templateGenerates an HTML documentation site
@asyncapi/markdown-templateGenerates documentation in Markdown format
@asyncapi/ts-nats-templateGenerates a TypeScript NATS client
@asyncapi/go-watermill-templateGenerates a Go client using Watermill
@asyncapi/dotnet-nats-templateGenerates a .NET C# client using NATS

Template Development

Template directory holds all the fiels that will be used for generating output. The generator will process all the files stored in this directory.

Example of an index.js file

executing this index.js will produce a asyncapi.md file with AsyncAPI document information.

jsxVerifyOpen In EditorECopy code1import { File, Text } from "@asyncapi/generator-react-sdk";
2
3export default function ({ asyncapi, params, originalAsyncAPI }) {
4  return (
5    <File name="asyncapi.md">
6      <Text>My application's markdown file.</Text>
7      <Text>App name: **{asyncapi.info().title()}**</Text>
8    </File>
9  );
10}

For the Render engine to work smoothly we need to make sure our package.json file identifies the template name, hence we add the template name to the package.json.

@asyncapi/generator-react-sdk package has all the templates file’s basic components.

jsxVerifyOpen In EditorEditCopy code1{
2  "name": "myTemplate",
3  "generator": {
4    "renderer": "react"
5  },
6  "dependencies": {
7    "@asyncapi/generator-react-sdk": "^0.2.25" 
8  }
9}

Template Parameters

Template parameters are used to configure the template. The following parameters are available:

NameTypeDescription
rendererStringIts value can be either react or nunjucks (default).
supportedProtocols[String]A list with all the protocols this template supports.
parametersObject[String, Object]An object with all the parameters that can be passed when generating the template.
parameters[param].descriptionStringA user-friendly description about the parameter.
parameters[param].defaultAnyDefault value of the parameter if not specified.
parameters[param].requiredBooleanWhether the parameter is required or not.

Parser

Parser is a package used to validate these document using dedicated schema-supported plugins like OpenAPI schema, Json schema, Avro schema, RAML schema and AsyncAPI schema.

Parser allows the template developer to easily access the schemas provided in plugin supported formats as javascript parser converts everything into Json schema.


Configuration File

The configuration file is used to specify template settings. The following options are available:

NameTypeDescription
rendererStringIts value can be either react or nunjucks (default).
apiVersionStringDetermines which major version of the Parser-API the template uses.
supportedProtocols[String]A list with all the protocols this template supports.
parametersObject[String, Object]An object with all the parameters that can be passed when generating the template.
conditionalFilesObject[String, Object]An object containing all the file paths that should be conditionally rendered.
nonRenderableFiles[String]A list of file paths or globs that must be copied "as-is" to the target directory.
generatorStringA string representing the generator version-range the template is compatible with.
filters[String]A list of modules containing functions that can be used as Nunjucks filters.
hooksObject[String, String] or Object[String, Array[String]]A list of modules containing hooks.

Hooks

Hooks are function called by generator on a specific moment in the generation process. Hooks are anonymous function with arguments provided to them with some expected return value:

Hook TypeDescriptionReturn TypeArguments
generate:beforeCalled after registration of all filters and before the Generator starts processing of the template.voidThe Generator instance
generate:afterCalled at the very end of the generation.voidThe Generator instance
setFileTemplateNameCalled right before saving a new file generated by file template.stringThe Generator instance and object in the form of { "originalFilename" : string }

Conclusion

Hope this guide has provided you with valuable insights into how these templates can streamline your development process and enhance your projects.

Your engagement and interest in this topic are greatly appreciated. I encourage you to experiment with these templates and discover the potential they hold for your applications.

Stay curious, keep coding, and we look forward to seeing you in our future posts!

Want to contribute to the project ?

Feel free to join the Organization Slack.

Github repository

Here is the contribution guideline.

For more information you can refer the following resources: