@asyncapi/cli 3.1.1 → 3.3.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.
Files changed (194) hide show
  1. package/README.md +4 -0
  2. package/assets/create-glee-app/templates/default/package-lock.json +1185 -676
  3. package/assets/create-glee-app/templates/tutorial/package-lock.json +1148 -554
  4. package/assets/create-template/templates/default/package-lock.json +55 -132
  5. package/lib/apps/api/app.d.ts +15 -0
  6. package/lib/apps/api/app.js +89 -0
  7. package/lib/apps/api/configs/development.json +16 -0
  8. package/lib/apps/api/configs/production.json +16 -0
  9. package/lib/apps/api/configs/test.json +16 -0
  10. package/lib/apps/api/constants.d.ts +1 -0
  11. package/lib/apps/api/constants.js +4 -0
  12. package/lib/apps/api/controllers/bundle.controller.d.ts +7 -0
  13. package/lib/apps/api/controllers/bundle.controller.js +44 -0
  14. package/lib/apps/api/controllers/convert.controller.d.ts +11 -0
  15. package/lib/apps/api/controllers/convert.controller.js +69 -0
  16. package/lib/apps/api/controllers/diff.controller.d.ts +7 -0
  17. package/lib/apps/api/controllers/diff.controller.js +42 -0
  18. package/lib/apps/api/controllers/docs.controller.d.ts +6 -0
  19. package/lib/apps/api/controllers/docs.controller.js +24 -0
  20. package/lib/apps/api/controllers/generate.controller.d.ts +22 -0
  21. package/lib/apps/api/controllers/generate.controller.js +176 -0
  22. package/lib/apps/api/controllers/help.controller.d.ts +6 -0
  23. package/lib/apps/api/controllers/help.controller.js +101 -0
  24. package/lib/apps/api/controllers/parse.controller.d.ts +10 -0
  25. package/lib/apps/api/controllers/parse.controller.js +35 -0
  26. package/lib/apps/api/controllers/validate.controller.d.ts +10 -0
  27. package/lib/apps/api/controllers/validate.controller.js +50 -0
  28. package/lib/apps/api/exceptions/problem.exception.d.ts +14 -0
  29. package/lib/apps/api/exceptions/problem.exception.js +10 -0
  30. package/lib/apps/api/index.d.ts +9 -0
  31. package/lib/apps/api/index.js +21 -0
  32. package/lib/apps/api/middlewares/problem.middleware.d.ts +6 -0
  33. package/lib/apps/api/middlewares/problem.middleware.js +27 -0
  34. package/lib/apps/api/middlewares/validation.middleware.d.ts +12 -0
  35. package/lib/apps/api/middlewares/validation.middleware.js +236 -0
  36. package/lib/apps/api/server.d.ts +3 -0
  37. package/lib/apps/api/server.js +19 -0
  38. package/lib/{commands → apps/cli/commands}/bundle.d.ts +1 -1
  39. package/lib/{commands → apps/cli/commands}/bundle.js +3 -3
  40. package/lib/{commands → apps/cli/commands}/config/analytics.d.ts +1 -1
  41. package/lib/{commands → apps/cli/commands}/config/analytics.js +4 -3
  42. package/lib/{commands → apps/cli/commands}/config/context/add.d.ts +1 -1
  43. package/lib/{commands → apps/cli/commands}/config/context/add.js +12 -6
  44. package/lib/{commands → apps/cli/commands}/config/context/current.d.ts +1 -1
  45. package/lib/{commands → apps/cli/commands}/config/context/current.js +4 -4
  46. package/lib/{commands → apps/cli/commands}/config/context/edit.d.ts +1 -1
  47. package/lib/{commands → apps/cli/commands}/config/context/edit.js +12 -6
  48. package/lib/{commands → apps/cli/commands}/config/context/index.d.ts +1 -1
  49. package/lib/{commands → apps/cli/commands}/config/context/index.js +1 -1
  50. package/lib/{commands → apps/cli/commands}/config/context/init.d.ts +1 -1
  51. package/lib/{commands → apps/cli/commands}/config/context/init.js +7 -4
  52. package/lib/{commands → apps/cli/commands}/config/context/list.d.ts +1 -1
  53. package/lib/{commands → apps/cli/commands}/config/context/list.js +4 -4
  54. package/lib/{commands → apps/cli/commands}/config/context/remove.d.ts +1 -1
  55. package/lib/{commands → apps/cli/commands}/config/context/remove.js +8 -5
  56. package/lib/{commands → apps/cli/commands}/config/context/use.d.ts +1 -1
  57. package/lib/{commands → apps/cli/commands}/config/context/use.js +8 -5
  58. package/lib/{commands → apps/cli/commands}/config/index.d.ts +1 -1
  59. package/lib/{commands → apps/cli/commands}/config/index.js +1 -1
  60. package/lib/{commands → apps/cli/commands}/config/versions.d.ts +1 -1
  61. package/lib/{commands → apps/cli/commands}/config/versions.js +2 -2
  62. package/lib/{commands → apps/cli/commands}/convert.d.ts +2 -7
  63. package/lib/apps/cli/commands/convert.js +85 -0
  64. package/lib/{commands → apps/cli/commands}/diff.d.ts +11 -2
  65. package/lib/{commands → apps/cli/commands}/diff.js +85 -31
  66. package/lib/{commands → apps/cli/commands}/format.d.ts +1 -1
  67. package/lib/{commands → apps/cli/commands}/format.js +4 -4
  68. package/lib/{commands → apps/cli/commands}/generate/fromTemplate.d.ts +4 -5
  69. package/lib/{commands → apps/cli/commands}/generate/fromTemplate.js +89 -122
  70. package/lib/{commands → apps/cli/commands}/generate/index.d.ts +1 -1
  71. package/lib/{commands → apps/cli/commands}/generate/index.js +1 -1
  72. package/lib/{commands → apps/cli/commands}/generate/models.d.ts +6 -1
  73. package/lib/{commands → apps/cli/commands}/generate/models.js +48 -9
  74. package/lib/{commands → apps/cli/commands}/new/file.d.ts +1 -1
  75. package/lib/{commands → apps/cli/commands}/new/file.js +11 -8
  76. package/lib/{commands → apps/cli/commands}/new/glee.d.ts +2 -2
  77. package/lib/{commands → apps/cli/commands}/new/glee.js +4 -4
  78. package/lib/{commands → apps/cli/commands}/new/index.d.ts +1 -1
  79. package/lib/{commands → apps/cli/commands}/new/index.js +1 -1
  80. package/lib/{commands → apps/cli/commands}/new/template.d.ts +1 -1
  81. package/lib/{commands → apps/cli/commands}/new/template.js +10 -8
  82. package/lib/{commands → apps/cli/commands}/optimize.d.ts +1 -1
  83. package/lib/{commands → apps/cli/commands}/optimize.js +71 -30
  84. package/lib/{commands → apps/cli/commands}/pretty.d.ts +1 -1
  85. package/lib/{commands → apps/cli/commands}/pretty.js +8 -5
  86. package/lib/apps/cli/commands/start/api.d.ts +11 -0
  87. package/lib/apps/cli/commands/start/api.js +23 -0
  88. package/lib/{commands → apps/cli/commands}/start/index.d.ts +1 -1
  89. package/lib/{commands → apps/cli/commands}/start/index.js +1 -1
  90. package/lib/{commands → apps/cli/commands}/start/preview.d.ts +1 -1
  91. package/lib/{commands → apps/cli/commands}/start/preview.js +9 -6
  92. package/lib/{commands → apps/cli/commands}/start/studio.d.ts +1 -1
  93. package/lib/{commands → apps/cli/commands}/start/studio.js +10 -7
  94. package/lib/{commands → apps/cli/commands}/validate.d.ts +6 -2
  95. package/lib/apps/cli/commands/validate.js +94 -0
  96. package/lib/{core → apps/cli/internal}/base.d.ts +1 -1
  97. package/lib/{core → apps/cli/internal}/base.js +12 -4
  98. package/lib/apps/cli/internal/flags/bundle.flags.js +26 -0
  99. package/lib/apps/cli/internal/flags/config/analytics.flags.js +24 -0
  100. package/lib/{core → apps/cli/internal}/flags/config/context.flags.js +1 -1
  101. package/lib/{core → apps/cli/internal}/flags/convert.flags.js +9 -2
  102. package/lib/{core → apps/cli/internal}/flags/diff.flags.d.ts +1 -1
  103. package/lib/{core → apps/cli/internal}/flags/diff.flags.js +3 -3
  104. package/lib/{core → apps/cli/internal}/flags/generate/fromTemplate.flags.js +12 -12
  105. package/lib/{core → apps/cli/internal}/flags/generate/models.flags.js +2 -2
  106. package/lib/{core → apps/cli/internal}/flags/global.flags.js +1 -1
  107. package/lib/{core → apps/cli/internal}/flags/new/file.flags.js +7 -2
  108. package/lib/{core → apps/cli/internal}/flags/optimize.flags.js +24 -4
  109. package/lib/apps/cli/internal/flags/parser.flags.d.ts +10 -0
  110. package/lib/apps/cli/internal/flags/parser.flags.js +28 -0
  111. package/lib/{core → apps/cli/internal}/flags/proxy.flags.js +3 -3
  112. package/lib/apps/cli/internal/flags/start/api.flags.d.ts +5 -0
  113. package/lib/apps/cli/internal/flags/start/api.flags.js +20 -0
  114. package/lib/apps/cli/internal/flags/start/preview.flags.js +31 -0
  115. package/lib/{core → apps/cli/internal}/flags/start/studio.flags.js +9 -2
  116. package/lib/{core → apps/cli/internal}/flags/validate.flags.d.ts +3 -1
  117. package/lib/{core → apps/cli/internal}/flags/validate.flags.js +11 -3
  118. package/lib/{core → apps/cli/internal}/globals.d.ts +1 -1
  119. package/lib/{core → apps/cli/internal}/globals.js +2 -4
  120. package/lib/{core → apps/cli/internal}/hooks/command_not_found/myhook.js +14 -4
  121. package/lib/{core → domains}/models/Context.js +1 -1
  122. package/lib/{core → domains}/models/Preview.js +35 -19
  123. package/lib/{core → domains}/models/SpecificationFile.d.ts +2 -2
  124. package/lib/{core → domains}/models/SpecificationFile.js +8 -5
  125. package/lib/{core → domains}/models/Studio.js +4 -4
  126. package/lib/domains/services/archiver.service.d.ts +17 -0
  127. package/lib/domains/services/archiver.service.js +53 -0
  128. package/lib/domains/services/base.service.d.ts +6 -0
  129. package/lib/domains/services/base.service.js +26 -0
  130. package/lib/domains/services/convert.service.d.ts +12 -0
  131. package/lib/domains/services/convert.service.js +65 -0
  132. package/lib/domains/services/generator.service.d.ts +15 -0
  133. package/lib/domains/services/generator.service.js +95 -0
  134. package/lib/domains/services/validation.service.d.ts +54 -0
  135. package/lib/domains/services/validation.service.js +288 -0
  136. package/lib/{core/errors → errors}/specification-file.js +1 -1
  137. package/lib/{core/errors → errors}/validation-error.js +6 -5
  138. package/lib/interfaces/index.d.ts +87 -0
  139. package/lib/interfaces/index.js +7 -0
  140. package/lib/utils/ajv.d.ts +2 -0
  141. package/lib/utils/ajv.js +18 -0
  142. package/lib/utils/app-openapi.d.ts +4 -0
  143. package/lib/utils/app-openapi.js +28 -0
  144. package/lib/{core/utils/generator.js → utils/fileWatcher.js} +8 -2
  145. package/lib/utils/logger.d.ts +6 -0
  146. package/lib/utils/logger.js +33 -0
  147. package/lib/utils/retrieve-language.d.ts +1 -0
  148. package/lib/utils/retrieve-language.js +9 -0
  149. package/lib/utils/temp-dir.d.ts +2 -0
  150. package/lib/utils/temp-dir.js +25 -0
  151. package/oclif.manifest.json +124 -1
  152. package/package.json +37 -22
  153. package/lib/commands/convert.js +0 -132
  154. package/lib/commands/validate.js +0 -45
  155. package/lib/core/flags/bundle.flags.js +0 -14
  156. package/lib/core/flags/config/analytics.flags.js +0 -13
  157. package/lib/core/flags/start/preview.flags.js +0 -15
  158. package/lib/core/parser.d.ts +0 -33
  159. package/lib/core/parser.js +0 -176
  160. /package/lib/{core → apps/cli/internal}/flags/bundle.flags.d.ts +0 -0
  161. /package/lib/{core → apps/cli/internal}/flags/config/analytics.flags.d.ts +0 -0
  162. /package/lib/{core → apps/cli/internal}/flags/config/context.flags.d.ts +0 -0
  163. /package/lib/{core → apps/cli/internal}/flags/convert.flags.d.ts +0 -0
  164. /package/lib/{core → apps/cli/internal}/flags/format.flags.d.ts +0 -0
  165. /package/lib/{core → apps/cli/internal}/flags/format.flags.js +0 -0
  166. /package/lib/{core → apps/cli/internal}/flags/generate/fromTemplate.flags.d.ts +0 -0
  167. /package/lib/{core → apps/cli/internal}/flags/generate/models.flags.d.ts +0 -0
  168. /package/lib/{core → apps/cli/internal}/flags/global.flags.d.ts +0 -0
  169. /package/lib/{core → apps/cli/internal}/flags/new/file.flags.d.ts +0 -0
  170. /package/lib/{core → apps/cli/internal}/flags/new/glee.flags.d.ts +0 -0
  171. /package/lib/{core → apps/cli/internal}/flags/new/glee.flags.js +0 -0
  172. /package/lib/{core → apps/cli/internal}/flags/new/template.flags.d.ts +0 -0
  173. /package/lib/{core → apps/cli/internal}/flags/new/template.flags.js +0 -0
  174. /package/lib/{core → apps/cli/internal}/flags/optimize.flags.d.ts +0 -0
  175. /package/lib/{core → apps/cli/internal}/flags/pretty.flags.d.ts +0 -0
  176. /package/lib/{core → apps/cli/internal}/flags/pretty.flags.js +0 -0
  177. /package/lib/{core → apps/cli/internal}/flags/proxy.flags.d.ts +0 -0
  178. /package/lib/{core → apps/cli/internal}/flags/start/preview.flags.d.ts +0 -0
  179. /package/lib/{core → apps/cli/internal}/flags/start/studio.flags.d.ts +0 -0
  180. /package/lib/{core → apps/cli/internal}/hooks/command_not_found/myhook.d.ts +0 -0
  181. /package/lib/{core → domains}/models/Context.d.ts +0 -0
  182. /package/lib/{core → domains}/models/Preview.d.ts +0 -0
  183. /package/lib/{core → domains}/models/Studio.d.ts +0 -0
  184. /package/lib/{core/errors → errors}/context-error.d.ts +0 -0
  185. /package/lib/{core/errors → errors}/context-error.js +0 -0
  186. /package/lib/{core/errors → errors}/diff-error.d.ts +0 -0
  187. /package/lib/{core/errors → errors}/diff-error.js +0 -0
  188. /package/lib/{core/errors → errors}/generator-error.d.ts +0 -0
  189. /package/lib/{core/errors → errors}/generator-error.js +0 -0
  190. /package/lib/{core/errors → errors}/specification-file.d.ts +0 -0
  191. /package/lib/{core/errors → errors}/validation-error.d.ts +0 -0
  192. /package/lib/{core/utils/generator.d.ts → utils/fileWatcher.d.ts} +0 -0
  193. /package/lib/{core/utils → utils}/scoreCalculator.d.ts +0 -0
  194. /package/lib/{core/utils → utils}/scoreCalculator.js +0 -0
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiffController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = require("express");
6
+ const diff_1 = require("@asyncapi/diff");
7
+ const validation_middleware_1 = require("../middlewares/validation.middleware");
8
+ const problem_exception_1 = require("../exceptions/problem.exception");
9
+ class DiffController {
10
+ constructor() {
11
+ this.basepath = '/diff';
12
+ }
13
+ diff(req, res, next) {
14
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
15
+ const { asyncapis } = req.body;
16
+ try {
17
+ const output = (0, diff_1.diff)(asyncapis[0], asyncapis[1]).getOutput();
18
+ res.status(200).json({ diff: output });
19
+ }
20
+ catch (err) {
21
+ return next(new problem_exception_1.ProblemException({
22
+ type: 'internal-diff-error',
23
+ title: 'Internal Diff error',
24
+ status: 500,
25
+ detail: err.message,
26
+ }));
27
+ }
28
+ });
29
+ }
30
+ boot() {
31
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
32
+ const router = (0, express_1.Router)();
33
+ router.post(this.basepath, yield (0, validation_middleware_1.validationMiddleware)({
34
+ path: this.basepath,
35
+ method: 'post',
36
+ documents: ['asyncapis'],
37
+ }), this.diff.bind(this));
38
+ return router;
39
+ });
40
+ }
41
+ }
42
+ exports.DiffController = DiffController;
@@ -0,0 +1,6 @@
1
+ import { Router } from 'express';
2
+ import { Controller } from '../../../interfaces';
3
+ export declare class DocsController implements Controller {
4
+ basepath: string;
5
+ boot(): Router;
6
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocsController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = require("express");
6
+ const redoc_express_1 = tslib_1.__importDefault(require("redoc-express"));
7
+ const constants_1 = require("../constants");
8
+ class DocsController {
9
+ constructor() {
10
+ this.basepath = '/docs';
11
+ }
12
+ boot() {
13
+ const router = (0, express_1.Router)();
14
+ router.get(`${this.basepath}/openapi.yaml`, (_, res) => {
15
+ res.sendFile('openapi.yaml', { root: '.' });
16
+ });
17
+ router.get(this.basepath, (0, redoc_express_1.default)({
18
+ title: 'OpenAPI Documentation',
19
+ specUrl: `/${constants_1.API_VERSION}${this.basepath}/openapi.yaml`,
20
+ }));
21
+ return router;
22
+ }
23
+ }
24
+ exports.DocsController = DocsController;
@@ -0,0 +1,22 @@
1
+ import { Router } from 'express';
2
+ import { Controller } from '../../../interfaces';
3
+ /**
4
+ * Controller which exposes the Generator functionality
5
+ */
6
+ export declare class GenerateController implements Controller {
7
+ basepath: string;
8
+ private archiverService;
9
+ private generatorService;
10
+ private ajv;
11
+ private generate;
12
+ private validateTemplateParameters;
13
+ /**
14
+ * Retrieve proper AJV's validator function, create or reuse it.
15
+ */
16
+ getAjvValidator(templateName: string): Promise<import("ajv/dist/types").AnyValidateFunction<unknown> | undefined>;
17
+ /**
18
+ * Serialize template parameters. Read all parameters from template's package.json and create a proper JSON Schema for validating parameters.
19
+ */
20
+ serializeTemplateParameters(templateName: string): Promise<object | undefined>;
21
+ boot(): Promise<Router>;
22
+ }
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GenerateController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs_1 = tslib_1.__importDefault(require("fs"));
6
+ const path_1 = tslib_1.__importDefault(require("path"));
7
+ const express_1 = require("express");
8
+ const ajv_1 = tslib_1.__importDefault(require("ajv"));
9
+ const validation_middleware_1 = require("../middlewares/validation.middleware");
10
+ const archiver_service_1 = require("../../../domains/services/archiver.service");
11
+ const generator_service_1 = require("../../../domains/services/generator.service");
12
+ const problem_exception_1 = require("../exceptions/problem.exception");
13
+ const SpecificationFile_1 = require("../../../domains/models/SpecificationFile");
14
+ /**
15
+ * Controller which exposes the Generator functionality
16
+ */
17
+ class GenerateController {
18
+ constructor() {
19
+ this.basepath = '/generate';
20
+ this.archiverService = new archiver_service_1.ArchiverService();
21
+ this.generatorService = new generator_service_1.GeneratorService();
22
+ }
23
+ generate(req, res, next) {
24
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
25
+ // try {
26
+ // await this.validateTemplateParameters(req);
27
+ // } catch (err) {
28
+ // return next(err);
29
+ // }
30
+ const zip = this.archiverService.createZip(res);
31
+ let tmpDir;
32
+ try {
33
+ tmpDir = yield this.archiverService.createTempDirectory();
34
+ const { asyncapi, template, parameters } = req.body;
35
+ const options = {
36
+ forceWrite: true,
37
+ templateParams: parameters,
38
+ };
39
+ const generateFunc = req.body['use-fallback-generator']
40
+ ? this.generatorService.generate.bind(this.generatorService)
41
+ : this.generatorService.generateUsingNewGenerator.bind(this.generatorService);
42
+ try {
43
+ const result = yield generateFunc(new SpecificationFile_1.Specification(typeof asyncapi === 'object' ? JSON.stringify(asyncapi) : asyncapi), template, tmpDir, options, {}, true);
44
+ if (!result.success) {
45
+ return next(new problem_exception_1.ProblemException({
46
+ type: 'generation-error',
47
+ title: 'Generation Error',
48
+ status: 500,
49
+ detail: result.error || 'An error occurred during generation.',
50
+ }));
51
+ }
52
+ }
53
+ catch (genErr) {
54
+ return next(new problem_exception_1.ProblemException({
55
+ type: 'internal-generator-error',
56
+ title: 'Internal Generator error',
57
+ status: 500,
58
+ detail: genErr.message,
59
+ }));
60
+ }
61
+ this.archiverService.appendDirectory(zip, tmpDir, 'template');
62
+ this.archiverService.appendAsyncAPIDocument(zip, asyncapi);
63
+ res.status(200);
64
+ return yield this.archiverService.finalize(zip);
65
+ }
66
+ catch (err) {
67
+ return next(new problem_exception_1.ProblemException({
68
+ type: 'internal-server-error',
69
+ title: 'Internal server error',
70
+ status: 500,
71
+ detail: err.message,
72
+ }));
73
+ }
74
+ finally {
75
+ if (tmpDir) {
76
+ // Remove the temporary directory after the response is sent
77
+ // to avoid leaving temporary files on the server.
78
+ yield this.archiverService.removeTempDirectory(tmpDir);
79
+ }
80
+ }
81
+ });
82
+ }
83
+ validateTemplateParameters(req) {
84
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
85
+ const { template, parameters } = req.body;
86
+ const validate = yield this.getAjvValidator(template);
87
+ if (!validate) {
88
+ throw new problem_exception_1.ProblemException({
89
+ type: 'invalid-template',
90
+ title: 'Invalid Generator Template',
91
+ status: 422,
92
+ detail: `Template ${template} is not valid or does not exist.`,
93
+ });
94
+ }
95
+ const valid = validate(parameters || {});
96
+ const errors = validate.errors && [...validate.errors];
97
+ if (valid === false) {
98
+ throw new problem_exception_1.ProblemException({
99
+ type: 'invalid-template-parameters',
100
+ title: 'Invalid Generator Template parameters',
101
+ status: 422,
102
+ validationErrors: errors,
103
+ });
104
+ }
105
+ });
106
+ }
107
+ /**
108
+ * Retrieve proper AJV's validator function, create or reuse it.
109
+ */
110
+ getAjvValidator(templateName) {
111
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
112
+ if (!this.ajv) {
113
+ throw new Error('AJV instance is not initialized');
114
+ }
115
+ let validate = this.ajv.getSchema(templateName);
116
+ if (!validate) {
117
+ this.ajv.addSchema((yield this.serializeTemplateParameters(templateName)) || {}, templateName);
118
+ validate = this.ajv.getSchema(templateName);
119
+ }
120
+ return validate;
121
+ });
122
+ }
123
+ /**
124
+ * Serialize template parameters. Read all parameters from template's package.json and create a proper JSON Schema for validating parameters.
125
+ */
126
+ serializeTemplateParameters(templateName) {
127
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
128
+ const pathToPackageJSON = path_1.default.join(__dirname, `../../../../node_modules/${templateName}/package.json`);
129
+ const packageJSONContent = yield fs_1.default.promises.readFile(pathToPackageJSON, 'utf-8');
130
+ const packageJSON = JSON.parse(packageJSONContent);
131
+ if (!packageJSON) {
132
+ return;
133
+ }
134
+ const generator = packageJSON.generator;
135
+ if (!generator || !generator.parameters) {
136
+ return;
137
+ }
138
+ const parameters = generator.parameters || {};
139
+ const required = [];
140
+ for (const parameter in parameters) {
141
+ // at the moment all parameters have to be passed to the Generator instance as string
142
+ parameters[String(parameter)].type = 'string';
143
+ if (parameters[String(parameter)].required) {
144
+ required.push(parameter);
145
+ }
146
+ delete parameters[String(parameter)].required;
147
+ }
148
+ return {
149
+ $schema: 'http://json-schema.org/draft-07/schema#',
150
+ type: 'object',
151
+ properties: parameters,
152
+ required,
153
+ // don't allow non supported properties
154
+ additionalProperties: false,
155
+ };
156
+ });
157
+ }
158
+ boot() {
159
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
160
+ this.ajv = new ajv_1.default({
161
+ inlineRefs: true,
162
+ allErrors: true,
163
+ schemaId: 'id',
164
+ logger: false,
165
+ });
166
+ const router = (0, express_1.Router)();
167
+ router.post(`${this.basepath}`, yield (0, validation_middleware_1.validationMiddleware)({
168
+ path: this.basepath,
169
+ method: 'post',
170
+ documents: ['asyncapi'],
171
+ }), this.generate.bind(this));
172
+ return router;
173
+ });
174
+ }
175
+ }
176
+ exports.GenerateController = GenerateController;
@@ -0,0 +1,6 @@
1
+ import { Router } from 'express';
2
+ import { Controller } from '../../../interfaces';
3
+ export declare class HelpController implements Controller {
4
+ basepath: string;
5
+ boot(): Promise<Router>;
6
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HelpController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = require("express");
6
+ const problem_exception_1 = require("../exceptions/problem.exception");
7
+ const app_openapi_1 = require("../../../utils/app-openapi");
8
+ const getCommandsFromRequest = (req) => {
9
+ return req.params.command
10
+ ? req.params.command.split('/').filter((cmd) => cmd.trim())
11
+ : [];
12
+ };
13
+ const isKeyValid = (key, obj) => {
14
+ return Object.keys(obj).includes(key);
15
+ };
16
+ const getPathKeysMatchingCommands = (commands, pathKeys) => {
17
+ if (!Array.isArray(pathKeys) ||
18
+ !pathKeys.every((key) => typeof key === 'string')) {
19
+ return undefined;
20
+ }
21
+ return pathKeys.find((pathKey) => {
22
+ const pathParts = pathKey.split('/').filter((part) => part !== '');
23
+ return pathParts.every((pathPart, i) => {
24
+ const command = commands[Number(i)];
25
+ return pathPart === command || pathPart.startsWith('{');
26
+ });
27
+ });
28
+ };
29
+ const getFullRequestBodySpec = (operationDetails) => {
30
+ return isKeyValid('requestBody', operationDetails)
31
+ ? operationDetails.requestBody.content['application/json'].schema
32
+ : null;
33
+ };
34
+ const buildResponseObject = (matchedPathKey, method, operationDetails, requestBodySchema) => {
35
+ return {
36
+ command: matchedPathKey,
37
+ method: method.toUpperCase(),
38
+ summary: operationDetails.summary || '',
39
+ requestBody: requestBodySchema,
40
+ };
41
+ };
42
+ class HelpController {
43
+ constructor() {
44
+ this.basepath = '/help';
45
+ }
46
+ boot() {
47
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
48
+ const router = (0, express_1.Router)();
49
+ router.get('/help/:command*?', (req, res, next) => tslib_1.__awaiter(this, void 0, void 0, function* () {
50
+ const commands = getCommandsFromRequest(req);
51
+ let openapiSpec;
52
+ try {
53
+ openapiSpec = yield (0, app_openapi_1.getAppOpenAPI)();
54
+ }
55
+ catch (err) {
56
+ return next(err);
57
+ }
58
+ if (commands.length === 0) {
59
+ const routes = isKeyValid('paths', openapiSpec)
60
+ ? Object.keys(openapiSpec.paths).map((path) => ({
61
+ command: path.replace(/^\//, ''),
62
+ url: `${this.basepath}${path}`,
63
+ }))
64
+ : [];
65
+ return res.json(routes);
66
+ }
67
+ const pathKeys = isKeyValid('paths', openapiSpec)
68
+ ? Object.keys(openapiSpec.paths)
69
+ : [];
70
+ const matchedPathKey = getPathKeysMatchingCommands(commands, pathKeys);
71
+ if (!matchedPathKey) {
72
+ return next(new problem_exception_1.ProblemException({
73
+ type: 'invalid-asyncapi-command',
74
+ title: 'Invalid AsyncAPI Command',
75
+ status: 404,
76
+ detail: 'The given AsyncAPI command is not valid.',
77
+ }));
78
+ }
79
+ const pathInfo = isKeyValid(matchedPathKey, openapiSpec.paths)
80
+ ? openapiSpec.paths[String(matchedPathKey)]
81
+ : undefined;
82
+ const method = commands.length > 1 ? 'get' : 'post';
83
+ const operationDetails = isKeyValid(method, pathInfo)
84
+ ? pathInfo[String(method)]
85
+ : undefined;
86
+ if (!operationDetails) {
87
+ return next(new problem_exception_1.ProblemException({
88
+ type: 'invalid-asyncapi-command',
89
+ title: 'Invalid AsyncAPI Command',
90
+ status: 404,
91
+ detail: 'The given AsyncAPI command is not valid.',
92
+ }));
93
+ }
94
+ const requestBodySchema = getFullRequestBodySpec(operationDetails);
95
+ return res.json(buildResponseObject(matchedPathKey, method, operationDetails, requestBodySchema));
96
+ }));
97
+ return router;
98
+ });
99
+ }
100
+ }
101
+ exports.HelpController = HelpController;
@@ -0,0 +1,10 @@
1
+ import { Router } from 'express';
2
+ import { Controller } from '../../../interfaces';
3
+ /**
4
+ * Controller which exposes the Parser functionality, to parse the AsyncAPI document.
5
+ */
6
+ export declare class ParseController implements Controller {
7
+ basepath: string;
8
+ private parse;
9
+ boot(): Promise<Router>;
10
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParseController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = require("express");
6
+ const validation_middleware_1 = require("../middlewares/validation.middleware");
7
+ /**
8
+ * Controller which exposes the Parser functionality, to parse the AsyncAPI document.
9
+ */
10
+ class ParseController {
11
+ constructor() {
12
+ this.basepath = '/parse';
13
+ }
14
+ parse(req, res) {
15
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
+ var _a;
17
+ const stringified = JSON.stringify((_a = req.asyncapi) === null || _a === void 0 ? void 0 : _a.parsedDocument);
18
+ res.status(200).json({
19
+ parsed: stringified,
20
+ });
21
+ });
22
+ }
23
+ boot() {
24
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
25
+ const router = (0, express_1.Router)();
26
+ router.post(`${this.basepath}`, yield (0, validation_middleware_1.validationMiddleware)({
27
+ path: this.basepath,
28
+ method: 'post',
29
+ documents: ['asyncapi'],
30
+ }), this.parse.bind(this));
31
+ return router;
32
+ });
33
+ }
34
+ }
35
+ exports.ParseController = ParseController;
@@ -0,0 +1,10 @@
1
+ import { Router } from 'express';
2
+ import { Controller } from '../../../interfaces';
3
+ /**
4
+ * Controller which exposes the Parser functionality, to validate the AsyncAPI document.
5
+ */
6
+ export declare class ValidateController implements Controller {
7
+ basepath: string;
8
+ private validate;
9
+ boot(): Promise<Router>;
10
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidateController = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = require("express");
6
+ const validation_middleware_1 = require("../middlewares/validation.middleware");
7
+ /**
8
+ * Controller which exposes the Parser functionality, to validate the AsyncAPI document.
9
+ */
10
+ class ValidateController {
11
+ constructor() {
12
+ this.basepath = '/validate';
13
+ }
14
+ validate(req, res) {
15
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
17
+ if ((_b = (_a = req.asyncapi) === null || _a === void 0 ? void 0 : _a.parsedDocuments) === null || _b === void 0 ? void 0 : _b.length) {
18
+ const results = ((_c = req.asyncapi.validationResults) === null || _c === void 0 ? void 0 : _c.map((result) => ({
19
+ status: result.status,
20
+ asyncapi: result.document,
21
+ diagnostics: result.diagnostics,
22
+ score: result.score,
23
+ }))) || [];
24
+ res.status(200).json({
25
+ results,
26
+ });
27
+ }
28
+ else {
29
+ res.status(200).json({
30
+ status: (_e = (_d = req.asyncapi) === null || _d === void 0 ? void 0 : _d.validationResult) === null || _e === void 0 ? void 0 : _e.status,
31
+ asyncapi: (_g = (_f = req.asyncapi) === null || _f === void 0 ? void 0 : _f.validationResult) === null || _g === void 0 ? void 0 : _g.document,
32
+ diagnostics: (_j = (_h = req.asyncapi) === null || _h === void 0 ? void 0 : _h.validationResult) === null || _j === void 0 ? void 0 : _j.diagnostics,
33
+ score: (_l = (_k = req.asyncapi) === null || _k === void 0 ? void 0 : _k.validationResult) === null || _l === void 0 ? void 0 : _l.score,
34
+ });
35
+ }
36
+ });
37
+ }
38
+ boot() {
39
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
40
+ const router = (0, express_1.Router)();
41
+ router.post(`${this.basepath}`, yield (0, validation_middleware_1.validationMiddleware)({
42
+ path: this.basepath,
43
+ method: 'post',
44
+ documents: ['asyncapi'],
45
+ }), this.validate.bind(this));
46
+ return router;
47
+ });
48
+ }
49
+ }
50
+ exports.ValidateController = ValidateController;
@@ -0,0 +1,14 @@
1
+ export interface ProblemExceptionProps {
2
+ status: number;
3
+ [key: string]: any;
4
+ }
5
+ declare const ProblemException_base: {
6
+ new (problem: import("@asyncapi/problem").ProblemBase & ProblemExceptionProps, options?: import("@asyncapi/problem").ProblemOptions): import("@asyncapi/problem").Problem<ProblemExceptionProps>;
7
+ createType(type: string): string;
8
+ captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
9
+ prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
10
+ stackTraceLimit: number;
11
+ };
12
+ export declare class ProblemException extends ProblemException_base {
13
+ }
14
+ export {};
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProblemException = void 0;
4
+ const problem_1 = require("@asyncapi/problem");
5
+ const typePrefix = 'https://api.asyncapi.com/problem';
6
+ class ProblemException extends (0, problem_1.ProblemMixin)({
7
+ typePrefix,
8
+ }) {
9
+ }
10
+ exports.ProblemException = ProblemException;
@@ -0,0 +1,9 @@
1
+ import { ValidateController } from './controllers/validate.controller';
2
+ import { ParseController } from './controllers/parse.controller';
3
+ import { GenerateController } from './controllers/generate.controller';
4
+ import { ConvertController } from './controllers/convert.controller';
5
+ import { BundleController } from './controllers/bundle.controller';
6
+ import { DiffController } from './controllers/diff.controller';
7
+ import { DocsController } from './controllers/docs.controller';
8
+ import { HelpController } from './controllers/help.controller';
9
+ export declare const CONTROLLERS: (ValidateController | ParseController | GenerateController | ConvertController | BundleController | DiffController | DocsController | HelpController)[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CONTROLLERS = void 0;
4
+ const validate_controller_1 = require("./controllers/validate.controller");
5
+ const parse_controller_1 = require("./controllers/parse.controller");
6
+ const generate_controller_1 = require("./controllers/generate.controller");
7
+ const convert_controller_1 = require("./controllers/convert.controller");
8
+ const bundle_controller_1 = require("./controllers/bundle.controller");
9
+ const diff_controller_1 = require("./controllers/diff.controller");
10
+ const docs_controller_1 = require("./controllers/docs.controller");
11
+ const help_controller_1 = require("./controllers/help.controller");
12
+ exports.CONTROLLERS = [
13
+ new validate_controller_1.ValidateController(),
14
+ new parse_controller_1.ParseController(),
15
+ new generate_controller_1.GenerateController(),
16
+ new convert_controller_1.ConvertController(),
17
+ new bundle_controller_1.BundleController(),
18
+ new diff_controller_1.DiffController(),
19
+ new docs_controller_1.DocsController(),
20
+ new help_controller_1.HelpController(),
21
+ ];
@@ -0,0 +1,6 @@
1
+ import { NextFunction, Request, Response } from 'express';
2
+ import { ProblemException } from '../exceptions/problem.exception';
3
+ /**
4
+ * Catch problem exception, log it and serialize error to human readable form.
5
+ */
6
+ export declare function problemMiddleware(error: ProblemException, req: Request, res: Response, next: NextFunction): void;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.problemMiddleware = problemMiddleware;
4
+ const logger_1 = require("../../../utils/logger");
5
+ /**
6
+ * Catch problem exception, log it and serialize error to human readable form.
7
+ */
8
+ function problemMiddleware(error, req, res, next) {
9
+ if (res.headersSent) {
10
+ return next(error);
11
+ }
12
+ try {
13
+ const problemShape = error.get();
14
+ const status = (problemShape.status = problemShape.status || 500);
15
+ problemShape.title = problemShape.title || 'Internal server error';
16
+ logger_1.logger.error(`[${req.method}] ${req.path} >> Status:: ${status}, Type:: ${problemShape.type.replace('https://api.asyncapi.com/problem/', '')}, Title:: ${problemShape.title}, Detail:: ${problemShape.detail}`);
17
+ const isError = status >= 500;
18
+ const problem = error.toObject({
19
+ includeStack: isError,
20
+ includeCause: isError,
21
+ });
22
+ res.status(status).json(problem);
23
+ }
24
+ catch (err) {
25
+ next(err);
26
+ }
27
+ }
@@ -0,0 +1,12 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export interface ValidationMiddlewareOptions {
3
+ path: string;
4
+ method: 'all' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
5
+ documents?: Array<string>;
6
+ version?: 'v1';
7
+ condition?: (req: Request) => boolean;
8
+ }
9
+ /**
10
+ * Validate RequestBody and sent AsyncAPI document(s) for given path and method based on the OpenAPI Document.
11
+ */
12
+ export declare function validationMiddleware(options: ValidationMiddlewareOptions): Promise<(req: Request, res: Response, next: NextFunction) => Promise<void>>;