|
| 1 | +# @swagger-api/apidom-parser-adapter-asyncapi-json-3 |
| 2 | + |
| 3 | +`@swagger-api/apidom-parser-adapter-asyncapi-json-3` is a parser adapter for following AsyncAPI specification versions defined in [JSON format](https://www.json.org/json-en.html): |
| 4 | + |
| 5 | +- [AsyncAPI 3.0.0 specification](https://github.com/asyncapi/spec/blob/v3.0.0/spec/asyncapi.md) |
| 6 | +- [AsyncAPI 3.0.1 specification](https://github.com/asyncapi/spec/blob/v3.0.1/spec/asyncapi.md) |
| 7 | + |
| 8 | +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) |
| 9 | +to parse a source string into generic ApiDOM in [base ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-core#base-namespace) |
| 10 | +which is then refracted with [AsyncApi 3.x.y Refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#refractors). |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it |
| 15 | +via [npm CLI](https://docs.npmjs.com/cli) by running the following command: |
| 16 | + |
| 17 | +```sh |
| 18 | + $ npm install @swagger-api/apidom-parser-adapter-asyncapi-json-3 |
| 19 | +``` |
| 20 | + |
| 21 | +## Parser adapter API |
| 22 | + |
| 23 | +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) |
| 24 | +and implements all required properties. |
| 25 | + |
| 26 | +### mediaTypes |
| 27 | + |
| 28 | +Defines list of media types that this parser adapter recognizes. |
| 29 | + |
| 30 | +```js |
| 31 | +[ |
| 32 | + 'application/vnd.aai.asyncapi;version=3.0.0', |
| 33 | + 'application/vnd.aai.asyncapi+json;version=3.0.0', |
| 34 | + 'application/vnd.aai.asyncapi;version=3.0.1', |
| 35 | + 'application/vnd.aai.asyncapi+json;version=3.0.1', |
| 36 | +] |
| 37 | +``` |
| 38 | + |
| 39 | +### detect |
| 40 | + |
| 41 | +[Detection](https://github.com/swagger-api/apidom/blob/main/packages/apidom-parser-adapter-asyncapi-json-3/src/adapter.ts#L18) is based on a regular expression matching required AsyncApi 3.0.0 specification symbols in JSON format. |
| 42 | + |
| 43 | +### namespace |
| 44 | + |
| 45 | +This adapter exposes an instance of [AsyncApi 3.x.y ApiDOM namespace](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#asyncapi-3xy-namespace). |
| 46 | + |
| 47 | +### parse |
| 48 | + |
| 49 | +`parse` function consumes various options as a second argument. Here is a list of these options: |
| 50 | + |
| 51 | +Option | Type | Default | Description |
| 52 | +--- | --- | --- | --- |
| 53 | +<a name="specObj"></a>`specObj` | `Object` | [Specification Object](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-asyncapi-3/src/refractor/specification.ts) | This specification object drives the JSON AST transformation to AsyncAPI 3.x ApiDOM namespace. |
| 54 | +<a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate whether to generate source maps. |
| 55 | +<a name="refractorOpts"></a>`refractorOpts` | `Object` | `{}` | Refractor options are [passed to refractors](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-asyncapi-3#refractor-plugins) during refracting phase. |
| 56 | + |
| 57 | +All unrecognized arbitrary options will be ignored. |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +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). |
| 62 | + |
| 63 | +### Direct usage |
| 64 | + |
| 65 | +During direct usage you don't need to provide `mediaType` as the `parse` function is already pre-bound |
| 66 | +with [supported media types](#mediatypes). |
| 67 | + |
| 68 | +```js |
| 69 | +import { parse, detect } from '@swagger-api/apidom-parser-adapter-asyncapi-json-3'; |
| 70 | + |
| 71 | +// detecting |
| 72 | +await detect('{"asyncapi": "3.0.0"}'); // => true |
| 73 | +await detect('{"asyncapi": "3.0.1"}'); // => true |
| 74 | +await detect('test'); // => false |
| 75 | + |
| 76 | +// parsing |
| 77 | +const parseResult = await parse('{"asyncapi": "3.0.0"}', { sourceMap: true }); |
| 78 | +``` |
| 79 | + |
| 80 | +### Indirect usage |
| 81 | + |
| 82 | +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. |
| 83 | + |
| 84 | +```js |
| 85 | +import ApiDOMParser from '@swagger-api/apidom-parser'; |
| 86 | +import * as asyncApiJsonAdapter from '@swagger-api/apidom-parser-adapter-asyncapi-json-3'; |
| 87 | + |
| 88 | +const parser = new ApiDOMParser(); |
| 89 | + |
| 90 | +parser.use(asyncApiJsonAdapter); |
| 91 | + |
| 92 | +const parseResult = await parser.parse('{"asyncapi": "3.0.1"}', { mediaType: asyncApiJsonAdapter.mediaTypes.latest('json') }); |
| 93 | +``` |
0 commit comments