@jentic/arazzo-parser 1.0.0-alpha.3 → 1.0.0-alpha.31
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 +154 -0
- package/NOTICE +8 -0
- package/README.md +439 -48
- package/dist/jentic-arazzo-parser.browser.js +64188 -43093
- package/dist/jentic-arazzo-parser.browser.min.js +1 -1
- package/package.json +19 -10
- package/src/index.cjs +7 -152
- package/src/index.mjs +2 -147
- 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/types/arazzo-parser.d.ts +64 -29
- package/dist/937fc7a248317278ae14.wasm +0 -0
package/README.md
CHANGED
|
@@ -3,6 +3,15 @@
|
|
|
3
3
|
`@jentic/arazzo-parser` is a parser for [Arazzo Specification](https://spec.openapis.org/arazzo/latest.html) documents.
|
|
4
4
|
It produces [SpecLynx ApiDOM](https://github.com/speclynx/apidom) data model using the [Arazzo 1.x namespace](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-arazzo-1#readme).
|
|
5
5
|
|
|
6
|
+
**Supported Arazzo versions:**
|
|
7
|
+
- [Arazzo 1.0.0](https://spec.openapis.org/arazzo/v1.0.0)
|
|
8
|
+
- [Arazzo 1.0.1](https://spec.openapis.org/arazzo/v1.0.1)
|
|
9
|
+
|
|
10
|
+
**Supported OpenAPI versions (for source descriptions):**
|
|
11
|
+
- [OpenAPI 2.0](https://spec.openapis.org/oas/v2.0)
|
|
12
|
+
- [OpenAPI 3.0.x](https://spec.openapis.org/oas/v3.0.4)
|
|
13
|
+
- [OpenAPI 3.1.x](https://spec.openapis.org/oas/v3.1.2)
|
|
14
|
+
|
|
6
15
|
## Installation
|
|
7
16
|
|
|
8
17
|
You can install this package via [npm](https://npmjs.org/) CLI by running the following command:
|
|
@@ -13,17 +22,21 @@ npm install @jentic/arazzo-parser
|
|
|
13
22
|
|
|
14
23
|
## Usage
|
|
15
24
|
|
|
16
|
-
`@jentic/arazzo-parser` provides a
|
|
25
|
+
`@jentic/arazzo-parser` provides a `parseArazzo` function for parsing Arazzo documents.
|
|
26
|
+
|
|
27
|
+
## Parsing Arazzo Documents
|
|
17
28
|
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
The `parseArazzo` function accepts multiple input types:
|
|
30
|
+
|
|
31
|
+
1. **Plain JavaScript object** - converts to JSON and parses (source maps supported with `strict: false`)
|
|
32
|
+
2. **String content** - detects Arazzo content and parses inline JSON or YAML
|
|
20
33
|
3. **File system path** - resolves and parses local Arazzo Documents
|
|
21
34
|
4. **HTTP(S) URL** - fetches and parses remote Arazzo Documents
|
|
22
35
|
|
|
23
|
-
###
|
|
36
|
+
### From object
|
|
24
37
|
|
|
25
38
|
```js
|
|
26
|
-
import {
|
|
39
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
27
40
|
|
|
28
41
|
const arazzoDocument = {
|
|
29
42
|
arazzo: '1.0.1',
|
|
@@ -41,20 +54,20 @@ const arazzoDocument = {
|
|
|
41
54
|
workflows: [],
|
|
42
55
|
};
|
|
43
56
|
|
|
44
|
-
const parseResult = await
|
|
57
|
+
const parseResult = await parseArazzo(arazzoDocument);
|
|
45
58
|
// parseResult is ParseResultElement containing ArazzoSpecification1Element
|
|
46
59
|
```
|
|
47
60
|
|
|
48
|
-
###
|
|
61
|
+
### From string
|
|
49
62
|
|
|
50
63
|
```js
|
|
51
|
-
import {
|
|
64
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
52
65
|
|
|
53
66
|
// JSON string
|
|
54
|
-
const parseResult = await
|
|
67
|
+
const parseResult = await parseArazzo('{"arazzo": "1.0.1", "info": {...}}');
|
|
55
68
|
|
|
56
69
|
// YAML string
|
|
57
|
-
const parseResult = await
|
|
70
|
+
const parseResult = await parseArazzo(`
|
|
58
71
|
arazzo: '1.0.1'
|
|
59
72
|
info:
|
|
60
73
|
title: My API Workflow
|
|
@@ -62,55 +75,50 @@ info:
|
|
|
62
75
|
`);
|
|
63
76
|
```
|
|
64
77
|
|
|
65
|
-
###
|
|
78
|
+
### From file
|
|
66
79
|
|
|
67
80
|
```js
|
|
68
|
-
import {
|
|
81
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
69
82
|
|
|
70
|
-
const parseResult = await
|
|
83
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json');
|
|
71
84
|
```
|
|
72
85
|
|
|
73
|
-
###
|
|
86
|
+
### From URL
|
|
74
87
|
|
|
75
88
|
```js
|
|
76
|
-
import {
|
|
89
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
77
90
|
|
|
78
|
-
const parseResult = await
|
|
91
|
+
const parseResult = await parseArazzo('https://example.com/arazzo.yaml');
|
|
79
92
|
```
|
|
80
93
|
|
|
81
94
|
## Parse options
|
|
82
95
|
|
|
83
|
-
The `
|
|
96
|
+
The `parseArazzo` function accepts an optional second argument with reference options compatible with [SpecLynx ApiDOM Reference Options](https://github.com/speclynx/apidom/blob/main/packages/apidom-reference/src/options/index.ts):
|
|
84
97
|
|
|
85
98
|
```js
|
|
86
|
-
import {
|
|
87
|
-
|
|
88
|
-
const parseResult = await
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
100
|
+
|
|
101
|
+
const parseResult = await parseArazzo(source, {
|
|
102
|
+
parse: {
|
|
103
|
+
parserOpts: {
|
|
104
|
+
strict: true, // Use strict parsing mode (default: true)
|
|
105
|
+
sourceMap: false, // Include source maps (default: false)
|
|
106
|
+
style: false, // Capture style information for round-trip preservation (default: false)
|
|
107
|
+
},
|
|
108
|
+
},
|
|
93
109
|
});
|
|
94
110
|
```
|
|
95
111
|
|
|
96
|
-
### Options
|
|
97
|
-
|
|
98
|
-
| Option | Type | Default | Description |
|
|
99
|
-
|--------|------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
100
|
-
| `strict` | `boolean` | `true` | Whether to enforce strict parsing mode. Strict mode uses native JSON and YAML parsers without error recovery. |
|
|
101
|
-
| `sourceMap` | `boolean` | `false` | Whether to include [source maps](https://github.com/speclynx/apidom/blob/main/packages/apidom-datamodel/README.md#source-maps) in the parsed result. |
|
|
102
|
-
| `parserOpts` | `Record<string, unknown>` | `{}` | Additional options passed to the underlying parsers. |
|
|
103
|
-
| `resolverOpts` | `Record<string, unknown>` | `{}` | Additional options passed to the underlying resolvers. |
|
|
104
|
-
|
|
105
112
|
### Default options
|
|
106
113
|
|
|
107
114
|
You can import the default options:
|
|
108
115
|
|
|
109
116
|
```js
|
|
110
|
-
import {
|
|
117
|
+
import { defaultParseArazzoOptions } from '@jentic/arazzo-parser';
|
|
118
|
+
import { defaultParseOpenAPIOptions } from '@jentic/arazzo-parser';
|
|
111
119
|
|
|
112
|
-
console.
|
|
113
|
-
|
|
120
|
+
console.dir(defaultParseArazzoOptions, { depth: null });
|
|
121
|
+
console.dir(defaultParseOpenAPIOptions, { depth: null });
|
|
114
122
|
```
|
|
115
123
|
|
|
116
124
|
## Error handling
|
|
@@ -118,10 +126,10 @@ console.log(defaultParseOptions);
|
|
|
118
126
|
When parsing fails, a `ParseError` is thrown. The original error is available via the `cause` property:
|
|
119
127
|
|
|
120
128
|
```js
|
|
121
|
-
import {
|
|
129
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
122
130
|
|
|
123
131
|
try {
|
|
124
|
-
await
|
|
132
|
+
await parseArazzo('invalid content');
|
|
125
133
|
} catch (error) {
|
|
126
134
|
console.error(error.message); // 'Failed to parse Arazzo Document'
|
|
127
135
|
console.error(error.cause); // Original error from underlying parser
|
|
@@ -130,12 +138,12 @@ try {
|
|
|
130
138
|
|
|
131
139
|
## Working with the result
|
|
132
140
|
|
|
133
|
-
The `
|
|
141
|
+
The `parseArazzo` function returns a [ParseResultElement](https://github.com/speclynx/apidom/blob/main/packages/apidom-datamodel/README.md#parseresultelement) representing the result of the parsing operation.
|
|
134
142
|
|
|
135
143
|
```js
|
|
136
|
-
import {
|
|
144
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
137
145
|
|
|
138
|
-
const parseResult = await
|
|
146
|
+
const parseResult = await parseArazzo(source);
|
|
139
147
|
|
|
140
148
|
// Access the main Arazzo specification element
|
|
141
149
|
const arazzoSpec = parseResult.api;
|
|
@@ -147,6 +155,387 @@ const hasErrors = parseResult.errors.length > 0;
|
|
|
147
155
|
const isEmpty = parseResult.isEmpty;
|
|
148
156
|
```
|
|
149
157
|
|
|
158
|
+
### Retrieval URI metadata
|
|
159
|
+
|
|
160
|
+
When parsing from a file system path or HTTP(S) URL, the `retrievalURI` metadata is set on the parse result:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
164
|
+
|
|
165
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json');
|
|
166
|
+
|
|
167
|
+
// Get the URI from which the document was retrieved
|
|
168
|
+
const uri = parseResult.meta.get('retrievalURI');
|
|
169
|
+
// '/path/to/arazzo.json'
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Note: `retrievalURI` is not set when parsing from inline content (string) or plain objects.
|
|
173
|
+
|
|
174
|
+
### Source maps
|
|
175
|
+
|
|
176
|
+
Source maps allow you to track the original position (line, column) of each element in the parsed document. This is useful for error reporting, IDE integrations, linting, and any tooling that needs to show precise locations in the original source.
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
To enable source maps, set `sourceMap: true` and `strict: false` in the parser options:
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
183
|
+
|
|
184
|
+
const parseResult = await parseArazzo('/path/to/arazzo.yaml', {
|
|
185
|
+
parse: {
|
|
186
|
+
parserOpts: {
|
|
187
|
+
sourceMap: true,
|
|
188
|
+
strict: false,
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
When source maps are enabled, each element in the parsed result contains positional properties stored directly on the element. Position values use UTF-16 code units for compatibility with Language Server Protocol (LSP) and JavaScript string indexing:
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
198
|
+
|
|
199
|
+
const parseResult = await parseArazzo('/path/to/arazzo.yaml', {
|
|
200
|
+
parse: { parserOpts: { sourceMap: true, strict: false } },
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const arazzoSpec = parseResult.api;
|
|
204
|
+
|
|
205
|
+
// Access source map properties directly on the element
|
|
206
|
+
arazzoSpec.startLine; // 0-based line number where element begins
|
|
207
|
+
arazzoSpec.startCharacter; // 0-based column number where element begins
|
|
208
|
+
arazzoSpec.startOffset; // 0-based character offset from document start
|
|
209
|
+
arazzoSpec.endLine; // 0-based line number where element ends
|
|
210
|
+
arazzoSpec.endCharacter; // 0-based column number where element ends
|
|
211
|
+
arazzoSpec.endOffset; // 0-based character offset where element ends
|
|
212
|
+
|
|
213
|
+
// Access source map on nested elements
|
|
214
|
+
const workflow = arazzoSpec.workflows.get(0);
|
|
215
|
+
console.log(`Workflow starts at line ${workflow.startLine}, column ${workflow.startCharacter}`);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
For more details about source maps, see the [SpecLynx ApiDOM Data Model documentation](https://github.com/speclynx/apidom/tree/main/packages/apidom-datamodel#source-maps).
|
|
219
|
+
|
|
220
|
+
**Note:** Source maps require `strict: false` to be set. When parsing from objects, they are converted to pretty-printed JSON strings internally (2-space indentation), so source map positions refer to this generated JSON representation, not the original object structure:
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
// Source maps with objects (requires strict: false)
|
|
224
|
+
// Positions will reference the internally generated JSON string
|
|
225
|
+
await parseArazzo({ arazzo: '1.0.1', ... }, {
|
|
226
|
+
parse: {
|
|
227
|
+
parserOpts: {
|
|
228
|
+
sourceMap: true,
|
|
229
|
+
strict: false
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Style preservation
|
|
236
|
+
|
|
237
|
+
Style preservation captures format-specific style information for round-trip preservation. When enabled, the parser records formatting details (e.g., YAML quoting styles, flow/block indicators, comments, indentation; JSON indentation, raw number representation) on each parsed element. These details can then be used by [`toYAML`](https://github.com/speclynx/apidom/tree/main/packages/apidom-core#toyaml) and [`toJSON`](https://github.com/speclynx/apidom/tree/main/packages/apidom-core#tojson) from `@speclynx/apidom-core` to reproduce the original formatting.
|
|
238
|
+
|
|
239
|
+
To enable style preservation, set `style: true` and `strict: false` in the parser options:
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
243
|
+
import { toYAML } from '@speclynx/apidom-core';
|
|
244
|
+
|
|
245
|
+
const parseResult = await parseArazzo('/path/to/arazzo.yaml', {
|
|
246
|
+
parse: {
|
|
247
|
+
parserOpts: {
|
|
248
|
+
style: true,
|
|
249
|
+
strict: false,
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// round-trip back to YAML preserving original formatting
|
|
255
|
+
const yaml = toYAML(parseResult.api, { preserveStyle: true });
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
```js
|
|
259
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
260
|
+
import { toJSON } from '@speclynx/apidom-core';
|
|
261
|
+
|
|
262
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
263
|
+
parse: {
|
|
264
|
+
parserOpts: {
|
|
265
|
+
style: true,
|
|
266
|
+
strict: false,
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// round-trip back to JSON preserving original formatting
|
|
272
|
+
const json = toJSON(parseResult.api, undefined, undefined, { preserveStyle: true });
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**Note:** Style preservation requires `strict: false`. Both `style` and `sourceMap` can be enabled simultaneously — they are independent features.
|
|
276
|
+
|
|
277
|
+
## Parsing source descriptions
|
|
278
|
+
|
|
279
|
+
Arazzo documents can reference external API specifications (OpenAPI, Arazzo) through [Source Descriptions](https://spec.openapis.org/arazzo/latest.html#source-description-object). The parser can automatically fetch and parse these referenced documents.
|
|
280
|
+
|
|
281
|
+
**Note:** Source descriptions parsing is disabled by default for performance reasons. Enable it explicitly when you need to resolve and parse referenced API specifications.
|
|
282
|
+
|
|
283
|
+
### Enabling source descriptions parsing
|
|
284
|
+
|
|
285
|
+
To parse source descriptions, enable the `sourceDescriptions` option in `parserOpts`:
|
|
286
|
+
|
|
287
|
+
```js
|
|
288
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
289
|
+
|
|
290
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
291
|
+
parse: {
|
|
292
|
+
parserOpts: {
|
|
293
|
+
sourceDescriptions: true,
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Alternatively, you can configure it per parser for more granular control:
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
303
|
+
parse: {
|
|
304
|
+
parserOpts: {
|
|
305
|
+
'arazzo-json-1': { sourceDescriptions: true },
|
|
306
|
+
'arazzo-yaml-1': { sourceDescriptions: true },
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Selective parsing
|
|
313
|
+
|
|
314
|
+
You can selectively parse only specific source descriptions by providing an array of names:
|
|
315
|
+
|
|
316
|
+
```js
|
|
317
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
318
|
+
parse: {
|
|
319
|
+
parserOpts: {
|
|
320
|
+
sourceDescriptions: ['petStoreApi', 'paymentApi'],
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Result structure
|
|
327
|
+
|
|
328
|
+
When source descriptions are parsed, each parsed document that is a *direct* source description of the main Arazzo document is added to the main `ParseResultElement` as an additional top-level element. The first element is always the main Arazzo document, and subsequent top-level elements are these directly parsed source descriptions. When recursive parsing discovers further source descriptions from within an already parsed source description, those recursively parsed documents are attached as nested `ParseResultElement` instances beneath the source-description element that referenced them (they are not duplicated at the top level). Consumers that need to see all documents should traverse both the top-level elements and any nested `ParseResultElement`s reachable from source-description elements.
|
|
329
|
+
|
|
330
|
+
Source descriptions are parsed into their appropriate SpecLynx ApiDOM namespace data models based on document type:
|
|
331
|
+
|
|
332
|
+
- [Arazzo 1.x](https://spec.openapis.org/arazzo/latest.html) → [@speclynx/apidom-ns-arazzo-1](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-arazzo-1)
|
|
333
|
+
- [OpenAPI 2.0 (Swagger)](https://spec.openapis.org/oas/v2.0) → [@speclynx/apidom-ns-openapi-2](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-2)
|
|
334
|
+
- [OpenAPI 3.0.x](https://spec.openapis.org/oas/v3.0.4) → [@speclynx/apidom-ns-openapi-3-0](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-3-0)
|
|
335
|
+
- [OpenAPI 3.1.x](https://spec.openapis.org/oas/v3.1.2) → [@speclynx/apidom-ns-openapi-3-1](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-3-1)
|
|
336
|
+
|
|
337
|
+
```
|
|
338
|
+
ParseResultElement
|
|
339
|
+
├── .api: ArazzoSpecification1Element
|
|
340
|
+
│
|
|
341
|
+
├── ParseResultElement (petStoreApi) ─┐
|
|
342
|
+
│ └── .api: OpenApi3_1Element │ source
|
|
343
|
+
│ │ descriptions
|
|
344
|
+
└── ParseResultElement (legacyApi) ─┘
|
|
345
|
+
├── .errors
|
|
346
|
+
└── .warnings
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
```js
|
|
350
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
351
|
+
import { toValue } from '@speclynx/apidom-core';
|
|
352
|
+
|
|
353
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
354
|
+
parse: {
|
|
355
|
+
parserOpts: {
|
|
356
|
+
sourceDescriptions: true,
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// Main Arazzo document
|
|
362
|
+
const arazzoSpec = parseResult.api;
|
|
363
|
+
|
|
364
|
+
// Number of elements (1 main + N source descriptions)
|
|
365
|
+
console.log(parseResult.length);
|
|
366
|
+
|
|
367
|
+
// Access parsed source descriptions (filter by 'source-description' class)
|
|
368
|
+
for (let i = 0; i < parseResult.length; i++) {
|
|
369
|
+
const element = parseResult.get(i);
|
|
370
|
+
|
|
371
|
+
if (element.classes.includes('source-description')) {
|
|
372
|
+
// Source description metadata
|
|
373
|
+
const name = toValue(element.meta.get('name'));
|
|
374
|
+
const type = toValue(element.meta.get('type'));
|
|
375
|
+
|
|
376
|
+
console.log(`Source description "${name}" (${type})`);
|
|
377
|
+
|
|
378
|
+
// The parsed API document
|
|
379
|
+
const api = element.api;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### Accessing via SourceDescriptionElement
|
|
385
|
+
|
|
386
|
+
An alternative way to access parsed source descriptions is through the `SourceDescriptionElement` metadata.
|
|
387
|
+
When source descriptions are parsed, a `ParseResultElement` is attached to each `SourceDescriptionElement`'s metadata under the key `'parseResult'`.
|
|
388
|
+
|
|
389
|
+
```js
|
|
390
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
391
|
+
import { toValue } from '@speclynx/apidom-core';
|
|
392
|
+
|
|
393
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
394
|
+
parse: {
|
|
395
|
+
parserOpts: {
|
|
396
|
+
sourceDescriptions: true,
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
const arazzoSpec = parseResult.api;
|
|
402
|
+
|
|
403
|
+
// Access parsed document via SourceDescriptionElement
|
|
404
|
+
const sourceDesc = arazzoSpec.sourceDescriptions.get(0);
|
|
405
|
+
const sdParseResult = sourceDesc.meta.get('parseResult');
|
|
406
|
+
|
|
407
|
+
// Check for errors before using
|
|
408
|
+
if (sdParseResult.errors.length === 0) {
|
|
409
|
+
// Access the parsed API
|
|
410
|
+
const api = sdParseResult.api;
|
|
411
|
+
console.log(`API type: ${api.element}`); // e.g., 'openApi3_1'
|
|
412
|
+
|
|
413
|
+
// Get the retrieval URI
|
|
414
|
+
const retrievalURI = sdParseResult.meta.get('retrievalURI');
|
|
415
|
+
console.log(`Loaded from: ${retrievalURI}`);
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
This approach is useful when you need to:
|
|
420
|
+
- Access a specific source description by its position in the `sourceDescriptions` array
|
|
421
|
+
- Get the `retrievalURI` metadata indicating where the document was fetched from
|
|
422
|
+
- Correlate parsed documents with their source description definitions
|
|
423
|
+
|
|
424
|
+
### Recursive parsing
|
|
425
|
+
|
|
426
|
+
When a source description is of type `arazzo`, the parser recursively parses that document's source descriptions as well. This allows you to parse entire dependency trees of Arazzo documents.
|
|
427
|
+
|
|
428
|
+
### Limiting recursion depth
|
|
429
|
+
|
|
430
|
+
To prevent excessive recursion or handle deeply nested documents, use the `sourceDescriptionsMaxDepth` option:
|
|
431
|
+
|
|
432
|
+
```js
|
|
433
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
434
|
+
parse: {
|
|
435
|
+
parserOpts: {
|
|
436
|
+
sourceDescriptions: true,
|
|
437
|
+
sourceDescriptionsMaxDepth: 2, // Only parse 2 levels deep
|
|
438
|
+
},
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
The default value is `+Infinity` (no limit). Setting it to `0` will create error annotations instead of parsing any source descriptions.
|
|
444
|
+
|
|
445
|
+
### Cycle detection
|
|
446
|
+
|
|
447
|
+
The parser automatically detects circular references between Arazzo documents. When a cycle is detected, a warning annotation is added instead of causing infinite recursion:
|
|
448
|
+
|
|
449
|
+
```js
|
|
450
|
+
// arazzo-a.json references arazzo-b.json
|
|
451
|
+
// arazzo-b.json references arazzo-a.json (cycle!)
|
|
452
|
+
|
|
453
|
+
const parseResult = await parseArazzo('/path/to/arazzo-a.json', {
|
|
454
|
+
parse: {
|
|
455
|
+
parserOpts: {
|
|
456
|
+
sourceDescriptions: true,
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
// The cycle is handled gracefully - check for warning annotations
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
### Error and warning handling
|
|
465
|
+
|
|
466
|
+
When issues occur during source description parsing, the parser does not throw errors. Instead, it adds annotation elements to the source description's parse result:
|
|
467
|
+
|
|
468
|
+
- **`error`** class - Parsing failed (e.g., file not found, invalid document, max depth exceeded)
|
|
469
|
+
- **`warning`** class - Non-fatal issues (e.g., cycle detected, type mismatch between declared and actual)
|
|
470
|
+
|
|
471
|
+
This allows partial parsing to succeed even if some source descriptions have issues:
|
|
472
|
+
|
|
473
|
+
```js
|
|
474
|
+
const parseResult = await parseArazzo('/path/to/arazzo.json', {
|
|
475
|
+
parse: {
|
|
476
|
+
parserOpts: {
|
|
477
|
+
sourceDescriptions: true,
|
|
478
|
+
},
|
|
479
|
+
},
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
// Check each source description for errors and warnings
|
|
483
|
+
for (let i = 0; i < parseResult.length; i++) {
|
|
484
|
+
const element = parseResult.get(i);
|
|
485
|
+
|
|
486
|
+
if (element.classes.includes('source-description')) {
|
|
487
|
+
const name = toValue(element.meta.get('name'));
|
|
488
|
+
|
|
489
|
+
// Use built-in accessors for errors and warnings
|
|
490
|
+
element.errors.forEach((error) => {
|
|
491
|
+
console.error(`Error in "${name}": ${toValue(error)}`);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
element.warnings.forEach((warning) => {
|
|
495
|
+
console.warn(`Warning in "${name}": ${toValue(warning)}`);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
## Parsing OpenAPI Documents
|
|
502
|
+
|
|
503
|
+
The `parseOpenAPI` function provides complete control for parsing OpenAPI documents manually.
|
|
504
|
+
This is useful when you need to parse source descriptions independently or implement custom source description resolution logic.
|
|
505
|
+
|
|
506
|
+
The function accepts the same input types as `parseArazzo`:
|
|
507
|
+
|
|
508
|
+
1. **Plain JavaScript object** - converts to JSON and parses
|
|
509
|
+
2. **String content** - detects OpenAPI content and parses inline JSON or YAML
|
|
510
|
+
3. **File system path** - resolves and parses local OpenAPI Documents
|
|
511
|
+
4. **HTTP(S) URL** - fetches and parses remote OpenAPI Documents
|
|
512
|
+
|
|
513
|
+
Documents are parsed into their appropriate SpecLynx ApiDOM namespace data models:
|
|
514
|
+
|
|
515
|
+
- [OpenAPI 2.0 (Swagger)](https://spec.openapis.org/oas/v2.0) → [@speclynx/apidom-ns-openapi-2](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-2)
|
|
516
|
+
- [OpenAPI 3.0.x](https://spec.openapis.org/oas/v3.0.4) → [@speclynx/apidom-ns-openapi-3-0](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-3-0)
|
|
517
|
+
- [OpenAPI 3.1.x](https://spec.openapis.org/oas/v3.1.2) → [@speclynx/apidom-ns-openapi-3-1](https://github.com/speclynx/apidom/tree/main/packages/apidom-ns-openapi-3-1)
|
|
518
|
+
|
|
519
|
+
```js
|
|
520
|
+
import { parseOpenAPI } from '@jentic/arazzo-parser';
|
|
521
|
+
|
|
522
|
+
// From object
|
|
523
|
+
const parseResult = await parseOpenAPI({
|
|
524
|
+
openapi: '3.1.0',
|
|
525
|
+
info: { title: 'My API', version: '1.0.0' },
|
|
526
|
+
paths: {},
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
// From string
|
|
530
|
+
const parseResult = await parseOpenAPI('{"openapi": "3.1.0", ...}');
|
|
531
|
+
|
|
532
|
+
// From file
|
|
533
|
+
const parseResult = await parseOpenAPI('/path/to/openapi.json');
|
|
534
|
+
|
|
535
|
+
// From URL
|
|
536
|
+
const parseResult = await parseOpenAPI('https://example.com/openapi.yaml');
|
|
537
|
+
```
|
|
538
|
+
|
|
150
539
|
## SpecLynx ApiDOM tooling
|
|
151
540
|
|
|
152
541
|
Since `@jentic/arazzo-parser` produces a SpecLynx ApiDOM data model, you have access to the full suite of ApiDOM tools for manipulating, traversing, and transforming the parsed document.
|
|
@@ -156,10 +545,11 @@ Since `@jentic/arazzo-parser` produces a SpecLynx ApiDOM data model, you have ac
|
|
|
156
545
|
The [@speclynx/apidom-core](https://github.com/speclynx/apidom/tree/main/packages/apidom-core) package provides essential utilities for working with ApiDOM elements. Here are just a few examples:
|
|
157
546
|
|
|
158
547
|
```js
|
|
159
|
-
import {
|
|
160
|
-
import {
|
|
548
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
549
|
+
import { cloneDeep, cloneShallow } from '@speclynx/apidom-datamodel';
|
|
550
|
+
import { toValue, toJSON, toYAML, sexprs } from '@speclynx/apidom-core';
|
|
161
551
|
|
|
162
|
-
const parseResult = await
|
|
552
|
+
const parseResult = await parseArazzo(source);
|
|
163
553
|
const arazzoSpec = parseResult.api;
|
|
164
554
|
|
|
165
555
|
// Convert to plain JavaScript object
|
|
@@ -171,8 +561,9 @@ const json = toJSON(arazzoSpec);
|
|
|
171
561
|
// Serialize to YAML string
|
|
172
562
|
const yaml = toYAML(arazzoSpec);
|
|
173
563
|
|
|
174
|
-
//
|
|
175
|
-
const
|
|
564
|
+
// Clone the element
|
|
565
|
+
const clonedShallow = cloneShallow(arazzoSpec);
|
|
566
|
+
const clonedDeep = cloneDeep(arazzoSpec);
|
|
176
567
|
|
|
177
568
|
// Get S-expression representation (useful for debugging)
|
|
178
569
|
const sexpr = sexprs(arazzoSpec);
|
|
@@ -183,10 +574,10 @@ const sexpr = sexprs(arazzoSpec);
|
|
|
183
574
|
The [@speclynx/apidom-traverse](https://github.com/speclynx/apidom/tree/main/packages/apidom-traverse) package provides powerful traversal capabilities. Here is a basic example:
|
|
184
575
|
|
|
185
576
|
```js
|
|
186
|
-
import {
|
|
577
|
+
import { parseArazzo } from '@jentic/arazzo-parser';
|
|
187
578
|
import { traverse } from '@speclynx/apidom-traverse';
|
|
188
579
|
|
|
189
|
-
const parseResult = await
|
|
580
|
+
const parseResult = await parseArazzo(source);
|
|
190
581
|
|
|
191
582
|
// Traverse and collect steps using semantic visitor hook
|
|
192
583
|
const steps = [];
|