@asyncapi/generator 1.17.0 → 1.17.2

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
@@ -13,6 +13,7 @@ Reference API documentation for AsyncAPI Generator library.
13
13
  * [Generator](#Generator)
14
14
  * [new Generator(templateName, targetDir, options)](#new_Generator_new)
15
15
  * _instance_
16
+ * [.registry](#Generator+registry) : `Object`
16
17
  * [.templateName](#Generator+templateName) : `String`
17
18
  * [.targetDir](#Generator+targetDir) : `String`
18
19
  * [.entrypoint](#Generator+entrypoint) : `String`
@@ -64,6 +65,10 @@ Instantiates a new Generator object.
64
65
  - [.install] `Boolean` ` = false` - Install the template and its dependencies, even when the template has already been installed.
65
66
  - [.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.
66
67
  - [.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/ .
68
+ - [.registry] `Object` - Optional parameter with private registry configuration
69
+ - [.url] `String` - Parameter to pass npm registry url
70
+ - [.auth] `String` - Optional parameter to pass npm registry username and password encoded with base64, formatted like username:password value should be encoded
71
+ - [.token] `String` - Optional parameter to pass npm registry auth token that you can grab from .npmrc file
67
72
 
68
73
  **Example**
69
74
  ```js
@@ -80,6 +85,13 @@ const generator = new Generator('@asyncapi/html-template', path.resolve(__dirnam
80
85
  });
81
86
  ```
82
87
 
88
+ <a name="Generator+registry"></a>
89
+
90
+ * generator.registry : `Object`** :
91
+ Npm registry information.
92
+
93
+ **Kind**: instance property of [`Generator`](#Generator)
94
+
83
95
  <a name="Generator+templateName"></a>
84
96
 
85
97
  * generator.templateName : `String`** :
package/docs/index.md CHANGED
@@ -5,6 +5,9 @@ weight: 10
5
5
 
6
6
  The AsyncAPI generator is a tool that generates anything you want using the **[AsyncAPI Document](generator/asyncapi-document)** 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
7
 
8
+ > **Note:**
9
+ > If your primary objective is to generate models/classes for your event-driven architecture apps, use [AsyncAPI Modelina](/docs/tools/generator/model-generation), which is supported in the AsyncAPI CLI, instead of using the AsyncAPI Generator. Modelina is specifically designed for model generation and provides utilities for working with the AsyncAPI document.
10
+
8
11
  ### Generator use cases
9
12
  - Generation of interactive and understandable API documentation
10
13
  - Generation of APIs' client libraries
@@ -53,7 +53,7 @@ You can install in Linux by using `dpkg`, a package manager for debian:
53
53
  For further installation instructions for different operating systems, read the [AsyncAPI CLI documentation](https://github.com/asyncapi/cli#installation).
54
54
 
55
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.
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 AsyncAPI CLI, check the template's `package.json` for the version of the AsyncAPI 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
57
 
58
58
  ### Update AsyncAPI CLI
59
59
  There are several reasons why you might want to update your generator version:
@@ -0,0 +1,84 @@
1
+ ---
2
+ title: "Adding models generation in template"
3
+ weight: 200
4
+ ---
5
+
6
+ This guide will walk you through the process of enabling models/types generation in a template by using [Modelina](https://www.asyncapi.com/tools/modelina).
7
+
8
+ Modelina is an AsyncAPI library designed for generating data models using inputs such as [AsyncAPI](generator/asyncapi-document), OpenAPI, or JSON schema inputs. Its functionality revolves around creating data models from the provided AsyncAPI document and the model template, which defines message payloads. It is better to use Modelina in your template to handle model generation rather than providing custom templates.
9
+
10
+ You can integrate the work shown in this guide into a template by following the [tutorial about creating a template](https://www.asyncapi.com/docs/tools/generator/generator-template).
11
+
12
+ In this guide, you'll learn how to use Modelina in a template code to enable support for Python data model generation.
13
+
14
+ ## Add Modelina dependency
15
+
16
+ Install Modelina in your project using npm: `npm install --save @asyncapi/modelina`.
17
+
18
+ Ensure your template's `package.json` file now contains Modelina pointing to its latest version:
19
+
20
+ ```json
21
+ "dependencies": {
22
+ // ...
23
+ "@asyncapi/modelina": "^2.0.5"
24
+ // ...
25
+ }
26
+ ```
27
+
28
+ ## Create a models.js file
29
+
30
+ Create a new directory in the **template** directory named **src/models** and create a **models.js** file within it. In the **models.js** file, add the following code:
31
+
32
+ ```python
33
+ // 1
34
+ import { File } from '@asyncapi/generator-react-sdk';
35
+ // 2
36
+ import { PythonGenerator, FormatHelpers } from '@asyncapi/modelina';
37
+
38
+ /**
39
+ * @typedef RenderArgument
40
+ * @type {object}
41
+ * @property {AsyncAPIDocument} asyncapi document object received from the generator.
42
+ */
43
+
44
+ /**
45
+ * Render all schema models
46
+ * @param {RenderArgument} param0
47
+ * @returns
48
+ */
49
+ // 3
50
+ export default async function schemaRender({ asyncapi }) {
51
+ // 4
52
+ const pythonGenerator = new PythonGenerator();
53
+ // 5
54
+ const models = await pythonGenerator.generate(asyncapi);
55
+ // 6
56
+ const files = [];
57
+ // 7
58
+ for (const model of models) {
59
+ // 8
60
+ const modelFileName = `${FormatHelpers.toPascalCase(model.modelName)}.py`;
61
+ // 9
62
+ files.push(<File name={modelFileName}>{model.result}</File>);
63
+ }
64
+ return files;
65
+ }
66
+ ```
67
+
68
+ Let's break it down. The code snippet above does the following:
69
+
70
+ 1. The `File` component from the [generator react SDK](https://github.com/asyncapi/generator-react-sdk) is needed to handle the further rendering of generated models into files.
71
+ 2. The `PythonGenerator` generator is the core needed for model generation. Additionally, you can import [FormatHelpers](https://github.com/asyncapi/modelina/blob/master/src/helpers/FormatHelpers.ts) that provides a set of helpers making it easier to modify model names to match your required case.
72
+ 3. You can change the name `schemaRender` to anything else like `modelRenderer`. More importantly, this must be an `async` function and a default export. This function is invoked during generation process and should contain the logic behind models generation.
73
+ 4. First, create an instance of the `PythonGenerator` model generator. If you decide to use present functionality from Modelina, you need to pass your presets here during instance creation.
74
+ 5. The actual model generation is one line of code, and as a result you get an array of models that later you need to turn into files.
75
+ 6. You need to define an array that must be returned from `schemaRender` function. The array must contain React components, and in this case, the `<File>` component.
76
+ 7. Iterate over generated models and use their content to create proper definitions of `<File>` components.
77
+ 8. Notice how using Modelina helpers, in this case the `toPascalCase` function, let's you make sure that the filename of your model follows specific case pattern.
78
+ 9. Each component must be added into the `files` array that you later return from the default function. Notice the definition of the `<File>` component that enables you to provide the name of resulting file and the content of the model. Notice also `model.result` that shows that initially generated array with models did not contain raw models content but a set of output objects that contain not only `result` but also other info, like for example `modelName`.
79
+
80
+ With such a model template that uses Modelina, as a result of generation process you would receive a set of model files in `$OUTPUT_DIR/src/models` directory.
81
+
82
+ ## Conclusion
83
+
84
+ Modelina provides a flexible and powerful way to generate data models from AsyncAPI, OpenAPI, or JSON Schema documents. By integrating Modelina you can much faster enable models generation in your template.
@@ -2,6 +2,8 @@
2
2
  title: "Template development"
3
3
  weight: 80
4
4
  ---
5
+ > **Note**
6
+ > It is advised against attempting to manually template types and models from scratch using the AsyncAPI templating engines such as Nunjucks and React render engines. Instead, it is recommended to use [AsyncAPI Modelina](/docs/tools/generator/model-generation) a dedicated library for model generation.
5
7
 
6
8
  ## Minimum template requirements
7
9
 
@@ -130,7 +132,7 @@ Newer:
130
132
  <Text>Version is: **{params.version || asyncapi.info.version()}**</Text>
131
133
  ```
132
134
 
133
- Now that you have added all the configuration options, you can start the generation process using the generator CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
135
+ Now that you have added all the configuration options, you can start the generation process using the AsyncAPI CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
134
136
  The above configuration helps template users override the existing version with a new version on the command line. (Example: `-p version=2.0.0`)
135
137
 
136
138
  ## Hooks
package/docs/template.md CHANGED
@@ -24,7 +24,7 @@ You can store template projects on a local drive or as a `git` repository during
24
24
 
25
25
  1. Template is provided as input to the **Generator**.
26
26
  2. **asyncapi** is the original AsyncAPI document injected into your template file by default.
27
- 3. **params** are the parameters you pass to the generator CLI. Later, you can also pass these **params** further to other components.
27
+ 3. **params** are the parameters you pass to the AsyncAPI CLI. Later, you can also pass these **params** further to other components.
28
28
  4. The generator passes both the original **asyncapi**, the original AsyncAPI document, and the **params** to the **Template Context**.
29
29
  5. Concurrently, the generator passes **Template files** to the **Render engine** as well. AsyncAPI uses two render engines — _react_ and _nunjucks_.
30
30
  6. Once the Render Engine receives both the Template Files and the Template Context, it injects all the dynamic values into your react or nunjucks engine, based on the Template Files using the Template Context.
package/docs/usage.md CHANGED
@@ -4,10 +4,10 @@ weight: 30
4
4
  ---
5
5
 
6
6
  There are two ways to use the generator:
7
- - [Generator CLI](#generator-cli)
7
+ - [AsyncAPI CLI](#generator-cli)
8
8
  - [Generator library](#using-as-a-modulepackage)
9
9
 
10
- ## Generator CLI
10
+ ## AsyncAPI CLI
11
11
  ```bash
12
12
  Usage: asyncapi generate fromTemplate <asyncapi> <template> [<options>]
13
13
 
@@ -43,7 +43,7 @@ npm install <folder>
43
43
 
44
44
  ### Global templates installed with `yarn` or `npm`
45
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.
46
+ You can preinstall templates globally before installing the [AsyncAPI CLI](https://www.asyncapi.com/docs/tools/cli). The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
47
47
 
48
48
  ```bash
49
49
  npm install -g @asyncapi/html-template@0.16.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyncapi/generator",
3
- "version": "1.17.0",
3
+ "version": "1.17.2",
4
4
  "description": "The AsyncAPI generator. It can generate documentation, code, anything!",
5
5
  "main": "./lib/generator.js",
6
6
  "bin": {
@@ -48,8 +48,8 @@
48
48
  "license": "Apache-2.0",
49
49
  "homepage": "https://github.com/asyncapi/generator",
50
50
  "dependencies": {
51
- "@asyncapi/generator-react-sdk": "^1.0.6",
52
- "@asyncapi/parser": "^3.0.2",
51
+ "@asyncapi/generator-react-sdk": "^1.0.7",
52
+ "@asyncapi/parser": "^3.0.3",
53
53
  "@npmcli/arborist": "5.6.3",
54
54
  "@smoya/multi-parser": "^5.0.0",
55
55
  "ajv": "^8.12.0",