npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swagger-api/apidom-parser

v0.99.2

Published

Parser consumes parser adapters and provides unified API for parsing.

Downloads

4,824

Readme

@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.