@asyncapi/converter 0.8.0 → 0.11.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 +30 -4
  2. package/cli.js +18 -6
  3. package/lib/convert.d.ts +3 -0
  4. package/lib/convert.js +198 -0
  5. package/lib/index.d.ts +2 -0
  6. package/lib/index.js +5 -212
  7. package/lib/interfaces.d.ts +7 -0
  8. package/lib/interfaces.js +2 -0
  9. package/lib/utils.d.ts +21 -0
  10. package/lib/utils.js +114 -0
  11. package/package.json +21 -6
  12. package/.github/workflows/add-good-first-issue-labels.yml +0 -68
  13. package/.github/workflows/automerge-for-humans-add-ready-to-merge-or-do-not-merge-label.yml +0 -54
  14. package/.github/workflows/automerge-for-humans-merging.yml +0 -32
  15. package/.github/workflows/automerge-for-humans-remove-ready-to-merge-label-on-edit.yml +0 -35
  16. package/.github/workflows/automerge-orphans.yml +0 -63
  17. package/.github/workflows/automerge.yml +0 -50
  18. package/.github/workflows/autoupdate.yml +0 -32
  19. package/.github/workflows/bump.yml +0 -33
  20. package/.github/workflows/help-command.yml +0 -43
  21. package/.github/workflows/if-go-pr-testing.yml +0 -68
  22. package/.github/workflows/if-nodejs-pr-testing.yml +0 -60
  23. package/.github/workflows/if-nodejs-release.yml +0 -85
  24. package/.github/workflows/if-nodejs-version-bump.yml +0 -48
  25. package/.github/workflows/issues-prs-notifications.yml +0 -72
  26. package/.github/workflows/lint-pr-title.yml +0 -22
  27. package/.github/workflows/notify-tsc-members-mention.yml +0 -142
  28. package/.github/workflows/release-announcements.yml +0 -76
  29. package/.github/workflows/sentiment-analysis.yml +0 -44
  30. package/.github/workflows/stale-issues-prs.yml +0 -42
  31. package/.github/workflows/welcome-first-time-contrib.yml +0 -83
  32. package/CODEOWNERS +0 -8
  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
@@ -34,22 +34,48 @@ id: 'urn:com.asynapi.streetlights'
34
34
  ...
35
35
  ```
36
36
 
37
- Save the result in a file:
37
+ Save the result in a file by stream:
38
38
 
39
39
  ```sh
40
40
  asyncapi-converter streetlights.yml > streetlights2.yml
41
41
  ```
42
42
 
43
- ### As a package
43
+ Save the result in a file by `-o, --output` flag:
44
+
45
+ ```sh
46
+ asyncapi-converter streetlights.yml -o streetlights2.yml
47
+ ```
48
+
49
+ ### In JS
44
50
 
45
51
  ```js
52
+ const fs = require('fs');
46
53
  const { convert } = require('@asyncapi/converter')
47
54
 
48
55
  try {
49
56
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
50
57
  console.log(convert(asyncapi, '2.0.0', {
51
- id: 'urn:com.asyncapi.streetlights'
52
- }))
58
+ id: 'urn:com.asyncapi.streetlights'
59
+ }));
60
+ } catch (e) {
61
+ console.error(e);
62
+ }
63
+ ```
64
+
65
+ ### In TS
66
+
67
+ ```ts
68
+ import { convert } from '@asyncapi/converter';
69
+ import type { ConvertVersion, ConvertOptions } from '@asyncapi/converter';
70
+
71
+ try {
72
+ const toVersion: ConvertVersion = '2.0.0';
73
+ const options: ConvertOptions = {
74
+ id: 'urn:com.asyncapi.streetlights'
75
+ };
76
+
77
+ const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
78
+ console.log(convert(asyncapi, toVersion, options));
53
79
  } catch (e) {
54
80
  console.error(e)
55
81
  }
package/cli.js CHANGED
@@ -10,6 +10,16 @@ const red = text => `\x1b[31m${text}\x1b[0m`;
10
10
 
11
11
  let asyncapiFile;
12
12
  let version;
13
+ let output;
14
+
15
+ const parseArguments = (asyncAPIPath, v) => {
16
+ asyncapiFile = path.resolve(asyncAPIPath);
17
+ version = v;
18
+ }
19
+
20
+ const parseOutput = (filePath) => {
21
+ output = path.resolve(filePath);
22
+ }
13
23
 
14
24
  const showErrorAndExit = err => {
15
25
  console.error(red('Something went wrong:'));
@@ -20,11 +30,9 @@ const showErrorAndExit = err => {
20
30
  program
21
31
  .version(packageInfo.version)
22
32
  .arguments('<document> [version]')
23
- .action((asyncAPIPath, v) => {
24
- asyncapiFile = path.resolve(asyncAPIPath);
25
- version = v;
26
- })
33
+ .action(parseArguments)
27
34
  .option('--id <id>', 'application id (defaults to a generated one)')
35
+ .option('-o, --output <outputFile>', 'file where to put the converted AsyncAPI document', parseOutput)
28
36
  .parse(process.argv);
29
37
 
30
38
  if (!asyncapiFile) {
@@ -32,7 +40,7 @@ if (!asyncapiFile) {
32
40
  program.help(); // This exits the process
33
41
  }
34
42
  if (!version) {
35
- version = '2.3.0';
43
+ version = '2.4.0';
36
44
  }
37
45
 
38
46
  try {
@@ -46,7 +54,11 @@ try {
46
54
  converted = JSON.stringify(converted, undefined, 2);
47
55
  }
48
56
 
49
- console.log(converted);
57
+ if (output) {
58
+ fs.writeFileSync(output, converted, 'utf-8');
59
+ } else {
60
+ console.log(converted);
61
+ }
50
62
  } catch (e) {
51
63
  showErrorAndExit(e);
52
64
  }
@@ -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,198 @@
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, options) {
72
+ asyncapi.asyncapi = '2.0.0-rc1';
73
+ asyncapi.id = options.id || `urn:${asyncapi.info.title.toLowerCase().split(' ').join('.')}`;
74
+ if (asyncapi.servers) {
75
+ const security = asyncapi.security;
76
+ asyncapi.servers = asyncapi.servers.map((server) => {
77
+ const { scheme, schemeVersion } = server, rest = __rest(server, ["scheme", "schemeVersion"]);
78
+ const out = Object.assign(Object.assign({}, rest), { protocol: scheme });
79
+ if (schemeVersion) {
80
+ out.protocolVersion = schemeVersion;
81
+ }
82
+ if (security) {
83
+ out.security = security;
84
+ }
85
+ return out;
86
+ });
87
+ }
88
+ if (asyncapi.topics) {
89
+ const baseTopic = asyncapi.baseTopic ? `${asyncapi.baseTopic}.` : "";
90
+ asyncapi.channels = Object.entries(asyncapi.topics).reduce((newChannels, [channelName, channel]) => {
91
+ if (channel.publish) {
92
+ channel.publish = { message: channel.publish };
93
+ }
94
+ if (channel.subscribe) {
95
+ channel.subscribe = { message: channel.subscribe };
96
+ }
97
+ channelName = (0, utils_1.dotsToSlashes)(`${baseTopic}${channelName}`);
98
+ newChannels[channelName] = channel;
99
+ return newChannels;
100
+ }, {});
101
+ }
102
+ else if (asyncapi.stream) {
103
+ asyncapi.channels = {
104
+ '/': (0, utils_1.streamToChannel)(asyncapi.stream),
105
+ };
106
+ }
107
+ else if (asyncapi.events) {
108
+ asyncapi.channels = {
109
+ '/': (0, utils_1.eventToChannel)(asyncapi.events),
110
+ };
111
+ }
112
+ delete asyncapi.topics;
113
+ delete asyncapi.stream;
114
+ delete asyncapi.events;
115
+ delete asyncapi.baseTopic;
116
+ delete asyncapi.security;
117
+ return asyncapi;
118
+ }
119
+ function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi, options) {
120
+ asyncapi.asyncapi = '2.0.0-rc2';
121
+ asyncapi.id = asyncapi.id || options.id;
122
+ if (asyncapi.servers) {
123
+ const serverMap = {};
124
+ asyncapi.servers.forEach((server, index) => {
125
+ if (server.baseChannel)
126
+ delete server.baseChannel;
127
+ const name = index === 0 ? 'default' : `server${index}`;
128
+ serverMap[name] = server;
129
+ });
130
+ asyncapi.servers = serverMap;
131
+ }
132
+ if (asyncapi.channels) {
133
+ Object.entries(asyncapi.channels).forEach(([channelName, channel]) => {
134
+ if (channel.parameters) {
135
+ const parametersMap = {};
136
+ const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2)); // NOSONAR
137
+ channel.parameters.forEach((parameter, index) => {
138
+ const name = parameter.name || paramNames[index];
139
+ if (parameter.name)
140
+ delete parameter.name;
141
+ parametersMap[name] = parameter;
142
+ });
143
+ channel.parameters = parametersMap;
144
+ }
145
+ if (channel.publish && channel.publish.message) {
146
+ const message = channel.publish.message;
147
+ (0, utils_1.convertMessage)(message);
148
+ }
149
+ if (channel.subscribe && channel.subscribe.message) {
150
+ const message = channel.subscribe.message;
151
+ (0, utils_1.convertMessage)(message);
152
+ }
153
+ if (channel.protocolInfo) {
154
+ channel.bindings = channel.protocolInfo;
155
+ delete channel.protocolInfo;
156
+ }
157
+ if (channel.publish && channel.publish.protocolInfo) {
158
+ channel.publish.bindings = channel.publish.protocolInfo;
159
+ delete channel.publish.protocolInfo;
160
+ }
161
+ if (channel.subscribe && channel.subscribe.protocolInfo) {
162
+ channel.subscribe.bindings = channel.subscribe.protocolInfo;
163
+ delete channel.subscribe.protocolInfo;
164
+ }
165
+ });
166
+ }
167
+ if (asyncapi.components && asyncapi.components.parameters) {
168
+ Object.values(asyncapi.components.parameters).forEach((parameter) => {
169
+ if (parameter.name)
170
+ delete parameter.name;
171
+ });
172
+ }
173
+ if (!options.id)
174
+ delete asyncapi.id;
175
+ return asyncapi;
176
+ }
177
+ function from__2_0_0_rc2__to__2_0_0(asyncapi, options) {
178
+ asyncapi.asyncapi = '2.0.0';
179
+ if (!options.id)
180
+ delete asyncapi.id;
181
+ return asyncapi;
182
+ }
183
+ function from__2_0_0__to__2_1_0(asyncapi, _) {
184
+ asyncapi.asyncapi = '2.1.0';
185
+ return asyncapi;
186
+ }
187
+ function from__2_1_0__to__2_2_0(asyncapi, _) {
188
+ asyncapi.asyncapi = '2.2.0';
189
+ return asyncapi;
190
+ }
191
+ function from__2_2_0__to__2_3_0(asyncapi, _) {
192
+ asyncapi.asyncapi = '2.3.0';
193
+ return asyncapi;
194
+ }
195
+ function from__2_3_0__to__2_4_0(asyncapi, _) {
196
+ asyncapi.asyncapi = '2.4.0';
197
+ return asyncapi;
198
+ }
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,7 @@
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
+ id?: string;
7
+ }
@@ -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;
package/lib/utils.js ADDED
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertMessage = exports.dotsToSlashes = exports.objectToSchema = exports.streamToChannel = exports.eventToChannel = exports.serializeInput = void 0;
4
+ const js_yaml_1 = require("js-yaml");
5
+ function serializeInput(document) {
6
+ let triedConvertToYaml = false;
7
+ try {
8
+ if (typeof document === 'object') {
9
+ return {
10
+ format: 'json',
11
+ document: JSON.parse(JSON.stringify(document)), // copy object
12
+ };
13
+ }
14
+ const maybeJSON = JSON.parse(document);
15
+ if (typeof maybeJSON === 'object') {
16
+ return {
17
+ format: 'json',
18
+ document: maybeJSON,
19
+ };
20
+ }
21
+ triedConvertToYaml = true; // NOSONAR
22
+ // if `maybeJSON` is object, then we have 100% sure that we operate on JSON,
23
+ // but if it's `string` then we have option that it can be YAML but it doesn't have to be
24
+ return {
25
+ format: 'yaml',
26
+ document: (0, js_yaml_1.load)(document),
27
+ };
28
+ }
29
+ catch (e) {
30
+ try {
31
+ if (triedConvertToYaml) {
32
+ throw e;
33
+ }
34
+ // try to parse (again) YAML, because the text itself may not have a JSON representation and cannot be represented as a JSON object/string
35
+ return {
36
+ format: 'yaml',
37
+ document: (0, js_yaml_1.load)(document),
38
+ };
39
+ }
40
+ catch (err) {
41
+ throw new Error('AsyncAPI document must be a valid JSON or YAML document.');
42
+ }
43
+ }
44
+ }
45
+ exports.serializeInput = serializeInput;
46
+ function eventToChannel(event) {
47
+ const out = {};
48
+ if (event.receive) {
49
+ out.publish = {
50
+ message: {
51
+ oneOf: event.receive
52
+ }
53
+ };
54
+ }
55
+ if (event.send) {
56
+ out.subscribe = {
57
+ message: {
58
+ oneOf: event.send
59
+ }
60
+ };
61
+ }
62
+ return out;
63
+ }
64
+ exports.eventToChannel = eventToChannel;
65
+ function streamToChannel(stream) {
66
+ const out = {};
67
+ if (stream.read) {
68
+ out.publish = {
69
+ message: {
70
+ oneOf: stream.read
71
+ }
72
+ };
73
+ }
74
+ if (stream.write) {
75
+ out.subscribe = {
76
+ message: {
77
+ oneOf: stream.write
78
+ }
79
+ };
80
+ }
81
+ return out;
82
+ }
83
+ exports.streamToChannel = streamToChannel;
84
+ function objectToSchema(obj) {
85
+ return { type: 'object', properties: Object.assign({}, obj) };
86
+ }
87
+ exports.objectToSchema = objectToSchema;
88
+ function dotsToSlashes(topic) {
89
+ return topic.replace(/\./g, '/');
90
+ }
91
+ exports.dotsToSlashes = dotsToSlashes;
92
+ function convertMessage(message) {
93
+ if (message.oneOf) {
94
+ message.oneOf.forEach((m) => {
95
+ if (m.protocolInfo) {
96
+ m.bindings = m.protocolInfo;
97
+ delete m.protocolInfo;
98
+ }
99
+ if (m.headers) {
100
+ m.headers = objectToSchema(m.headers);
101
+ }
102
+ });
103
+ }
104
+ else {
105
+ if (message.protocolInfo) {
106
+ message.bindings = message.protocolInfo;
107
+ delete message.protocolInfo;
108
+ }
109
+ if (message.headers) {
110
+ message.headers = objectToSchema(message.headers);
111
+ }
112
+ }
113
+ }
114
+ exports.convertMessage = convertMessage;