@swagger-api/apidom-parser-adapter-asyncapi-json-2 0.68.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 ADDED
@@ -0,0 +1,114 @@
1
+ # @swagger-api/apidom-parser-adapter-asyncapi-json-2
2
+
3
+ `@swagger-api/apidom-parser-adapter-asyncapi-json-2` is a parser adapter for following AsyncAPI specification versions defined in [JSON format](https://www.json.org/json-en.html):
4
+
5
+ - [AsyncAPI 2.6.0 specification](https://github.com/asyncapi/spec/blob/v2.6.0/spec/asyncapi.md)
6
+ - [AsyncAPI 2.5.0 specification](https://github.com/asyncapi/spec/blob/v2.5.0/spec/asyncapi.md)
7
+ - [AsyncAPI 2.4.0 specification](https://github.com/asyncapi/spec/blob/v2.4.0/spec/asyncapi.md)
8
+ - [AsyncAPI 2.3.0 specification](https://github.com/asyncapi/spec/blob/v2.3.0/spec/asyncapi.md)
9
+ - [AsyncAPI 2.2.0 specification](https://github.com/asyncapi/spec/blob/v2.2.0/spec/asyncapi.md)
10
+ - [AsyncAPI 2.1.0 specification](https://github.com/asyncapi/spec/blob/v2.1.0/spec/asyncapi.md)
11
+ - [AsyncAPI 2.0.0 specification](https://github.com/asyncapi/spec/blob/2.0.0/versions/2.0.0/asyncapi.md)
12
+
13
+
14
+ Under the hood this adapter uses [@swagger-api/apidom-parser-adapter-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-json)
15
+ to parse a source string into generic ApiDOM in [base ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom#base-namespace)
16
+ which is then refracted with [AsyncApi 2.x.y Refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-2#refractors).
17
+
18
+ ## Installation
19
+
20
+ After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it
21
+ via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
22
+
23
+ ```sh
24
+ $ npm install @swagger-api/apidom-parser-adapter-asyncapi-json-2
25
+ ```
26
+
27
+ ## Parser adapter API
28
+
29
+ This parser adapter is fully compatible with parser adapter interface required by [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#mounting-parser-adapters)
30
+ and implements all required properties.
31
+
32
+ ### mediaTypes
33
+
34
+ Defines list of media types that this parser adapter recognizes.
35
+
36
+ ```js
37
+ [
38
+ 'application/vnd.aai.asyncapi;version=2.0.0',
39
+ 'application/vnd.aai.asyncapi;version=2.1.0',
40
+ 'application/vnd.aai.asyncapi;version=2.2.0',
41
+ 'application/vnd.aai.asyncapi;version=2.3.0',
42
+ 'application/vnd.aai.asyncapi;version=2.4.0',
43
+ 'application/vnd.aai.asyncapi;version=2.5.0',
44
+ 'application/vnd.aai.asyncapi;version=2.6.0',
45
+ 'application/vnd.aai.asyncapi+json;version=2.0.0',
46
+ 'application/vnd.aai.asyncapi+json;version=2.1.0',
47
+ 'application/vnd.aai.asyncapi+json;version=2.2.0',
48
+ 'application/vnd.aai.asyncapi+json;version=2.3.0',
49
+ 'application/vnd.aai.asyncapi+json;version=2.4.0',
50
+ 'application/vnd.aai.asyncapi+json;version=2.5.0',
51
+ 'application/vnd.aai.asyncapi+json;version=2.6.0',
52
+ ]
53
+ ```
54
+
55
+ ### detect
56
+
57
+ [Detection](https://github.com/swagger-api/apidom/blob/main/packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts#L13) is based on a regular expression matching required AsyncApi 2.6.0 specification symbols in JSON format.
58
+
59
+ ### namespace
60
+
61
+ This adapter exposes an instance of [AsyncApi 2.x.y ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-2#asyncapi-2xy-namespace).
62
+
63
+ ### parse
64
+
65
+ `parse` function consumes various options as a second argument. Here is a list of these options:
66
+
67
+ Option | Type | Default | Description
68
+ --- | --- | --- | ---
69
+ <a name="specObj"></a>`specObj` | `Object` | [Specification Object](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-asyncapi-2/src/refractor/specification.ts) | This specification object drives the JSON AST transformation to AsyncAPI 2.x ApiDOM namespace.
70
+ <a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate whether to generate source maps.
71
+ <a name="refractorOpts"></a>`refractorOpts` | `Object` | `{}` | Refractor options are [passed to refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-2#refractor-plugins) during refracting phase.
72
+
73
+ All unrecognized arbitrary options will be ignored.
74
+
75
+ ## Usage
76
+
77
+ This parser adapter can be used directly or indirectly via [@swagger-api/apidom-parser](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser).
78
+
79
+ ### Direct usage
80
+
81
+ During direct usage you don't need to provide `mediaType` as the `parse` function is already pre-bound
82
+ with [supported media types](#mediatypes).
83
+
84
+ ```js
85
+ import { parse, detect } from '@swagger-api/apidom-parser-adapter-asyncapi-json-2';
86
+
87
+ // detecting
88
+ await detect('{"asyncapi": "2.0.0"}'); // => true
89
+ await detect('{"asyncapi": "2.1.0"}'); // => true
90
+ await detect('{"asyncapi": "2.2.0"}'); // => true
91
+ await detect('{"asyncapi": "2.3.0"}'); // => true
92
+ await detect('{"asyncapi": "2.4.0"}'); // => true
93
+ await detect('{"asyncapi": "2.5.0"}'); // => true
94
+ await detect('{"asyncapi": "2.6.0"}'); // => true
95
+ await detect('test'); // => false
96
+
97
+ // parsing
98
+ const parseResult = await parse('{"asyncapi": "2.6.0"}', { sourceMap: true });
99
+ ```
100
+
101
+ ### Indirect usage
102
+
103
+ You can omit the `mediaType` option here, but please read [Word on detect vs mediaTypes](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser#word-on-detect-vs-mediatypes) before you do so.
104
+
105
+ ```js
106
+ import ApiDOMParser from '@swagger-api/apidom-parser';
107
+ import * as asyncApiJsonAdapter from '@swagger-api/apidom-parser-adapter-asyncapi-json-2';
108
+
109
+ const parser = ApiDOMParser();
110
+
111
+ parser.use(asyncApiJsonAdapter);
112
+
113
+ const parseResult = await parser.parse('{"asyncapi": "2.6.0"}', { mediaType: asyncApiJsonAdapter.mediaTypes.latest('json') });
114
+ ```
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
4
+ exports.__esModule = true;
5
+ exports.parse = exports.namespace = exports.mediaTypes = exports.detectionRegExp = exports.detect = void 0;
6
+ var _ramda = require("ramda");
7
+ var _ramdaAdjunct = require("ramda-adjunct");
8
+ var _apidomCore = require("@swagger-api/apidom-core");
9
+ var _apidomParserAdapterJson = require("@swagger-api/apidom-parser-adapter-json");
10
+ var _apidomNsAsyncapi = _interopRequireWildcard(require("@swagger-api/apidom-ns-asyncapi-2"));
11
+ var _mediaTypes = _interopRequireDefault(require("./media-types.cjs"));
12
+ exports.mediaTypes = _mediaTypes.default;
13
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+ const detectionRegExp = /"asyncapi"\s*:\s*"(?<version_json>2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/;
16
+ exports.detectionRegExp = detectionRegExp;
17
+ const detect = async source => detectionRegExp.test(source) && (await (0, _apidomParserAdapterJson.detect)(source));
18
+ exports.detect = detect;
19
+ const parse = async (source, options = {}) => {
20
+ const refractorOpts = (0, _ramda.propOr)({}, 'refractorOpts', options);
21
+ const parserOpts = (0, _ramda.omit)(['refractorOpts'], options);
22
+ const parseResultElement = await (0, _apidomParserAdapterJson.parse)(source, parserOpts);
23
+ const {
24
+ result
25
+ } = parseResultElement;
26
+ if ((0, _ramdaAdjunct.isNotUndefined)(result)) {
27
+ const asyncApiElement = _apidomNsAsyncapi.AsyncApi2Element.refract(result, refractorOpts);
28
+ asyncApiElement.classes.push('result');
29
+ parseResultElement.replaceResult(asyncApiElement);
30
+ }
31
+ return parseResultElement;
32
+ };
33
+ exports.parse = parse;
34
+ const namespace = (0, _apidomCore.createNamespace)(_apidomNsAsyncapi.default);
35
+ exports.namespace = namespace;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _apidomNsAsyncapi = require("@swagger-api/apidom-ns-asyncapi-2");
6
+ const jsonMediaTypes = new _apidomNsAsyncapi.AsyncAPIMediaTypes(..._apidomNsAsyncapi.mediaTypes.filterByFormat('generic'), ..._apidomNsAsyncapi.mediaTypes.filterByFormat('json'));
7
+ var _default = jsonMediaTypes;
8
+ exports.default = _default;