@swagger-api/apidom-parser
consumes parser adapters and provides unified API for parsing.
You can install this package via npm CLI by running the following command:
$ npm install @swagger-api/apidom-parser
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);
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' });
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'
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:
All unrecognized arbitrary options will be further passed to underlying parser adapter.
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.
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.
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.