@asyncapi/converter 0.9.0 → 0.10.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 (57) hide show
  1. package/README.md +23 -3
  2. package/lib/convert.d.ts +3 -0
  3. package/lib/convert.js +193 -0
  4. package/lib/index.d.ts +2 -0
  5. package/lib/index.js +5 -212
  6. package/lib/interfaces.d.ts +7 -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 +21 -6
  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/lib/helpers.js +0 -109
  33. package/test/index.js +0 -257
  34. package/test/input/1.0.0/streetlights.yml +0 -120
  35. package/test/input/1.1.0/streetlights.yml +0 -120
  36. package/test/input/1.2.0/gitter-streaming.yml +0 -140
  37. package/test/input/1.2.0/slack-rtm.yml +0 -876
  38. package/test/input/1.2.0/streetlights.yml +0 -120
  39. package/test/input/2.0.0/streetlights.json +0 -172
  40. package/test/input/2.0.0/streetlights.yml +0 -112
  41. package/test/input/2.0.0-rc1/streetlights.yml +0 -109
  42. package/test/input/2.0.0-rc2/streetlights.yml +0 -112
  43. package/test/input/2.1.0/streetlights.yml +0 -112
  44. package/test/input/2.2.0/streetlights.yml +0 -112
  45. package/test/output/2.0.0/gitter-streaming.yml +0 -137
  46. package/test/output/2.0.0/slack-rtm.yml +0 -879
  47. package/test/output/2.0.0/streetlights.yml +0 -112
  48. package/test/output/2.0.0-rc1/gitter-streaming.yml +0 -137
  49. package/test/output/2.0.0-rc1/slack-rtm.yml +0 -879
  50. package/test/output/2.0.0-rc1/streetlights.yml +0 -109
  51. package/test/output/2.0.0-rc2/gitter-streaming.yml +0 -137
  52. package/test/output/2.0.0-rc2/slack-rtm.yml +0 -879
  53. package/test/output/2.0.0-rc2/streetlights.yml +0 -112
  54. package/test/output/2.1.0/streetlights.json +0 -172
  55. package/test/output/2.1.0/streetlights.yml +0 -112
  56. package/test/output/2.2.0/streetlights.yml +0 -112
  57. package/test/output/2.3.0/streetlights.yml +0 -112
package/README.md CHANGED
@@ -46,16 +46,36 @@ Save the result in a file by `-o, --output` flag:
46
46
  asyncapi-converter streetlights.yml -o streetlights2.yml
47
47
  ```
48
48
 
49
- ### As a package
49
+ ### In JS
50
50
 
51
51
  ```js
52
+ const fs = require('fs');
52
53
  const { convert } = require('@asyncapi/converter')
53
54
 
54
55
  try {
55
56
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
56
57
  console.log(convert(asyncapi, '2.0.0', {
57
- id: 'urn:com.asyncapi.streetlights'
58
- }))
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));
59
79
  } catch (e) {
60
80
  console.error(e)
61
81
  }
@@ -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,193 @@
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
+ };
31
+ const conversionVersions = Object.keys(conversions);
32
+ function convert(asyncapi, version, options = {}) {
33
+ const { format, document } = (0, utils_1.serializeInput)(asyncapi);
34
+ const asyncapiVersion = document.asyncapi;
35
+ let fromVersion = conversionVersions.indexOf(asyncapiVersion);
36
+ const toVersion = conversionVersions.indexOf(version);
37
+ if (fromVersion === -1 || toVersion === -1) {
38
+ throw new Error(`Cannot convert from ${asyncapiVersion} to ${version}.`);
39
+ }
40
+ if (fromVersion > toVersion) {
41
+ throw new Error(`Cannot downgrade from ${asyncapiVersion} to ${version}.`);
42
+ }
43
+ if (fromVersion === toVersion) {
44
+ throw new Error(`Cannot convert to the same version.`);
45
+ }
46
+ // add 1 to `fromVersion` because we convert from previous to next
47
+ fromVersion++;
48
+ let converted = document;
49
+ for (let i = fromVersion; i <= toVersion; i++) {
50
+ converted = conversions[conversionVersions[i]](converted, options);
51
+ }
52
+ if (format === 'yaml') {
53
+ return (0, js_yaml_1.dump)(converted, { skipInvalid: true });
54
+ }
55
+ return converted;
56
+ }
57
+ exports.convert = convert;
58
+ function from__undefined__to__1_0_0(asyncapi, _) {
59
+ asyncapi.asyncapi = '1.0.0';
60
+ return asyncapi;
61
+ }
62
+ function from__1_0_0__to__1_1_0(asyncapi, _) {
63
+ asyncapi.asyncapi = '1.1.0';
64
+ return asyncapi;
65
+ }
66
+ function from__1_1_0__to__1_2_0(asyncapi, _) {
67
+ asyncapi.asyncapi = '1.2.0';
68
+ return asyncapi;
69
+ }
70
+ function from__1_2_0__to__2_0_0_rc1(asyncapi, options) {
71
+ asyncapi.asyncapi = '2.0.0-rc1';
72
+ asyncapi.id = options.id || `urn:${asyncapi.info.title.toLowerCase().split(' ').join('.')}`;
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, options) {
119
+ asyncapi.asyncapi = '2.0.0-rc2';
120
+ asyncapi.id = asyncapi.id || options.id;
121
+ if (asyncapi.servers) {
122
+ const serverMap = {};
123
+ asyncapi.servers.forEach((server, index) => {
124
+ if (server.baseChannel)
125
+ delete server.baseChannel;
126
+ const name = index === 0 ? 'default' : `server${index}`;
127
+ serverMap[name] = server;
128
+ });
129
+ asyncapi.servers = serverMap;
130
+ }
131
+ if (asyncapi.channels) {
132
+ Object.entries(asyncapi.channels).forEach(([channelName, channel]) => {
133
+ if (channel.parameters) {
134
+ const parametersMap = {};
135
+ const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2)); // NOSONAR
136
+ channel.parameters.forEach((parameter, index) => {
137
+ const name = parameter.name || paramNames[index];
138
+ if (parameter.name)
139
+ delete parameter.name;
140
+ parametersMap[name] = parameter;
141
+ });
142
+ channel.parameters = parametersMap;
143
+ }
144
+ if (channel.publish && channel.publish.message) {
145
+ const message = channel.publish.message;
146
+ (0, utils_1.convertMessage)(message);
147
+ }
148
+ if (channel.subscribe && channel.subscribe.message) {
149
+ const message = channel.subscribe.message;
150
+ (0, utils_1.convertMessage)(message);
151
+ }
152
+ if (channel.protocolInfo) {
153
+ channel.bindings = channel.protocolInfo;
154
+ delete channel.protocolInfo;
155
+ }
156
+ if (channel.publish && channel.publish.protocolInfo) {
157
+ channel.publish.bindings = channel.publish.protocolInfo;
158
+ delete channel.publish.protocolInfo;
159
+ }
160
+ if (channel.subscribe && channel.subscribe.protocolInfo) {
161
+ channel.subscribe.bindings = channel.subscribe.protocolInfo;
162
+ delete channel.subscribe.protocolInfo;
163
+ }
164
+ });
165
+ }
166
+ if (asyncapi.components && asyncapi.components.parameters) {
167
+ Object.values(asyncapi.components.parameters).forEach((parameter) => {
168
+ if (parameter.name)
169
+ delete parameter.name;
170
+ });
171
+ }
172
+ if (!options.id)
173
+ delete asyncapi.id;
174
+ return asyncapi;
175
+ }
176
+ function from__2_0_0_rc2__to__2_0_0(asyncapi, options) {
177
+ asyncapi.asyncapi = '2.0.0';
178
+ if (!options.id)
179
+ delete asyncapi.id;
180
+ return asyncapi;
181
+ }
182
+ function from__2_0_0__to__2_1_0(asyncapi, _) {
183
+ asyncapi.asyncapi = '2.1.0';
184
+ return asyncapi;
185
+ }
186
+ function from__2_1_0__to__2_2_0(asyncapi, _) {
187
+ asyncapi.asyncapi = '2.2.0';
188
+ return asyncapi;
189
+ }
190
+ function from__2_2_0__to__2_3_0(asyncapi, _) {
191
+ asyncapi.asyncapi = '2.3.0';
192
+ return asyncapi;
193
+ }
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';
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;
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@asyncapi/converter",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Convert AsyncAPI documents from older to newer versions.",
5
5
  "bin": {
6
6
  "asyncapi-converter": "cli.js"
7
7
  },
8
8
  "main": "lib/index.js",
9
+ "types": "lib/index.d.ts",
9
10
  "scripts": {
10
- "test": "mocha",
11
+ "build": "tsc",
12
+ "test": "cross-env CI=true NODE_ENV=test jest --coverage",
11
13
  "lint": "echo 'no linter configured yet'",
12
14
  "release": "semantic-release",
13
15
  "generate:assets": "echo 'No additional assets need to be generated at the moment'",
14
- "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION"
16
+ "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION",
17
+ "prepublishOnly": "npm run build"
15
18
  },
16
19
  "keywords": [
17
20
  "asyncapi",
@@ -19,6 +22,12 @@
19
22
  "version",
20
23
  "convert"
21
24
  ],
25
+ "files": [
26
+ "/lib",
27
+ "cli.js",
28
+ "./README.md",
29
+ "./LICENSE"
30
+ ],
22
31
  "repository": {
23
32
  "type": "git",
24
33
  "url": "git+https://github.com/asyncapi/converter-js.git"
@@ -34,17 +43,23 @@
34
43
  "license": "Apache-2.0",
35
44
  "dependencies": {
36
45
  "commander": "^8.3.0",
37
- "js-yaml": "^3.14.1",
38
- "lodash": "^4.17.21"
46
+ "js-yaml": "^3.14.1"
39
47
  },
40
48
  "devDependencies": {
49
+ "@jest/types": "^27.5.1",
41
50
  "@semantic-release/commit-analyzer": "^9.0.2",
42
51
  "@semantic-release/github": "^8.0.2",
43
52
  "@semantic-release/npm": "^8.0.3",
44
53
  "@semantic-release/release-notes-generator": "^10.0.3",
54
+ "@types/jest": "^27.4.1",
55
+ "@types/js-yaml": "^4.0.5",
45
56
  "conventional-changelog-conventionalcommits": "^4.6.3",
57
+ "cross-env": "^7.0.3",
58
+ "jest": "^27.5.1",
46
59
  "semantic-release": "^18.0.1",
47
- "mocha": "^9.1.3"
60
+ "ts-jest": "^27.1.3",
61
+ "ts-node": "^10.7.0",
62
+ "typescript": "^4.6.2"
48
63
  },
49
64
  "release": {
50
65
  "branches": [