@asyncapi/converter 0.7.2 → 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.
- package/README.md +30 -4
- package/cli.js +17 -5
- package/lib/convert.d.ts +3 -0
- package/lib/convert.js +193 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -215
- package/lib/interfaces.d.ts +7 -0
- package/lib/interfaces.js +2 -0
- package/lib/utils.d.ts +21 -0
- package/lib/utils.js +114 -0
- package/package.json +21 -6
- package/.github/workflows/add-good-first-issue-labels.yml +0 -68
- package/.github/workflows/automerge-for-humans-add-ready-to-merge-or-do-not-merge-label.yml +0 -54
- package/.github/workflows/automerge-for-humans-merging.yml +0 -32
- package/.github/workflows/automerge-for-humans-remove-ready-to-merge-label-on-edit.yml +0 -35
- package/.github/workflows/automerge-orphans.yml +0 -63
- package/.github/workflows/automerge.yml +0 -50
- package/.github/workflows/autoupdate.yml +0 -32
- package/.github/workflows/bump.yml +0 -33
- package/.github/workflows/help-command.yml +0 -43
- package/.github/workflows/if-go-pr-testing.yml +0 -68
- package/.github/workflows/if-nodejs-pr-testing.yml +0 -60
- package/.github/workflows/if-nodejs-release.yml +0 -85
- package/.github/workflows/if-nodejs-version-bump.yml +0 -48
- package/.github/workflows/issues-prs-notifications.yml +0 -72
- package/.github/workflows/lint-pr-title.yml +0 -22
- package/.github/workflows/notify-tsc-members-mention.yml +0 -142
- package/.github/workflows/release-announcements.yml +0 -76
- package/.github/workflows/sentiment-analysis.yml +0 -44
- package/.github/workflows/stale-issues-prs.yml +0 -42
- package/.github/workflows/welcome-first-time-contrib.yml +0 -83
- package/CODEOWNERS +0 -8
- package/lib/helpers.js +0 -109
- package/test/index.js +0 -251
- package/test/input/1.0.0/streetlights.yml +0 -120
- package/test/input/1.1.0/streetlights.yml +0 -120
- package/test/input/1.2.0/gitter-streaming.yml +0 -140
- package/test/input/1.2.0/slack-rtm.yml +0 -876
- package/test/input/1.2.0/streetlights.yml +0 -120
- package/test/input/2.0.0/streetlights.json +0 -172
- package/test/input/2.0.0/streetlights.yml +0 -112
- package/test/input/2.0.0-rc1/streetlights.yml +0 -109
- package/test/input/2.0.0-rc2/streetlights.yml +0 -112
- package/test/input/2.1.0/streetlights.yml +0 -112
- package/test/input/2.2.0/streetlights.yml +0 -112
- package/test/output/2.0.0/gitter-streaming.yml +0 -137
- package/test/output/2.0.0/slack-rtm.yml +0 -879
- package/test/output/2.0.0/streetlights.yml +0 -112
- package/test/output/2.0.0-rc1/gitter-streaming.yml +0 -137
- package/test/output/2.0.0-rc1/slack-rtm.yml +0 -879
- package/test/output/2.0.0-rc1/streetlights.yml +0 -109
- package/test/output/2.0.0-rc2/gitter-streaming.yml +0 -137
- package/test/output/2.0.0-rc2/slack-rtm.yml +0 -879
- package/test/output/2.0.0-rc2/streetlights.yml +0 -112
- package/test/output/2.1.0/streetlights.json +0 -172
- package/test/output/2.1.0/streetlights.yml +0 -112
- package/test/output/2.2.0/streetlights.yml +0 -112
- 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
|
-
|
|
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
|
-
|
|
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(
|
|
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) {
|
|
@@ -46,7 +54,11 @@ try {
|
|
|
46
54
|
converted = JSON.stringify(converted, undefined, 2);
|
|
47
55
|
}
|
|
48
56
|
|
|
49
|
-
|
|
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
|
}
|
package/lib/convert.d.ts
ADDED
|
@@ -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
package/lib/index.js
CHANGED
|
@@ -1,215 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
console.error(`Cannot convert from ${parsed.asyncapi} to ${version}.`);
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (fromVersion > toVersion) {
|
|
41
|
-
console.error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
if (fromVersion === toVersion) {
|
|
45
|
-
console.error(`Cannot convert to the same version.`);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// add 1 to `fromVersion` because we convert from previous to next
|
|
50
|
-
fromVersion++;
|
|
51
|
-
let converted = parsed;
|
|
52
|
-
for (let i = fromVersion; i <= toVersion; i++) {
|
|
53
|
-
const fn = conversions[conversionVersions[i]];
|
|
54
|
-
converted = fn(converted, options);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (isYAML) {
|
|
58
|
-
converted = yaml.safeDump(converted, { skipInvalid: true });
|
|
59
|
-
}
|
|
60
|
-
return converted;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
function from__1_0_0__to__1_1_0(asyncapi) {
|
|
64
|
-
return asyncapi;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function from__1_1_0__to__1_2_0(asyncapi) {
|
|
68
|
-
return asyncapi;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function from__1_2_0__to__2_0_0_rc1(asyncapi1, options) { // NOSONAR
|
|
72
|
-
const result = asyncapi1;
|
|
73
|
-
|
|
74
|
-
result.asyncapi = '2.0.0-rc1';
|
|
75
|
-
result.id = options.id || `urn:${asyncapi1.info.title.toLowerCase().split(' ').join('.')}`;
|
|
76
|
-
|
|
77
|
-
if (asyncapi1.servers) {
|
|
78
|
-
const security = asyncapi1.security;
|
|
79
|
-
result.servers = asyncapi1.servers.map(server => {
|
|
80
|
-
const { scheme, schemeVersion, ...rest } = server;
|
|
81
|
-
|
|
82
|
-
const out = {
|
|
83
|
-
...rest,
|
|
84
|
-
...{
|
|
85
|
-
protocol: scheme,
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
if (schemeVersion) out.protocolVersion = schemeVersion;
|
|
90
|
-
|
|
91
|
-
if (security) {
|
|
92
|
-
out.security = security;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return out;
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (asyncapi1.topics) {
|
|
100
|
-
const baseTopic = asyncapi1.baseTopic ? `${asyncapi1.baseTopic}.` : "";
|
|
101
|
-
result.channels = _.mapKeys(result.topics, (__, topicName) => dotsToSlashes(`${baseTopic}${topicName}`));
|
|
102
|
-
_.map(result.channels, ch => {
|
|
103
|
-
if (ch.publish) {
|
|
104
|
-
ch.publish = { message: ch.publish };
|
|
105
|
-
}
|
|
106
|
-
if (ch.subscribe) {
|
|
107
|
-
ch.subscribe = { message: ch.subscribe };
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
} else if (asyncapi1.stream) {
|
|
111
|
-
result.channels = {
|
|
112
|
-
'/': streamToChannel(asyncapi1.stream),
|
|
113
|
-
};
|
|
114
|
-
} else if (asyncapi1.events) {
|
|
115
|
-
result.channels = {
|
|
116
|
-
'/': eventToChannel(asyncapi1.events),
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
delete result.topics;
|
|
121
|
-
delete result.stream;
|
|
122
|
-
delete result.events;
|
|
123
|
-
delete result.baseTopic;
|
|
124
|
-
delete result.security;
|
|
125
|
-
|
|
126
|
-
return result;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi2rc1, options) { // NOSONAR
|
|
130
|
-
const result = asyncapi2rc1;
|
|
131
|
-
|
|
132
|
-
result.asyncapi = '2.0.0-rc2';
|
|
133
|
-
result.id = result.id || options.id;
|
|
134
|
-
|
|
135
|
-
if (asyncapi2rc1.servers) {
|
|
136
|
-
const serverMap = {};
|
|
137
|
-
asyncapi2rc1.servers.forEach((server, index) => {
|
|
138
|
-
if (server.baseChannel) delete server.baseChannel;
|
|
139
|
-
const name = index === 0 ? 'default' : `server${index}`;
|
|
140
|
-
serverMap[name] = server;
|
|
141
|
-
});
|
|
142
|
-
result.servers = serverMap;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (result.channels) {
|
|
146
|
-
_.each(result.channels, (channel, channelName) => {
|
|
147
|
-
if (channel.parameters) {
|
|
148
|
-
const parametersMap = {};
|
|
149
|
-
const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2));
|
|
150
|
-
channel.parameters.forEach((parameter, index) => {
|
|
151
|
-
const name = parameter.name || paramNames[index];
|
|
152
|
-
if (parameter.name) delete parameter.name;
|
|
153
|
-
parametersMap[name] = parameter;
|
|
154
|
-
});
|
|
155
|
-
channel.parameters = parametersMap;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (channel.publish && channel.publish.message) {
|
|
159
|
-
const message = channel.publish.message;
|
|
160
|
-
convertMessage(message);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (channel.subscribe && channel.subscribe.message) {
|
|
164
|
-
const message = channel.subscribe.message;
|
|
165
|
-
convertMessage(message);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (channel.protocolInfo) {
|
|
169
|
-
channel.bindings = channel.protocolInfo;
|
|
170
|
-
delete channel.protocolInfo;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (channel.publish && channel.publish.protocolInfo) {
|
|
174
|
-
channel.publish.bindings = channel.publish.protocolInfo;
|
|
175
|
-
delete channel.publish.protocolInfo;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (channel.subscribe && channel.subscribe.protocolInfo) {
|
|
179
|
-
channel.subscribe.bindings = channel.subscribe.protocolInfo;
|
|
180
|
-
delete channel.subscribe.protocolInfo;
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (result.components && result.components.parameters) {
|
|
186
|
-
_.each(result.components.parameters, (parameter) => {
|
|
187
|
-
if (parameter.name) delete parameter.name;
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (!options.id) delete result.id;
|
|
192
|
-
return result;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function from__2_0_0_rc2__to__2_0_0(asyncapi2rc2, options) {
|
|
196
|
-
const result = asyncapi2rc2;
|
|
197
|
-
if (!options.id) delete result.id;
|
|
198
|
-
result.asyncapi = '2.0.0';
|
|
199
|
-
return result;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function from__2_0_0__to__2_1_0(asyncapi) {
|
|
203
|
-
asyncapi.asyncapi = '2.1.0';
|
|
204
|
-
return asyncapi;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function from__2_1_0__to__2_2_0(asyncapi) {
|
|
208
|
-
asyncapi.asyncapi = '2.2.0';
|
|
209
|
-
return asyncapi;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function from__2_2_0__to__2_3_0(asyncapi) {
|
|
213
|
-
asyncapi.asyncapi = '2.3.0';
|
|
214
|
-
return asyncapi;
|
|
215
|
-
}
|
|
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
|
+
}
|
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;
|