@jentic/arazzo-parser 1.0.0-alpha.24 → 1.0.0-alpha.25
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 +6 -0
- package/package.json +3 -4
- package/src/errors/ParseError.cjs +11 -0
- package/src/errors/ParseError.mjs +7 -0
- package/src/index.cjs +10 -0
- package/src/index.mjs +2 -0
- package/src/parse-arazzo.cjs +204 -0
- package/src/parse-arazzo.mjs +197 -0
- package/src/parse-openapi.cjs +234 -0
- package/src/parse-openapi.mjs +227 -0
- package/src/resolve/resolvers/memory/index.cjs +29 -0
- package/src/resolve/resolvers/memory/index.mjs +25 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.0.0-alpha.25](https://github.com/jentic/jentic-arazzo-tools/compare/v1.0.0-alpha.24...v1.0.0-alpha.25) (2026-03-11)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **release:** fix issue in lerna not publishing _.cjs|_.mjs files ([8982184](https://github.com/jentic/jentic-arazzo-tools/commit/8982184ddae167b6f2a772114f9a9321f3b67d14))
|
|
11
|
+
|
|
6
12
|
# [1.0.0-alpha.24](https://github.com/jentic/jentic-arazzo-tools/compare/v1.0.0-alpha.23...v1.0.0-alpha.24) (2026-03-11)
|
|
7
13
|
|
|
8
14
|
**Note:** Version bump only for package @jentic/arazzo-parser
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jentic/arazzo-parser",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.25",
|
|
4
4
|
"description": "Parser for Arazzo Documents producing SpecLynx ApiDOM data model.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arazzo",
|
|
@@ -67,8 +67,7 @@
|
|
|
67
67
|
"@speclynx/apidom-core": "^4.0.3"
|
|
68
68
|
},
|
|
69
69
|
"files": [
|
|
70
|
-
"src
|
|
71
|
-
"src/**/*.cjs",
|
|
70
|
+
"src/",
|
|
72
71
|
"dist/",
|
|
73
72
|
"types/arazzo-parser.d.ts",
|
|
74
73
|
"LICENSE",
|
|
@@ -76,5 +75,5 @@
|
|
|
76
75
|
"README.md",
|
|
77
76
|
"CHANGELOG.md"
|
|
78
77
|
],
|
|
79
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "b9a3213b0561fc58e0491c7e638d6a96383befff"
|
|
80
79
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _apidomError = require("@speclynx/apidom-error");
|
|
6
|
+
class ParseError extends _apidomError.ApiDOMError {
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
var _default = exports.default = ParseError;
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.parseOpenAPI = exports.parseArazzo = exports.defaultOpenAPIOptions = exports.defaultArazzoOptions = void 0;
|
|
5
|
+
var _parseArazzo = require("./parse-arazzo.cjs");
|
|
6
|
+
exports.parseArazzo = _parseArazzo.parse;
|
|
7
|
+
exports.defaultArazzoOptions = _parseArazzo.defaultOptions;
|
|
8
|
+
var _parseOpenapi = require("./parse-openapi.cjs");
|
|
9
|
+
exports.parseOpenAPI = _parseOpenapi.parse;
|
|
10
|
+
exports.defaultOpenAPIOptions = _parseOpenapi.defaultOptions;
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.defaultOptions = void 0;
|
|
6
|
+
exports.parse = parse;
|
|
7
|
+
var _empty = require("@speclynx/apidom-reference/configuration/empty");
|
|
8
|
+
var _arazzoJson = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/arazzo-json-1"));
|
|
9
|
+
var _arazzoYaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/arazzo-yaml-1"));
|
|
10
|
+
var _openapiJson = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-2"));
|
|
11
|
+
var _openapiYaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-2"));
|
|
12
|
+
var _openapiJson2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-0"));
|
|
13
|
+
var _openapiYaml2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0"));
|
|
14
|
+
var _openapiJson3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-1"));
|
|
15
|
+
var _openapiYaml3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1"));
|
|
16
|
+
var _file = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/file"));
|
|
17
|
+
var _httpAxios = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/http-axios"));
|
|
18
|
+
var _apidomParserAdapterArazzoJson = require("@speclynx/apidom-parser-adapter-arazzo-json-1");
|
|
19
|
+
var _apidomNsArazzo = require("@speclynx/apidom-ns-arazzo-1");
|
|
20
|
+
var _apidomParserAdapterArazzoYaml = require("@speclynx/apidom-parser-adapter-arazzo-yaml-1");
|
|
21
|
+
var _ramdaAdjunct = require("ramda-adjunct");
|
|
22
|
+
var _ParseError = _interopRequireDefault(require("./errors/ParseError.cjs"));
|
|
23
|
+
var _index = _interopRequireDefault(require("./resolve/resolvers/memory/index.cjs"));
|
|
24
|
+
/**
|
|
25
|
+
* Options for parsing Arazzo Documents.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Default reference options for parsing Arazzo Documents.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
const defaultOptions = exports.defaultOptions = {
|
|
34
|
+
parse: {
|
|
35
|
+
parsers: [new _arazzoJson.default({
|
|
36
|
+
allowEmpty: false
|
|
37
|
+
}), new _arazzoYaml.default({
|
|
38
|
+
allowEmpty: false
|
|
39
|
+
}), new _openapiJson.default({
|
|
40
|
+
allowEmpty: false
|
|
41
|
+
}), new _openapiYaml.default({
|
|
42
|
+
allowEmpty: false
|
|
43
|
+
}), new _openapiJson2.default({
|
|
44
|
+
allowEmpty: false
|
|
45
|
+
}), new _openapiYaml2.default({
|
|
46
|
+
allowEmpty: false
|
|
47
|
+
}), new _openapiJson3.default({
|
|
48
|
+
allowEmpty: false
|
|
49
|
+
}), new _openapiYaml3.default({
|
|
50
|
+
allowEmpty: false
|
|
51
|
+
})],
|
|
52
|
+
parserOpts: {
|
|
53
|
+
sourceMap: false,
|
|
54
|
+
style: false,
|
|
55
|
+
strict: true,
|
|
56
|
+
sourceDescriptions: false
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
resolve: {
|
|
60
|
+
resolvers: [new _index.default(), new _file.default({
|
|
61
|
+
fileAllowList: ['*.json', '*.yaml', '*.yml']
|
|
62
|
+
}), new _httpAxios.default({
|
|
63
|
+
timeout: 15000,
|
|
64
|
+
redirects: 5,
|
|
65
|
+
withCredentials: false
|
|
66
|
+
})],
|
|
67
|
+
resolverOpts: {}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Parses an Arazzo Document from an object.
|
|
73
|
+
* @param source - The Arazzo Document as a plain object
|
|
74
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
75
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
76
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Parses an Arazzo Document from a string or URI.
|
|
82
|
+
* @param source - The Arazzo Document as string content, or a file system path / HTTP(S) URL
|
|
83
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
84
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
85
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Parses an Arazzo Document from a string, object, or URI.
|
|
91
|
+
* @param source - The Arazzo Document as a plain object, string content, or URI
|
|
92
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
93
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
94
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
95
|
+
* @public
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Parses an Arazzo Document from a string, object, or URI.
|
|
100
|
+
*
|
|
101
|
+
* The function handles three types of input:
|
|
102
|
+
* 1. Object - converts to JSON string and parses (source maps supported with `strict: false`)
|
|
103
|
+
* 2. String content - uses Arazzo detection to identify and parse inline JSON or YAML content
|
|
104
|
+
* 3. URI string - if not detected as Arazzo content, treats as file system path or HTTP(S) URL
|
|
105
|
+
*
|
|
106
|
+
* @param source - The Arazzo Document as an object, string content, or a file system path / HTTP(S) URL
|
|
107
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
108
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
109
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* Parse from object
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const result = await parseArazzo({ arazzo: '1.0.1', info: {...} });
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* Parse inline JSON
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const result = await parseArazzo('{"arazzo": "1.0.1", "info": {...}}');
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* Parse from file
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const result = await parseArazzo('/path/to/arazzo.json');
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* Parse from URL
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const result = await parseArazzo('https://example.com/arazzo.yaml');
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* Parse with custom options
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const result = await parseArazzo('/path/to/arazzo.json', customOptions);
|
|
139
|
+
* ```
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
async function parse(source, options = {}) {
|
|
143
|
+
let mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
|
|
144
|
+
const strict = mergedOptions.parse?.parserOpts?.strict ?? true;
|
|
145
|
+
let sourceProvenance;
|
|
146
|
+
if ((0, _ramdaAdjunct.isPlainObject)(source)) {
|
|
147
|
+
const document = JSON.stringify(source, null, 2);
|
|
148
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
149
|
+
resolve: {
|
|
150
|
+
resolverOpts: {
|
|
151
|
+
document
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
source = 'memory://arazzo.json';
|
|
156
|
+
sourceProvenance = '[object]';
|
|
157
|
+
} else if (await (0, _apidomParserAdapterArazzoJson.detect)(source, {
|
|
158
|
+
strict
|
|
159
|
+
})) {
|
|
160
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
161
|
+
resolve: {
|
|
162
|
+
resolverOpts: {
|
|
163
|
+
document: source
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
source = 'memory://arazzo.json';
|
|
168
|
+
sourceProvenance = '[inline JSON]';
|
|
169
|
+
} else if (await (0, _apidomParserAdapterArazzoYaml.detect)(source, {
|
|
170
|
+
strict
|
|
171
|
+
})) {
|
|
172
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
173
|
+
resolve: {
|
|
174
|
+
resolverOpts: {
|
|
175
|
+
document: source
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
source = 'memory://arazzo.yaml';
|
|
180
|
+
sourceProvenance = '[inline YAML]';
|
|
181
|
+
} else {
|
|
182
|
+
sourceProvenance = source;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// next we assume that source is either file system URI or HTTP(S) URL
|
|
186
|
+
try {
|
|
187
|
+
const parseResult = await (0, _empty.parse)(source, mergedOptions);
|
|
188
|
+
|
|
189
|
+
// set retrievalURI metadata for file/URL sources (not for inline content)
|
|
190
|
+
if (!source.startsWith('memory://')) {
|
|
191
|
+
parseResult.meta.set('retrievalURI', source);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// validate that the parsed document is an Arazzo specification
|
|
195
|
+
if (!(0, _apidomNsArazzo.isArazzoSpecification1Element)(parseResult.api)) {
|
|
196
|
+
throw new _empty.UnmatchedParserError(`Could not find a parser that can parse "${sourceProvenance}" as an Arazzo specification`);
|
|
197
|
+
}
|
|
198
|
+
return parseResult;
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw new _ParseError.default(`Failed to parse Arazzo Document from "${sourceProvenance}"`, {
|
|
201
|
+
cause: error
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { parse as parseURI, mergeOptions, UnmatchedParserError } from '@speclynx/apidom-reference/configuration/empty';
|
|
2
|
+
import ArazzoJSON1Parser from '@speclynx/apidom-reference/parse/parsers/arazzo-json-1';
|
|
3
|
+
import ArazzoYAML1Parser from '@speclynx/apidom-reference/parse/parsers/arazzo-yaml-1';
|
|
4
|
+
import OpenApiJSON2Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-2';
|
|
5
|
+
import OpenApiYAML2Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-2';
|
|
6
|
+
import OpenApiJSON3_0Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-0';
|
|
7
|
+
import OpenApiYAML3_0Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0';
|
|
8
|
+
import OpenApiJSON3_1Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-1';
|
|
9
|
+
import OpenApiYAML3_1Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1';
|
|
10
|
+
import FileResolver from '@speclynx/apidom-reference/resolve/resolvers/file';
|
|
11
|
+
import HTTPResolverAxios from '@speclynx/apidom-reference/resolve/resolvers/http-axios';
|
|
12
|
+
import { detect as detectArazzoJSON } from '@speclynx/apidom-parser-adapter-arazzo-json-1';
|
|
13
|
+
import { isArazzoSpecification1Element } from '@speclynx/apidom-ns-arazzo-1';
|
|
14
|
+
import { detect as detectArazzoYAML } from '@speclynx/apidom-parser-adapter-arazzo-yaml-1';
|
|
15
|
+
import { isPlainObject } from 'ramda-adjunct';
|
|
16
|
+
import ParseError from "./errors/ParseError.mjs";
|
|
17
|
+
import MemoryResolver from "./resolve/resolvers/memory/index.mjs";
|
|
18
|
+
/**
|
|
19
|
+
* Options for parsing Arazzo Documents.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Default reference options for parsing Arazzo Documents.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export const defaultOptions = {
|
|
27
|
+
parse: {
|
|
28
|
+
parsers: [new ArazzoJSON1Parser({
|
|
29
|
+
allowEmpty: false
|
|
30
|
+
}), new ArazzoYAML1Parser({
|
|
31
|
+
allowEmpty: false
|
|
32
|
+
}), new OpenApiJSON2Parser({
|
|
33
|
+
allowEmpty: false
|
|
34
|
+
}), new OpenApiYAML2Parser({
|
|
35
|
+
allowEmpty: false
|
|
36
|
+
}), new OpenApiJSON3_0Parser({
|
|
37
|
+
allowEmpty: false
|
|
38
|
+
}), new OpenApiYAML3_0Parser({
|
|
39
|
+
allowEmpty: false
|
|
40
|
+
}), new OpenApiJSON3_1Parser({
|
|
41
|
+
allowEmpty: false
|
|
42
|
+
}), new OpenApiYAML3_1Parser({
|
|
43
|
+
allowEmpty: false
|
|
44
|
+
})],
|
|
45
|
+
parserOpts: {
|
|
46
|
+
sourceMap: false,
|
|
47
|
+
style: false,
|
|
48
|
+
strict: true,
|
|
49
|
+
sourceDescriptions: false
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
resolve: {
|
|
53
|
+
resolvers: [new MemoryResolver(), new FileResolver({
|
|
54
|
+
fileAllowList: ['*.json', '*.yaml', '*.yml']
|
|
55
|
+
}), new HTTPResolverAxios({
|
|
56
|
+
timeout: 15000,
|
|
57
|
+
redirects: 5,
|
|
58
|
+
withCredentials: false
|
|
59
|
+
})],
|
|
60
|
+
resolverOpts: {}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Parses an Arazzo Document from an object.
|
|
66
|
+
* @param source - The Arazzo Document as a plain object
|
|
67
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
68
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
69
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parses an Arazzo Document from a string or URI.
|
|
75
|
+
* @param source - The Arazzo Document as string content, or a file system path / HTTP(S) URL
|
|
76
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
77
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
78
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Parses an Arazzo Document from a string, object, or URI.
|
|
84
|
+
* @param source - The Arazzo Document as a plain object, string content, or URI
|
|
85
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
86
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
87
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Parses an Arazzo Document from a string, object, or URI.
|
|
93
|
+
*
|
|
94
|
+
* The function handles three types of input:
|
|
95
|
+
* 1. Object - converts to JSON string and parses (source maps supported with `strict: false`)
|
|
96
|
+
* 2. String content - uses Arazzo detection to identify and parse inline JSON or YAML content
|
|
97
|
+
* 3. URI string - if not detected as Arazzo content, treats as file system path or HTTP(S) URL
|
|
98
|
+
*
|
|
99
|
+
* @param source - The Arazzo Document as an object, string content, or a file system path / HTTP(S) URL
|
|
100
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
101
|
+
* @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
|
|
102
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* Parse from object
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const result = await parseArazzo({ arazzo: '1.0.1', info: {...} });
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* Parse inline JSON
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const result = await parseArazzo('{"arazzo": "1.0.1", "info": {...}}');
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* Parse from file
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const result = await parseArazzo('/path/to/arazzo.json');
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* Parse from URL
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const result = await parseArazzo('https://example.com/arazzo.yaml');
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* Parse with custom options
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const result = await parseArazzo('/path/to/arazzo.json', customOptions);
|
|
132
|
+
* ```
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export async function parse(source, options = {}) {
|
|
136
|
+
let mergedOptions = mergeOptions(defaultOptions, options);
|
|
137
|
+
const strict = mergedOptions.parse?.parserOpts?.strict ?? true;
|
|
138
|
+
let sourceProvenance;
|
|
139
|
+
if (isPlainObject(source)) {
|
|
140
|
+
const document = JSON.stringify(source, null, 2);
|
|
141
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
142
|
+
resolve: {
|
|
143
|
+
resolverOpts: {
|
|
144
|
+
document
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
source = 'memory://arazzo.json';
|
|
149
|
+
sourceProvenance = '[object]';
|
|
150
|
+
} else if (await detectArazzoJSON(source, {
|
|
151
|
+
strict
|
|
152
|
+
})) {
|
|
153
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
154
|
+
resolve: {
|
|
155
|
+
resolverOpts: {
|
|
156
|
+
document: source
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
source = 'memory://arazzo.json';
|
|
161
|
+
sourceProvenance = '[inline JSON]';
|
|
162
|
+
} else if (await detectArazzoYAML(source, {
|
|
163
|
+
strict
|
|
164
|
+
})) {
|
|
165
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
166
|
+
resolve: {
|
|
167
|
+
resolverOpts: {
|
|
168
|
+
document: source
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
source = 'memory://arazzo.yaml';
|
|
173
|
+
sourceProvenance = '[inline YAML]';
|
|
174
|
+
} else {
|
|
175
|
+
sourceProvenance = source;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// next we assume that source is either file system URI or HTTP(S) URL
|
|
179
|
+
try {
|
|
180
|
+
const parseResult = await parseURI(source, mergedOptions);
|
|
181
|
+
|
|
182
|
+
// set retrievalURI metadata for file/URL sources (not for inline content)
|
|
183
|
+
if (!source.startsWith('memory://')) {
|
|
184
|
+
parseResult.meta.set('retrievalURI', source);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// validate that the parsed document is an Arazzo specification
|
|
188
|
+
if (!isArazzoSpecification1Element(parseResult.api)) {
|
|
189
|
+
throw new UnmatchedParserError(`Could not find a parser that can parse "${sourceProvenance}" as an Arazzo specification`);
|
|
190
|
+
}
|
|
191
|
+
return parseResult;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw new ParseError(`Failed to parse Arazzo Document from "${sourceProvenance}"`, {
|
|
194
|
+
cause: error
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.defaultOptions = void 0;
|
|
6
|
+
exports.parse = parse;
|
|
7
|
+
var _empty = require("@speclynx/apidom-reference/configuration/empty");
|
|
8
|
+
var _openapiJson = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-2"));
|
|
9
|
+
var _openapiYaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-2"));
|
|
10
|
+
var _openapiJson2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-0"));
|
|
11
|
+
var _openapiYaml2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0"));
|
|
12
|
+
var _openapiJson3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-1"));
|
|
13
|
+
var _openapiYaml3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1"));
|
|
14
|
+
var _apidomParserAdapterOpenapiJson = require("@speclynx/apidom-parser-adapter-openapi-json-3-1");
|
|
15
|
+
var _apidomParserAdapterOpenapiYaml = require("@speclynx/apidom-parser-adapter-openapi-yaml-3-1");
|
|
16
|
+
var _apidomParserAdapterOpenapiJson2 = require("@speclynx/apidom-parser-adapter-openapi-json-3-0");
|
|
17
|
+
var _apidomParserAdapterOpenapiYaml2 = require("@speclynx/apidom-parser-adapter-openapi-yaml-3-0");
|
|
18
|
+
var _apidomParserAdapterOpenapiJson3 = require("@speclynx/apidom-parser-adapter-openapi-json-2");
|
|
19
|
+
var _apidomParserAdapterOpenapiYaml3 = require("@speclynx/apidom-parser-adapter-openapi-yaml-2");
|
|
20
|
+
var _ramdaAdjunct = require("ramda-adjunct");
|
|
21
|
+
var _ParseError = _interopRequireDefault(require("./errors/ParseError.cjs"));
|
|
22
|
+
var _index = _interopRequireDefault(require("./resolve/resolvers/memory/index.cjs"));
|
|
23
|
+
var _parseArazzo = require("./parse-arazzo.cjs");
|
|
24
|
+
/**
|
|
25
|
+
* Options for parsing OpenAPI Documents.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Default reference options for parsing OpenAPI Documents.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
const defaultOptions = exports.defaultOptions = {
|
|
34
|
+
parse: {
|
|
35
|
+
parsers: [new _openapiJson.default({
|
|
36
|
+
allowEmpty: false
|
|
37
|
+
}), new _openapiYaml.default({
|
|
38
|
+
allowEmpty: false
|
|
39
|
+
}), new _openapiJson2.default({
|
|
40
|
+
allowEmpty: false
|
|
41
|
+
}), new _openapiYaml2.default({
|
|
42
|
+
allowEmpty: false
|
|
43
|
+
}), new _openapiJson3.default({
|
|
44
|
+
allowEmpty: false
|
|
45
|
+
}), new _openapiYaml3.default({
|
|
46
|
+
allowEmpty: false
|
|
47
|
+
})],
|
|
48
|
+
parserOpts: {
|
|
49
|
+
..._parseArazzo.defaultOptions.parse.parserOpts
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
resolve: {
|
|
53
|
+
resolvers: [new _index.default(), ..._parseArazzo.defaultOptions.resolve.resolvers.filter(r => r.name !== 'memory')],
|
|
54
|
+
resolverOpts: {}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Parses an OpenAPI Document from an object.
|
|
60
|
+
* @param source - The OpenAPI Document as a plain object
|
|
61
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
62
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
63
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Parses an OpenAPI Document from a string or URI.
|
|
69
|
+
* @param source - The OpenAPI Document as string content, or a file system path / HTTP(S) URL
|
|
70
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
71
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
72
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Parses an OpenAPI Document from a string, object, or URI.
|
|
78
|
+
* @param source - The OpenAPI Document as a plain object, string content, or URI
|
|
79
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
80
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
81
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Parses an OpenAPI Document from a string, object, or URI.
|
|
87
|
+
*
|
|
88
|
+
* The function handles three types of input:
|
|
89
|
+
* 1. Object - converts to JSON string and parses (source maps supported with `strict: false`)
|
|
90
|
+
* 2. String content - uses OpenAPI detection to identify and parse inline JSON or YAML content
|
|
91
|
+
* 3. URI string - if not detected as OpenAPI content, treats as file system path or HTTP(S) URL
|
|
92
|
+
*
|
|
93
|
+
* @param source - The OpenAPI Document as an object, string content, or a file system path / HTTP(S) URL
|
|
94
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
95
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
96
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* Parse from object
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const result = await parseOpenAPI({ openapi: '3.1.0', info: {...} });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* Parse inline JSON
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const result = await parseOpenAPI('{"openapi": "3.1.0", "info": {...}}');
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* Parse from file
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const result = await parseOpenAPI('/path/to/openapi.json');
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* Parse from URL
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const result = await parseOpenAPI('https://example.com/openapi.yaml');
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* Parse with custom options
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const result = await parseOpenAPI('/path/to/openapi.json', customOptions);
|
|
126
|
+
* ```
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
async function parse(source, options = {}) {
|
|
130
|
+
let mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
|
|
131
|
+
const strict = mergedOptions.parse?.parserOpts?.strict ?? true;
|
|
132
|
+
let sourceProvenance;
|
|
133
|
+
if ((0, _ramdaAdjunct.isPlainObject)(source)) {
|
|
134
|
+
const document = JSON.stringify(source, null, 2);
|
|
135
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
136
|
+
resolve: {
|
|
137
|
+
resolverOpts: {
|
|
138
|
+
document
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
source = 'memory://openapi.json';
|
|
143
|
+
sourceProvenance = '[object]';
|
|
144
|
+
} else if (await (0, _apidomParserAdapterOpenapiJson3.detect)(source, {
|
|
145
|
+
strict
|
|
146
|
+
})) {
|
|
147
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
148
|
+
resolve: {
|
|
149
|
+
resolverOpts: {
|
|
150
|
+
document: source
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
source = 'memory://openapi.json';
|
|
155
|
+
sourceProvenance = '[inline JSON]';
|
|
156
|
+
} else if (await (0, _apidomParserAdapterOpenapiYaml3.detect)(source, {
|
|
157
|
+
strict
|
|
158
|
+
})) {
|
|
159
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
160
|
+
resolve: {
|
|
161
|
+
resolverOpts: {
|
|
162
|
+
document: source
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
source = 'memory://openapi.yaml';
|
|
167
|
+
sourceProvenance = '[inline YAML]';
|
|
168
|
+
} else if (await (0, _apidomParserAdapterOpenapiJson2.detect)(source, {
|
|
169
|
+
strict
|
|
170
|
+
})) {
|
|
171
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
172
|
+
resolve: {
|
|
173
|
+
resolverOpts: {
|
|
174
|
+
document: source
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
source = 'memory://openapi.json';
|
|
179
|
+
sourceProvenance = '[inline JSON]';
|
|
180
|
+
} else if (await (0, _apidomParserAdapterOpenapiYaml2.detect)(source, {
|
|
181
|
+
strict
|
|
182
|
+
})) {
|
|
183
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
184
|
+
resolve: {
|
|
185
|
+
resolverOpts: {
|
|
186
|
+
document: source
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
source = 'memory://openapi.yaml';
|
|
191
|
+
sourceProvenance = '[inline YAML]';
|
|
192
|
+
} else if (await (0, _apidomParserAdapterOpenapiJson.detect)(source, {
|
|
193
|
+
strict
|
|
194
|
+
})) {
|
|
195
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
196
|
+
resolve: {
|
|
197
|
+
resolverOpts: {
|
|
198
|
+
document: source
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
source = 'memory://openapi.json';
|
|
203
|
+
sourceProvenance = '[inline JSON]';
|
|
204
|
+
} else if (await (0, _apidomParserAdapterOpenapiYaml.detect)(source, {
|
|
205
|
+
strict
|
|
206
|
+
})) {
|
|
207
|
+
mergedOptions = (0, _empty.mergeOptions)(mergedOptions, {
|
|
208
|
+
resolve: {
|
|
209
|
+
resolverOpts: {
|
|
210
|
+
document: source
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
source = 'memory://openapi.yaml';
|
|
215
|
+
sourceProvenance = '[inline YAML]';
|
|
216
|
+
} else {
|
|
217
|
+
sourceProvenance = source;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// next we assume that source is either file system URI or HTTP(S) URL
|
|
221
|
+
try {
|
|
222
|
+
const parseResult = await (0, _empty.parse)(source, mergedOptions);
|
|
223
|
+
|
|
224
|
+
// set retrievalURI metadata for file/URL sources (not for inline content)
|
|
225
|
+
if (!source.startsWith('memory://')) {
|
|
226
|
+
parseResult.meta.set('retrievalURI', source);
|
|
227
|
+
}
|
|
228
|
+
return parseResult;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
throw new _ParseError.default(`Failed to parse OpenAPI Document from "${sourceProvenance}"`, {
|
|
231
|
+
cause: error
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { parse as parseURI, mergeOptions } from '@speclynx/apidom-reference/configuration/empty';
|
|
2
|
+
import OpenApiJSON2Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-2';
|
|
3
|
+
import OpenApiYAML2Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-2';
|
|
4
|
+
import OpenApiJSON3_0Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-0';
|
|
5
|
+
import OpenApiYAML3_0Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0';
|
|
6
|
+
import OpenApiJSON3_1Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-1';
|
|
7
|
+
import OpenApiYAML3_1Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1';
|
|
8
|
+
import { detect as detectOpenApiJSON3_1 } from '@speclynx/apidom-parser-adapter-openapi-json-3-1';
|
|
9
|
+
import { detect as detectOpenApiYAML3_1 } from '@speclynx/apidom-parser-adapter-openapi-yaml-3-1';
|
|
10
|
+
import { detect as detectOpenApiJSON3_0 } from '@speclynx/apidom-parser-adapter-openapi-json-3-0';
|
|
11
|
+
import { detect as detectOpenApiYAML3_0 } from '@speclynx/apidom-parser-adapter-openapi-yaml-3-0';
|
|
12
|
+
import { detect as detectOpenApiJSON2 } from '@speclynx/apidom-parser-adapter-openapi-json-2';
|
|
13
|
+
import { detect as detectOpenApiYAML2 } from '@speclynx/apidom-parser-adapter-openapi-yaml-2';
|
|
14
|
+
import { isPlainObject } from 'ramda-adjunct';
|
|
15
|
+
import ParseError from "./errors/ParseError.mjs";
|
|
16
|
+
import MemoryResolver from "./resolve/resolvers/memory/index.mjs";
|
|
17
|
+
import { defaultOptions as arazzoDefaultOptions } from "./parse-arazzo.mjs";
|
|
18
|
+
/**
|
|
19
|
+
* Options for parsing OpenAPI Documents.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Default reference options for parsing OpenAPI Documents.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export const defaultOptions = {
|
|
27
|
+
parse: {
|
|
28
|
+
parsers: [new OpenApiJSON2Parser({
|
|
29
|
+
allowEmpty: false
|
|
30
|
+
}), new OpenApiYAML2Parser({
|
|
31
|
+
allowEmpty: false
|
|
32
|
+
}), new OpenApiJSON3_0Parser({
|
|
33
|
+
allowEmpty: false
|
|
34
|
+
}), new OpenApiYAML3_0Parser({
|
|
35
|
+
allowEmpty: false
|
|
36
|
+
}), new OpenApiJSON3_1Parser({
|
|
37
|
+
allowEmpty: false
|
|
38
|
+
}), new OpenApiYAML3_1Parser({
|
|
39
|
+
allowEmpty: false
|
|
40
|
+
})],
|
|
41
|
+
parserOpts: {
|
|
42
|
+
...arazzoDefaultOptions.parse.parserOpts
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
resolve: {
|
|
46
|
+
resolvers: [new MemoryResolver(), ...arazzoDefaultOptions.resolve.resolvers.filter(r => r.name !== 'memory')],
|
|
47
|
+
resolverOpts: {}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Parses an OpenAPI Document from an object.
|
|
53
|
+
* @param source - The OpenAPI Document as a plain object
|
|
54
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
55
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
56
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parses an OpenAPI Document from a string or URI.
|
|
62
|
+
* @param source - The OpenAPI Document as string content, or a file system path / HTTP(S) URL
|
|
63
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
64
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
65
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Parses an OpenAPI Document from a string, object, or URI.
|
|
71
|
+
* @param source - The OpenAPI Document as a plain object, string content, or URI
|
|
72
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
73
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
74
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Parses an OpenAPI Document from a string, object, or URI.
|
|
80
|
+
*
|
|
81
|
+
* The function handles three types of input:
|
|
82
|
+
* 1. Object - converts to JSON string and parses (source maps supported with `strict: false`)
|
|
83
|
+
* 2. String content - uses OpenAPI detection to identify and parse inline JSON or YAML content
|
|
84
|
+
* 3. URI string - if not detected as OpenAPI content, treats as file system path or HTTP(S) URL
|
|
85
|
+
*
|
|
86
|
+
* @param source - The OpenAPI Document as an object, string content, or a file system path / HTTP(S) URL
|
|
87
|
+
* @param options - Reference options (uses defaultOptions when not provided)
|
|
88
|
+
* @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
|
|
89
|
+
* @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Parse from object
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const result = await parseOpenAPI({ openapi: '3.1.0', info: {...} });
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* Parse inline JSON
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const result = await parseOpenAPI('{"openapi": "3.1.0", "info": {...}}');
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* Parse from file
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const result = await parseOpenAPI('/path/to/openapi.json');
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* Parse from URL
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const result = await parseOpenAPI('https://example.com/openapi.yaml');
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* Parse with custom options
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const result = await parseOpenAPI('/path/to/openapi.json', customOptions);
|
|
119
|
+
* ```
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export async function parse(source, options = {}) {
|
|
123
|
+
let mergedOptions = mergeOptions(defaultOptions, options);
|
|
124
|
+
const strict = mergedOptions.parse?.parserOpts?.strict ?? true;
|
|
125
|
+
let sourceProvenance;
|
|
126
|
+
if (isPlainObject(source)) {
|
|
127
|
+
const document = JSON.stringify(source, null, 2);
|
|
128
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
129
|
+
resolve: {
|
|
130
|
+
resolverOpts: {
|
|
131
|
+
document
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
source = 'memory://openapi.json';
|
|
136
|
+
sourceProvenance = '[object]';
|
|
137
|
+
} else if (await detectOpenApiJSON2(source, {
|
|
138
|
+
strict
|
|
139
|
+
})) {
|
|
140
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
141
|
+
resolve: {
|
|
142
|
+
resolverOpts: {
|
|
143
|
+
document: source
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
source = 'memory://openapi.json';
|
|
148
|
+
sourceProvenance = '[inline JSON]';
|
|
149
|
+
} else if (await detectOpenApiYAML2(source, {
|
|
150
|
+
strict
|
|
151
|
+
})) {
|
|
152
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
153
|
+
resolve: {
|
|
154
|
+
resolverOpts: {
|
|
155
|
+
document: source
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
source = 'memory://openapi.yaml';
|
|
160
|
+
sourceProvenance = '[inline YAML]';
|
|
161
|
+
} else if (await detectOpenApiJSON3_0(source, {
|
|
162
|
+
strict
|
|
163
|
+
})) {
|
|
164
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
165
|
+
resolve: {
|
|
166
|
+
resolverOpts: {
|
|
167
|
+
document: source
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
source = 'memory://openapi.json';
|
|
172
|
+
sourceProvenance = '[inline JSON]';
|
|
173
|
+
} else if (await detectOpenApiYAML3_0(source, {
|
|
174
|
+
strict
|
|
175
|
+
})) {
|
|
176
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
177
|
+
resolve: {
|
|
178
|
+
resolverOpts: {
|
|
179
|
+
document: source
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
source = 'memory://openapi.yaml';
|
|
184
|
+
sourceProvenance = '[inline YAML]';
|
|
185
|
+
} else if (await detectOpenApiJSON3_1(source, {
|
|
186
|
+
strict
|
|
187
|
+
})) {
|
|
188
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
189
|
+
resolve: {
|
|
190
|
+
resolverOpts: {
|
|
191
|
+
document: source
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
source = 'memory://openapi.json';
|
|
196
|
+
sourceProvenance = '[inline JSON]';
|
|
197
|
+
} else if (await detectOpenApiYAML3_1(source, {
|
|
198
|
+
strict
|
|
199
|
+
})) {
|
|
200
|
+
mergedOptions = mergeOptions(mergedOptions, {
|
|
201
|
+
resolve: {
|
|
202
|
+
resolverOpts: {
|
|
203
|
+
document: source
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
source = 'memory://openapi.yaml';
|
|
208
|
+
sourceProvenance = '[inline YAML]';
|
|
209
|
+
} else {
|
|
210
|
+
sourceProvenance = source;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// next we assume that source is either file system URI or HTTP(S) URL
|
|
214
|
+
try {
|
|
215
|
+
const parseResult = await parseURI(source, mergedOptions);
|
|
216
|
+
|
|
217
|
+
// set retrievalURI metadata for file/URL sources (not for inline content)
|
|
218
|
+
if (!source.startsWith('memory://')) {
|
|
219
|
+
parseResult.meta.set('retrievalURI', source);
|
|
220
|
+
}
|
|
221
|
+
return parseResult;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new ParseError(`Failed to parse OpenAPI Document from "${sourceProvenance}"`, {
|
|
224
|
+
cause: error
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _empty = require("@speclynx/apidom-reference/configuration/empty");
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
class MemoryResolver extends _empty.Resolver {
|
|
10
|
+
constructor() {
|
|
11
|
+
super({
|
|
12
|
+
name: 'memory'
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
canRead(file) {
|
|
16
|
+
return file.uri.startsWith('memory://') && this.document !== undefined;
|
|
17
|
+
}
|
|
18
|
+
async read(file) {
|
|
19
|
+
try {
|
|
20
|
+
const encoder = new TextEncoder();
|
|
21
|
+
return encoder.encode(this.document);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
throw new _empty.ResolverError(`Error opening file "${file.uri}"`, {
|
|
24
|
+
cause: error
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
var _default = exports.default = MemoryResolver;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Resolver, ResolverError } from '@speclynx/apidom-reference/configuration/empty';
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
class MemoryResolver extends Resolver {
|
|
6
|
+
constructor() {
|
|
7
|
+
super({
|
|
8
|
+
name: 'memory'
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
canRead(file) {
|
|
12
|
+
return file.uri.startsWith('memory://') && this.document !== undefined;
|
|
13
|
+
}
|
|
14
|
+
async read(file) {
|
|
15
|
+
try {
|
|
16
|
+
const encoder = new TextEncoder();
|
|
17
|
+
return encoder.encode(this.document);
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw new ResolverError(`Error opening file "${file.uri}"`, {
|
|
20
|
+
cause: error
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default MemoryResolver;
|