@jentic/arazzo-parser 1.0.0-alpha.2 → 1.0.0-alpha.21

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.
@@ -0,0 +1,188 @@
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: 5000,
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
+ *
85
+ * The function handles three types of input:
86
+ * 1. Object - converts to JSON string and parses (source maps supported with `strict: false`)
87
+ * 2. String content - uses Arazzo detection to identify and parse inline JSON or YAML content
88
+ * 3. URI string - if not detected as Arazzo content, treats as file system path or HTTP(S) URL
89
+ *
90
+ * @param source - The Arazzo Document as an object, string content, or a file system path / HTTP(S) URL
91
+ * @param options - Reference options (uses defaultOptions when not provided)
92
+ * @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
93
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
94
+ *
95
+ * @example
96
+ * Parse from object
97
+ * ```typescript
98
+ * const result = await parseArazzo({ arazzo: '1.0.1', info: {...} });
99
+ * ```
100
+ *
101
+ * @example
102
+ * Parse inline JSON
103
+ * ```typescript
104
+ * const result = await parseArazzo('{"arazzo": "1.0.1", "info": {...}}');
105
+ * ```
106
+ *
107
+ * @example
108
+ * Parse from file
109
+ * ```typescript
110
+ * const result = await parseArazzo('/path/to/arazzo.json');
111
+ * ```
112
+ *
113
+ * @example
114
+ * Parse from URL
115
+ * ```typescript
116
+ * const result = await parseArazzo('https://example.com/arazzo.yaml');
117
+ * ```
118
+ *
119
+ * @example
120
+ * Parse with custom options
121
+ * ```typescript
122
+ * const result = await parseArazzo('/path/to/arazzo.json', customOptions);
123
+ * ```
124
+ * @public
125
+ */
126
+ export async function parse(source, options = {}) {
127
+ let mergedOptions = mergeOptions(defaultOptions, options);
128
+ const strict = mergedOptions.parse?.parserOpts?.strict ?? true;
129
+ let sourceProvenance;
130
+ if (isPlainObject(source)) {
131
+ const document = JSON.stringify(source, null, 2);
132
+ mergedOptions = mergeOptions(mergedOptions, {
133
+ resolve: {
134
+ resolverOpts: {
135
+ document
136
+ }
137
+ }
138
+ });
139
+ source = 'memory://arazzo.json';
140
+ sourceProvenance = '[object]';
141
+ } else if (await detectArazzoJSON(source, {
142
+ strict
143
+ })) {
144
+ mergedOptions = mergeOptions(mergedOptions, {
145
+ resolve: {
146
+ resolverOpts: {
147
+ document: source
148
+ }
149
+ }
150
+ });
151
+ source = 'memory://arazzo.json';
152
+ sourceProvenance = '[inline JSON]';
153
+ } else if (await detectArazzoYAML(source, {
154
+ strict
155
+ })) {
156
+ mergedOptions = mergeOptions(mergedOptions, {
157
+ resolve: {
158
+ resolverOpts: {
159
+ document: source
160
+ }
161
+ }
162
+ });
163
+ source = 'memory://arazzo.yaml';
164
+ sourceProvenance = '[inline YAML]';
165
+ } else {
166
+ sourceProvenance = source;
167
+ }
168
+
169
+ // next we assume that source is either file system URI or HTTP(S) URL
170
+ try {
171
+ const parseResult = await parseURI(source, mergedOptions);
172
+
173
+ // set retrievalURI metadata for file/URL sources (not for inline content)
174
+ if (!source.startsWith('memory://')) {
175
+ parseResult.meta.set('retrievalURI', source);
176
+ }
177
+
178
+ // validate that the parsed document is an Arazzo specification
179
+ if (!isArazzoSpecification1Element(parseResult.api)) {
180
+ throw new UnmatchedParserError(`Could not find a parser that can parse "${sourceProvenance}" as an Arazzo specification`);
181
+ }
182
+ return parseResult;
183
+ } catch (error) {
184
+ throw new ParseError(`Failed to parse Arazzo Document from "${sourceProvenance}"`, {
185
+ cause: error
186
+ });
187
+ }
188
+ }
@@ -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 _file = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/file"));
15
+ var _httpAxios = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/http-axios"));
16
+ var _apidomParserAdapterOpenapiJson = require("@speclynx/apidom-parser-adapter-openapi-json-3-1");
17
+ var _apidomParserAdapterOpenapiYaml = require("@speclynx/apidom-parser-adapter-openapi-yaml-3-1");
18
+ var _apidomParserAdapterOpenapiJson2 = require("@speclynx/apidom-parser-adapter-openapi-json-3-0");
19
+ var _apidomParserAdapterOpenapiYaml2 = require("@speclynx/apidom-parser-adapter-openapi-yaml-3-0");
20
+ var _apidomParserAdapterOpenapiJson3 = require("@speclynx/apidom-parser-adapter-openapi-json-2");
21
+ var _apidomParserAdapterOpenapiYaml3 = require("@speclynx/apidom-parser-adapter-openapi-yaml-2");
22
+ var _ramdaAdjunct = require("ramda-adjunct");
23
+ var _ParseError = _interopRequireDefault(require("./errors/ParseError.cjs"));
24
+ var _index = _interopRequireDefault(require("./resolve/resolvers/memory/index.cjs"));
25
+ /**
26
+ * Options for parsing OpenAPI Documents.
27
+ * @public
28
+ */
29
+
30
+ /**
31
+ * Default reference options for parsing OpenAPI Documents.
32
+ * @public
33
+ */
34
+ const defaultOptions = exports.defaultOptions = {
35
+ parse: {
36
+ parsers: [new _openapiJson.default({
37
+ allowEmpty: false
38
+ }), new _openapiYaml.default({
39
+ allowEmpty: false
40
+ }), new _openapiJson2.default({
41
+ allowEmpty: false
42
+ }), new _openapiYaml2.default({
43
+ allowEmpty: false
44
+ }), new _openapiJson3.default({
45
+ allowEmpty: false
46
+ }), new _openapiYaml3.default({
47
+ allowEmpty: false
48
+ })],
49
+ parserOpts: {
50
+ sourceMap: false,
51
+ style: false,
52
+ strict: true
53
+ }
54
+ },
55
+ resolve: {
56
+ resolvers: [new _index.default(), new _file.default({
57
+ fileAllowList: ['*.json', '*.yaml', '*.yml']
58
+ }), new _httpAxios.default({
59
+ timeout: 5000,
60
+ redirects: 5,
61
+ withCredentials: false
62
+ })],
63
+ resolverOpts: {}
64
+ }
65
+ };
66
+
67
+ /**
68
+ * Parses an OpenAPI Document from an object.
69
+ * @param source - The OpenAPI Document as a plain object
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 or URI.
78
+ * @param source - The OpenAPI Document as string content, or a file system path / HTTP(S) URL
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 FileResolver from '@speclynx/apidom-reference/resolve/resolvers/file';
9
+ import HTTPResolverAxios from '@speclynx/apidom-reference/resolve/resolvers/http-axios';
10
+ import { detect as detectOpenApiJSON3_1 } from '@speclynx/apidom-parser-adapter-openapi-json-3-1';
11
+ import { detect as detectOpenApiYAML3_1 } from '@speclynx/apidom-parser-adapter-openapi-yaml-3-1';
12
+ import { detect as detectOpenApiJSON3_0 } from '@speclynx/apidom-parser-adapter-openapi-json-3-0';
13
+ import { detect as detectOpenApiYAML3_0 } from '@speclynx/apidom-parser-adapter-openapi-yaml-3-0';
14
+ import { detect as detectOpenApiJSON2 } from '@speclynx/apidom-parser-adapter-openapi-json-2';
15
+ import { detect as detectOpenApiYAML2 } from '@speclynx/apidom-parser-adapter-openapi-yaml-2';
16
+ import { isPlainObject } from 'ramda-adjunct';
17
+ import ParseError from "./errors/ParseError.mjs";
18
+ import MemoryResolver from "./resolve/resolvers/memory/index.mjs";
19
+ /**
20
+ * Options for parsing OpenAPI Documents.
21
+ * @public
22
+ */
23
+ /**
24
+ * Default reference options for parsing OpenAPI Documents.
25
+ * @public
26
+ */
27
+ export const defaultOptions = {
28
+ parse: {
29
+ parsers: [new OpenApiJSON2Parser({
30
+ allowEmpty: false
31
+ }), new OpenApiYAML2Parser({
32
+ allowEmpty: false
33
+ }), new OpenApiJSON3_0Parser({
34
+ allowEmpty: false
35
+ }), new OpenApiYAML3_0Parser({
36
+ allowEmpty: false
37
+ }), new OpenApiJSON3_1Parser({
38
+ allowEmpty: false
39
+ }), new OpenApiYAML3_1Parser({
40
+ allowEmpty: false
41
+ })],
42
+ parserOpts: {
43
+ sourceMap: false,
44
+ style: false,
45
+ strict: true
46
+ }
47
+ },
48
+ resolve: {
49
+ resolvers: [new MemoryResolver(), new FileResolver({
50
+ fileAllowList: ['*.json', '*.yaml', '*.yml']
51
+ }), new HTTPResolverAxios({
52
+ timeout: 5000,
53
+ redirects: 5,
54
+ withCredentials: false
55
+ })],
56
+ resolverOpts: {}
57
+ }
58
+ };
59
+
60
+ /**
61
+ * Parses an OpenAPI Document from an object.
62
+ * @param source - The OpenAPI Document as a plain object
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 or URI.
71
+ * @param source - The OpenAPI Document as string content, or a file system path / HTTP(S) URL
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;