@jentic/arazzo-parser 1.0.0-alpha.24 → 1.0.0-alpha.26

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,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;
@@ -2,29 +2,17 @@ import type { ApiDOMReferenceOptions } from '@speclynx/apidom-reference';
2
2
  import { ParseResultElement } from '@speclynx/apidom-datamodel';
3
3
  import type { PartialDeep } from 'type-fest';
4
4
 
5
- /**
6
- * Options for parsing Arazzo Documents.
7
- * @public
8
- */
9
- export declare type ArazzoOptions = PartialDeep<ApiDOMReferenceOptions>;
10
-
11
5
  /**
12
6
  * Default reference options for parsing Arazzo Documents.
13
7
  * @public
14
8
  */
15
- export declare const defaultArazzoOptions: ArazzoOptions;
9
+ export declare const defaultParseArazzoOptions: ParseArazzoOptions;
16
10
 
17
11
  /**
18
12
  * Default reference options for parsing OpenAPI Documents.
19
13
  * @public
20
14
  */
21
- export declare const defaultOpenAPIOptions: OpenAPIOptions;
22
-
23
- /**
24
- * Options for parsing OpenAPI Documents.
25
- * @public
26
- */
27
- export declare type OpenAPIOptions = PartialDeep<ApiDOMReferenceOptions>;
15
+ export declare const defaultParseOpenAPIOptions: ParseOpenAPIOptions;
28
16
 
29
17
  /**
30
18
  * Parses an Arazzo Document from an object.
@@ -34,7 +22,7 @@ export declare type OpenAPIOptions = PartialDeep<ApiDOMReferenceOptions>;
34
22
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
35
23
  * @public
36
24
  */
37
- export declare function parseArazzo(source: Record<string, unknown>, options?: ArazzoOptions): Promise<ParseResultElement>;
25
+ export declare function parseArazzo(source: Record<string, unknown>, options?: ParseArazzoOptions): Promise<ParseResultElement>;
38
26
 
39
27
  /**
40
28
  * Parses an Arazzo Document from a string or URI.
@@ -44,7 +32,7 @@ export declare function parseArazzo(source: Record<string, unknown>, options?: A
44
32
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
45
33
  * @public
46
34
  */
47
- export declare function parseArazzo(source: string, options?: ArazzoOptions): Promise<ParseResultElement>;
35
+ export declare function parseArazzo(source: string, options?: ParseArazzoOptions): Promise<ParseResultElement>;
48
36
 
49
37
  /**
50
38
  * Parses an Arazzo Document from a string, object, or URI.
@@ -54,7 +42,13 @@ export declare function parseArazzo(source: string, options?: ArazzoOptions): Pr
54
42
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
55
43
  * @public
56
44
  */
57
- export declare function parseArazzo(source: string | Record<string, unknown>, options?: ArazzoOptions): Promise<ParseResultElement>;
45
+ export declare function parseArazzo(source: string | Record<string, unknown>, options?: ParseArazzoOptions): Promise<ParseResultElement>;
46
+
47
+ /**
48
+ * Options for parsing Arazzo Documents.
49
+ * @public
50
+ */
51
+ export declare type ParseArazzoOptions = PartialDeep<ApiDOMReferenceOptions>;
58
52
 
59
53
  /**
60
54
  * Parses an OpenAPI Document from an object.
@@ -64,7 +58,7 @@ export declare function parseArazzo(source: string | Record<string, unknown>, op
64
58
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
65
59
  * @public
66
60
  */
67
- export declare function parseOpenAPI(source: Record<string, unknown>, options?: OpenAPIOptions): Promise<ParseResultElement>;
61
+ export declare function parseOpenAPI(source: Record<string, unknown>, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
68
62
 
69
63
  /**
70
64
  * Parses an OpenAPI Document from a string or URI.
@@ -74,7 +68,7 @@ export declare function parseOpenAPI(source: Record<string, unknown>, options?:
74
68
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
75
69
  * @public
76
70
  */
77
- export declare function parseOpenAPI(source: string, options?: OpenAPIOptions): Promise<ParseResultElement>;
71
+ export declare function parseOpenAPI(source: string, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
78
72
 
79
73
  /**
80
74
  * Parses an OpenAPI Document from a string, object, or URI.
@@ -84,7 +78,13 @@ export declare function parseOpenAPI(source: string, options?: OpenAPIOptions):
84
78
  * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
85
79
  * @public
86
80
  */
87
- export declare function parseOpenAPI(source: string | Record<string, unknown>, options?: OpenAPIOptions): Promise<ParseResultElement>;
81
+ export declare function parseOpenAPI(source: string | Record<string, unknown>, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
82
+
83
+ /**
84
+ * Options for parsing OpenAPI Documents.
85
+ * @public
86
+ */
87
+ export declare type ParseOpenAPIOptions = PartialDeep<ApiDOMReferenceOptions>;
88
88
 
89
89
  export { ParseResultElement }
90
90