@speclynx/apidom-parser 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -0
- package/LICENSE +202 -0
- package/LICENSES/AFL-3.0.txt +182 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/BSD-3-Clause.txt +26 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +65 -0
- package/README.md +139 -0
- package/dist/apidom-parser.browser.js +2983 -0
- package/dist/apidom-parser.browser.min.js +1 -0
- package/package.json +59 -0
- package/src/errors/ParserError.cjs +24 -0
- package/src/errors/ParserError.mjs +21 -0
- package/src/parser.cjs +101 -0
- package/src/parser.mjs +97 -0
- package/types/apidom-parser.d.ts +68 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Redistribution and use in source and binary forms, with or without
|
|
2
|
+
modification, are permitted provided that the following conditions are
|
|
3
|
+
met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright
|
|
6
|
+
notice, this list of conditions and the following disclaimer.
|
|
7
|
+
|
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
|
10
|
+
documentation and/or other materials provided with the distribution.
|
|
11
|
+
|
|
12
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
13
|
+
contributors may be used to endorse or promote products derived from
|
|
14
|
+
this software without specific prior written permission.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
20
|
+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
21
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
22
|
+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/LICENSES/MIT.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) <year> <copyright holders>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Additional Notices for SpecLynx Distribution
|
|
2
|
+
Copyright 2025 SpecLynx
|
|
3
|
+
This distribution is known as "SpecLynx ApiDOM". It is a fork of the ApiDOM project maintained by SmartBear.
|
|
4
|
+
This distribution includes modifications by SpecLynx.
|
|
5
|
+
All such modifications are licensed under the Apache License, Version 2.0.
|
|
6
|
+
A copy of the Apache 2.0 license can be found in `LICENSES/Apache-2.0.txt`.
|
|
7
|
+
|
|
8
|
+
ApiDOM
|
|
9
|
+
Copyright 2020 SmartBear Software Inc.
|
|
10
|
+
ApiDOM is licensed under Apache 2.0 license.
|
|
11
|
+
Copy of the Apache 2.0 license can be found in `LICENSES/Apache-2.0.txt` file.
|
|
12
|
+
|
|
13
|
+
json-schema-ref-parser
|
|
14
|
+
Copyright (c) 2015 James Messinger
|
|
15
|
+
File packages/apidom-reference/src/util/url.ts (the file) was originally created under MIT license in https://github.com/APIDevTools/json-schema-ref-parser repository.
|
|
16
|
+
The file has been copied into this project and modified. All modifications are licensed under Apache 2.0 License.
|
|
17
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
18
|
+
|
|
19
|
+
graphql-js
|
|
20
|
+
Copyright (c) GraphQL Contributors
|
|
21
|
+
File packages/apidom-ast/src/traversal/visitor.ts (the file) was originally created under MIT license in https://github.com/graphql/graphql-js repository.
|
|
22
|
+
The file has been copied into this project and modified. All modifications are licensed under Apache 2.0 License.
|
|
23
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
24
|
+
|
|
25
|
+
babel-plugin-add-import-extension
|
|
26
|
+
Copyright (c) 2019 Karl Prieb
|
|
27
|
+
File scripts/babel-plugin-add-import-extension.cjs (the file) was originally created under MIT license in https://codeberg.org/karl/babel-plugin-add-import-extension.
|
|
28
|
+
The file has been copied into this project and modified. All modifications are licensed under Apache 2.0 License.
|
|
29
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
30
|
+
|
|
31
|
+
refract-spec
|
|
32
|
+
Copyright (c) 2015 refractproject
|
|
33
|
+
Specific texts in README.md (the file) were originally created under MIT license in https://github.com/refractproject/refract-spec repository.
|
|
34
|
+
Specific texts have been copied into this project and modified. All modifications are licensed under Apache 2.0 License.
|
|
35
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
36
|
+
|
|
37
|
+
api-elements
|
|
38
|
+
Copyright (c) 2015 Apiary Inc.
|
|
39
|
+
Specific texts in README.md (the file) were originally created under MIT license in https://github.com/refractproject/refract-spec repository.
|
|
40
|
+
Specific texts have been copied into this project and modified. All modifications are licensed under Apache 2.0 License.
|
|
41
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
42
|
+
|
|
43
|
+
deepmerge
|
|
44
|
+
Copyright (c) 2012 James Halliday, Josh Duff, and other contributors
|
|
45
|
+
- File packages/apidom-core/src/deepmerge.ts contains algorithms that we originally created
|
|
46
|
+
in https://github.com/TehShrike/deepmerge/blob/master/index.js to handle deep merging of JavaScript Objects and Arrays.
|
|
47
|
+
These algorithms have been reverse engineered and adapted to support deep merging of ApiDOM structures.
|
|
48
|
+
- File packages/apidom-core/test/deepmerge.ts contains tests and fixtures that were originally created
|
|
49
|
+
in https://github.com/TehShrike/deepmerge/blob/master/test/merge.js to test deep merging of JavaScript Objects and Arrays.
|
|
50
|
+
These tests have been adapted to support testing deep merging of ApiDOM structures.
|
|
51
|
+
- File packages/apidom-core/README.md contains text fragments that were originally created
|
|
52
|
+
in https://github.com/TehShrike/deepmerge/blob/master/readme.md to document deepmerge library.
|
|
53
|
+
These text fragments have been amended to describe merging of ApiDOM structures.
|
|
54
|
+
Copy of the MIT license can be found in `LICENSES/MIT.txt` file.
|
|
55
|
+
All modifications are licensed under Apache 2.0 License.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
If the SPDX-FileCopyrightText and SPDX-License-Identifier tags are not present in the file,
|
|
60
|
+
it is assumed that these tags have following implicit value:
|
|
61
|
+
|
|
62
|
+
SPDX-FileCopyrightText: Copyright 2025 SpecLynx
|
|
63
|
+
SPDX-FileCopyrightText: Copyright 2020-2021 SmartBear Software Inc.
|
|
64
|
+
SPDX-License-Identifier: Apache-2.0
|
|
65
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @speclynx/apidom-parser
|
|
2
|
+
|
|
3
|
+
`@speclynx/apidom-parser` consumes parser adapters and provides unified API for parsing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
$ npm install @speclynx/apidom-parser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Mounting parser adapters
|
|
14
|
+
|
|
15
|
+
ApiDOM parser can consume any parser adapter as long as its shape is compatible.
|
|
16
|
+
In order for parsing adapter to be compatible it must be a [JavaScript Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
17
|
+
containing following 4 properties (2 required, 2 optional):
|
|
18
|
+
|
|
19
|
+
Property | Type | Default | Description
|
|
20
|
+
--- | --- | --- | ---
|
|
21
|
+
<a name="detect"></a>`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](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#synchronous_javascript) or [asynchronous function](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#asynchronous_javascript).
|
|
22
|
+
<a name="mediaTypes"></a>`mediaTypes` | `String[]` | `undefined` | This is a property of parser adapter that contains list of supported [media types](https://www.iana.org/assignments/media-types/media-types.xhtml) by this parser adapter. Note that other media types that are not officially registered by [iana](https://www.iana.org/) can be used as well.
|
|
23
|
+
<a name="namespace"></a>`namespace` | `Namespace` | | **REQUIRED** An ApiDOM namespace instance.
|
|
24
|
+
<a name="parse"></a>`parse` | `(source: String, options = {}) => ParseResult` | | **REQUIRED** This method should contain logic of actual parsing and should return instance of [ParseResult Element](https://github.com/speclynx/apidom/blob/main/packages/apidom/src/elements/ParseResult.ts).
|
|
25
|
+
|
|
26
|
+
Now, let's mount some adapters:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import ApiDOMParser from '@speclynx/apidom-parser';
|
|
30
|
+
import * as jsonParserAdapter from '@speclynx/apidom-parser-adapter-json';
|
|
31
|
+
import * as yamlParserAdapter from '@speclynx/apidom-parser-adapter-yaml-1-2';
|
|
32
|
+
|
|
33
|
+
const parser = new ApiDOMParser();
|
|
34
|
+
|
|
35
|
+
parser.use(jsonParserAdapter);
|
|
36
|
+
parser.use(yamlParserAdapter);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Finding an appropriate ApiDOM namespace
|
|
40
|
+
|
|
41
|
+
ApiDOM parser contains logic of mapping a `source string` + `mediaType` to appropriate ApiDOM namespace.
|
|
42
|
+
It will return either [base namespace instance](https://github.com/speclynx/apidom/tree/main/packages/apidom#base-namespace) or a specific one like [OpenApi 3.1.0](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-3-1#openapi-310-namespace) or [AsyncApi 2.6.0](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-asyncapi-2#asyncapi-2xy-namespace).
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import ApiDOMParser from '@speclynx/apidom-parser';
|
|
46
|
+
import * as jsonParserAdapter from '@speclynx/apidom-parser-adapter-json';
|
|
47
|
+
import * as yamlParserAdapter from '@speclynx/apidom-parser-adapter-yaml';
|
|
48
|
+
|
|
49
|
+
const parser = new ApiDOMParser();
|
|
50
|
+
|
|
51
|
+
parser.use(jsonParserAdapter);
|
|
52
|
+
parser.use(yamlParserAdapter);
|
|
53
|
+
|
|
54
|
+
const namespace = await parser.findNamespace('{"prop", "value"}', { mediaType: 'application/json' });
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Finding an appropriate media type
|
|
58
|
+
|
|
59
|
+
ApiDOM parser contains logic of mapping a `source string` to appropriate media type.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import ApiDOMParser from '@speclynx/apidom-parser';
|
|
63
|
+
import * as jsonParserAdapter from '@speclynx/apidom-parser-adapter-json';
|
|
64
|
+
import * as yamlParserAdapter from '@speclynx/apidom-parser-adapter-yaml';
|
|
65
|
+
|
|
66
|
+
const parser = new ApiDOMParser();
|
|
67
|
+
|
|
68
|
+
parser.use(jsonParserAdapter);
|
|
69
|
+
parser.use(yamlParserAdapter);
|
|
70
|
+
|
|
71
|
+
await parser.findMediaType('{"prop", "value"}'); // => 'application/json'
|
|
72
|
+
await parser.findMediaType('key: value'); // => 'application/yaml'
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
## Parsing
|
|
77
|
+
|
|
78
|
+
ApiDOM parser doesn't contain any parsing logic. It uses parser adapter to provide the parsing logic for it.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import ApiDOMParser from '@speclynx/apidom-parser';
|
|
82
|
+
import * as jsonParserAdapter from '@speclynx/apidom-parser-adapter-json';
|
|
83
|
+
import * as yamlParserAdapter from '@speclynx/apidom-parser-adapter-yaml';
|
|
84
|
+
|
|
85
|
+
const parser = new ApiDOMParser();
|
|
86
|
+
|
|
87
|
+
parser.use(jsonParserAdapter);
|
|
88
|
+
parser.use(yamlParserAdapter);
|
|
89
|
+
|
|
90
|
+
const parseResult = await parser.parse('{"prop", "value"}', { mediaType: 'application/json' });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`parse` method consumes various options as a second argument. Here is a list of options recognized by all parser adapters:
|
|
94
|
+
|
|
95
|
+
Option | Type | Default | Description
|
|
96
|
+
--- | --- | --- | ---
|
|
97
|
+
<a name="mediaType"></a>`mediaType` | `String` | `undefined` | Indicate to parser that the source string should be understood and parsed as provided by this option.
|
|
98
|
+
<a name="sourceMap"></a>`sourceMap` | `Boolean` | `false` | Indicate to parser whether to generate source maps.
|
|
99
|
+
|
|
100
|
+
All unrecognized arbitrary options will be further passed to underlying parser adapter.
|
|
101
|
+
|
|
102
|
+
#### Parsing errors
|
|
103
|
+
|
|
104
|
+
If no parser adapter was mounted before the parsing, calling `parse` method will result in Error.
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
import ApiDOMParser from '@speclynx/apidom-parser';
|
|
108
|
+
|
|
109
|
+
const parser = new ApiDOMParser();
|
|
110
|
+
const parseResult = await parser.parse('{"prop", "value"}', { mediaType: 'application/json' });
|
|
111
|
+
// => ParserError('Source did not match any registered parsers')
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Along with this, if underlying parser adapter produces an error, this error is propagated to ApiDOM
|
|
115
|
+
parser.
|
|
116
|
+
|
|
117
|
+
#### Word on detect vs mediaTypes
|
|
118
|
+
|
|
119
|
+
We strongly encourage users of this package to use explicit `mediaType` option when calling
|
|
120
|
+
`parse` method. If `mediaType` is not provided the parser will fallback to calling `detect` method
|
|
121
|
+
on parser adapter to indicate if the source string can be parsed by a parser adapter. As there are
|
|
122
|
+
ambiguities and common forms in different formats like JSON vs YAML, it's not always possible to
|
|
123
|
+
detect the format correctly and choose appropriate parser adapter.
|
|
124
|
+
|
|
125
|
+
Here is an example of YAML fragment:
|
|
126
|
+
|
|
127
|
+
```yaml
|
|
128
|
+
{"prop": "value"}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
This is a valid YAML, but it's also a valid JSON. It's not possible for parser adapter to properly
|
|
132
|
+
detect which format was intended by the author.
|
|
133
|
+
|
|
134
|
+
#### Word on ordering
|
|
135
|
+
|
|
136
|
+
If multiple parser adapters contain identical `mediaTypes` or `detect` logic then for the purposes
|
|
137
|
+
of [parsing](#parsing) or [finding an appropriate namespace](#finding-an-appropriate-apidom-namespace)
|
|
138
|
+
the order of [mounting the parser adapters](#mounting-parser-adapters) matter. The first parser adapter that matches its `mediaTypes`
|
|
139
|
+
or returns `true` from a `detect` is used.
|