@asyncapi/converter 0.9.0 → 1.0.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 (58) hide show
  1. package/README.md +96 -21
  2. package/lib/convert.d.ts +3 -0
  3. package/lib/convert.js +192 -0
  4. package/lib/index.d.ts +2 -0
  5. package/lib/index.js +5 -212
  6. package/lib/interfaces.d.ts +6 -0
  7. package/lib/interfaces.js +2 -0
  8. package/lib/utils.d.ts +21 -0
  9. package/lib/utils.js +114 -0
  10. package/package.json +24 -12
  11. package/.github/workflows/add-good-first-issue-labels.yml +0 -68
  12. package/.github/workflows/automerge-for-humans-add-ready-to-merge-or-do-not-merge-label.yml +0 -54
  13. package/.github/workflows/automerge-for-humans-merging.yml +0 -32
  14. package/.github/workflows/automerge-for-humans-remove-ready-to-merge-label-on-edit.yml +0 -35
  15. package/.github/workflows/automerge-orphans.yml +0 -63
  16. package/.github/workflows/automerge.yml +0 -50
  17. package/.github/workflows/autoupdate.yml +0 -32
  18. package/.github/workflows/bump.yml +0 -33
  19. package/.github/workflows/help-command.yml +0 -43
  20. package/.github/workflows/if-go-pr-testing.yml +0 -68
  21. package/.github/workflows/if-nodejs-pr-testing.yml +0 -60
  22. package/.github/workflows/if-nodejs-release.yml +0 -85
  23. package/.github/workflows/if-nodejs-version-bump.yml +0 -48
  24. package/.github/workflows/issues-prs-notifications.yml +0 -72
  25. package/.github/workflows/lint-pr-title.yml +0 -22
  26. package/.github/workflows/notify-tsc-members-mention.yml +0 -142
  27. package/.github/workflows/release-announcements.yml +0 -76
  28. package/.github/workflows/sentiment-analysis.yml +0 -44
  29. package/.github/workflows/stale-issues-prs.yml +0 -42
  30. package/.github/workflows/welcome-first-time-contrib.yml +0 -83
  31. package/CODEOWNERS +0 -8
  32. package/cli.js +0 -66
  33. package/lib/helpers.js +0 -109
  34. package/test/index.js +0 -257
  35. package/test/input/1.0.0/streetlights.yml +0 -120
  36. package/test/input/1.1.0/streetlights.yml +0 -120
  37. package/test/input/1.2.0/gitter-streaming.yml +0 -140
  38. package/test/input/1.2.0/slack-rtm.yml +0 -876
  39. package/test/input/1.2.0/streetlights.yml +0 -120
  40. package/test/input/2.0.0/streetlights.json +0 -172
  41. package/test/input/2.0.0/streetlights.yml +0 -112
  42. package/test/input/2.0.0-rc1/streetlights.yml +0 -109
  43. package/test/input/2.0.0-rc2/streetlights.yml +0 -112
  44. package/test/input/2.1.0/streetlights.yml +0 -112
  45. package/test/input/2.2.0/streetlights.yml +0 -112
  46. package/test/output/2.0.0/gitter-streaming.yml +0 -137
  47. package/test/output/2.0.0/slack-rtm.yml +0 -879
  48. package/test/output/2.0.0/streetlights.yml +0 -112
  49. package/test/output/2.0.0-rc1/gitter-streaming.yml +0 -137
  50. package/test/output/2.0.0-rc1/slack-rtm.yml +0 -879
  51. package/test/output/2.0.0-rc1/streetlights.yml +0 -109
  52. package/test/output/2.0.0-rc2/gitter-streaming.yml +0 -137
  53. package/test/output/2.0.0-rc2/slack-rtm.yml +0 -879
  54. package/test/output/2.0.0-rc2/streetlights.yml +0 -112
  55. package/test/output/2.1.0/streetlights.json +0 -172
  56. package/test/output/2.1.0/streetlights.yml +0 -112
  57. package/test/output/2.2.0/streetlights.yml +0 -112
  58. package/test/output/2.3.0/streetlights.yml +0 -112
package/README.md CHANGED
@@ -2,60 +2,96 @@
2
2
 
3
3
  Convert [AsyncAPI](https://asyncapi.com) documents older to newer versions.
4
4
 
5
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
6
+ [![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors-)
7
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
8
+
9
+ <!-- toc is generated with GitHub Actions do not remove toc markers -->
10
+
11
+ <!-- toc -->
12
+
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ * [From CLI](#from-cli)
16
+ * [In JS](#in-js)
17
+ * [In TS](#in-ts)
18
+ - [Known missing features](#known-missing-features)
19
+ - [Development](#development)
20
+ - [Contribution](#contribution)
21
+ - [Contributors ✨](#contributors-%E2%9C%A8)
22
+
23
+ <!-- tocstop -->
24
+
5
25
  ## Installation
6
26
 
7
27
  ```sh
8
- npm i -g @asyncapi/converter
28
+ npm i @asyncapi/converter
9
29
  ```
10
30
 
11
31
  ## Usage
12
32
 
13
33
  ### From CLI
14
34
 
15
- Minimal example:
35
+ To convert an AsyncAPi document in the console needs the official [AsyncAPI CLI](https://github.com/asyncapi/cli).
16
36
 
17
- ```sh
18
- asyncapi-converter streetlights.yml
37
+ If you don't have CLI installed, run this command to install the CLI globally on your system:
19
38
 
20
- # Result:
21
- asyncapi: '2.0.0'
22
- channels:
23
- ...
39
+ ```sh
40
+ npm install -g @asyncapi/cli
24
41
  ```
25
42
 
26
- Specify the application id:
43
+ Minimal usage example with output given:
27
44
 
28
45
  ```sh
29
- asyncapi-converter --id=urn:com.asynapi.streetlights streetlights.yml
46
+ asyncapi convert streetlights.yml -o streetlights2.yml
30
47
 
31
48
  # Result:
32
49
  asyncapi: '2.0.0'
33
- id: 'urn:com.asynapi.streetlights'
50
+ channels:
34
51
  ...
35
52
  ```
36
53
 
37
- Save the result in a file by stream:
54
+ Convert to specific version:
38
55
 
39
56
  ```sh
40
- asyncapi-converter streetlights.yml > streetlights2.yml
41
- ```
57
+ asyncapi convert streetlights.yml -o streetlights2.yml -t 2.3.0
42
58
 
43
- Save the result in a file by `-o, --output` flag:
44
-
45
- ```sh
46
- asyncapi-converter streetlights.yml -o streetlights2.yml
59
+ # Result:
60
+ asyncapi: '2.3.0'
61
+ channels:
62
+ ...
47
63
  ```
48
64
 
49
- ### As a package
65
+ ### In JS
50
66
 
51
67
  ```js
68
+ const fs = require('fs');
52
69
  const { convert } = require('@asyncapi/converter')
53
70
 
54
71
  try {
55
72
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
56
73
  console.log(convert(asyncapi, '2.0.0', {
57
- id: 'urn:com.asyncapi.streetlights'
58
- }))
74
+ id: 'urn:com.asyncapi.streetlights'
75
+ }));
76
+ } catch (e) {
77
+ console.error(e);
78
+ }
79
+ ```
80
+
81
+ ### In TS
82
+
83
+ ```ts
84
+ import { convert } from '@asyncapi/converter';
85
+ import type { ConvertVersion, ConvertOptions } from '@asyncapi/converter';
86
+
87
+ try {
88
+ const toVersion: ConvertVersion = '2.0.0';
89
+ const options: ConvertOptions = {
90
+ id: 'urn:com.asyncapi.streetlights'
91
+ };
92
+
93
+ const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
94
+ console.log(convert(asyncapi, toVersion, options));
59
95
  } catch (e) {
60
96
  console.error(e)
61
97
  }
@@ -64,3 +100,42 @@ try {
64
100
  ## Known missing features
65
101
 
66
102
  * When converting from 1.x to 2.x, Streaming APIs (those using `stream` instead of `topics` or `events`) are converted correctly but information about framing type and delimiter is missing until a [protocolInfo](https://github.com/asyncapi/extensions-catalog/issues/1) for that purpose is created.
103
+
104
+ ## Development
105
+
106
+ 1. Setup project by installing dependencies `npm install`
107
+ 2. Write code and tests.
108
+ 3. Make sure all tests pass `npm test`
109
+
110
+ ## Contribution
111
+
112
+ Read [CONTRIBUTING](https://github.com/asyncapi/.github/blob/master/CONTRIBUTING.md) guide.
113
+
114
+ ## Contributors ✨
115
+
116
+ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
117
+
118
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
119
+ <!-- prettier-ignore-start -->
120
+ <!-- markdownlint-disable -->
121
+ <table>
122
+ <tr>
123
+ <td align="center"><a href="https://github.com/magicmatatjahu"><img src="https://avatars.githubusercontent.com/u/20404945?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Maciej Urbańczyk</b></sub></a><br /><a href="#maintenance-magicmatatjahu" title="Maintenance">🚧</a> <a href="https://github.com/asyncapi/converter-js/commits?author=magicmatatjahu" title="Code">💻</a> <a href="https://github.com/asyncapi/converter-js/issues?q=author%3Amagicmatatjahu" title="Bug reports">🐛</a> <a href="https://github.com/asyncapi/converter-js/pulls?q=is%3Apr+reviewed-by%3Amagicmatatjahu" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/asyncapi/converter-js/commits?author=magicmatatjahu" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/converter-js/commits?author=magicmatatjahu" title="Documentation">📖</a></td>
124
+ <td align="center"><a href="http://www.fmvilas.com/"><img src="https://avatars.githubusercontent.com/u/242119?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fran Méndez</b></sub></a><br /><a href="#maintenance-fmvilas" title="Maintenance">🚧</a> <a href="https://github.com/asyncapi/converter-js/commits?author=fmvilas" title="Code">💻</a> <a href="https://github.com/asyncapi/converter-js/issues?q=author%3Afmvilas" title="Bug reports">🐛</a> <a href="https://github.com/asyncapi/converter-js/pulls?q=is%3Apr+reviewed-by%3Afmvilas" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/asyncapi/converter-js/commits?author=fmvilas" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/converter-js/commits?author=fmvilas" title="Documentation">📖</a></td>
125
+ <td align="center"><a href="https://www.brainfart.dev/"><img src="https://avatars.githubusercontent.com/u/6995927?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lukasz Gornicki</b></sub></a><br /><a href="#maintenance-derberg" title="Maintenance">🚧</a> <a href="https://github.com/asyncapi/converter-js/commits?author=derberg" title="Code">💻</a> <a href="https://github.com/asyncapi/converter-js/issues?q=author%3Aderberg" title="Bug reports">🐛</a> <a href="https://github.com/asyncapi/converter-js/pulls?q=is%3Apr+reviewed-by%3Aderberg" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/asyncapi/converter-js/commits?author=derberg" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/converter-js/commits?author=derberg" title="Documentation">📖</a> <a href="#infra-derberg" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
126
+ <td align="center"><a href="https://github.com/germanschnyder"><img src="https://avatars.githubusercontent.com/u/1844525?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Germán Schnyder</b></sub></a><br /><a href="https://github.com/asyncapi/converter-js/commits?author=germanschnyder" title="Code">💻</a> <a href="https://github.com/asyncapi/converter-js/commits?author=germanschnyder" title="Tests">⚠️</a></td>
127
+ <td align="center"><a href="https://github.com/bszwarc"><img src="https://avatars.githubusercontent.com/u/17266942?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Barbara Czyż</b></sub></a><br /><a href="#infra-bszwarc" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
128
+ <td align="center"><a href="https://github.com/depimomo"><img src="https://avatars.githubusercontent.com/u/12368942?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depimomo</b></sub></a><br /><a href="https://github.com/asyncapi/converter-js/commits?author=depimomo" title="Code">💻</a></td>
129
+ <td align="center"><a href="https://github.com/crypto-cmd"><img src="https://avatars.githubusercontent.com/u/54287503?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Orville Daley</b></sub></a><br /><a href="https://github.com/asyncapi/converter-js/commits?author=crypto-cmd" title="Code">💻</a></td>
130
+ </tr>
131
+ <tr>
132
+ <td align="center"><a href="https://www.arctype.co/"><img src="https://avatars.githubusercontent.com/u/549273?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ryan R Sundberg</b></sub></a><br /><a href="https://github.com/asyncapi/converter-js/commits?author=sundbry" title="Code">💻</a></td>
133
+ </tr>
134
+ </table>
135
+
136
+ <!-- markdownlint-restore -->
137
+ <!-- prettier-ignore-end -->
138
+
139
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
140
+
141
+ This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
@@ -0,0 +1,3 @@
1
+ import type { AsyncAPIDocument, ConvertVersion, ConvertOptions } from './interfaces';
2
+ export declare function convert(asyncapi: string, version: ConvertVersion, options?: ConvertOptions): string;
3
+ export declare function convert(asyncapi: AsyncAPIDocument, version: ConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
package/lib/convert.js ADDED
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.convert = void 0;
15
+ const js_yaml_1 = require("js-yaml");
16
+ const utils_1 = require("./utils");
17
+ /**
18
+ * Value for key (version) represents the function which converts specification from previous version to the given as key.
19
+ */
20
+ const conversions = {
21
+ '1.0.0': from__undefined__to__1_0_0,
22
+ '1.1.0': from__1_0_0__to__1_1_0,
23
+ '1.2.0': from__1_1_0__to__1_2_0,
24
+ '2.0.0-rc1': from__1_2_0__to__2_0_0_rc1,
25
+ '2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2,
26
+ '2.0.0': from__2_0_0_rc2__to__2_0_0,
27
+ '2.1.0': from__2_0_0__to__2_1_0,
28
+ '2.2.0': from__2_1_0__to__2_2_0,
29
+ '2.3.0': from__2_2_0__to__2_3_0,
30
+ '2.4.0': from__2_3_0__to__2_4_0,
31
+ };
32
+ const conversionVersions = Object.keys(conversions);
33
+ function convert(asyncapi, version, options = {}) {
34
+ const { format, document } = (0, utils_1.serializeInput)(asyncapi);
35
+ const asyncapiVersion = document.asyncapi;
36
+ let fromVersion = conversionVersions.indexOf(asyncapiVersion);
37
+ const toVersion = conversionVersions.indexOf(version);
38
+ if (fromVersion === -1 || toVersion === -1) {
39
+ throw new Error(`Cannot convert from ${asyncapiVersion} to ${version}.`);
40
+ }
41
+ if (fromVersion > toVersion) {
42
+ throw new Error(`Cannot downgrade from ${asyncapiVersion} to ${version}.`);
43
+ }
44
+ if (fromVersion === toVersion) {
45
+ throw new Error(`Cannot convert to the same version.`);
46
+ }
47
+ // add 1 to `fromVersion` because we convert from previous to next
48
+ fromVersion++;
49
+ let converted = document;
50
+ for (let i = fromVersion; i <= toVersion; i++) {
51
+ converted = conversions[conversionVersions[i]](converted, options);
52
+ }
53
+ if (format === 'yaml') {
54
+ return (0, js_yaml_1.dump)(converted, { skipInvalid: true });
55
+ }
56
+ return converted;
57
+ }
58
+ exports.convert = convert;
59
+ function from__undefined__to__1_0_0(asyncapi, _) {
60
+ asyncapi.asyncapi = '1.0.0';
61
+ return asyncapi;
62
+ }
63
+ function from__1_0_0__to__1_1_0(asyncapi, _) {
64
+ asyncapi.asyncapi = '1.1.0';
65
+ return asyncapi;
66
+ }
67
+ function from__1_1_0__to__1_2_0(asyncapi, _) {
68
+ asyncapi.asyncapi = '1.2.0';
69
+ return asyncapi;
70
+ }
71
+ function from__1_2_0__to__2_0_0_rc1(asyncapi, _) {
72
+ asyncapi.asyncapi = '2.0.0-rc1';
73
+ if (asyncapi.servers) {
74
+ const security = asyncapi.security;
75
+ asyncapi.servers = asyncapi.servers.map((server) => {
76
+ const { scheme, schemeVersion } = server, rest = __rest(server, ["scheme", "schemeVersion"]);
77
+ const out = Object.assign(Object.assign({}, rest), { protocol: scheme });
78
+ if (schemeVersion) {
79
+ out.protocolVersion = schemeVersion;
80
+ }
81
+ if (security) {
82
+ out.security = security;
83
+ }
84
+ return out;
85
+ });
86
+ }
87
+ if (asyncapi.topics) {
88
+ const baseTopic = asyncapi.baseTopic ? `${asyncapi.baseTopic}.` : "";
89
+ asyncapi.channels = Object.entries(asyncapi.topics).reduce((newChannels, [channelName, channel]) => {
90
+ if (channel.publish) {
91
+ channel.publish = { message: channel.publish };
92
+ }
93
+ if (channel.subscribe) {
94
+ channel.subscribe = { message: channel.subscribe };
95
+ }
96
+ channelName = (0, utils_1.dotsToSlashes)(`${baseTopic}${channelName}`);
97
+ newChannels[channelName] = channel;
98
+ return newChannels;
99
+ }, {});
100
+ }
101
+ else if (asyncapi.stream) {
102
+ asyncapi.channels = {
103
+ '/': (0, utils_1.streamToChannel)(asyncapi.stream),
104
+ };
105
+ }
106
+ else if (asyncapi.events) {
107
+ asyncapi.channels = {
108
+ '/': (0, utils_1.eventToChannel)(asyncapi.events),
109
+ };
110
+ }
111
+ delete asyncapi.topics;
112
+ delete asyncapi.stream;
113
+ delete asyncapi.events;
114
+ delete asyncapi.baseTopic;
115
+ delete asyncapi.security;
116
+ return asyncapi;
117
+ }
118
+ function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi, _) {
119
+ asyncapi.asyncapi = '2.0.0-rc2';
120
+ if (asyncapi.servers) {
121
+ const serverMap = {};
122
+ asyncapi.servers.forEach((server, index) => {
123
+ if (server.baseChannel)
124
+ delete server.baseChannel;
125
+ const name = index === 0 ? 'default' : `server${index}`;
126
+ serverMap[name] = server;
127
+ });
128
+ asyncapi.servers = serverMap;
129
+ }
130
+ if (asyncapi.channels) {
131
+ Object.entries(asyncapi.channels).forEach(([channelName, channel]) => {
132
+ if (channel.parameters) {
133
+ const parametersMap = {};
134
+ const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2)); // NOSONAR
135
+ channel.parameters.forEach((parameter, index) => {
136
+ const name = parameter.name || paramNames[index];
137
+ if (parameter.name)
138
+ delete parameter.name;
139
+ parametersMap[name] = parameter;
140
+ });
141
+ channel.parameters = parametersMap;
142
+ }
143
+ if (channel.publish && channel.publish.message) {
144
+ const message = channel.publish.message;
145
+ (0, utils_1.convertMessage)(message);
146
+ }
147
+ if (channel.subscribe && channel.subscribe.message) {
148
+ const message = channel.subscribe.message;
149
+ (0, utils_1.convertMessage)(message);
150
+ }
151
+ if (channel.protocolInfo) {
152
+ channel.bindings = channel.protocolInfo;
153
+ delete channel.protocolInfo;
154
+ }
155
+ if (channel.publish && channel.publish.protocolInfo) {
156
+ channel.publish.bindings = channel.publish.protocolInfo;
157
+ delete channel.publish.protocolInfo;
158
+ }
159
+ if (channel.subscribe && channel.subscribe.protocolInfo) {
160
+ channel.subscribe.bindings = channel.subscribe.protocolInfo;
161
+ delete channel.subscribe.protocolInfo;
162
+ }
163
+ });
164
+ }
165
+ if (asyncapi.components && asyncapi.components.parameters) {
166
+ Object.values(asyncapi.components.parameters).forEach((parameter) => {
167
+ if (parameter.name)
168
+ delete parameter.name;
169
+ });
170
+ }
171
+ return asyncapi;
172
+ }
173
+ function from__2_0_0_rc2__to__2_0_0(asyncapi, _) {
174
+ asyncapi.asyncapi = '2.0.0';
175
+ return asyncapi;
176
+ }
177
+ function from__2_0_0__to__2_1_0(asyncapi, _) {
178
+ asyncapi.asyncapi = '2.1.0';
179
+ return asyncapi;
180
+ }
181
+ function from__2_1_0__to__2_2_0(asyncapi, _) {
182
+ asyncapi.asyncapi = '2.2.0';
183
+ return asyncapi;
184
+ }
185
+ function from__2_2_0__to__2_3_0(asyncapi, _) {
186
+ asyncapi.asyncapi = '2.3.0';
187
+ return asyncapi;
188
+ }
189
+ function from__2_3_0__to__2_4_0(asyncapi, _) {
190
+ asyncapi.asyncapi = '2.4.0';
191
+ return asyncapi;
192
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { convert } from './convert';
2
+ export type { ConvertVersion, ConvertOptions } from './interfaces';
package/lib/index.js CHANGED
@@ -1,212 +1,5 @@
1
- const yaml = require('js-yaml');
2
- const _ = require('lodash');
3
- const {
4
- serialize,
5
- dotsToSlashes,
6
- eventToChannel,
7
- streamToChannel,
8
- convertMessage,
9
- } = require('./helpers');
10
-
11
- const lib = module.exports;
12
-
13
- /**
14
- * Value for key (version) represents the function which converts specification from previous version to the given as key.
15
- */
16
- const conversions = {
17
- '1.0.0': undefined,
18
- '1.1.0': from__1_0_0__to__1_1_0,
19
- '1.2.0': from__1_1_0__to__1_2_0,
20
- '2.0.0-rc1': from__1_2_0__to__2_0_0_rc1,
21
- '2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2,
22
- '2.0.0': from__2_0_0_rc2__to__2_0_0,
23
- '2.1.0': from__2_0_0__to__2_1_0,
24
- '2.2.0': from__2_1_0__to__2_2_0,
25
- '2.3.0': from__2_2_0__to__2_3_0,
26
- }
27
- const conversionVersions = Object.keys(conversions);
28
-
29
- lib.convert = (asyncapi, version, options = {}) => {
30
- const { isYAML, parsed } = serialize(asyncapi);
31
- if (parsed === undefined) return '';
32
-
33
- let fromVersion = conversionVersions.indexOf(parsed.asyncapi);
34
- const toVersion = conversionVersions.indexOf(version);
35
-
36
- if (fromVersion === -1 || toVersion === -1) {
37
- throw new Error(`Cannot convert from ${parsed.asyncapi} to ${version}.`);
38
- }
39
- if (fromVersion > toVersion) {
40
- throw new Error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`);
41
- }
42
- if (fromVersion === toVersion) {
43
- throw new Error(`Cannot convert to the same version.`);
44
- }
45
-
46
- // add 1 to `fromVersion` because we convert from previous to next
47
- fromVersion++;
48
- let converted = parsed;
49
- for (let i = fromVersion; i <= toVersion; i++) {
50
- const fn = conversions[conversionVersions[i]];
51
- converted = fn(converted, options);
52
- }
53
-
54
- if (isYAML) {
55
- converted = yaml.safeDump(converted, { skipInvalid: true });
56
- }
57
- return converted;
58
- };
59
-
60
- function from__1_0_0__to__1_1_0(asyncapi) {
61
- return asyncapi;
62
- }
63
-
64
- function from__1_1_0__to__1_2_0(asyncapi) {
65
- return asyncapi;
66
- }
67
-
68
- function from__1_2_0__to__2_0_0_rc1(asyncapi1, options) { // NOSONAR
69
- const result = asyncapi1;
70
-
71
- result.asyncapi = '2.0.0-rc1';
72
- result.id = options.id || `urn:${asyncapi1.info.title.toLowerCase().split(' ').join('.')}`;
73
-
74
- if (asyncapi1.servers) {
75
- const security = asyncapi1.security;
76
- result.servers = asyncapi1.servers.map(server => {
77
- const { scheme, schemeVersion, ...rest } = server;
78
-
79
- const out = {
80
- ...rest,
81
- ...{
82
- protocol: scheme,
83
- }
84
- };
85
-
86
- if (schemeVersion) out.protocolVersion = schemeVersion;
87
-
88
- if (security) {
89
- out.security = security;
90
- }
91
-
92
- return out;
93
- });
94
- }
95
-
96
- if (asyncapi1.topics) {
97
- const baseTopic = asyncapi1.baseTopic ? `${asyncapi1.baseTopic}.` : "";
98
- result.channels = _.mapKeys(result.topics, (__, topicName) => dotsToSlashes(`${baseTopic}${topicName}`));
99
- _.map(result.channels, ch => {
100
- if (ch.publish) {
101
- ch.publish = { message: ch.publish };
102
- }
103
- if (ch.subscribe) {
104
- ch.subscribe = { message: ch.subscribe };
105
- }
106
- });
107
- } else if (asyncapi1.stream) {
108
- result.channels = {
109
- '/': streamToChannel(asyncapi1.stream),
110
- };
111
- } else if (asyncapi1.events) {
112
- result.channels = {
113
- '/': eventToChannel(asyncapi1.events),
114
- };
115
- }
116
-
117
- delete result.topics;
118
- delete result.stream;
119
- delete result.events;
120
- delete result.baseTopic;
121
- delete result.security;
122
-
123
- return result;
124
- }
125
-
126
- function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi2rc1, options) { // NOSONAR
127
- const result = asyncapi2rc1;
128
-
129
- result.asyncapi = '2.0.0-rc2';
130
- result.id = result.id || options.id;
131
-
132
- if (asyncapi2rc1.servers) {
133
- const serverMap = {};
134
- asyncapi2rc1.servers.forEach((server, index) => {
135
- if (server.baseChannel) delete server.baseChannel;
136
- const name = index === 0 ? 'default' : `server${index}`;
137
- serverMap[name] = server;
138
- });
139
- result.servers = serverMap;
140
- }
141
-
142
- if (result.channels) {
143
- _.each(result.channels, (channel, channelName) => {
144
- if (channel.parameters) {
145
- const parametersMap = {};
146
- const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2));
147
- channel.parameters.forEach((parameter, index) => {
148
- const name = parameter.name || paramNames[index];
149
- if (parameter.name) delete parameter.name;
150
- parametersMap[name] = parameter;
151
- });
152
- channel.parameters = parametersMap;
153
- }
154
-
155
- if (channel.publish && channel.publish.message) {
156
- const message = channel.publish.message;
157
- convertMessage(message);
158
- }
159
-
160
- if (channel.subscribe && channel.subscribe.message) {
161
- const message = channel.subscribe.message;
162
- convertMessage(message);
163
- }
164
-
165
- if (channel.protocolInfo) {
166
- channel.bindings = channel.protocolInfo;
167
- delete channel.protocolInfo;
168
- }
169
-
170
- if (channel.publish && channel.publish.protocolInfo) {
171
- channel.publish.bindings = channel.publish.protocolInfo;
172
- delete channel.publish.protocolInfo;
173
- }
174
-
175
- if (channel.subscribe && channel.subscribe.protocolInfo) {
176
- channel.subscribe.bindings = channel.subscribe.protocolInfo;
177
- delete channel.subscribe.protocolInfo;
178
- }
179
- });
180
- }
181
-
182
- if (result.components && result.components.parameters) {
183
- _.each(result.components.parameters, (parameter) => {
184
- if (parameter.name) delete parameter.name;
185
- });
186
- }
187
-
188
- if (!options.id) delete result.id;
189
- return result;
190
- }
191
-
192
- function from__2_0_0_rc2__to__2_0_0(asyncapi2rc2, options) {
193
- const result = asyncapi2rc2;
194
- if (!options.id) delete result.id;
195
- result.asyncapi = '2.0.0';
196
- return result;
197
- }
198
-
199
- function from__2_0_0__to__2_1_0(asyncapi) {
200
- asyncapi.asyncapi = '2.1.0';
201
- return asyncapi;
202
- }
203
-
204
- function from__2_1_0__to__2_2_0(asyncapi) {
205
- asyncapi.asyncapi = '2.2.0';
206
- return asyncapi;
207
- }
208
-
209
- function from__2_2_0__to__2_3_0(asyncapi) {
210
- asyncapi.asyncapi = '2.3.0';
211
- return asyncapi;
212
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convert = void 0;
4
+ var convert_1 = require("./convert");
5
+ Object.defineProperty(exports, "convert", { enumerable: true, get: function () { return convert_1.convert; } });
@@ -0,0 +1,6 @@
1
+ export declare type AsyncAPIDocument = {
2
+ asyncapi: string;
3
+ } & Record<string, any>;
4
+ export declare type ConvertVersion = '1.1.0' | '1.2.0' | '2.0.0-rc1' | '2.0.0-rc2' | '2.0.0' | '2.1.0' | '2.2.0' | '2.3.0' | '2.4.0';
5
+ export interface ConvertOptions {
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/utils.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import type { AsyncAPIDocument } from "./interfaces";
2
+ export declare function serializeInput(document: string | AsyncAPIDocument): {
3
+ format: 'json' | 'yaml';
4
+ document: AsyncAPIDocument;
5
+ } | never;
6
+ export declare function eventToChannel(event: any): {
7
+ publish: any;
8
+ subscribe: any;
9
+ };
10
+ export declare function streamToChannel(stream: any): {
11
+ publish: any;
12
+ subscribe: any;
13
+ };
14
+ export declare function objectToSchema(obj: Record<string, unknown>): {
15
+ type: string;
16
+ properties: {
17
+ [x: string]: unknown;
18
+ };
19
+ };
20
+ export declare function dotsToSlashes(topic: string): string;
21
+ export declare function convertMessage(message: any): void;