0.99.2 • Published 6 days ago

@swagger-api/apidom-parser v0.99.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 days ago

@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):

PropertyTypeDefaultDescription
detect(source: String) => BooleanundefinedThis 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.
mediaTypesString[]undefinedThis 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.
namespaceNamespaceREQUIRED An ApiDOM namespace instance.
parse(source: String, options = {}) => ParseResultREQUIRED 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:

OptionTypeDefaultDescription
mediaTypeStringundefinedIndicate to parser that the source string should be understood and parsed as provided by this option.
sourceMapBooleanfalseIndicate 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.

0.99.2

6 days ago

0.99.1

1 month ago

0.99.0

1 month ago

0.98.0

2 months ago

0.97.0

2 months ago

0.96.0

2 months ago

0.95.0

3 months ago

0.94.0

3 months ago

0.93.0

3 months ago

0.92.0

4 months ago

0.91.0

4 months ago

0.90.0

4 months ago

0.89.0

5 months ago

0.88.0

5 months ago

0.87.0

5 months ago

0.86.0

5 months ago

0.84.0

5 months ago

0.85.0

5 months ago

0.82.2

6 months ago

0.80.0

6 months ago

0.81.0

6 months ago

0.82.0

6 months ago

0.83.0

6 months ago

0.82.1

6 months ago

0.78.0

7 months ago

0.76.2

8 months ago

0.77.0

7 months ago

0.76.1

8 months ago

0.79.0

6 months ago

0.74.0

10 months ago

0.73.0

10 months ago

0.76.0

8 months ago

0.75.0

9 months ago

0.74.1

9 months ago

0.72.0

10 months ago

0.71.0

10 months ago

0.70.1

11 months ago

0.70.0

12 months ago

0.69.2

1 year ago

0.69.1

1 year ago

0.69.3

1 year ago

0.69.0

1 year ago

0.68.1

1 year ago