@asyncapi/converter 1.5.1 → 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
@@ -196,7 +196,7 @@ Conversion to version `3.x.x` from `2.x.x` has several assumptions that should b
196
196
  examples: ["test"]
197
197
  ```
198
198
 
199
- ### OpenAPI 3.0 to AsyncAPI 3.0 Conversion
199
+ ## OpenAPI 3.0 to AsyncAPI 3.0 Conversion
200
200
 
201
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.
202
202
 
@@ -239,6 +239,48 @@ The perspective option can be set to either 'server' (default) or 'client'.
239
239
 
240
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.
241
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
+
242
284
  ## Development
243
285
 
244
286
  1. Setup project by installing dependencies `npm install`
@@ -276,4 +318,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
276
318
 
277
319
  <!-- ALL-CONTRIBUTORS-LIST:END -->
278
320
 
279
- 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;
@@ -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.1",
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",