@swagger-api/apidom-parser v0.99.2
@swagger-api/apidom-parser
@swagger-api/apidom-parser
consumes parser adapters and provides unified API for parsing.
Installation
You can install this package via npm CLI by running the following command:
$ npm install @swagger-api/apidom-parser
Mounting parser adapters
ApiDOM parser can consume any parser adapter as long as its shape is compatible. In order for parsing adapter to be compatible it must be a JavaScript Object containing following 4 properties (2 required, 2 optional):
Property | Type | Default | Description |
---|---|---|---|
detect | (source: String) => Boolean | undefined | This method is called from a parser with a single argument of string that is going to be parsed. Returns a boolean value indicating if the source string was recognized by the parser adapter. It can be defined either as synchronous or asynchronous function. |
mediaTypes | String[] | undefined | This is a property of parser adapter that contains list of supported media types by this parser adapter. Note that other media types that are not officially registered by iana can be used as well. |
namespace | Namespace | REQUIRED An ApiDOM namespace instance. | |
parse | (source: String, options = {}) => ParseResult | REQUIRED This method should contain logic of actual parsing and should return instance of ParseResult Element. |
Now, let's mount some adapters:
import ApiDOMParser from '@swagger-api/apidom-parser';
import * as jsonParserAdapter from '@swagger-api/apidom-parser-adapter-json';
import * as yamlParserAdapter from '@swagger-api/apidom-parser-adapter-yaml';
const parser = new ApiDOMParser();
parser.use(jsonParserAdapter);
parser.use(yamlParserAdapter);
Finding an appropriate ApiDOM namespace
ApiDOM parser contains logic of mapping a source string
+ mediaType
to appropriate ApiDOM namespace.
It will return either base namespace instance or a specific one like OpenApi 3.1.0 or AsyncApi 2.6.0.
import ApiDOMParser from '@swagger-api/apidom-parser';
import * as jsonParserAdapter from '@swagger-api/apidom-parser-adapter-json';
import * as yamlParserAdapter from '@swagger-api/apidom-parser-adapter-yaml';
const parser = new ApiDOMParser();
parser.use(jsonParserAdapter);
parser.use(yamlParserAdapter);
const namespace = await parser.findNamespace('{"prop", "value"}', { mediaType: 'application/json' });
Finding an appropriate media type
ApiDOM parser contains logic of mapping a source string
to appropriate media type.
import ApiDOMParser from '@swagger-api/apidom-parser';
import * as jsonParserAdapter from '@swagger-api/apidom-parser-adapter-json';
import * as yamlParserAdapter from '@swagger-api/apidom-parser-adapter-yaml';
const parser = new ApiDOMParser();
parser.use(jsonParserAdapter);
parser.use(yamlParserAdapter);
await parser.findMediaType('{"prop", "value"}'); // => 'application/json'
await parser.findMediaType('key: value'); // => 'application/yaml'
Parsing
ApiDOM parser doesn't contain any parsing logic. It uses parser adapter to provide the parsing logic for it.
import ApiDOMParser from '@swagger-api/apidom-parser';
import * as jsonParserAdapter from '@swagger-api/apidom-parser-adapter-json';
import * as yamlParserAdapter from '@swagger-api/apidom-parser-adapter-yaml';
const parser = new ApiDOMParser();
parser.use(jsonParserAdapter);
parser.use(yamlParserAdapter);
const parseResult = await parser.parse('{"prop", "value"}', { mediaType: 'application/json' });
parse
method consumes various options as a second argument. Here is a list of options recognized by all parser adapters:
Option | Type | Default | Description |
---|---|---|---|
mediaType | String | undefined | Indicate to parser that the source string should be understood and parsed as provided by this option. |
sourceMap | Boolean | false | Indicate to parser whether to generate source maps. |
All unrecognized arbitrary options will be further passed to underlying parser adapter.
Parsing errors
If no parser adapter was mounted before the parsing, calling parse
method will result in Error.
import ApiDOMParser from '@swagger-api/apidom-parser';
const parser = new ApiDOMParser();
const parseResult = await parser.parse('{"prop", "value"}', { mediaType: 'application/json' });
// => ParserError('Source did not match any registered parsers')
Along with this, if underlying parser adapter produces an error, this error is propagated to ApiDOM parser.
Word on detect vs mediaTypes
We strongly encourage users of this package to use explicit mediaType
option when calling
parse
method. If mediaType
is not provided the parser will fallback to calling detect
method
on parser adapter to indicate if the source string can be parsed by a parser adapter. As there are
ambiguities and common forms in different formats like JSON vs YAML, it's not always possible to
detect the format correctly and choose appropriate parser adapter.
Here is an example of YAML fragment:
{"prop": "value"}
This is a valid YAML, but it's also a valid JSON. It's not possible for parser adapter to properly detect which format was intended by the author.
Word on ordering
If multiple parser adapters contain identical mediaTypes
or detect
logic then for the purposes
of parsing or finding an appropriate namespace
the order of mounting the parser adapters matter. The first parser adapter that matches its mediaTypes
or returns true
from a detect
is used.
6 days ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
7 months ago
8 months ago
7 months ago
8 months ago
6 months ago
10 months ago
10 months ago
8 months ago
9 months ago
9 months ago
10 months ago
10 months ago
11 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago