@asyncapi/generator 1.11.0 → 1.12.0

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/CODEOWNERS CHANGED
@@ -9,4 +9,4 @@
9
9
  * @derberg @magicmatatjahu @jonaslagoni @asyncapi-bot-eve
10
10
 
11
11
  # All .md files
12
- *.md @Florence-Njeri @pratik2315 @asyncapi-bot-eve
12
+ *.md @Florence-Njeri @derberg @asyncapi-bot-eve
@@ -8,7 +8,7 @@ The `generator` property from `package.json` file must contain a JSON object tha
8
8
  |Name|Type|Description|
9
9
  |---|---|---|
10
10
  |`renderer`| String | Its value can be either `react` or `nunjucks` (default).
11
- |`apiVersion`| String | Determines which **major** version of the [Parser-API](https://github.com/asyncapi/parser-api) the template uses. For example, `v1` for `v1.x.x`. If not specified, the Generator assumes the template is not compatible with the Parser-API so it will use the [Parser-JS v1 API](https://github.com/asyncapi/parser-js/tree/v1.18.1#api-documentation). If the template uses a version of the Parser-API that is not supported by the Generator, the Generator will throw an error.
11
+ |`apiVersion`| String | Determines which **major** version of the [Parser-API](https://github.com/asyncapi/parser-api) the template uses. For example, `v2` for `v2.x.x`. If not specified, the Generator assumes the template is not compatible with the Parser-API so it will use the [Parser-JS v1 API](https://github.com/asyncapi/parser-js/tree/v1.18.1#api-documentation). For templates that need to support AsyncAPI specification v3 make sure to use `v2` [Parser-API](https://github.com/asyncapi/parser-api). If the template uses a version of the Parser-API that is not supported by the Generator, the Generator will throw an error.
12
12
  |`supportedProtocols`| [String] | A list with all the protocols this template supports.
13
13
  |`parameters`| Object[String, Object] | An object with all the parameters that can be passed when generating the template. When using the command line, it's done by indicating `--param name=value` or `-p name=value`.
14
14
  |`parameters[param].description`| String | A user-friendly description about the parameter.
@@ -28,7 +28,7 @@ The `generator` property from `package.json` file must contain a JSON object tha
28
28
  "generator":
29
29
  {
30
30
  "renderer": "react",
31
- "apiVersion": "v1",
31
+ "apiVersion": "v2",
32
32
  "supportedProtocols": ["amqp", "mqtt"],
33
33
  "parameters": {
34
34
  "server": {
package/lib/generator.js CHANGED
@@ -179,7 +179,7 @@ class Generator {
179
179
  validateTemplateConfig(this.templateConfig, this.templateParams, asyncapiDocument);
180
180
  await this.configureTemplate();
181
181
 
182
- // use new or old document API based on `templateConfig.apiVersion` value
182
+ // use the expected document API based on `templateConfig.apiVersion` value
183
183
  this.asyncapi = asyncapiDocument = getProperApiDocument(asyncapiDocument, this.templateConfig);
184
184
 
185
185
  if (!isReactTemplate(this.templateConfig)) {
@@ -261,7 +261,7 @@ class Generator {
261
261
  /** @type {AsyncAPIDocument} Parsed AsyncAPI schema. See {@link https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument|AsyncAPIDocument} for details on object structure. */
262
262
  const { document, diagnostics } = await parse(asyncapiString, parseOptions, this);
263
263
  if (!document) {
264
- const err = new Error('Input is not a corrent AsyncAPI document so it cannot be processed.');
264
+ const err = new Error('Input is not a correct AsyncAPI document so it cannot be processed.');
265
265
  err.diagnostics = diagnostics;
266
266
  throw err;
267
267
  }
@@ -436,7 +436,7 @@ class Generator {
436
436
  }
437
437
  });
438
438
  }
439
-
439
+
440
440
  if (asyncapiDocument.hasComponents()) {
441
441
  for (const [key, value] of Object.entries(asyncapiDocument.components().parameters())) {
442
442
  parameters.set(key, value);
package/lib/parser.js CHANGED
@@ -1,33 +1,47 @@
1
1
  const fs = require('fs');
2
-
3
- const { Parser, convertToOldAPI } = require('@asyncapi/parser/cjs');
4
- const { OpenAPISchemaParser } = require('@asyncapi/openapi-schema-parser');
5
- const { AvroSchemaParser } = require('@asyncapi/avro-schema-parser');
6
- const { RamlDTSchemaParser } = require('@asyncapi/raml-dt-schema-parser');
7
- const { ProtoBuffSchemaParser } = require('@asyncapi/protobuf-schema-parser');
2
+ const { convertToOldAPI } = require('@asyncapi/parser');
3
+ const { ConvertDocumentParserAPIVersion, NewParser } = require('@smoya/multi-parser');
8
4
 
9
5
  const parser = module.exports;
10
6
 
11
- const defaultParser = new Parser({
12
- schemaParsers: [
13
- OpenAPISchemaParser(),
14
- AvroSchemaParser(),
15
- RamlDTSchemaParser(),
16
- ProtoBuffSchemaParser(),
17
- ],
18
- });
7
+ /**
8
+ * Conver the template defined value `apiVersion: 'v1'` to only contain the numeric value `1`.
9
+ */
10
+ parser.sanitizeTemplateApiVersion = (apiVersion) => {
11
+ if (apiVersion && apiVersion.length > 1) {
12
+ return apiVersion.substring('1');
13
+ }
14
+ return apiVersion;
15
+ };
19
16
 
20
17
  parser.parse = (asyncapi, oldOptions, generator) => {
18
+ let apiVersion = this.sanitizeTemplateApiVersion(generator.templateConfig.apiVersion);
19
+ // Defaulting to apiVersion v1 to convert it to the Parser-API v1 afterwards.
20
+ if (!this.usesNewAPI(generator.templateConfig)) {
21
+ apiVersion = '1';
22
+ }
21
23
  const options = convertOldOptionsToNew(oldOptions, generator);
22
- return defaultParser.parse(asyncapi, options);
24
+ const parser = NewParser(apiVersion, {parserOptions: options, includeSchemaParsers: true});
25
+ return parser.parse(asyncapi, options);
23
26
  };
24
27
 
28
+ /**
29
+ * If the template expect one of the Parser-API versions, it must be above 0
30
+ */
25
31
  parser.usesNewAPI = (templateConfig = {}) => {
26
- return templateConfig.apiVersion === 'v1';
32
+ return Number(this.sanitizeTemplateApiVersion(templateConfig.apiVersion)) > 0;
27
33
  };
28
34
 
29
- parser.getProperApiDocument = (asyncapiDocument, templateConfig) => {
30
- return parser.usesNewAPI(templateConfig) ? asyncapiDocument : convertToOldAPI(asyncapiDocument);
35
+ /**
36
+ * Based on the current parsed AsyncAPI document, convert it to expected API version from the template.
37
+ */
38
+ parser.getProperApiDocument = (asyncapiDocument, templateConfig = {}) => {
39
+ const apiVersion = this.sanitizeTemplateApiVersion(templateConfig.apiVersion);
40
+ if (apiVersion === undefined) {
41
+ // Convert to old API from JS Parser v1
42
+ return convertToOldAPI(asyncapiDocument);
43
+ }
44
+ return ConvertDocumentParserAPIVersion(asyncapiDocument, apiVersion);
31
45
  };
32
46
 
33
47
  // The new options for the v2 Parser are different from those for the v1 version, but in order not to release Generator v2, we are converting the old options of Parser to the new ones.
@@ -8,6 +8,7 @@ const ajv = new Ajv({ allErrors: true });
8
8
  // See https://github.com/asyncapi/parser-api
9
9
  const supportedParserAPIMajorVersions = [
10
10
  'v1',
11
+ 'v2'
11
12
  ];
12
13
 
13
14
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyncapi/generator",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "The AsyncAPI generator. It can generate documentation, code, anything!",
5
5
  "main": "./lib/generator.js",
6
6
  "bin": {
@@ -48,13 +48,10 @@
48
48
  "license": "Apache-2.0",
49
49
  "homepage": "https://github.com/asyncapi/generator",
50
50
  "dependencies": {
51
- "@asyncapi/avro-schema-parser": "^3.0.3",
52
- "@asyncapi/generator-react-sdk": "^1.0.0",
53
- "@asyncapi/openapi-schema-parser": "^3.0.4",
54
- "@asyncapi/parser": "^2.1.0",
55
- "@asyncapi/protobuf-schema-parser": "3.0.0",
56
- "@asyncapi/raml-dt-schema-parser": "^4.0.4",
51
+ "@asyncapi/generator-react-sdk": "^0.2.23",
52
+ "@asyncapi/parser": "2.1.0",
57
53
  "@npmcli/arborist": "^2.2.4",
54
+ "@smoya/multi-parser": "3.0.0",
58
55
  "ajv": "^8.12.0",
59
56
  "chokidar": "^3.4.0",
60
57
  "commander": "^6.1.0",