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.
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 versioninfo
: provides metadata about the API, such as title and versionchannels
: defines the channels (topics or queues) used for communicationoperations
: 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 Name | Description |
@asyncapi/nodejs-template | Generates a Node.js service using the Hermes package |
@asyncapi/nodejs-ws-template | Generates a Node.js service supporting the WebSocket protocol |
@asyncapi/java-template | Generates a Java JMS application |
@asyncapi/java-spring-template | Generates a Java Spring service |
@asyncapi/java-spring-cloud-stream-template | Generates a Java Spring Cloud Stream service |
@asyncapi/python-paho-template | Generates a Python service using the Paho library |
@asyncapi/html-template | Generates an HTML documentation site |
@asyncapi/markdown-template | Generates documentation in Markdown format |
@asyncapi/ts-nats-template | Generates a TypeScript NATS client |
@asyncapi/go-watermill-template | Generates a Go client using Watermill |
@asyncapi/dotnet-nats-template | Generates 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:
Name | Type | Description |
renderer | String | Its value can be either react or nunjucks (default). |
supportedProtocols | [String] | A list with all the protocols this template supports. |
parameters | Object[String, Object] | An object with all the parameters that can be passed when generating the template. |
parameters[param].description | String | A user-friendly description about the parameter. |
parameters[param].default | Any | Default value of the parameter if not specified. |
parameters[param].required | Boolean | Whether 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.
The configuration file is used to specify template settings. The following options are available:
Name | Type | Description |
renderer | String | Its value can be either react or nunjucks (default). |
apiVersion | String | Determines which major version of the Parser-API the template uses. |
supportedProtocols | [String] | A list with all the protocols this template supports. |
parameters | Object[String, Object] | An object with all the parameters that can be passed when generating the template. |
conditionalFiles | Object[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. |
generator | String | A 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. |
hooks | Object[String, String] or Object[String, Array[String]] | A list of modules containing 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 Type | Description | Return Type | Arguments |
generate:before | Called after registration of all filters and before the Generator starts processing of the template. | void | The Generator instance |
generate:after | Called at the very end of the generation. | void | The Generator instance |
setFileTemplateName | Called right before saving a new file generated by file template. | string | The 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:
Subscribe to my newsletter
Read articles from Sarvesh.Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sarvesh.Patil
Sarvesh.Patil
I'm a passionate tech enthusiast eagerly awaiting my next opportunity to make contribution to the world of technology. Hungry for knowledge, I consider myself a perpetual learner, constantly exploring and honing my skills in the vast realm of tech and software.