@asyncapi/generator 1.10.7 → 1.10.9
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/asyncapi-document.md +21 -17
- package/docs/parser.md +30 -30
- package/package.json +4 -4
|
@@ -5,24 +5,25 @@ weight: 40
|
|
|
5
5
|
|
|
6
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
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.
|
|
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
9
|
|
|
10
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
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).
|
|
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
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
14
|
|
|
15
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
16
|
|
|
17
17
|
## AsyncAPI document generation process
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
1. The **Generator** receives the **AsyncAPI Document** as input.
|
|
19
20
|
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](
|
|
21
|
+
3. The **Parser** validates the **AsyncAPI Document** using additional schema-related plugins, either the OpenAPI schema, RAML data types, or Avro schema.
|
|
22
|
+
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).
|
|
23
|
+
5. At this point, the **Generator** passes the **originalAsyncAPI** and the **asyncapi** which make up part of the **[Template Context](template-context)** to the **Render Engine**.
|
|
23
24
|
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
|
-
```
|
|
25
|
+
|
|
26
|
+
```mermaid
|
|
26
27
|
graph LR
|
|
27
28
|
A[Template Context]
|
|
28
29
|
B{Generator}
|
|
@@ -35,14 +36,17 @@ graph LR
|
|
|
35
36
|
B--> | originalAsyncAPI -> Stringified document | A
|
|
36
37
|
A --> D
|
|
37
38
|
end
|
|
38
|
-
|
|
39
|
+
```
|
|
40
|
+
|
|
39
41
|
The AsyncAPI document's content is accessible to you while writing your template in two distinct ways:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
|
|
43
|
+
- The `originalAsyncAPI`, which is a stringified version of the AsyncAPI document provided as input, without any modifications.
|
|
44
|
+
- 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
45
|
|
|
43
46
|
In the following sections, you will learn how to use either the **originalAsyncAPI** or the **asyncapi** in your template.
|
|
44
47
|
|
|
45
|
-
### Method 1: `originalAsyncAPI` and template
|
|
48
|
+
### Method 1: `originalAsyncAPI` and template
|
|
49
|
+
|
|
46
50
|
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
51
|
|
|
48
52
|
```js
|
|
@@ -54,7 +58,7 @@ const path = require('path');
|
|
|
54
58
|
function createAsyncapiFile(generator) {
|
|
55
59
|
const asyncapi = generator.originalAsyncAPI;
|
|
56
60
|
let extension;
|
|
57
|
-
|
|
61
|
+
|
|
58
62
|
try {
|
|
59
63
|
JSON.parse(asyncapi);
|
|
60
64
|
extension = 'json';
|
|
@@ -69,9 +73,9 @@ function createAsyncapiFile(generator) {
|
|
|
69
73
|
fs.writeFileSync(asyncapiOutputLocation, asyncapi);
|
|
70
74
|
```
|
|
71
75
|
|
|
72
|
-
|
|
73
76
|
### Method 2: `asyncapi` and template
|
|
74
|
-
|
|
77
|
+
|
|
78
|
+
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
79
|
|
|
76
80
|
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
81
|
|
|
@@ -80,8 +84,8 @@ For example, if you want to extract the version of your API from AsyncAPI docume
|
|
|
80
84
|
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
85
|
|
|
82
86
|
```js
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
const apiName = asyncapi.info().title();
|
|
88
|
+
const channels = asyncapi.channels();
|
|
85
89
|
```
|
|
86
90
|
|
|
87
91
|
> 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)
|
package/docs/parser.md
CHANGED
|
@@ -5,9 +5,9 @@ weight: 60
|
|
|
5
5
|
|
|
6
6
|
## Parser
|
|
7
7
|
|
|
8
|
-
The AsyncAPI Parser is a package used to parse and validate the [AsyncAPI documents](asyncapi-document
|
|
8
|
+
The AsyncAPI Parser is a package used to parse and validate the [AsyncAPI documents](asyncapi-document) in your Node.js or browser application. These documents can be either in YAML or JSON format.
|
|
9
9
|
|
|
10
|
-
The Parser validates these documents using dedicated schema-supported plugins.
|
|
10
|
+
The Parser validates these documents using dedicated schema-supported plugins.
|
|
11
11
|
|
|
12
12
|
Supported schemas:
|
|
13
13
|
|
|
@@ -19,15 +19,15 @@ Supported schemas:
|
|
|
19
19
|
|
|
20
20
|
The Parser allows the template developer to easily access schemas provided in the above supported formats. This is because the JavaScript parser converts all of them into JSON schema.
|
|
21
21
|
|
|
22
|
-
If the document is valid, the Parser returns an `AsyncAPIDocument instance` with a set of helper functions that enable easier access to the contents of the AsyncAPI document. The parser provides dereferenced output. During the dereference process, the AsyncAPI parser substitutes a reference with a full definition. The dereferenced output is always in JSON format. The parser provides a message listing all errors if a document is invalid. The original AsyncAPI document is part of the [Template Context](template-context
|
|
22
|
+
If the document is valid, the Parser returns an `AsyncAPIDocument instance` with a set of helper functions that enable easier access to the contents of the AsyncAPI document. The parser provides dereferenced output. During the dereference process, the AsyncAPI parser substitutes a reference with a full definition. The dereferenced output is always in JSON format. The parser provides a message listing all errors if a document is invalid. The original AsyncAPI document is part of the [Template Context](template-context) as the generator also passes the original AsyncAPI document to the template context.
|
|
23
23
|
|
|
24
24
|
The following AsyncAPI document example has two channels: `channelOne` and `channelTwo`. Each channel has one operation and a single message:
|
|
25
25
|
|
|
26
26
|
```yaml
|
|
27
|
-
asyncapi:
|
|
27
|
+
asyncapi: "2.5.0"
|
|
28
28
|
info:
|
|
29
29
|
title: Demo API
|
|
30
|
-
version:
|
|
30
|
+
version: "1.0.0"
|
|
31
31
|
channels:
|
|
32
32
|
channelOne:
|
|
33
33
|
publish:
|
|
@@ -36,14 +36,14 @@ channels:
|
|
|
36
36
|
message:
|
|
37
37
|
name: FirstMessage
|
|
38
38
|
payload:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
id:
|
|
40
|
+
type: integer
|
|
41
|
+
minimum: 0
|
|
42
|
+
description: Id of the channel
|
|
43
|
+
sentAt:
|
|
44
|
+
type: string
|
|
45
|
+
format: date-time
|
|
46
|
+
description: Date and time when the message was sent.
|
|
47
47
|
channelTwo:
|
|
48
48
|
publish:
|
|
49
49
|
summary: This is the second sample channel
|
|
@@ -51,36 +51,37 @@ channels:
|
|
|
51
51
|
message:
|
|
52
52
|
name: SecondMessage
|
|
53
53
|
payload:
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
id:
|
|
55
|
+
type: integer
|
|
56
|
+
minimum: 0
|
|
57
|
+
description: Id of the channel
|
|
58
|
+
sentAt:
|
|
59
|
+
type: string
|
|
60
|
+
format: date-time
|
|
61
|
+
description: Date and time when the message was sent.
|
|
62
62
|
```
|
|
63
|
+
|
|
63
64
|
We can use helper functions provided by the Parser to operate on the above JSON file. For example, we can use the helper method `asyncAPIDocument.channelNames()`, which returns an array of all channel names currently present in the AsyncAPI document. Another example where you can use a helper function is to list out messages present in your JSON file. Instead of fetching a single message one at a time, you can use the function `asyncAPIDocument.allMessages()` that returns the map of all messages in your AsyncAPI document.
|
|
64
65
|
|
|
65
66
|
```js
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
const channelNames = asyncAPIDocument.channelNames();
|
|
68
|
+
const messages = asyncAPIDocument.allMessages();
|
|
68
69
|
```
|
|
69
70
|
|
|
70
|
-
> The Parser gives you access to a number of these [helper functions](https://github.com/asyncapi/parser-js/blob/master/API.md) that you can implement to access the contents of your AsyncAPI document.
|
|
71
|
+
> The Parser gives you access to a number of these [helper functions](https://github.com/asyncapi/parser-js/blob/master/API.md) that you can implement to access the contents of your AsyncAPI document.
|
|
71
72
|
|
|
72
73
|
## AsyncAPI document validation process
|
|
73
74
|
|
|
74
75
|
1. **AsyncAPI document** is fed as an input to the Generator.
|
|
75
76
|
1. Generator sends the AsyncAPI document to the Parser as **asyncapiString**; the stringified version of the original AsyncAPI document.
|
|
76
77
|
1. 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.
|
|
77
|
-
1. If the AsyncAPI document is invalid, it throws an error based on the encountered failure type. For example, if the AsyncAPI document is not a string nor a JavaScript object, the Parser throws an `invalid-document-type` error.
|
|
78
|
+
1. If the AsyncAPI document is invalid, it throws an error based on the encountered failure type. For example, if the AsyncAPI document is not a string nor a JavaScript object, the Parser throws an `invalid-document-type` error.
|
|
78
79
|
Similarly, you may encounter errors such as:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
- `invalid-json`
|
|
81
|
+
- `invalid-yaml`
|
|
82
|
+
- `impossible-to-convert-to-json`
|
|
82
83
|
1. If the document is valid, the Parser modifies the AsyncAPI document, returns a set of helper functions, and bundles them together into the **asyncapi** variable. These helper functions in the form of an **asyncapi** variable are passed to the **Template Context**.
|
|
83
|
-
1. The Template Context passes all of these values to the [**Render Engine**](react-render-engine
|
|
84
|
+
1. The Template Context passes all of these values to the [**Render Engine**](react-render-engine) of your choice. Finally, the Render Engine generates whatever output you may have specified in your template. (i.e. code, documentation, diagrams, pdfs, applications, etc.)
|
|
84
85
|
|
|
85
86
|
```mermaid
|
|
86
87
|
graph TD
|
|
@@ -93,4 +94,3 @@ graph TD
|
|
|
93
94
|
```
|
|
94
95
|
|
|
95
96
|
> To learn more about the Parser and access all of its features, check out the AsyncAPI [Parser’s GitHub repository](https://github.com/asyncapi/parser-js).
|
|
96
|
-
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asyncapi/generator",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.9",
|
|
4
4
|
"description": "The AsyncAPI generator. It can generate documentation, code, anything!",
|
|
5
5
|
"main": "./lib/generator.js",
|
|
6
6
|
"bin": {
|
|
@@ -48,11 +48,11 @@
|
|
|
48
48
|
"license": "Apache-2.0",
|
|
49
49
|
"homepage": "https://github.com/asyncapi/generator",
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@asyncapi/avro-schema-parser": "^3.0.
|
|
51
|
+
"@asyncapi/avro-schema-parser": "^3.0.2",
|
|
52
52
|
"@asyncapi/generator-react-sdk": "^0.2.23",
|
|
53
|
-
"@asyncapi/openapi-schema-parser": "^3.0.
|
|
53
|
+
"@asyncapi/openapi-schema-parser": "^3.0.3",
|
|
54
54
|
"@asyncapi/parser": "^2.0.3",
|
|
55
|
-
"@asyncapi/raml-dt-schema-parser": "^4.0.
|
|
55
|
+
"@asyncapi/raml-dt-schema-parser": "^4.0.3",
|
|
56
56
|
"@npmcli/arborist": "^2.2.4",
|
|
57
57
|
"ajv": "^8.12.0",
|
|
58
58
|
"chokidar": "^3.4.0",
|