@asyncapi/generator 1.9.13 → 1.9.15

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/docs/api.md CHANGED
@@ -1,3 +1,10 @@
1
+ ---
2
+ title: Library API
3
+ weight: 75
4
+ ---
5
+
6
+ Reference API documentation for AsyncAPI Generator library.
7
+
1
8
  <a name="Generator"></a>
2
9
 
3
10
  ## Generator
@@ -6,49 +13,49 @@
6
13
  * [Generator](#Generator)
7
14
  * [new Generator(templateName, targetDir, options)](#new_Generator_new)
8
15
  * _instance_
9
- * [.templateName](#Generator+templateName) : <code>String</code>
10
- * [.targetDir](#Generator+targetDir) : <code>String</code>
11
- * [.entrypoint](#Generator+entrypoint) : <code>String</code>
12
- * [.noOverwriteGlobs](#Generator+noOverwriteGlobs) : <code>Array.&lt;String&gt;</code>
13
- * [.disabledHooks](#Generator+disabledHooks) : <code>Object.&lt;String, (Boolean\|String\|Array.&lt;String&gt;)&gt;</code>
14
- * [.output](#Generator+output) : <code>String</code>
15
- * [.forceWrite](#Generator+forceWrite) : <code>Boolean</code>
16
- * [.debug](#Generator+debug) : <code>Boolean</code>
17
- * [.install](#Generator+install) : <code>Boolean</code>
18
- * [.templateConfig](#Generator+templateConfig) : <code>Object</code>
19
- * [.hooks](#Generator+hooks) : <code>Object</code>
20
- * [.mapBaseUrlToFolder](#Generator+mapBaseUrlToFolder) : <code>Object</code>
21
- * [.templateParams](#Generator+templateParams) : <code>Object</code>
22
- * [.originalAsyncAPI](#Generator+originalAsyncAPI) : <code>String</code>
23
- * [.generate(asyncapiDocument)](#Generator+generate) ⇒ <code>Promise</code>
16
+ * [.templateName](#Generator+templateName) : `String`
17
+ * [.targetDir](#Generator+targetDir) : `String`
18
+ * [.entrypoint](#Generator+entrypoint) : `String`
19
+ * [.noOverwriteGlobs](#Generator+noOverwriteGlobs) : `Array.<String>`
20
+ * [.disabledHooks](#Generator+disabledHooks) : `Object.<String, (Boolean|String|Array.<String>)>`
21
+ * [.output](#Generator+output) : `String`
22
+ * [.forceWrite](#Generator+forceWrite) : `Boolean`
23
+ * [.debug](#Generator+debug) : `Boolean`
24
+ * [.install](#Generator+install) : `Boolean`
25
+ * [.templateConfig](#Generator+templateConfig) : `Object`
26
+ * [.hooks](#Generator+hooks) : `Object`
27
+ * [.mapBaseUrlToFolder](#Generator+mapBaseUrlToFolder) : `Object`
28
+ * [.templateParams](#Generator+templateParams) : `Object`
29
+ * [.originalAsyncAPI](#Generator+originalAsyncAPI) : `String`
30
+ * [.generate(asyncapiDocument)](#Generator+generate) ⇒ `Promise`
24
31
  * [.configureTemplate()](#Generator+configureTemplate)
25
- * [.generateFromString(asyncapiString, [parserOptions])](#Generator+generateFromString) ⇒ <code>Promise</code>
26
- * [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ <code>Promise</code>
27
- * [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ <code>Promise</code>
32
+ * [.generateFromString(asyncapiString, [parserOptions])](#Generator+generateFromString) ⇒ `Promise`
33
+ * [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ `Promise`
34
+ * [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ `Promise`
28
35
  * [.installTemplate([force])](#Generator+installTemplate)
29
36
  * _static_
30
- * [.getTemplateFile(templateName, filePath, [templatesDir])](#Generator.getTemplateFile) ⇒ <code>Promise</code>
37
+ * [.getTemplateFile(templateName, filePath, [templatesDir])](#Generator.getTemplateFile) ⇒ `Promise`
38
+
31
39
 
32
40
  <a name="new_Generator_new"></a>
33
41
 
34
- ### new Generator(templateName, targetDir, options)
42
+ ### new Generator
35
43
  Instantiates a new Generator object.
36
44
 
37
-
38
- | Param | Type | Default | Description |
39
- | --- | --- | --- | --- |
40
- | templateName | <code>String</code> | | Name of the template to generate. |
41
- | targetDir | <code>String</code> | | Path to the directory where the files will be generated. |
42
- | options | <code>Object</code> | | |
43
- | [options.templateParams] | <code>String</code> | | Optional parameters to pass to the template. Each template define their own params. |
44
- | [options.entrypoint] | <code>String</code> | | Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template. |
45
- | [options.noOverwriteGlobs] | <code>Array.&lt;String&gt;</code> | | List of globs to skip when regenerating the template. |
46
- | [options.disabledHooks] | <code>Object.&lt;String, (Boolean\|String\|Array.&lt;String&gt;)&gt;</code> | | Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array. |
47
- | [options.output] | <code>String</code> | <code>&#x27;fs&#x27;</code> | Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set. |
48
- | [options.forceWrite] | <code>Boolean</code> | <code>false</code> | Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false. |
49
- | [options.install] | <code>Boolean</code> | <code>false</code> | Install the template and its dependencies, even when the template has already been installed. |
50
- | [options.debug] | <code>Boolean</code> | <code>false</code> | Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive. |
51
- | [options.mapBaseUrlToFolder] | <code>Object.&lt;String, String&gt;</code> | | Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ . |
45
+ **Params**
46
+
47
+ - templateName `String` - Name of the template to generate.
48
+ - targetDir `String` - Path to the directory where the files will be generated.
49
+ - options `Object`
50
+ - [.templateParams] `String` - Optional parameters to pass to the template. Each template define their own params.
51
+ - [.entrypoint] `String` - Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.
52
+ - [.noOverwriteGlobs] `Array.<String>` - List of globs to skip when regenerating the template.
53
+ - [.disabledHooks] `Object.<String, (Boolean|String|Array.<String>)>` - Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.
54
+ - [.output] `String` ` = 'fs'` - Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.
55
+ - [.forceWrite] `Boolean` ` = false` - Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
56
+ - [.install] `Boolean` ` = false` - Install the template and its dependencies, even when the template has already been installed.
57
+ - [.debug] `Boolean` ` = false` - Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
58
+ - [.mapBaseUrlToFolder] `Object.<String, String>` - Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ .
52
59
 
53
60
  **Example**
54
61
  ```js
@@ -64,100 +71,114 @@ const generator = new Generator('@asyncapi/html-template', path.resolve(__dirnam
64
71
  }
65
72
  });
66
73
  ```
74
+
67
75
  <a name="Generator+templateName"></a>
68
76
 
69
- ### generator.templateName : <code>String</code>
77
+ * generator.templateName : `String`** :
70
78
  Name of the template to generate.
71
79
 
72
- **Kind**: instance property of [<code>Generator</code>](#Generator)
80
+ **Kind**: instance property of [`Generator`](#Generator)
81
+
73
82
  <a name="Generator+targetDir"></a>
74
83
 
75
- ### generator.targetDir : <code>String</code>
84
+ * generator.targetDir : `String`** :
76
85
  Path to the directory where the files will be generated.
77
86
 
78
- **Kind**: instance property of [<code>Generator</code>](#Generator)
87
+ **Kind**: instance property of [`Generator`](#Generator)
88
+
79
89
  <a name="Generator+entrypoint"></a>
80
90
 
81
- ### generator.entrypoint : <code>String</code>
91
+ * generator.entrypoint : `String`** :
82
92
  Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.
83
93
 
84
- **Kind**: instance property of [<code>Generator</code>](#Generator)
94
+ **Kind**: instance property of [`Generator`](#Generator)
95
+
85
96
  <a name="Generator+noOverwriteGlobs"></a>
86
97
 
87
- ### generator.noOverwriteGlobs : <code>Array.&lt;String&gt;</code>
98
+ * generator.noOverwriteGlobs : `Array.<String>`** :
88
99
  List of globs to skip when regenerating the template.
89
100
 
90
- **Kind**: instance property of [<code>Generator</code>](#Generator)
101
+ **Kind**: instance property of [`Generator`](#Generator)
102
+
91
103
  <a name="Generator+disabledHooks"></a>
92
104
 
93
- ### generator.disabledHooks : <code>Object.&lt;String, (Boolean\|String\|Array.&lt;String&gt;)&gt;</code>
105
+ * generator.disabledHooks : `Object.<String, (Boolean|String|Array.<String>)>`** :
94
106
  Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.
95
107
 
96
- **Kind**: instance property of [<code>Generator</code>](#Generator)
108
+ **Kind**: instance property of [`Generator`](#Generator)
109
+
97
110
  <a name="Generator+output"></a>
98
111
 
99
- ### generator.output : <code>String</code>
112
+ * generator.output : `String`** :
100
113
  Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.
101
114
 
102
- **Kind**: instance property of [<code>Generator</code>](#Generator)
115
+ **Kind**: instance property of [`Generator`](#Generator)
116
+
103
117
  <a name="Generator+forceWrite"></a>
104
118
 
105
- ### generator.forceWrite : <code>Boolean</code>
119
+ * generator.forceWrite : `Boolean`** :
106
120
  Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
107
121
 
108
- **Kind**: instance property of [<code>Generator</code>](#Generator)
122
+ **Kind**: instance property of [`Generator`](#Generator)
123
+
109
124
  <a name="Generator+debug"></a>
110
125
 
111
- ### generator.debug : <code>Boolean</code>
126
+ * generator.debug : `Boolean`** :
112
127
  Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
113
128
 
114
- **Kind**: instance property of [<code>Generator</code>](#Generator)
129
+ **Kind**: instance property of [`Generator`](#Generator)
130
+
115
131
  <a name="Generator+install"></a>
116
132
 
117
- ### generator.install : <code>Boolean</code>
133
+ * generator.install : `Boolean`** :
118
134
  Install the template and its dependencies, even when the template has already been installed.
119
135
 
120
- **Kind**: instance property of [<code>Generator</code>](#Generator)
136
+ **Kind**: instance property of [`Generator`](#Generator)
137
+
121
138
  <a name="Generator+templateConfig"></a>
122
139
 
123
- ### generator.templateConfig : <code>Object</code>
140
+ * generator.templateConfig : `Object`** :
124
141
  The template configuration.
125
142
 
126
- **Kind**: instance property of [<code>Generator</code>](#Generator)
143
+ **Kind**: instance property of [`Generator`](#Generator)
144
+
127
145
  <a name="Generator+hooks"></a>
128
146
 
129
- ### generator.hooks : <code>Object</code>
147
+ * generator.hooks : `Object`** :
130
148
  Hooks object with hooks functions grouped by the hook type.
131
149
 
132
- **Kind**: instance property of [<code>Generator</code>](#Generator)
150
+ **Kind**: instance property of [`Generator`](#Generator)
151
+
133
152
  <a name="Generator+mapBaseUrlToFolder"></a>
134
153
 
135
- ### generator.mapBaseUrlToFolder : <code>Object</code>
154
+ * generator.mapBaseUrlToFolder : `Object`** :
136
155
  Maps schema URL to folder.
137
156
 
138
- **Kind**: instance property of [<code>Generator</code>](#Generator)
157
+ **Kind**: instance property of [`Generator`](#Generator)
158
+
139
159
  <a name="Generator+templateParams"></a>
140
160
 
141
- ### generator.templateParams : <code>Object</code>
161
+ * generator.templateParams : `Object`** :
142
162
  The template parameters. The structure for this object is based on each individual template.
143
163
 
144
- **Kind**: instance property of [<code>Generator</code>](#Generator)
164
+ **Kind**: instance property of [`Generator`](#Generator)
165
+
145
166
  <a name="Generator+originalAsyncAPI"></a>
146
167
 
147
- ### generator.originalAsyncAPI : <code>String</code>
168
+ * generator.originalAsyncAPI : `String`** :
148
169
  AsyncAPI string to use as a source.
149
170
 
150
- **Kind**: instance property of [<code>Generator</code>](#Generator)
171
+ **Kind**: instance property of [`Generator`](#Generator)
172
+
151
173
  <a name="Generator+generate"></a>
152
174
 
153
- ### generator.generate(asyncapiDocument) ⇒ <code>Promise</code>
175
+ ### generator.generate
154
176
  Generates files from a given template and an AsyncAPIDocument object.
155
177
 
156
- **Kind**: instance method of [<code>Generator</code>](#Generator)
178
+ **Kind**: instance method of [`Generator`](#Generator)
179
+ **Params**
157
180
 
158
- | Param | Type | Description |
159
- | --- | --- | --- |
160
- | asyncapiDocument | <code>AsyncAPIDocument</code> | AsyncAPIDocument object to use as source. |
181
+ - asyncapiDocument `AsyncAPIDocument` - AsyncAPIDocument object to use as source.
161
182
 
162
183
  **Example**
163
184
  ```js
@@ -177,23 +198,24 @@ try {
177
198
  console.error(e);
178
199
  }
179
200
  ```
201
+
180
202
  <a name="Generator+configureTemplate"></a>
181
203
 
182
- ### generator.configureTemplate()
204
+ * generator.configureTemplate()** :
183
205
  Configure the templates based the desired renderer.
184
206
 
185
- **Kind**: instance method of [<code>Generator</code>](#Generator)
207
+ **Kind**: instance method of [`Generator`](#Generator)
208
+
186
209
  <a name="Generator+generateFromString"></a>
187
210
 
188
- ### generator.generateFromString(asyncapiString, [parserOptions]) ⇒ <code>Promise</code>
211
+ ### generator.generateFromString
189
212
  Generates files from a given template and AsyncAPI string.
190
213
 
191
- **Kind**: instance method of [<code>Generator</code>](#Generator)
214
+ **Kind**: instance method of [`Generator`](#Generator)
215
+ **Params**
192
216
 
193
- | Param | Type | Default | Description |
194
- | --- | --- | --- | --- |
195
- | asyncapiString | <code>String</code> | | AsyncAPI string to use as source. |
196
- | [parserOptions] | <code>Object</code> | <code>{}</code> | AsyncAPI parser options. Check out [@asyncapi/parser](https://www.github.com/asyncapi/parser-js) for more information. |
217
+ - asyncapiString `String` - AsyncAPI string to use as source.
218
+ - [parserOptions] `Object` ` = {}` - AsyncAPI parser options. Check out [@asyncapi/parser](https://www.github.com/asyncapi/parser-js) for more information.
197
219
 
198
220
  **Example**
199
221
  ```js
@@ -228,16 +250,16 @@ try {
228
250
  console.error(e);
229
251
  }
230
252
  ```
253
+
231
254
  <a name="Generator+generateFromURL"></a>
232
255
 
233
- ### generator.generateFromURL(asyncapiURL) ⇒ <code>Promise</code>
256
+ ### generator.generateFromURL
234
257
  Generates files from a given template and AsyncAPI file stored on external server.
235
258
 
236
- **Kind**: instance method of [<code>Generator</code>](#Generator)
259
+ **Kind**: instance method of [`Generator`](#Generator)
260
+ **Params**
237
261
 
238
- | Param | Type | Description |
239
- | --- | --- | --- |
240
- | asyncapiURL | <code>String</code> | Link to AsyncAPI file |
262
+ - asyncapiURL `String` - Link to AsyncAPI file
241
263
 
242
264
  **Example**
243
265
  ```js
@@ -257,16 +279,16 @@ try {
257
279
  console.error(e);
258
280
  }
259
281
  ```
282
+
260
283
  <a name="Generator+generateFromFile"></a>
261
284
 
262
- ### generator.generateFromFile(asyncapiFile) ⇒ <code>Promise</code>
285
+ ### generator.generateFromFile
263
286
  Generates files from a given template and AsyncAPI file.
264
287
 
265
- **Kind**: instance method of [<code>Generator</code>](#Generator)
288
+ **Kind**: instance method of [`Generator`](#Generator)
289
+ **Params**
266
290
 
267
- | Param | Type | Description |
268
- | --- | --- | --- |
269
- | asyncapiFile | <code>String</code> | AsyncAPI file to use as source. |
291
+ - asyncapiFile `String` - AsyncAPI file to use as source.
270
292
 
271
293
  **Example**
272
294
  ```js
@@ -286,37 +308,37 @@ try {
286
308
  console.error(e);
287
309
  }
288
310
  ```
311
+
289
312
  <a name="Generator+installTemplate"></a>
290
313
 
291
- ### generator.installTemplate([force])
314
+ ### generator.installTemplate
292
315
  Downloads and installs a template and its dependencies
293
316
 
294
- **Kind**: instance method of [<code>Generator</code>](#Generator)
317
+ **Kind**: instance method of [`Generator`](#Generator)
318
+ **Params**
319
+
320
+ - [force] `Boolean` ` = false` - Whether to force installation (and skip cache) or not.
295
321
 
296
- | Param | Type | Default | Description |
297
- | --- | --- | --- | --- |
298
- | [force] | <code>Boolean</code> | <code>false</code> | Whether to force installation (and skip cache) or not. |
299
322
 
300
323
  <a name="Generator.getTemplateFile"></a>
301
324
 
302
- ### Generator.getTemplateFile(templateName, filePath, [templatesDir]) ⇒ <code>Promise</code>
325
+ ### Generator.getTemplateFile
303
326
  Returns the content of a given template file.
304
327
 
305
- **Kind**: static method of [<code>Generator</code>](#Generator)
328
+ **Kind**: static method of [`Generator`](#Generator)
329
+ **Params**
306
330
 
307
- | Param | Type | Default | Description |
308
- | --- | --- | --- | --- |
309
- | templateName | <code>String</code> | | Name of the template to generate. |
310
- | filePath | <code>String</code> | | Path to the file to render. Relative to the template directory. |
311
- | [templatesDir] | <code>String</code> | <code>DEFAULT_TEMPLATES_DIR</code> | Path to the directory where the templates are installed. |
331
+ - templateName `String` - Name of the template to generate.
332
+ - filePath `String` - Path to the file to render. Relative to the template directory.
333
+ - [templatesDir] `String` ` = DEFAULT_TEMPLATES_DIR` - Path to the directory where the templates are installed.
312
334
 
313
335
  **Example**
314
336
  ```js
315
- const Generator = require('asyncapi-generator');
337
+ const Generator = require('@asyncapi/generator');
316
338
  const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html');
317
339
  ```
318
340
  **Example** *(Using a custom &#x60;templatesDir&#x60;)*
319
341
  ```js
320
- const Generator = require('asyncapi-generator');
342
+ const Generator = require('@asyncapi/generator');
321
343
  const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');
322
344
  ```
@@ -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,6 +1,6 @@
1
1
  ---
2
- title: "Configuration File"
3
- weight: 30
2
+ title: "Configuration file"
3
+ weight: 90
4
4
  ---
5
5
 
6
6
  The `generator` property from `package.json` file must contain a JSON object that may have the following information:
@@ -17,7 +17,7 @@ The `generator` property from `package.json` file must contain a JSON object tha
17
17
  |`conditionalFiles[filePath].subject`| String | The `subject` is a [JMESPath](http://jmespath.org/) query to grab the value you want to apply the condition to. It queries an object with the whole AsyncAPI document and, when specified, the given server. The object looks like this: `{ asyncapi: { ... }, server: { ... } }`. If the template supports `server` parameter, you can access server details like for example protocol this way: `server.protocol`. During validation with `conditionalFiles` only the server that template user selected is available in the specification file. For more information about `server` parameter [read about special parameters](#special-parameters).
18
18
  |`conditionalFiles[filePath].validation`| Object | The `validation` is a JSON Schema Draft 07 object. This JSON Schema definition will be applied to the JSON value resulting from the `subject` query. If validation doesn't have errors, the condition is met, and therefore the given file will be rendered. Otherwise, the file is ignored. Check [JSON Schema Validation](https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6) document for a list of all possible validation keywords.
19
19
  |`nonRenderableFiles`| [String] | A list of file paths or [globs](https://en.wikipedia.org/wiki/Glob_(programming)) that must be copied "as-is" to the target directory, i.e., without performing any rendering process. This is useful when you want to copy binary files.
20
- |`generator`| [String] | A string representing the Generator version-range the template is compatible with. This value must follow the [semver](https://nodejs.dev/learn/semantic-versioning-using-npm) syntax. E.g., `>=1.0.0`, `>=1.0.0 <=2.0.0`, `~1.0.0`, `^1.0.0`, `1.0.0`, etc. [Read more about semver](https://docs.npmjs.com/about-semantic-versioning).
20
+ |`generator`| [String] | A string representing the generator version-range the template is compatible with. This value must follow the [semver](https://nodejs.dev/learn/semantic-versioning-using-npm) syntax. E.g., `>=1.0.0`, `>=1.0.0 <=2.0.0`, `~1.0.0`, `^1.0.0`, `1.0.0`, etc. [Read more about semver](https://docs.npmjs.com/about-semantic-versioning).
21
21
  |`filters`| [String] | A list of modules containing functions that can be used as Nunjucks filters. In case of external modules, remember they need to be added as a dependency in `package.json` of your template.
22
22
  |`hooks`| Object[String, String] or Object[String, Array[String]] | A list of modules containing hooks, except for the ones you keep locally in your template in default location. For each module you must specify the exact name of the hook that should be used in the template. For a single hook you can specify it as a string, for more you must pass an array of strings. In case of external modules, remember they need to be added as a dependency in `package.json` of your template.
23
23
 
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  title: "File templates"
3
- weight: 110
3
+ weight: 140
4
4
  ---
5
5
 
6
6
  It is possible to generate files for each specific object in your AsyncAPI documentation. For example, you can specify a filename like `$$channel$$.js` to generate a file for each channel defined in your AsyncAPI. The following file-template names and extra variables in them are available:
package/docs/hooks.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  title: "Hooks"
3
- weight: 100
3
+ weight: 130
4
4
  ---
5
5
 
6
6
  Hooks are functions called by the generator on a specific moment in the generation process. Hooks can be anonymous functions but you can also add function names. These hooks can have arguments provided to them or being expected to return a value.
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.