@asyncapi/generator 1.9.12 → 1.9.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,16 +12,8 @@
12
12
  - [List of official generator templates](#list-of-official-generator-templates)
13
13
  - [Requirements](#requirements)
14
14
  - [Using from the command-line interface (CLI)](#using-from-the-command-line-interface-cli)
15
- * [Install the CLI](#install-the-cli)
16
- * [Update the CLI](#update-the-cli)
15
+ * [CLI installation](#cli-installation)
17
16
  * [CLI usage](#cli-usage)
18
- * [Global templates installed with yarn or npm](#global-templates-installed-with-yarn-or-npm)
19
- * [CLI usage examples](#cli-usage-examples)
20
- * [CLI usage with Docker](#cli-usage-with-docker)
21
- * [CLI usage with npx instead of npm](#cli-usage-with-npx-instead-of-npm)
22
- - [Using as a module/package](#using-as-a-modulepackage)
23
- * [Install the module](#install-the-module)
24
- * [Example using the module](#example-using-the-module)
25
17
  - [Generator version vs Template version](#generator-version-vs-template-version)
26
18
  - [How to create a template](#how-to-create-a-template)
27
19
  - [Contributing](#contributing)
@@ -67,221 +59,20 @@ You can find above templates and the ones provided by the community in **[this l
67
59
 
68
60
  Install both packages using [official installer](https://nodejs.org/en/download/). After installation make sure both packages have proper version by running `node -v` and `npm -v`. To upgrade invalid npm version run `npm install npm@latest -g`
69
61
 
70
- > Generator is tested at the moment against Node 14 and NPM 6. Using newer versions is enabled but we do not guarantee they work well. Please provide feedback on the issues.
62
+ > The generator is tested at the moment against Node 14 and NPM 6. Using newer versions is enabled but we don't guarantee they work well. Please provide feedback via issues.
71
63
 
72
64
  ## Using from the command-line interface (CLI)
73
65
 
74
- ### Install the CLI
75
-
76
- To use it as CLI, install generator globally:
77
-
78
- ```bash
79
- npm install -g @asyncapi/generator
80
- ```
81
-
82
- ### Update the CLI
83
-
84
- You might want to update your local installation of generator for different reasons:
85
-
86
- * You want the latest generator to have its latest features. Perform usual installation and in case you had generator installed already, it will upgrade to latest available:
87
- ```bash
88
- npm install -g @asyncapi/generator
89
- ```
90
- * You want a specific version of the generator because your template might not be compatible with the latest generator. Check [what version you need](https://github.com/asyncapi/generator/releases) and perform installation, specifying the exact version with the `@` character:
91
- ```bash
92
- npm install -g @asyncapi/generator@0.50.0
93
- ```
94
-
95
- > Sometimes you have to force additional npm installation like this: `npm install -g --force @asyncapi/generator`
96
-
97
- ### CLI usage
98
-
99
- ```bash
100
- Usage: ag [options] <asyncapi> <template>
101
-
102
- - <asyncapi>: Local path or URL pointing to AsyncAPI specification file
103
- - <template>: Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template
104
-
105
- Options:
106
- -V, --version output the version number
107
- -d, --disable-hook [hooks...] disable a specific hook type or hooks from given hook type
108
- --debug enable more specific errors in the console
109
- -i, --install installs the template and its dependencies (defaults to false)
110
- -n, --no-overwrite <glob> glob or path of the file(s) to skip when regenerating
111
- -o, --output <outputDir> directory where to put the generated files (defaults to current directory)
112
- -p, --param <name=value> additional param to pass to templates
113
- --force-write force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)
114
- --watch-template watches the template directory and the AsyncAPI document, and re-generate the files when changes occur. Ignores the output directory. This flag should be used only for template development.
115
- --map-base-url <url:folder> maps all schema references from base url to local folder
116
- -h, --help display help for command
117
- ```
118
-
119
- <details>
120
- <summary>Click here to read more about supported values for the <code>&lt;template&gt;</code> parameter.</summary>
121
- <br>
122
- Templates are installable npm packages. Therefore, the value of <code>&lt;template&gt;</code> can be anything supported by <code>npm install</code>. Here's a summary of the possibilities:
123
- <br><br>
124
- <pre><code>
125
- npm install [&lt;@scope&gt;/]&lt;name&gt;
126
- npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;tag&gt;
127
- npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;version&gt;
128
- npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;version range&gt;
129
- npm install &lt;git-host&gt;:&lt;git-user&gt;/&lt;repo-name&gt;
130
- npm install &lt;git repo url&gt;
131
- npm install &lt;tarball file&gt;
132
- npm install &lt;tarball url&gt;
133
- npm install &lt;folder&gt;</code></pre>
134
- </details>
135
- <br>
66
+ ### CLI installation
67
+ Learn to install the AsyncAPI CLI from the [installation guide](installation-guide.md).
136
68
 
137
- ### Global templates installed with yarn or npm
69
+ ### CLI usage
138
70
 
139
- You can preinstall templates globally. The generator first tries to locate template in local dependencies and then in location where global packages are installed.
140
-
141
- ```bash
142
- npm install -g @asyncapi/html-template@0.16.0
143
- ag asyncapi.yaml @asyncapi/html-template
144
- # The generator uses template in version 0.16.0 and not latest
145
- ```
146
-
147
- ### CLI usage examples
148
-
149
- **The shortest possible syntax:**
150
- ```bash
151
- ag asyncapi.yaml @asyncapi/html-template
152
- ```
153
-
154
- **Generating from a URL:**
155
- ```bash
156
- ag https://bit.ly/asyncapi @asyncapi/html-template
157
- ```
158
-
159
- **Specify where to put the result:**
160
- ```bash
161
- ag asyncapi.yaml @asyncapi/html-template -o ./docs
162
- ```
163
-
164
- **Passing parameters to templates:**
165
- ```bash
166
- ag asyncapi.yaml @asyncapi/html-template -o ./docs -p title='Hello from param'
167
- ```
168
-
169
- In the template you can use it like this: ` {{ params.title }}`
170
-
171
- **Disabling the hooks:**
172
- ```bash
173
- ag asyncapi.yaml @asyncapi/html-template -o ./docs -d generate:before generate:after=foo,bar
174
- ```
175
-
176
- The generator skips all hooks of the `generate:before` type and `foo`, `bar` hooks of the `generate:after` type.
177
-
178
- **Installing the template from a folder:**
179
- ```bash
180
- ag asyncapi.yaml ~/my-template
181
- ```
182
-
183
- It creates a symbolic link to the target directory (`~/my-template` in this case).
184
-
185
- **Installing the template from a git URL:**
186
- ```bash
187
- ag asyncapi.yaml https://github.com/asyncapi/html-template.git
188
- ```
189
-
190
- **Map schema references from baseUrl to local folder:**
191
- ```bash
192
- ag test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/:./test/docs/
193
- ```
194
-
195
- The parameter `--map-base-url` maps external schema references to local folders.
196
-
197
- ### CLI usage with Docker
198
-
199
- Install [Docker](https://docs.docker.com/get-docker/) first. Thanks to Docker you do not need Node.js even though the generator is written with it.
200
-
201
- ```bash
202
- docker run --rm -it \
203
- -v [ASYNCAPI SPEC FILE LOCATION]:/app/asyncapi.yml \
204
- -v [GENERATED FILES LOCATION]:/app/output \
205
- asyncapi/generator [COMMAND HERE]
206
-
207
- # Example that you can run inside generator directory after cloning this repository. First you specify mount in location of your AsyncAPI specification file and then you mount in directory where generation result should be saved.
208
- docker run --rm -it \
209
- -v ${PWD}/test/docs/dummy.yml:/app/asyncapi.yml \
210
- -v ${PWD}/output:/app/output \
211
- asyncapi/generator -o /app/output /app/asyncapi.yml @asyncapi/html-template --force-write
212
- ```
213
-
214
- ### CLI usage with npx instead of npm
215
-
216
- The [npx](https://www.npmjs.com/package/npx) is very useful when you want to run Generator in CI/CD environment. In such a scenario, you do not want to install generator globally and most environments that provide Node.js and npm, also provide npx out of the box.
217
-
218
- ```bash
219
- npx -p @asyncapi/generator ag ./asyncapi.yaml @asyncapi/html-template
220
- ```
221
-
222
- ## Using as a module/package
223
-
224
- ### Install the module
225
-
226
- ```bash
227
- npm install @asyncapi/generator --save
228
- ```
229
-
230
- ### Example using the module
231
-
232
- Below you can find an example of HTML generation using official `@asyncapi/html-template` template and fetching the spec document from server like `https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml` :
233
-
234
- ```js
235
- const path = require('path');
236
- const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));
237
-
238
- try {
239
- await generator.generateFromURL('https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml');
240
- console.log('Done!');
241
- } catch (e) {
242
- console.error(e);
243
- }
244
- ```
245
-
246
- See [API documentation](docs/api.md) for more example and full API reference information.
71
+ Learn more about different ways of using the CLI from the [usage document](usage.md).
247
72
 
248
73
  ## Generator version vs Template version
249
74
 
250
- The Generator is a tool that you can use to generate whatever you want, taking an AsyncAPI specification file as the input. A template is a tool that uses Generator features and helpers to specify what should be generated.
251
-
252
- In other words, a template depends on the Generator and its features. For example, it might work with the latest version of the Generator but not the previous ones.
253
-
254
- The owner of the template specifies in the configuration what version of the Generator it is compatible with:
255
- ```bash
256
- "generator": ">=0.50.0 <2.0.0",
257
- ```
258
-
259
- The Generator doesn't work in case the template is not compatible:
260
- ```bash
261
- Something went wrong:
262
- Error: This template is not compatible with the current version of the generator (0.50.0). This template is compatible with the following version range: >=0.60.0 <2.0.0.
263
- at Generator.validateTemplateConfig (/Users/wookiee/.nvm/versions/node/v12.16.1/lib/node_modules/@asyncapi/generator/lib/generator.js:678:13)
264
- at Generator.loadTemplateConfig (/Users/wookiee/.nvm/versions/node/v12.16.1/lib/node_modules/@asyncapi/generator/lib/generator.js:663:16)
265
- at Generator.generate (/Users/wookiee/.nvm/versions/node/v12.16.1/lib/node_modules/@asyncapi/generator/lib/generator.js:146:18)
266
- at processTicksAndRejections (internal/process/task_queues.js:97:5)
267
- at async /Users/wookiee/.nvm/versions/node/v12.16.1/lib/node_modules/@asyncapi/generator/cli.js:135:7
268
- ```
269
-
270
- In case you use Generator CLI and a specific template on production, it is safer to lock to a specific version of the template and the Generator.
271
-
272
- Instead of generating HTML with latest `html-template` and the generator CLI:
273
- ```bash
274
- npm install -g @asyncapi/generator
275
- ag asyncapi.yaml @asyncapi/html-template -o ./docs
276
- ```
277
-
278
- Generate HTML with the version of the `html-template` and the Generator CLI that you are happy with:
279
- ```bash
280
- npm install -g @asyncapi/generator@0.50.0
281
- ag asyncapi.yaml @asyncapi/html-template@0.7.0 -o ./docs
282
- ```
283
-
284
- Before using newer versions of the template, always look at the [changelog](https://github.com/asyncapi/html-template/releases) first. Generator features are not important for you, just make sure to use a version compatible with the template.
75
+ Learn more about versioning from the [versioning document](versioning.md).
285
76
 
286
77
  ## How to create a template
287
78
 
package/docs/README.md CHANGED
@@ -1,15 +1,18 @@
1
1
  Table of Contents
2
2
 
3
3
  - [Introduction](index.md)
4
- - [AsyncAPI Specification File](asyncapi-file.md)
4
+ - [AsyncAPI document](asyncapi-document.md)
5
+ - [Installation Guide](installation-guide.md)
6
+ - [Usage](usage.md)
5
7
  - [Configuration File](configuration-file.md)
6
8
  - [Templates](template.md)
7
- - [Authoring Templates](authoring-templates.md)
8
- - [React Render Engine](react-render-engine.md)
9
- - [Nunjucks Render Engine](nunjucks-render-engine.md)
10
- - [AsyncAPI Context](asyncapi-context.md)
9
+ - [Authoring templates](authoring-templates.md)
10
+ - [Generator version vs template version](versioning.md)
11
+ - [React render engine](react-render-engine.md)
12
+ - [Nunjucks render engine](nunjucks-render-engine.md)
13
+ - [Template context](template-context.md)
11
14
  - [Parser](parser.md)
12
15
  - [Hooks](hooks.md)
13
- - [File Templates](file-templates.md)
14
- - [TypeScript Support](typescript-support.md)
16
+ - [File templates](file-templates.md)
17
+ - [TypeScript support](typescript-support.md)
15
18
  - [Special file names](special-file-names.md)
@@ -0,0 +1,87 @@
1
+ ---
2
+ title: "AsyncAPI document"
3
+ weight: 40
4
+ ---
5
+
6
+ The **AsyncAPI document** defines a standard, protocol-agnostic interface that describes message-based or event-driven APIs. The AsyncAPI document allows people or machines communicating with one another, to understand the capabilities of an event-driven API without requiring access to the source code, documentation, or inspecting the network traffic.
7
+
8
+ This document allows you to define your API structures and formats, including channels the end user can subscribe to and the message formats they receive.
9
+
10
+ The documents describing the message-driven API under the AsyncAPI specification are represented as JSON objects and conform to JSON standards. YAML, a superset of JSON, can also be used to represent an API.
11
+
12
+ > - To learn how to create an AsyncAPI document or refresh your knowledge about the syntax and structure of the AsyncAPI document, check out our [latest specification documentation](https://www.asyncapi.com/docs/reference/specification/latest).
13
+ > - You can develop, validate, and convert the AsyncAPI document to the latest version or preview your AsyncAPI document in a more readable way using the [AsyncAPI Studio](https://studio.asyncapi.com/).
14
+
15
+ In the following sections, you'll learn about the inner working of the generator, what happens once the AsyncAPI document is fed to the generator, and how you can access the content of the document in your template.
16
+
17
+ ## AsyncAPI document generation process
18
+ 1. The **Generator** receives the **AsyncAPI Document** as input.
19
+ 2. The **Generator** sends to the **[Parser](parser)** the **asyncapiString** is a stringified version of the original **AsyncAPI Document** to validate and parse it.
20
+ 3. The **Parser** validates the **AsyncAPI Document** using additional schema-related plugins, either the OpenAPI schema, RAML data types, or Avro schema.
21
+ 4. If the **Parser** determines that the **AsyncAPI Document** is valid, it manipulates the original JSON/YAML document and provides a set of helper functions in return, bundling them together into an **asyncapi** variable that is an instance of [**AsyncAPIDocument**](https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument).
22
+ 5. At this point, the **Generator** passes the **originalAsyncAPI** and the **asyncapi** which make up part of the **[Template Context](asyncapi-context)** to the **Render Engine**.
23
+ 6. The **Template Context** is accessible to the template files that are passed to either the [react](react-render-engine) or [nunjucks](nunjucks-render-engine) **Render Engines**.
24
+
25
+ ``` mermaid
26
+ graph LR
27
+ A[Template Context]
28
+ B{Generator}
29
+ C[Parser]
30
+ D[Render Engine]
31
+ E[AsyncAPI Document] --> B
32
+ subgraph Generator
33
+ B -->| asyncapiString | C
34
+ C --> | asyncapi -> AsyncAPIDocument type | A
35
+ B--> | originalAsyncAPI -> Stringified document | A
36
+ A --> D
37
+ end
38
+ ```
39
+ The AsyncAPI document's content is accessible to you while writing your template in two distinct ways:
40
+ * The `originalAsyncAPI`, which is a stringified version of the AsyncAPI document provided as input, without any modifications.
41
+ * The `asyncapi` (`AsyncAPIDocument`) which is an object with a set of helper functions, that comes as a result of the `Parser` manipulating the `originalAyncAPI` .The `asyncapi` functions make it easier to access the contents of AsyncAPI documents in your templates.
42
+
43
+ In the following sections, you will learn how to use either the **originalAsyncAPI** or the **asyncapi** in your template.
44
+
45
+ ### Method 1: `originalAsyncAPI` and template
46
+ One way of using the contents of the AsyncAPI document inside your template files is by using its stringified version that reflects the same contents as the AsyncAPI document provided as input to the generator. You can access it directly in your templates using the `originalAsyncAPI` variable. You also access it via the [hooks](hooks) `generator.originalAsyncAPI` because `originalAsyncAPI` is also a part of the generator instance that is passed to hooks.
47
+
48
+ ```js
49
+ //example use case for using a stringified AsyncAPI document inside template hooks
50
+
51
+ const fs = require('fs');
52
+ const path = require('path');
53
+
54
+ function createAsyncapiFile(generator) {
55
+ const asyncapi = generator.originalAsyncAPI;
56
+ let extension;
57
+
58
+ try {
59
+ JSON.parse(asyncapi);
60
+ extension = 'json';
61
+ } catch (e) {
62
+ extension = 'yaml';
63
+ }
64
+
65
+ const outputFileName = `asyncapi.${extension}`;
66
+
67
+ const asyncapiOutputLocation = path.resolve('./'', outputFileName);
68
+
69
+ fs.writeFileSync(asyncapiOutputLocation, asyncapi);
70
+ ```
71
+
72
+
73
+ ### Method 2: `asyncapi` and template
74
+ A major advantage of using `asyncapi` (which is an instance of `AsyncAPIDocument`) is that it enables the developer to easily access the AsyncAPI documents' content by simply invoking a function.
75
+
76
+ Once the specification YAML or JSON document is passed as input to the generator, it is passed on to the [Parser](parser) library, which then manipulates the asyncAPI document to a more structured document called the `AsyncAPIDocument`. Once the parser returns the document to the generator, the generator passes it to the render engine. The render engine makes the `AsyncAPIDocument` object accessible to your template through the `asyncapi` variable.
77
+
78
+ For example, if you want to extract the version of your API from AsyncAPI document, you can do that by calling `asyncapi.version()`. You can say that this one is easy to access from JSON objects, but there are more complex scenarios. For example, to get access to all messages from all channels, you can call `asyncapi.allMessages()` instead of iterating through a complex JSON object on your own.
79
+
80
+ In the sample code snippet below, notice how you can access the contents of the AsyncAPI document in your template using `asyncapi` variable from the context:
81
+
82
+ ```js
83
+ const apiName = asyncapi.info().title();
84
+ const channels = asyncapi.channels();
85
+ ```
86
+
87
+ > To learn about the various instances you can use to access the documents' content, look at the API of the AsyncAPI JavaScript Parser and the structure of [AsyncAPIDocument](https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument)
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "Authoring Templates"
2
+ title: "Authoring templates"
3
3
  weight: 50
4
4
  ---
5
5
 
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "Configuration File"
2
+ title: "Configuration file"
3
3
  weight: 30
4
4
  ---
5
5
 
package/docs/index.md CHANGED
@@ -3,4 +3,51 @@ title: "Introduction"
3
3
  weight: 10
4
4
  ---
5
5
 
6
- >This document is under construction.
6
+ The AsyncAPI generator is a tool that generates anything you want using the **[AsyncAPI Document](generator/asyncapi-documents)** and **[Template](generator/template)** that are supplied as inputs to the AsyncAPI CLI. The generator was built with extensibility in mind; you can use the generator to generate anything you want, provided that it can be defined in a template, such as code, diagrams, markdown files, microservices, and applications. A number of [community-maintained templates](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) are now available for immediate usage.
7
+
8
+ ### Generator use cases
9
+ - Generation of interactive and understandable API documentation
10
+ - Generation of APIs' client libraries
11
+ - Generation of APIs' boilerplate code
12
+
13
+ ### Generator advantages
14
+ - Quick to setup and easy to use on a regular basis
15
+ - Effortless generation of complex documents
16
+ - Number of community maintained AsyncAPI templates
17
+
18
+ ### Generation process
19
+ 1. The **Generator** receives the **[Template](generator/template)** and **[AsyncAPI Document](generator/asyncapi-document)** as inputs.
20
+ 2. The **Generator** sends to the **[Parser](generator/parser)** the **asyncapiString** which is a stringified version of the original **AsyncAPI Document**.
21
+ 3. The **Parser** uses additional plugins such as the OpenAPI, RAML, or Avro schemas to validate custom schemas of message payloads defined in the **AsyncAPI Document**.
22
+ 4. If the **Parser** determines that the original **AsyncAPI Document** is valid, it manipulates the document and returns a set of helper functions and properties and bundles them together into an **asyncapi** variable that is an instance of [**AsyncAPIDocument**](https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument). The **asyncapi** helper functions make it easier to access the contents of the AsyncAPI Document.
23
+ 5. At this point, the **Generator** passes the **[asyncapi](generator/asyncapi-document#method-2-asyncapi-and-template)**, the **[originalAsyncAPI](generator/asyncapi-document#method-1-originalasyncapi-and-template)**, and the **params** which collectively make up the **[Template Context](generator/asyncapi-context)** to the **Render Engine**.
24
+ 6. AsyncAPI has two **Render Engines**([react](generator/react-render-engine) and [nunjucks](generator/nunjucks-render-engine). Depending on which one you've specified in your `package.json`, the **Generator** knows the right **Render Engine** to pass both the **Template Files** and the **Template Context**.
25
+ 7. Once the **Render Engine** receives the **Template Files** and the **Template Context**, it injects all the dynamic values in your react or nunjucks based **Template Files** using the **Template Context**. As a result, the **Render Engine** generates **markdown**, **pdf**, **boilerplate code**, and **anything else** you specified to be generated as output.
26
+
27
+ > You can generate anything you want using the generator as long as it can be defined in a **Template**.
28
+
29
+ The diagram below depicts the entire process of passing the **Template** and **AsyncAPI Document** to the AsyncAPI generator tool, how the generator uses these inputs to generate the desired output, and example outputs you can get from the render engine.
30
+
31
+ ``` mermaid
32
+ graph LR
33
+ A[Template Context]
34
+ B{Generator}
35
+ C[Parser]
36
+ D[Render Engine]
37
+ E[AsyncAPI Document] --> B
38
+ F[Template] --> B
39
+ subgraph Generator Library
40
+ B -->| asyncapiString | C
41
+ C --> | asyncapi | A
42
+ B--> | originalAsyncAPI | A
43
+ B--> | params | A
44
+ A --> D
45
+ B --> | Template Files | D
46
+ end
47
+ D --> O[HTML]
48
+ D --> M[Markdown]
49
+ D --> N[Node.js]
50
+ D --> J[Java Spring Boot]
51
+ D --> K[Anything else]
52
+ ```
53
+ **`params`** are template-specific options passed to the `asyncapi generate fromTemplate` CLI command to customize the generated output.
@@ -0,0 +1,81 @@
1
+ ---
2
+ title: "Installation guide"
3
+ weight: 20
4
+ ---
5
+
6
+ You can use the generator library to generate whatever you want in your event-driven architecture apps. Find your preferred method below:
7
+ - [AsyncAPI CLI](#asyncapi-cli)
8
+ - [Generator library in Node.js apps](#generator-library-in-nodejs-apps)
9
+
10
+ ## Prerequisites
11
+ Before you install and use the AsyncAPI CLI and the generator library, ensure you meet the prerequisites below, then [install the CLI](#installation).
12
+ 1. Node.js v12.16 and higher
13
+ 2. Npm v6.13.7 and higher
14
+
15
+ To verify the versions of Node and Npm you have, run the following command on your terminal:
16
+ ```
17
+ npm -v
18
+ ```
19
+ ```
20
+ node -v
21
+ ```
22
+
23
+ If you don't have either Node or Npm installed, use the [official node.js installer](https://nodejs.org/en/download/).
24
+
25
+ If you have the correct versions installed, proceed to the CLI installation guide below. Otherwise, upgrading the Npm or Node version is lower than the recommended versions specified above.
26
+
27
+ ## AsyncAPI CLI
28
+ The AsyncAPI CLI tool allows you to do many different things with the [AsyncAPI document](asyncapi-document). You can generate message-based API boilerplate code, documentation, or anything else you need as long as you specify it in your [template](template) or the existing template already supports it. To use the generator via the AsyncAPI CLI, you need to install the AsyncAPI CLI tool.
29
+
30
+ ### Installation
31
+
32
+ #### Install AsyncAPI CLI using NPM
33
+
34
+ The AsyncAPI CLI is a NodeJS project, so the easiest way to install it is by using the following `npm` command:
35
+ ```
36
+ npm install -g @asyncapi/cli
37
+ ```
38
+
39
+ To install a specific version of the generator tool, pass the version during installation:
40
+ ```
41
+ npm install -g @asyncapi/cli@{version}
42
+ ```
43
+
44
+ #### MacOS
45
+ You can install in MacOS by using brew: `brew install asyncapi`.
46
+
47
+ #### Linux
48
+ You can install in Linux by using `dpkg`, a package manager for debian:
49
+ 1. `curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.deb`
50
+ 2. `sudo dpkg -i asyncapi.deb`
51
+
52
+ #### Other operating systems
53
+ For further installation instructions for different operating systems, read the [AsyncAPI CLI documentation](https://github.com/asyncapi/cli#installation).
54
+
55
+ > **Remember:**
56
+ > Each [community-developed template](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) is dependent on a certain version of the generator for it to work correctly. Before you install the generator CLI, check the template's `package.json` for the version of the generator CLI your template is compatible with. Read the [versioning docs](versioning) to learn why it's important to use certain generator versions with your templates.
57
+
58
+ ### Update AsyncAPI CLI
59
+ There are several reasons why you might want to update your generator version:
60
+ * You have the generator tool installed but want to use the latest released features. To upgrade to the latest version, use the command below:
61
+ ```
62
+ npm install -g @asyncapi/cli
63
+ ```
64
+ * If your template isn't compatible with the latest generator version, you can update it to a specific version of the generator. Check the [version you need](https://github.com/asyncapi/cli/releases) and specify the version you want by using the **@** symbol as shown in the command below:
65
+ ```
66
+ npm install -g @asyncapi/cli@{version}
67
+ ```
68
+ > Sometimes you have to force additional npm installation like this: `npm install -g --force @asyncapi/cli`
69
+
70
+ ### Uninstall AsyncAPI CLI
71
+ To uninstall the generator, use the following command:
72
+ ```
73
+ npm uninstall @asyncapi/cli -g
74
+ ```
75
+
76
+ > :memo: **Note:** To use the generator in your CI/CD pipeline to automate whatever you generate for your event-driven architecture apps, install the AsyncAPI CLI in your pipeline. If you are using GitHub Actions, use [Github Actions for Generator](https://github.com/marketplace/actions/generator-for-asyncapi-documents).
77
+
78
+ ## Generator library in Node.js apps
79
+ Use the generator library in your Node.js projects by installing it via the following command: `npm install @asyncapi/generator`.
80
+
81
+ > Don't include the `-g` flag in the installation command above since you're not installing the generator library globally but in your Node.js project.
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "Nunjucks Render Engine"
2
+ title: "Nunjucks render engine"
3
3
  weight: 70
4
4
  ---
5
5
 
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "React Render Engine"
2
+ title: "React render engine"
3
3
  weight: 60
4
4
  ---
5
5
 
@@ -0,0 +1,41 @@
1
+ ---
2
+ title: "Template context"
3
+ weight: 60
4
+ ---
5
+
6
+ While using the generator tool, you may want dynamic values populated to your templates and rendered in the output. The generator can achieve that using the **template context**.
7
+ The **template context** allows you to access the contents of the [AsyncAPI document](asyncapi-document.md) and inject dynamic values to the template files passed to the asyncAPI CLI during the generation process. The render engine then displays these dynamically assigned values in the output.
8
+
9
+ ## Generation process
10
+ 1. The **Generator** receives **Template** and **params** as input.
11
+ 2. The **Generator** sends to the **Parser** the **asyncapiString** which is a stringified version of the original **AsyncAPI document**.
12
+ 3. The **Parser** validates the format of the **asyncapiString** using OpenAPI, RAML, or Avro schemas.
13
+ 4. If the **asyncapiString** is valid, the **parser** manipulates it, returns a set of helper functions and properties, and bundles them into an **asyncapi** variable. The **asyncapi** variable is an instance of the **AsyncAPI document**. The helper functions and properties make it easier to access the contents of the **AsyncAPI document** in the template.
14
+ 5. The **Generator** then passes the **params**, which are template-specific options used to customize the output, the **Template files**, and the **asyncapi** which collectively make up the **Template Context**.
15
+ 6. The **Template Context** is then passed to the **Render Engine**. The **Render Engine** then injects all the dynamic values from the **Template Context** into the **Template files**.
16
+
17
+ ``` mermaid
18
+ graph LR
19
+ A[Template Context]
20
+ B{Generator}
21
+ C[Parser]
22
+ D[Render Engine]
23
+ E[Template] --> B
24
+ F[Params] --> B
25
+ G[AsyncAPI Document] --> B
26
+ subgraph Generator
27
+ B -->| params | A
28
+ B--> | asyncapiString| C
29
+ B -->| originalAsyncAPI | A
30
+ C --> | asyncapi -> AsyncAPIDocument type | A
31
+ B--> | Template Files | D
32
+ A --> D
33
+ end
34
+ ```
35
+
36
+ ## Template context
37
+ The extra context passed to the render engine during the generation process and made accessible in the templates includes:
38
+
39
+ - **`originalAsyncAPI`** is a stringified version of the original AsyncAPI document that the user passed to the Generator.
40
+ - **`asyncapi`** is a parsed AsyncAPI document with helper functions and properties. You should use it to access document contents e.g `asyncapi.title`.
41
+ - **`params`** is an object with all the parameters passed to the Generator by the user.
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "TypeScript Support"
2
+ title: "TypeScript support"
3
3
  weight: 120
4
4
  ---
5
5
 
package/docs/usage.md ADDED
@@ -0,0 +1,151 @@
1
+ ---
2
+ title: "Usage"
3
+ weight: 30
4
+ ---
5
+
6
+ There are two ways to use the generator:
7
+ - [Generator CLI](#generator-cli)
8
+ - [Generator library](#using-as-a-modulepackage)
9
+
10
+ ## Generator CLI
11
+ ```bash
12
+ Usage: asyncapi generate fromTemplate <asyncapi> <template> [<options>]
13
+
14
+ - <asyncapi>: Local path or URL pointing to AsyncAPI document for example https://bit.ly/asyncapi
15
+ - <template>: Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template
16
+
17
+ - <options>:
18
+ -V, --version output the generator version
19
+ -d, --disable-hook [hooks...] disable a specific hook type or hooks from a given hook type
20
+ --debug enable more specific errors in the console
21
+ -i, --install install the template and its dependencies (defaults to false)
22
+ -n, --no-overwrite <glob> glob or path of the file(s) to skip when regenerating
23
+ -o, --output <outputDir> directory to put the generated files (defaults to current directory)
24
+ -p, --param <name=value> additional parameters to pass to templates
25
+ --force-write force writing of the generated files to a given directory even if it is a Git repository with unstaged files or not empty dir (defaults to false)
26
+ --watch-template watches the template directory and the AsyncAPI document, and re-generates the files when changes occur. Ignores the output directory. This flag should be used only for template development.
27
+ --map-base-url <url:folder> maps all schema references from base URL to local folder
28
+ -h, --help display help for command
29
+ ```
30
+
31
+ All templates are installable npm packages. Therefore, the value of `template` can be anything supported by `npm install`. Here's a summary of the possibilities:
32
+ ```
33
+ npm install [<@scope>/]<name>
34
+ npm install [<@scope>/]<name>@<tag>
35
+ npm install [<@scope>/]<name>@<version>
36
+ npm install [<@scope>/]<name>@<version range>
37
+ npm install <git-host>:<git-user>/<repo-name>
38
+ npm install <git repo url>
39
+ npm install <tarball file>
40
+ npm install <tarball url>
41
+ npm install <folder>
42
+ ```
43
+
44
+ ### Global templates installed with `yarn` or `npm`
45
+
46
+ You can preinstall templates globally before installing the generator CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
47
+
48
+ ```bash
49
+ npm install -g @asyncapi/html-template@0.16.0
50
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template
51
+ # The generator uses html-template version 0.16.0 and not the latest version.
52
+ ```
53
+
54
+ ### CLI usage examples
55
+
56
+ **The shortest possible syntax:**
57
+ ```bash
58
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template
59
+ ```
60
+
61
+ **Generating from a URL:**
62
+ ```bash
63
+ asyncapi generate fromTemplate https://bit.ly/asyncapi @asyncapi/html-template
64
+ ```
65
+
66
+ **Specify where to put the result:**
67
+ ```bash
68
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs
69
+ ```
70
+
71
+ **Passing parameters to templates:**
72
+ ```bash
73
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs -p title='Hello from param'
74
+ ```
75
+
76
+ In the template you can use it like this: ` {{ params.title }}`
77
+
78
+ **Disabling the hooks:**
79
+ ```bash
80
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs -d generate:before generate:after=foo,bar
81
+ ```
82
+
83
+ The generator skips all hooks of the `generate:before` type and `foo`, `bar` hooks of the `generate:after` type.
84
+
85
+ **Installing the template from a folder:**
86
+ ```bash
87
+ asyncapi generate fromTemplate asyncapi.yaml ~/my-template
88
+ ```
89
+
90
+ It creates a symbolic link to the target directory (`~/my-template` in this case).
91
+
92
+ **Installing the template from a git URL:**
93
+ ```bash
94
+ asyncapi generate fromTemplate asyncapi.yaml https://github.com/asyncapi/html-template.git
95
+ ```
96
+
97
+ **Map schema references from baseUrl to local folder:**
98
+ ```bash
99
+ asyncapi generate fromTemplate test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/:./test/docs/
100
+ ```
101
+
102
+ The parameter `--map-base-url` maps external schema references to local folders.
103
+
104
+ ### CLI usage with Docker
105
+
106
+ When using our docker image that we regularly update, you don't need to install Node.js or Npm, even though the generator is written with it since the Docker image has the generator installed.
107
+
108
+ Install [Docker](https://docs.docker.com/get-docker/) first, then use docker to pull and run the image using the following command:
109
+
110
+ ```bash
111
+ docker run --rm -it \
112
+ -v [ASYNCAPI SPEC FILE LOCATION]:/app/asyncapi.yml \
113
+ -v [GENERATED FILES LOCATION]:/app/output \
114
+ asyncapi/generator [COMMAND HERE]
115
+
116
+ # Example that you can run inside the generator directory after cloning this repository. First, you specify the mount in the location of your AsyncAPI specification file and then you mount it in the directory where the generation result should be saved.
117
+ docker run --rm -it \
118
+ -v ${PWD}/test/docs/dummy.yml:/app/asyncapi.yml \
119
+ -v ${PWD}/output:/app/output \
120
+ asyncapi/generator -o /app/output /app/asyncapi.yml @asyncapi/html-template --force-write
121
+ ```
122
+
123
+ ### CLI usage with `npx` instead of `npm`
124
+
125
+ [npx](https://www.npmjs.com/package/npx) is very useful when you want to run the generator in a CI/CD environment. In such a scenario, do not install the generator globally because most environments that provide Node.js and Npm, also provide npx out of the box.
126
+
127
+ Use the following npx command on your terminal:
128
+
129
+ ```bash
130
+ npx -p @asyncapi/cli asyncapi generate fromTemplate ./asyncapi.yaml @asyncapi/html-template
131
+ ```
132
+
133
+ ## Using as a module/package
134
+ Once you install the generator in your project, you can use it to generate whatever you want. The following code snippet is an example of HTML generation using the official `@asyncapi/html-template` template and fetching the spec document from the server using:
135
+ ```
136
+ https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml
137
+ ```
138
+
139
+ ```js
140
+ const path = require('path');
141
+ const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));
142
+
143
+ try {
144
+ await generator.generateFromURL('https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml');
145
+ console.log('Done!');
146
+ } catch (e) {
147
+ console.error(e);
148
+ }
149
+ ```
150
+
151
+ See the [API documentation](api.md) for more examples and full API reference information.
@@ -0,0 +1,31 @@
1
+ ---
2
+ title: "Generator version vs template version"
3
+ weight: 60
4
+ ---
5
+
6
+ The generator tool generates whatever you want, as long as it can be defined in a template based on the [AsyncAPI document](asyncapi-document). On the other hand, a **template** is a file or group of files that specify the kind of output you expect from using the generator's features. For example, you may want to use the [NodeJS template](https://github.com/asyncapi/nodejs-template) to generate boilerplate code for your message-based APIs.
7
+
8
+ Templates are dependent on the generators' features. For example, the template you want to use may be compatible with the latest generator version but incompatible with the previous versions. Check the configuration file or ReadME of the template to see the version of the generator it supports. The generator has an `isTemplateCompatible` function that checks if the template is compatible with the version of the generator you want to use. If the template isn't compatible, you will see a terminal error output similar to the following:
9
+ ```
10
+ Something went wrong:
11
+ Error: This template is not compatible with the current version of the generator (${generatorVersion}). This template is compatible with the following version range: ${generator}.`)
12
+ ```
13
+
14
+ > Use the following command to check the version of the AsyncAPI CLI you have installed; `asyncapi --version`
15
+
16
+ It is better to lock a specific version of the template and the generator if you plan to use the AsyncAPI CLI and a particular template in production. The differences between using the version of the AsyncAPI CLI you have installed and locking a certain version on production are demonstrated in the following code snippets.
17
+
18
+ Generate HTML with the latest AsyncAPI CLI using the html-template.
19
+ ```
20
+ npm install -g @asyncapi/cli
21
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs
22
+ ```
23
+
24
+ Generate HTML using a particular version of the AsyncAPI CLI using the html-template.
25
+
26
+ ```
27
+ npm install -g @asyncapi/cli@0.20.0
28
+ asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template@0.7.0 -o ./docs
29
+ ```
30
+
31
+ > Before using newer versions of the template, always look at the [changelog](https://github.com/asyncapi/html-template/releases) first. If the generator's features are not important to you, just make sure to use a version compatible with your template.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyncapi/generator",
3
- "version": "1.9.12",
3
+ "version": "1.9.14",
4
4
  "description": "The AsyncAPI generator. It can generate documentation, code, anything!",
5
5
  "main": "./lib/generator.js",
6
6
  "bin": {
@@ -50,7 +50,7 @@
50
50
  "@asyncapi/avro-schema-parser": "^1.1.0",
51
51
  "@asyncapi/generator-react-sdk": "^0.2.23",
52
52
  "@asyncapi/openapi-schema-parser": "^2.0.1",
53
- "@asyncapi/parser": "^1.17.0",
53
+ "@asyncapi/parser": "^1.17.1",
54
54
  "@asyncapi/raml-dt-schema-parser": "^2.0.1",
55
55
  "@npmcli/arborist": "^2.2.4",
56
56
  "ajv": "^6.10.2",
@@ -73,8 +73,8 @@
73
73
  "semver": "^7.3.2",
74
74
  "simple-git": "^3.3.0",
75
75
  "source-map-support": "^0.5.19",
76
- "ts-node": "^9.1.1",
77
- "typescript": "^4.2.2"
76
+ "ts-node": "^10.9.1",
77
+ "typescript": "^4.9.3"
78
78
  },
79
79
  "devDependencies": {
80
80
  "eslint": "^6.8.0",
@@ -1,6 +0,0 @@
1
- ---
2
- title: "AsyncAPI Context"
3
- weight: 80
4
- ---
5
-
6
- >This document is under construction.
@@ -1,6 +0,0 @@
1
- ---
2
- title: "AsyncAPI Specification File"
3
- weight: 20
4
- ---
5
-
6
- >This document is under construction.