React is the render engine that we strongly suggest you should use for any new templates. The only reason it is not the default render engine is to stay backward compatible.
- It enables the possibility of debugging your template (this is not possible with Nunjucks).
- It provides better error stack traces.
- Provides better support for separating code into more manageable chunks/components.
- The readability of the template is much better compared to Nunjucks syntax.
- Better tool support for development.
- Introduces testability of components which is not possible with Nunjucks.
When writing React templates you decide whether to use CommonJS, ES5, or ES6 modules since everything is bundled together before the rendering process takes over. We use our own React renderer which can be found in the Generator React SDK. There you can find information about how the renderer works or how we transpile your template files.
Your React template always requires @asyncapi/generator-react-sdk
as a dependency. @asyncapi/generator-react-sdk
is required to access the File
component which is required as a root component for a file to be rendered. Furthermore, it provides some common components to make your development easier, like Text
or Indent
.
Let's consider a basic React template as the one below called MyTemplate.js
:
1import { File, Text } from "@asyncapi/generator-react-sdk";
2
3export default function({ asyncapi, params, originalAsyncAPI }) {
4 return (
5 <File name="asyncapi.md">
6 <Text>Some text that should render as is</Text>
7 </File>
8 );
9}
The exported default function returns a File
component as a root component which the generator uses to determine what file should be generated. In our case, we overwrite the default functionality of saving the file as MyTemplate.js
but instead use the filename asyncapi.md
. It is then specified that we should render Some text that should render as is\n
within that file. Notice the \n
character at the end, which is automatically added after the Text
component.
For further information about components, props, etc, see the Generator React SDK
Common assumptions
- Generator renders all files located in the
template
directory if they meet the following conditions:File
is the root component- The file is not in the list of
nonRenderableFiles
in the template configuration
- New lines are automatically added after each
Text
component. - The props you have access to in the rendering function are:
asyncapi
which is a parsed spec file object. Read the API of the Parser to understand what structure you have access to in this parameter.originalAsyncAPI
which is an original spec file before it is parsed.params
that contain the parameters provided when generating.
- All the file templates are supported where the variables are provided after the default props as listed above.
Debugging React template in VSCode
With React, it enables you to debug your templates. For Visual Studio Code, we have created a boilerplate launch configuration to enable debugging in your template. Add the following launch configuration:
1{
2 "version": "0.2.0",
3 "configurations": [
4 {
5 "type": "node",
6 "request": "launch",
7 "name": "Debug template",
8 "timeout": 10000,
9 "sourceMaps": true,
10 "args": [
11 "./asyncapi.yml",
12 "./template",
13 "--output",
14 "./output",
15 "--install",
16 "--force-write"
17 ],
18 "program": "ag"
19 }
20 ]
21}
Now replace ./asyncapi.yml
with your document of choice. Replace ./template
with the path to your React template. You can now debug your template by adding any breakpoints you want and inspecting your code.