@asyncapi/converter 1.5.0 → 1.6.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 CHANGED
@@ -17,7 +17,8 @@ Convert [AsyncAPI](https://asyncapi.com) documents older to newer versions and y
17
17
  * [In TS](#in-ts)
18
18
  - [Conversion 2.x.x to 3.x.x](#conversion-2xx-to-3xx)
19
19
  - [Known missing features](#known-missing-features)
20
- - [OpenAPI 3.0 to AsyncAPI 3.0 Conversion](#openapi-30-to-asyncapi-30-conversion)
20
+ * [OpenAPI 3.0 to AsyncAPI 3.0 Conversion](#openapi-30-to-asyncapi-30-conversion)
21
+ + [Limitations](#limitations)
21
22
  - [Development](#development)
22
23
  - [Contribution](#contribution)
23
24
  - [Contributors ✨](#contributors-%E2%9C%A8)
@@ -195,7 +196,7 @@ Conversion to version `3.x.x` from `2.x.x` has several assumptions that should b
195
196
  examples: ["test"]
196
197
  ```
197
198
 
198
- ### OpenAPI 3.0 to AsyncAPI 3.0 Conversion
199
+ ## OpenAPI 3.0 to AsyncAPI 3.0 Conversion
199
200
 
200
201
  The converter now supports transformation from OpenAPI 3.0 to AsyncAPI 3.0. This feature enables easy transition of existing OpenAPI 3.0 documents to AsyncAPI 3.0.
201
202
 
@@ -203,11 +204,11 @@ To use this new conversion feature:
203
204
 
204
205
  ```js
205
206
  const fs = require('fs');
206
- const { convert } = require('@asyncapi/converter')
207
+ const { convertOpenAPI } = require('@asyncapi/converter')
207
208
 
208
209
  try {
209
210
  const openapi = fs.readFileSync('openapi.yml', 'utf-8')
210
- const asyncapi = convert(openapi, '3.0.0', { from: 'openapi' });
211
+ const asyncapi = convertOpenAPI(openapi, '3.0.0', { from: 'openapi' });
211
212
  console.log(asyncapi);
212
213
  } catch (e) {
213
214
  console.error(e);
@@ -217,11 +218,11 @@ try {
217
218
  When converting from OpenAPI to AsyncAPI you can now specify the perspective of the conversion using the `perspective` option. This allows you to choose whether the conversion should be from an application or client point of view
218
219
 
219
220
  ```js
220
- const { convert } = require('@asyncapi/converter')
221
+ const { convertOpenAPI } = require('@asyncapi/converter')
221
222
 
222
223
  try {
223
224
  const asyncapi2 = fs.readFileSync('asyncapi2.yml', 'utf-8')
224
- const asyncapi3 = convert(asyncapi2, '3.0.0', { openAPIToAsyncAPI: { perspective: 'client' } });
225
+ const asyncapi3 = convertOpenAPI(asyncapi2, '3.0.0', { openAPIToAsyncAPI: { perspective: 'client' } });
225
226
  console.log(asyncapi3);
226
227
  } catch (e) {
227
228
  console.error(e);
@@ -238,6 +239,48 @@ The perspective option can be set to either 'server' (default) or 'client'.
238
239
 
239
240
  - External to internal references: The converter does not support scenarios where an external schema file references internal components of the OpenAPI document. In such cases, manual adjustment of the converted document may be necessary.
240
241
 
242
+ ### Postman Collection to AsyncAPI conversion
243
+
244
+ The converter now also supports conversion from postman collection to AsyncAPI 3.0. This feature enables easy transition of existing postman collection to any AsyncAPI 3.0 documents.
245
+
246
+ To use this new conversion feature:
247
+
248
+ ```js
249
+ const fs = require('fs');
250
+ const { convertPostman } = require('@asyncapi/converter')
251
+ try {
252
+ const postman = fs.readFileSync('postman-collection.yml', 'utf-8')
253
+ const asyncapi = convertPostman(postman, '3.0.0');
254
+ console.log(asyncapi);
255
+ } catch (e) {
256
+ console.error(e);
257
+ }
258
+ ```
259
+
260
+ When converting from postman collection to AsyncAPI you can now specify the perspective of the conversion using the `perspective` option. This allows you to choose whether the conversion should be from an application or client point of view
261
+
262
+ ```js
263
+ const { convertPostman } = require('@asyncapi/converter')
264
+ try {
265
+ const postman = fs.readFileSync('postman-collection.yml', 'utf-8')
266
+ const asyncapi = convertPostman(postman, '3.0.0', { perspective: 'client' });
267
+ console.log(asyncapi);
268
+ } catch (e) {
269
+ console.error(e);
270
+ }
271
+ ```
272
+
273
+ The perspective option can be set to either 'server' (default) or 'client'.
274
+
275
+ - With `server` perspective: `action` becomes `receive`
276
+
277
+ - With `client` perspective: `action` becomes `send`
278
+
279
+ #### Limitations
280
+
281
+ - External to internal references: The converter does not support scenarios where an external schema file references internal components of the OpenAPI document. In such cases, manual adjustment of the converted document may be necessary.
282
+
283
+
241
284
  ## Development
242
285
 
243
286
  1. Setup project by installing dependencies `npm install`
@@ -275,4 +318,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
275
318
 
276
319
  <!-- ALL-CONTRIBUTORS-LIST:END -->
277
320
 
278
- This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
321
+ This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
package/lib/convert.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions, OpenAPIDocument, OpenAPIToAsyncAPIOptions } from './interfaces';
1
+ import type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions, OpenAPIDocument, OpenAPIToAsyncAPIOptions, PostmanToAsyncAPIOptions } from './interfaces';
2
2
  export declare function convert(input: string, version: AsyncAPIConvertVersion, options?: ConvertOptions): string;
3
3
  export declare function convert(input: AsyncAPIDocument, version: AsyncAPIConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
4
4
  export declare function convertOpenAPI(input: string, version: OpenAPIConvertVersion, options?: OpenAPIToAsyncAPIOptions): string;
5
5
  export declare function convertOpenAPI(input: OpenAPIDocument, version: OpenAPIConvertVersion, options?: OpenAPIToAsyncAPIOptions): AsyncAPIDocument;
6
+ export declare function convertPostman(input: string, version: OpenAPIConvertVersion, options?: PostmanToAsyncAPIOptions): string;
7
+ export declare function convertPostman(input: Record<string, any>, version: OpenAPIConvertVersion, options?: PostmanToAsyncAPIOptions): AsyncAPIDocument;
package/lib/convert.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertOpenAPI = exports.convert = void 0;
3
+ exports.convertPostman = exports.convertOpenAPI = exports.convert = void 0;
4
4
  const js_yaml_1 = require("js-yaml");
5
5
  const first_version_1 = require("./first-version");
6
6
  const second_version_1 = require("./second-version");
7
7
  const third_version_1 = require("./third-version");
8
8
  const openapi_1 = require("./openapi");
9
+ const postman_collection_1 = require("./postman-collection");
9
10
  const utils_1 = require("./utils");
10
11
  /**
11
12
  * Value for key (version) represents the function which converts specification from previous version to the given as key.
@@ -57,3 +58,13 @@ function convertOpenAPI(input, version, options = {}) {
57
58
  return convertedAsyncAPI;
58
59
  }
59
60
  exports.convertOpenAPI = convertOpenAPI;
61
+ function convertPostman(input, version, options = {}) {
62
+ const { format, document } = (0, utils_1.serializeInput)(input);
63
+ const postmantoAsyncapiConverter = postman_collection_1.converters[version];
64
+ const convertedAsyncAPI = postmantoAsyncapiConverter(document, options);
65
+ if (format === "yaml") {
66
+ return (0, js_yaml_1.dump)(convertedAsyncAPI, { skipInvalid: true });
67
+ }
68
+ return convertedAsyncAPI;
69
+ }
70
+ exports.convertPostman = convertPostman;
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { convert } from './convert';
1
+ export { convert, convertOpenAPI } from './convert';
2
2
  export type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions } from './interfaces';
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convert = void 0;
3
+ exports.convertOpenAPI = exports.convert = void 0;
4
4
  var convert_1 = require("./convert");
5
5
  Object.defineProperty(exports, "convert", { enumerable: true, get: function () { return convert_1.convert; } });
6
+ Object.defineProperty(exports, "convertOpenAPI", { enumerable: true, get: function () { return convert_1.convertOpenAPI; } });
@@ -29,9 +29,14 @@ export declare type OpenAPIToAsyncAPIOptions = {
29
29
  export declare type ConvertOptions = {
30
30
  v2tov3?: ConvertV2ToV3Options;
31
31
  openAPIToAsyncAPI?: OpenAPIToAsyncAPIOptions;
32
+ postmanToAsyncAPI?: PostmanToAsyncAPIOptions;
33
+ };
34
+ export declare type PostmanToAsyncAPIOptions = {
35
+ perspective?: 'client' | 'server';
32
36
  };
33
37
  /**
34
38
  * PRIVATE TYPES
35
39
  */
36
40
  export declare type ConvertFunction = (asyncapi: AsyncAPIDocument, options: ConvertOptions) => AsyncAPIDocument;
37
41
  export declare type ConvertOpenAPIFunction = (openapi: OpenAPIDocument, options: OpenAPIToAsyncAPIOptions) => AsyncAPIDocument;
42
+ export declare type ConvertPostmanFunction = (postman: any, options: PostmanToAsyncAPIOptions) => AsyncAPIDocument;
package/lib/openapi.d.ts CHANGED
@@ -1,2 +1,9 @@
1
- import { ConvertOpenAPIFunction } from "./interfaces";
1
+ import { AsyncAPIDocument, ConvertOpenAPIFunction, OpenAPIToAsyncAPIOptions, OpenAPIDocument } from "./interfaces";
2
2
  export declare const converters: Record<string, ConvertOpenAPIFunction>;
3
+ /**
4
+ * Converts an OpenAPI document to an AsyncAPI document.
5
+ * @param {OpenAPIDocument} openapi - The OpenAPI document to convert.
6
+ * @param {ConvertOptions} options - Conversion options.
7
+ * @returns {AsyncAPIDocument} The converted AsyncAPI document.
8
+ */
9
+ export declare function from_openapi_to_asyncapi(openapi: OpenAPIDocument, options?: OpenAPIToAsyncAPIOptions): AsyncAPIDocument;
package/lib/openapi.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.converters = void 0;
3
+ exports.from_openapi_to_asyncapi = exports.converters = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  exports.converters = {
6
6
  '3.0.0': from_openapi_to_asyncapi,
@@ -27,6 +27,7 @@ function from_openapi_to_asyncapi(openapi, options = {}) {
27
27
  (0, utils_1.removeEmptyObjects)(asyncapi);
28
28
  return (0, utils_1.sortObjectKeys)(asyncapi, ['asyncapi', 'info', 'defaultContentType', 'servers', 'channels', 'operations', 'components']);
29
29
  }
30
+ exports.from_openapi_to_asyncapi = from_openapi_to_asyncapi;
30
31
  /**
31
32
  * Converts openAPI info objects to asyncAPI info objects.
32
33
  * @param info - The openAPI info object to convert.
@@ -0,0 +1,2 @@
1
+ import { ConvertPostmanFunction } from 'interfaces';
2
+ export declare const converters: Record<string, ConvertPostmanFunction>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.converters = void 0;
4
+ const postman2openapi_1 = require("postman2openapi");
5
+ const openapi_1 = require("./openapi");
6
+ exports.converters = {
7
+ '3.0.0': from_postman_to_asyncapi
8
+ };
9
+ function from_postman_to_asyncapi(postman, options = {}) {
10
+ const perspective = options.perspective;
11
+ const openapi = (0, postman2openapi_1.transpile)(postman);
12
+ const asyncapi = (0, openapi_1.from_openapi_to_asyncapi)(openapi, { perspective: perspective });
13
+ return asyncapi;
14
+ }
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { AsyncAPIDocument, OpenAPIDocument } from "./interfaces";
2
- export declare function serializeInput(document: string | AsyncAPIDocument | OpenAPIDocument): {
2
+ export declare function serializeInput(document: string | AsyncAPIDocument | OpenAPIDocument | Record<string, any>): {
3
3
  format: 'json' | 'yaml';
4
4
  document: AsyncAPIDocument | OpenAPIDocument;
5
5
  } | never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asyncapi/converter",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Convert AsyncAPI documents from older to newer versions.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -39,7 +39,9 @@
39
39
  "license": "Apache-2.0",
40
40
  "dependencies": {
41
41
  "@asyncapi/parser": "^3.1.0",
42
- "js-yaml": "^3.14.1"
42
+ "js-yaml": "^3.14.1",
43
+ "path": "^0.12.7",
44
+ "postman2openapi": "^1.2.1"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@jest/types": "^27.5.1",