@asyncapi/converter 1.2.0 → 1.2.1

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 CHANGED
@@ -32,7 +32,7 @@ npm i @asyncapi/converter
32
32
 
33
33
  ### From CLI
34
34
 
35
- To convert an AsyncAPi document in the console needs the official [AsyncAPI CLI](https://github.com/asyncapi/cli).
35
+ To convert an AsyncAPI document in the console needs the official [AsyncAPI CLI](https://github.com/asyncapi/cli).
36
36
 
37
37
  If you don't have CLI installed, run this command to install the CLI globally on your system:
38
38
 
@@ -51,7 +51,7 @@ channels:
51
51
  ...
52
52
  ```
53
53
 
54
- Convert to specific version:
54
+ Convert to a specific version:
55
55
 
56
56
  ```sh
57
57
  asyncapi convert streetlights.yml -o streetlights2.yml -t 2.3.0
@@ -70,9 +70,7 @@ const { convert } = require('@asyncapi/converter')
70
70
 
71
71
  try {
72
72
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
73
- console.log(convert(asyncapi, '2.0.0', {
74
- id: 'urn:com.asyncapi.streetlights'
75
- }));
73
+ console.log(convert(asyncapi, '2.6.0'));
76
74
  } catch (e) {
77
75
  console.error(e);
78
76
  }
@@ -85,13 +83,9 @@ import { convert } from '@asyncapi/converter';
85
83
  import type { ConvertVersion, ConvertOptions } from '@asyncapi/converter';
86
84
 
87
85
  try {
88
- const toVersion: ConvertVersion = '2.0.0';
89
- const options: ConvertOptions = {
90
- id: 'urn:com.asyncapi.streetlights'
91
- };
92
-
86
+ const toVersion: ConvertVersion = '2.6.0';
93
87
  const asyncapi = fs.readFileSync('streetlights.yml', 'utf-8')
94
- console.log(convert(asyncapi, toVersion, options));
88
+ console.log(convert(asyncapi, toVersion));
95
89
  } catch (e) {
96
90
  console.error(e)
97
91
  }
package/lib/convert.js CHANGED
@@ -1,37 +1,15 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.convert = void 0;
15
4
  const js_yaml_1 = require("js-yaml");
5
+ const first_version_1 = require("./first-version");
6
+ const second_version_1 = require("./second-version");
16
7
  const utils_1 = require("./utils");
17
8
  /**
18
9
  * Value for key (version) represents the function which converts specification from previous version to the given as key.
19
10
  */
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
- '2.5.0': from__2_4_0__to__2_5_0,
32
- '2.6.0': from__2_5_0__to__2_6_0,
33
- };
34
- const conversionVersions = Object.keys(conversions);
11
+ const converters = Object.assign(Object.assign({}, first_version_1.converters), second_version_1.converters);
12
+ const conversionVersions = Object.keys(converters);
35
13
  function convert(asyncapi, version = '2.6.0', options = {}) {
36
14
  const { format, document } = (0, utils_1.serializeInput)(asyncapi);
37
15
  const asyncapiVersion = document.asyncapi;
@@ -50,7 +28,8 @@ function convert(asyncapi, version = '2.6.0', options = {}) {
50
28
  fromVersion++;
51
29
  let converted = document;
52
30
  for (let i = fromVersion; i <= toVersion; i++) {
53
- converted = conversions[conversionVersions[i]](converted, options);
31
+ const v = conversionVersions[i];
32
+ converted = converters[v](converted, options);
54
33
  }
55
34
  if (format === 'yaml') {
56
35
  return (0, js_yaml_1.dump)(converted, { skipInvalid: true });
@@ -58,145 +37,3 @@ function convert(asyncapi, version = '2.6.0', options = {}) {
58
37
  return converted;
59
38
  }
60
39
  exports.convert = convert;
61
- function from__undefined__to__1_0_0(asyncapi, _) {
62
- asyncapi.asyncapi = '1.0.0';
63
- return asyncapi;
64
- }
65
- function from__1_0_0__to__1_1_0(asyncapi, _) {
66
- asyncapi.asyncapi = '1.1.0';
67
- return asyncapi;
68
- }
69
- function from__1_1_0__to__1_2_0(asyncapi, _) {
70
- asyncapi.asyncapi = '1.2.0';
71
- return asyncapi;
72
- }
73
- function from__1_2_0__to__2_0_0_rc1(asyncapi, _) {
74
- asyncapi.asyncapi = '2.0.0-rc1';
75
- if (asyncapi.servers) {
76
- const security = asyncapi.security;
77
- asyncapi.servers = asyncapi.servers.map((server) => {
78
- const { scheme, schemeVersion } = server, rest = __rest(server, ["scheme", "schemeVersion"]);
79
- const out = Object.assign(Object.assign({}, rest), { protocol: scheme });
80
- if (schemeVersion) {
81
- out.protocolVersion = schemeVersion;
82
- }
83
- if (security) {
84
- out.security = security;
85
- }
86
- return out;
87
- });
88
- }
89
- if (asyncapi.topics) {
90
- const baseTopic = asyncapi.baseTopic ? `${asyncapi.baseTopic}.` : "";
91
- asyncapi.channels = Object.entries(asyncapi.topics).reduce((newChannels, [channelName, channel]) => {
92
- if (channel.publish) {
93
- channel.publish = { message: channel.publish };
94
- }
95
- if (channel.subscribe) {
96
- channel.subscribe = { message: channel.subscribe };
97
- }
98
- channelName = (0, utils_1.dotsToSlashes)(`${baseTopic}${channelName}`);
99
- newChannels[channelName] = channel;
100
- return newChannels;
101
- }, {});
102
- }
103
- else if (asyncapi.stream) {
104
- asyncapi.channels = {
105
- '/': (0, utils_1.streamToChannel)(asyncapi.stream),
106
- };
107
- }
108
- else if (asyncapi.events) {
109
- asyncapi.channels = {
110
- '/': (0, utils_1.eventToChannel)(asyncapi.events),
111
- };
112
- }
113
- delete asyncapi.topics;
114
- delete asyncapi.stream;
115
- delete asyncapi.events;
116
- delete asyncapi.baseTopic;
117
- delete asyncapi.security;
118
- return asyncapi;
119
- }
120
- function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi, _) {
121
- asyncapi.asyncapi = '2.0.0-rc2';
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
- return asyncapi;
174
- }
175
- function from__2_0_0_rc2__to__2_0_0(asyncapi, _) {
176
- asyncapi.asyncapi = '2.0.0';
177
- return asyncapi;
178
- }
179
- function from__2_0_0__to__2_1_0(asyncapi, _) {
180
- asyncapi.asyncapi = '2.1.0';
181
- return asyncapi;
182
- }
183
- function from__2_1_0__to__2_2_0(asyncapi, _) {
184
- asyncapi.asyncapi = '2.2.0';
185
- return asyncapi;
186
- }
187
- function from__2_2_0__to__2_3_0(asyncapi, _) {
188
- asyncapi.asyncapi = '2.3.0';
189
- return asyncapi;
190
- }
191
- function from__2_3_0__to__2_4_0(asyncapi, _) {
192
- asyncapi.asyncapi = '2.4.0';
193
- return asyncapi;
194
- }
195
- function from__2_4_0__to__2_5_0(asyncapi, _) {
196
- asyncapi.asyncapi = '2.5.0';
197
- return asyncapi;
198
- }
199
- function from__2_5_0__to__2_6_0(asyncapi, _) {
200
- asyncapi.asyncapi = '2.6.0';
201
- return asyncapi;
202
- }
@@ -0,0 +1,2 @@
1
+ import type { ConvertFunction } from './interfaces';
2
+ export declare const converters: Record<string, ConvertFunction>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.converters = void 0;
4
+ exports.converters = {
5
+ '1.0.0': from__undefined__to__1_0_0,
6
+ '1.1.0': from__1_0_0__to__1_1_0,
7
+ '1.2.0': from__1_1_0__to__1_2_0,
8
+ };
9
+ function from__undefined__to__1_0_0(asyncapi, _) {
10
+ asyncapi.asyncapi = '1.0.0';
11
+ return asyncapi;
12
+ }
13
+ function from__1_0_0__to__1_1_0(asyncapi, _) {
14
+ asyncapi.asyncapi = '1.1.0';
15
+ return asyncapi;
16
+ }
17
+ function from__1_1_0__to__1_2_0(asyncapi, _) {
18
+ asyncapi.asyncapi = '1.2.0';
19
+ return asyncapi;
20
+ }
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { convert } from './convert';
2
- export type { ConvertVersion, ConvertOptions } from './interfaces';
2
+ export type { AsyncAPIDocument, ConvertVersion, ConvertOptions } from './interfaces';
@@ -1,6 +1,12 @@
1
+ /**
2
+ * PUBLIC TYPES
3
+ */
1
4
  export declare type AsyncAPIDocument = {
2
5
  asyncapi: string;
3
6
  } & Record<string, any>;
4
7
  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' | '2.5.0' | '2.6.0';
5
- export interface ConvertOptions {
6
- }
8
+ export declare type ConvertOptions = {};
9
+ /**
10
+ * PRIVATE TYPES
11
+ */
12
+ export declare type ConvertFunction = (asyncapi: AsyncAPIDocument, options: ConvertOptions) => AsyncAPIDocument;
@@ -0,0 +1,2 @@
1
+ import type { ConvertFunction } from './interfaces';
2
+ export declare const converters: Record<string, ConvertFunction>;
@@ -0,0 +1,217 @@
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.converters = void 0;
15
+ const utils_1 = require("./utils");
16
+ exports.converters = {
17
+ '2.0.0-rc1': from__1_2_0__to__2_0_0_rc1,
18
+ '2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2,
19
+ '2.0.0': from__2_0_0_rc2__to__2_0_0,
20
+ '2.1.0': from__2_0_0__to__2_1_0,
21
+ '2.2.0': from__2_1_0__to__2_2_0,
22
+ '2.3.0': from__2_2_0__to__2_3_0,
23
+ '2.4.0': from__2_3_0__to__2_4_0,
24
+ '2.5.0': from__2_4_0__to__2_5_0,
25
+ '2.6.0': from__2_5_0__to__2_6_0,
26
+ };
27
+ function from__1_2_0__to__2_0_0_rc1(asyncapi, _) {
28
+ asyncapi.asyncapi = '2.0.0-rc1';
29
+ if (asyncapi.servers) {
30
+ const security = asyncapi.security;
31
+ asyncapi.servers = asyncapi.servers.map((server) => {
32
+ const { scheme, schemeVersion } = server, rest = __rest(server, ["scheme", "schemeVersion"]);
33
+ const out = Object.assign(Object.assign({}, rest), { protocol: scheme });
34
+ if (schemeVersion) {
35
+ out.protocolVersion = schemeVersion;
36
+ }
37
+ if (security) {
38
+ out.security = security;
39
+ }
40
+ return out;
41
+ });
42
+ }
43
+ if (asyncapi.topics) {
44
+ const baseTopic = asyncapi.baseTopic ? `${asyncapi.baseTopic}.` : "";
45
+ asyncapi.channels = Object.entries(asyncapi.topics).reduce((newChannels, [channelName, channel]) => {
46
+ if (channel.publish) {
47
+ channel.publish = { message: channel.publish };
48
+ }
49
+ if (channel.subscribe) {
50
+ channel.subscribe = { message: channel.subscribe };
51
+ }
52
+ channelName = (0, utils_1.dotsToSlashes)(`${baseTopic}${channelName}`);
53
+ newChannels[channelName] = channel;
54
+ return newChannels;
55
+ }, {});
56
+ }
57
+ else if (asyncapi.stream) {
58
+ asyncapi.channels = {
59
+ '/': streamToChannel(asyncapi.stream),
60
+ };
61
+ }
62
+ else if (asyncapi.events) {
63
+ asyncapi.channels = {
64
+ '/': eventToChannel(asyncapi.events),
65
+ };
66
+ }
67
+ delete asyncapi.topics;
68
+ delete asyncapi.stream;
69
+ delete asyncapi.events;
70
+ delete asyncapi.baseTopic;
71
+ delete asyncapi.security;
72
+ return asyncapi;
73
+ }
74
+ function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi, _) {
75
+ asyncapi.asyncapi = '2.0.0-rc2';
76
+ if (asyncapi.servers) {
77
+ const serverMap = {};
78
+ asyncapi.servers.forEach((server, index) => {
79
+ if (server.baseChannel)
80
+ delete server.baseChannel;
81
+ const name = index === 0 ? 'default' : `server${index}`;
82
+ serverMap[name] = server;
83
+ });
84
+ asyncapi.servers = serverMap;
85
+ }
86
+ if (asyncapi.channels) {
87
+ Object.entries(asyncapi.channels).forEach(([channelName, channel]) => {
88
+ if (channel.parameters) {
89
+ const parametersMap = {};
90
+ const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2)); // NOSONAR
91
+ channel.parameters.forEach((parameter, index) => {
92
+ const name = parameter.name || paramNames[index];
93
+ if (parameter.name)
94
+ delete parameter.name;
95
+ parametersMap[name] = parameter;
96
+ });
97
+ channel.parameters = parametersMap;
98
+ }
99
+ if (channel.publish && channel.publish.message) {
100
+ const message = channel.publish.message;
101
+ convertMessage(message);
102
+ }
103
+ if (channel.subscribe && channel.subscribe.message) {
104
+ const message = channel.subscribe.message;
105
+ convertMessage(message);
106
+ }
107
+ if (channel.protocolInfo) {
108
+ channel.bindings = channel.protocolInfo;
109
+ delete channel.protocolInfo;
110
+ }
111
+ if (channel.publish && channel.publish.protocolInfo) {
112
+ channel.publish.bindings = channel.publish.protocolInfo;
113
+ delete channel.publish.protocolInfo;
114
+ }
115
+ if (channel.subscribe && channel.subscribe.protocolInfo) {
116
+ channel.subscribe.bindings = channel.subscribe.protocolInfo;
117
+ delete channel.subscribe.protocolInfo;
118
+ }
119
+ });
120
+ }
121
+ if (asyncapi.components && asyncapi.components.parameters) {
122
+ Object.values(asyncapi.components.parameters).forEach((parameter) => {
123
+ if (parameter.name)
124
+ delete parameter.name;
125
+ });
126
+ }
127
+ return asyncapi;
128
+ }
129
+ function from__2_0_0_rc2__to__2_0_0(asyncapi, _) {
130
+ asyncapi.asyncapi = '2.0.0';
131
+ return asyncapi;
132
+ }
133
+ function from__2_0_0__to__2_1_0(asyncapi, _) {
134
+ asyncapi.asyncapi = '2.1.0';
135
+ return asyncapi;
136
+ }
137
+ function from__2_1_0__to__2_2_0(asyncapi, _) {
138
+ asyncapi.asyncapi = '2.2.0';
139
+ return asyncapi;
140
+ }
141
+ function from__2_2_0__to__2_3_0(asyncapi, _) {
142
+ asyncapi.asyncapi = '2.3.0';
143
+ return asyncapi;
144
+ }
145
+ function from__2_3_0__to__2_4_0(asyncapi, _) {
146
+ asyncapi.asyncapi = '2.4.0';
147
+ return asyncapi;
148
+ }
149
+ function from__2_4_0__to__2_5_0(asyncapi, _) {
150
+ asyncapi.asyncapi = '2.5.0';
151
+ return asyncapi;
152
+ }
153
+ function from__2_5_0__to__2_6_0(asyncapi, _) {
154
+ asyncapi.asyncapi = '2.6.0';
155
+ return asyncapi;
156
+ }
157
+ function eventToChannel(event) {
158
+ const out = {};
159
+ if (event.receive) {
160
+ out.publish = {
161
+ message: {
162
+ oneOf: event.receive
163
+ }
164
+ };
165
+ }
166
+ if (event.send) {
167
+ out.subscribe = {
168
+ message: {
169
+ oneOf: event.send
170
+ }
171
+ };
172
+ }
173
+ return out;
174
+ }
175
+ function streamToChannel(stream) {
176
+ const out = {};
177
+ if (stream.read) {
178
+ out.publish = {
179
+ message: {
180
+ oneOf: stream.read
181
+ }
182
+ };
183
+ }
184
+ if (stream.write) {
185
+ out.subscribe = {
186
+ message: {
187
+ oneOf: stream.write
188
+ }
189
+ };
190
+ }
191
+ return out;
192
+ }
193
+ function convertMessage(message) {
194
+ if (message.oneOf) {
195
+ message.oneOf.forEach((m) => {
196
+ if (m.protocolInfo) {
197
+ m.bindings = m.protocolInfo;
198
+ delete m.protocolInfo;
199
+ }
200
+ if (m.headers) {
201
+ m.headers = objectToSchema(m.headers);
202
+ }
203
+ });
204
+ }
205
+ else {
206
+ if (message.protocolInfo) {
207
+ message.bindings = message.protocolInfo;
208
+ delete message.protocolInfo;
209
+ }
210
+ if (message.headers) {
211
+ message.headers = objectToSchema(message.headers);
212
+ }
213
+ }
214
+ }
215
+ function objectToSchema(obj) {
216
+ return { type: 'object', properties: Object.assign({}, obj) };
217
+ }
package/lib/utils.d.ts CHANGED
@@ -3,14 +3,6 @@ export declare function serializeInput(document: string | AsyncAPIDocument): {
3
3
  format: 'json' | 'yaml';
4
4
  document: AsyncAPIDocument;
5
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
6
  export declare function objectToSchema(obj: Record<string, unknown>): {
15
7
  type: string;
16
8
  properties: {
@@ -18,4 +10,12 @@ export declare function objectToSchema(obj: Record<string, unknown>): {
18
10
  };
19
11
  };
20
12
  export declare function dotsToSlashes(topic: string): string;
21
- export declare function convertMessage(message: any): void;
13
+ export declare function isPlainObject(value: unknown): boolean;
14
+ export declare function createRefObject(...paths: Array<string | number>): {
15
+ $ref: string;
16
+ };
17
+ export declare function createRefPath(...paths: Array<string | number>): string;
18
+ export declare function isRefObject(value: unknown): boolean;
19
+ export declare function getValueByRef(root: any, ref: string): any;
20
+ export declare function getValueByPath(value: any, path: string[]): any;
21
+ export declare function sortObjectKeys(obj: any, keys: string[]): any;
package/lib/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertMessage = exports.dotsToSlashes = exports.objectToSchema = exports.streamToChannel = exports.eventToChannel = exports.serializeInput = void 0;
3
+ exports.sortObjectKeys = exports.getValueByPath = exports.getValueByRef = exports.isRefObject = exports.createRefPath = exports.createRefObject = exports.isPlainObject = exports.dotsToSlashes = exports.objectToSchema = exports.serializeInput = void 0;
4
4
  const js_yaml_1 = require("js-yaml");
5
5
  function serializeInput(document) {
6
6
  let triedConvertToYaml = false;
@@ -43,44 +43,6 @@ function serializeInput(document) {
43
43
  }
44
44
  }
45
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
46
  function objectToSchema(obj) {
85
47
  return { type: 'object', properties: Object.assign({}, obj) };
86
48
  }
@@ -89,26 +51,76 @@ function dotsToSlashes(topic) {
89
51
  return topic.replace(/\./g, '/');
90
52
  }
91
53
  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
- });
54
+ function isPlainObject(value) {
55
+ return Boolean(value && typeof value === 'object' && !Array.isArray(value));
56
+ }
57
+ exports.isPlainObject = isPlainObject;
58
+ function createRefObject(...paths) {
59
+ return {
60
+ $ref: createRefPath(...paths),
61
+ };
62
+ }
63
+ exports.createRefObject = createRefObject;
64
+ function createRefPath(...paths) {
65
+ return `#/${paths.map(String).map(tilde).join('/')}`;
66
+ }
67
+ exports.createRefPath = createRefPath;
68
+ function isRefObject(value) {
69
+ return Boolean(value && '$ref' in value);
70
+ }
71
+ exports.isRefObject = isRefObject;
72
+ function getValueByRef(root, ref) {
73
+ if (!ref.startsWith('#')) {
74
+ return;
75
+ }
76
+ // remove `#/` at start, split string by `/` and untilde (change ~1 to / etc)
77
+ const path = ref.substring(2).split('/').map(untilde);
78
+ return getValueByPath(root, path);
79
+ }
80
+ exports.getValueByRef = getValueByRef;
81
+ function getValueByPath(value, path) {
82
+ let index = 0;
83
+ const length = path.length;
84
+ while (value != null && index < length) {
85
+ value = value[path[index++]];
103
86
  }
104
- else {
105
- if (message.protocolInfo) {
106
- message.bindings = message.protocolInfo;
107
- delete message.protocolInfo;
87
+ return index == length ? value : undefined;
88
+ }
89
+ exports.getValueByPath = getValueByPath;
90
+ function sortObjectKeys(obj, keys) {
91
+ const newObject = {};
92
+ // add sorted keys
93
+ keys.forEach(key => {
94
+ if (key in obj) {
95
+ newObject[key] = obj[key];
108
96
  }
109
- if (message.headers) {
110
- message.headers = objectToSchema(message.headers);
97
+ });
98
+ // add rest of keys
99
+ Object.keys(obj).forEach(key => {
100
+ if (!keys.includes(key)) {
101
+ newObject[key] = obj[key];
111
102
  }
112
- }
103
+ });
104
+ return newObject;
105
+ }
106
+ exports.sortObjectKeys = sortObjectKeys;
107
+ function tilde(str) {
108
+ return str.replace(/[~/]{1}/g, (sub) => {
109
+ switch (sub) {
110
+ case '/': return '~1';
111
+ case '~': return '~0';
112
+ }
113
+ return sub;
114
+ });
115
+ }
116
+ function untilde(str) {
117
+ if (!str.includes('~'))
118
+ return str;
119
+ return str.replace(/~[01]/g, (sub) => {
120
+ switch (sub) {
121
+ case '~1': return '/';
122
+ case '~0': return '~';
123
+ }
124
+ return sub;
125
+ });
113
126
  }
114
- exports.convertMessage = convertMessage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyncapi/converter",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Convert AsyncAPI documents from older to newer versions.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -8,7 +8,6 @@
8
8
  "build": "tsc",
9
9
  "test": "cross-env CI=true NODE_ENV=test jest --coverage",
10
10
  "lint": "echo 'no linter configured yet'",
11
- "release": "semantic-release",
12
11
  "generate:assets": "npm run generate:readme:toc",
13
12
  "generate:readme:toc": "markdown-toc -i README.md",
14
13
  "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION",
@@ -39,44 +38,19 @@
39
38
  "author": "Fran Mendez <fmvilas@gmail.com> (fmvilas.com)",
40
39
  "license": "Apache-2.0",
41
40
  "dependencies": {
41
+ "@asyncapi/parser": "^2.0.0-next-major.11",
42
42
  "js-yaml": "^3.14.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@jest/types": "^27.5.1",
46
- "@semantic-release/commit-analyzer": "^9.0.2",
47
- "@semantic-release/github": "^8.0.2",
48
- "@semantic-release/npm": "^8.0.3",
49
- "@semantic-release/release-notes-generator": "^10.0.3",
50
46
  "@types/jest": "^27.4.1",
51
47
  "@types/js-yaml": "^4.0.5",
52
- "conventional-changelog-conventionalcommits": "^4.6.3",
48
+ "conventional-changelog-conventionalcommits": "^5.0.0",
53
49
  "cross-env": "^7.0.3",
54
50
  "jest": "^27.5.1",
55
- "semantic-release": "^19.0.3",
51
+ "markdown-toc": "^1.2.0",
56
52
  "ts-jest": "^27.1.3",
57
53
  "ts-node": "^10.7.0",
58
- "typescript": "^4.6.2",
59
- "markdown-toc": "^1.2.0"
60
- },
61
- "release": {
62
- "branches": [
63
- "master"
64
- ],
65
- "plugins": [
66
- [
67
- "@semantic-release/commit-analyzer",
68
- {
69
- "preset": "conventionalcommits"
70
- }
71
- ],
72
- [
73
- "@semantic-release/release-notes-generator",
74
- {
75
- "preset": "conventionalcommits"
76
- }
77
- ],
78
- "@semantic-release/npm",
79
- "@semantic-release/github"
80
- ]
54
+ "typescript": "^4.6.2"
81
55
  }
82
56
  }