@jentic/arazzo-resolver 1.0.0-alpha.21 → 1.0.0-alpha.23

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jentic/arazzo-resolver",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "Resolver for Arazzo Documents.",
5
5
  "keywords": [
6
6
  "arazzo",
@@ -49,15 +49,18 @@
49
49
  "license": "Apache-2.0",
50
50
  "dependencies": {
51
51
  "@babel/runtime-corejs3": "^7.28.4",
52
- "@speclynx/apidom-core": "^2.12.4",
53
- "@speclynx/apidom-datamodel": "^2.12.4",
54
- "@speclynx/apidom-error": "^2.12.4",
55
- "@speclynx/apidom-ns-arazzo-1": "^2.12.4",
56
- "@speclynx/apidom-ns-openapi-2": "^2.12.4",
57
- "@speclynx/apidom-ns-openapi-3-0": "^2.12.4",
58
- "@speclynx/apidom-ns-openapi-3-1": "^2.12.4",
59
- "@speclynx/apidom-reference": "^2.12.4",
60
- "type-fest": "^5.4.3"
52
+ "@jentic/arazzo-parser": "1.0.0-alpha.23",
53
+ "@speclynx/apidom-datamodel": "^3.2.1",
54
+ "@speclynx/apidom-error": "^3.2.1",
55
+ "@speclynx/apidom-ns-arazzo-1": "^3.2.1",
56
+ "@speclynx/apidom-ns-openapi-2": "^3.2.1",
57
+ "@speclynx/apidom-ns-openapi-3-0": "^3.2.1",
58
+ "@speclynx/apidom-ns-openapi-3-1": "^3.2.1",
59
+ "@speclynx/apidom-reference": "^3.2.1",
60
+ "type-fest": "^5.4.4"
61
+ },
62
+ "devDependencies": {
63
+ "@speclynx/apidom-core": "^3.2.1"
61
64
  },
62
65
  "files": [
63
66
  "src/**/*.mjs",
@@ -69,5 +72,5 @@
69
72
  "README.md",
70
73
  "CHANGELOG.md"
71
74
  ],
72
- "gitHead": "1f72bd8cba7381dba030eafa719724a56354f290"
75
+ "gitHead": "d3bd28db525dc1c4cf46d2b9ccf83fecade420a1"
73
76
  }
@@ -1,265 +0,0 @@
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.dereference = dereference;
7
- exports.dereferenceElement = dereferenceElement;
8
- var _apidomDatamodel = require("@speclynx/apidom-datamodel");
9
- var _empty = require("@speclynx/apidom-reference/configuration/empty");
10
- var _arazzo = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/arazzo-1"));
11
- var _openapi = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-2"));
12
- var _openapi2 = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-3-0"));
13
- var _openapi3 = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-3-1"));
14
- var _arazzoJson = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/arazzo-json-1"));
15
- var _arazzoYaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/arazzo-yaml-1"));
16
- var _openapiJson = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-2"));
17
- var _openapiYaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-2"));
18
- var _openapiJson2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-0"));
19
- var _openapiYaml2 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0"));
20
- var _openapiJson3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-json-3-1"));
21
- var _openapiYaml3 = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1"));
22
- var _json = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/json"));
23
- var _yaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/yaml-1-2"));
24
- var _binary = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/binary"));
25
- var _file = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/file"));
26
- var _httpAxios = _interopRequireDefault(require("@speclynx/apidom-reference/resolve/resolvers/http-axios"));
27
- var _apidomNsArazzo = require("@speclynx/apidom-ns-arazzo-1");
28
- var _apidomCore = require("@speclynx/apidom-core");
29
- var _DereferenceError = _interopRequireDefault(require("../errors/DereferenceError.cjs"));
30
- /**
31
- * Options for dereferencing Arazzo Documents.
32
- * @public
33
- */
34
-
35
- /**
36
- * Default reference options for dereferencing Arazzo Documents.
37
- * @public
38
- */
39
- const defaultOptions = exports.defaultOptions = {
40
- resolve: {
41
- resolvers: [new _file.default({
42
- fileAllowList: ['*.json', '*.yaml', '*.yml']
43
- }), new _httpAxios.default({
44
- timeout: 5000,
45
- redirects: 5,
46
- withCredentials: false
47
- })]
48
- },
49
- parse: {
50
- parsers: [new _arazzoJson.default({
51
- allowEmpty: false
52
- }), new _arazzoYaml.default({
53
- allowEmpty: false
54
- }), new _openapiJson.default({
55
- allowEmpty: false
56
- }), new _openapiYaml.default({
57
- allowEmpty: false
58
- }), new _openapiJson2.default({
59
- allowEmpty: false
60
- }), new _openapiYaml2.default({
61
- allowEmpty: false
62
- }), new _openapiJson3.default({
63
- allowEmpty: false
64
- }), new _openapiYaml3.default({
65
- allowEmpty: false
66
- }), new _json.default({
67
- allowEmpty: false
68
- }), new _yaml.default({
69
- allowEmpty: false
70
- }), new _binary.default({
71
- allowEmpty: false
72
- })],
73
- parserOpts: {
74
- sourceMap: false,
75
- style: false,
76
- strict: true
77
- }
78
- },
79
- dereference: {
80
- strategies: [new _arazzo.default(), new _openapi.default(), new _openapi2.default(), new _openapi3.default()],
81
- strategyOpts: {
82
- sourceDescriptions: false
83
- }
84
- }
85
- };
86
-
87
- /**
88
- * Dereferences an Arazzo Document from a file system path or HTTP(S) URL.
89
- *
90
- * This function resolves all JSON References ($ref) and Reusable Object references
91
- * ($components.*) in the Arazzo Document.
92
- *
93
- * Source descriptions can optionally be dereferenced using strategy options:
94
- * - `sourceDescriptions`: `true` (all) or `['name1', 'name2']` (specific names only)
95
- * - `sourceDescriptionsMaxDepth`: Maximum recursion depth for nested Arazzo source descriptions (default: `+Infinity`)
96
- *
97
- * Options can be passed globally via `strategyOpts` or strategy-specific via `strategyOpts['arazzo-1']`.
98
- *
99
- * @param uri - A file system path or HTTP(S) URL to the Arazzo Document
100
- * @param options - Reference options (uses defaultOptions when not provided)
101
- * @returns A promise that resolves to the dereferenced Arazzo Document as ApiDOM element
102
- * @throws DereferenceError - When dereferencing fails or document is not an Arazzo specification. The original error is available via the `cause` property.
103
- *
104
- * @example
105
- * // Dereference from file
106
- * const result = await dereferenceArazzo('/path/to/arazzo.json');
107
- *
108
- * @example
109
- * // Dereference from URL
110
- * const result = await dereferenceArazzo('https://example.com/arazzo.yaml');
111
- *
112
- * @example
113
- * Dereference with source descriptions
114
- * ```typescript
115
- * const result = await dereferenceArazzo('/path/to/arazzo.json', {
116
- * dereference: { strategyOpts: { sourceDescriptions: true } },
117
- * });
118
- * ```
119
- *
120
- * @example
121
- * // Dereference with custom options
122
- * const result = await dereferenceArazzo('/path/to/arazzo.json', customReferenceOptions);
123
- * @public
124
- */
125
- async function dereference(uri, options = {}) {
126
- const mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
127
- try {
128
- const parseResult = await (0, _empty.dereference)(uri, mergedOptions);
129
-
130
- // validate that the dereferenced document is an Arazzo specification
131
- if (!(0, _apidomNsArazzo.isArazzoSpecification1Element)(parseResult.api)) {
132
- throw new _empty.UnmatchedDereferenceStrategyError(`Could not find a dereference strategy that can dereference "${uri}" as an Arazzo specification`);
133
- }
134
- parseResult.meta.set('retrievalURI', uri);
135
- return parseResult;
136
- } catch (error) {
137
- throw new _DereferenceError.default(`Failed to dereference Arazzo Document at "${uri}"`, {
138
- cause: error
139
- });
140
- }
141
- }
142
-
143
- /**
144
- * Dereferences an ApiDOM element representing an Arazzo Document.
145
- *
146
- * This function resolves all JSON References ($ref) and Reusable Object references
147
- * ($components.*) in the Arazzo Document element.
148
- *
149
- * Supported scenarios:
150
- * - ParseResultElement with retrievalURI metadata: baseURI derived automatically
151
- * - ParseResultElement without retrievalURI: requires `options.resolve.baseURI`
152
- * - Child element (e.g., WorkflowElement) with parseResult in strategyOpts:
153
- * requires `options.dereference.strategyOpts.parseResult` for component resolution,
154
- * and `options.resolve.baseURI` if parseResult lacks retrievalURI metadata
155
- *
156
- * Source descriptions can optionally be dereferenced using strategy options:
157
- * - `sourceDescriptions`: `true` (all) or `['name1', 'name2']` (specific names only)
158
- * - `sourceDescriptionsMaxDepth`: Maximum recursion depth for nested Arazzo source descriptions (default: `+Infinity`)
159
- *
160
- * Options can be passed globally via `strategyOpts` or strategy-specific via `strategyOpts['arazzo-1']`.
161
- *
162
- * @param element - An ApiDOM element (ParseResultElement or child element like WorkflowElement)
163
- * @param options - Reference options (uses defaultOptions when not provided)
164
- * @returns A promise that resolves to the dereferenced element
165
- * @throws DereferenceError - When baseURI is required but not provided, or when dereferencing fails
166
- *
167
- * @example
168
- * Dereference ParseResultElement with retrievalURI (from file parsing)
169
- * ```typescript
170
- * import { parseArazzo } from '@jentic/arazzo-parser';
171
- *
172
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
173
- * const dereferenced = await dereferenceArazzoElement(parseResult);
174
- * ```
175
- *
176
- * @example
177
- * Dereference ParseResultElement without retrievalURI (from inline parsing)
178
- * ```typescript
179
- * const parseResult = await parseArazzo({ arazzo: '1.0.1', ... });
180
- * const dereferenced = await dereferenceArazzoElement(parseResult, {
181
- * resolve: { baseURI: 'https://example.com/arazzo.json' },
182
- * });
183
- * ```
184
- *
185
- * @example
186
- * Dereference child element (e.g., WorkflowElement)
187
- * ```typescript
188
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
189
- * const workflow = parseResult.api.workflows.get(0);
190
- * const dereferenced = await dereferenceArazzoElement(workflow, {
191
- * dereference: { strategyOpts: { parseResult } },
192
- * });
193
- * ```
194
- *
195
- * @example
196
- * Dereference with source descriptions
197
- * ```typescript
198
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
199
- * const dereferenced = await dereferenceArazzoElement(parseResult, {
200
- * dereference: { strategyOpts: { sourceDescriptions: true } },
201
- * });
202
- * ```
203
- * @public
204
- */
205
- async function dereferenceElement(element, options = {}) {
206
- const mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
207
- const refSet = mergedOptions.dereference?.refSet ?? new _empty.ReferenceSet();
208
- let baseURI = mergedOptions.resolve?.baseURI;
209
- let mediaType = 'text/plain';
210
- if (refSet.size === 0) {
211
- if ((0, _apidomDatamodel.isParseResultElement)(element)) {
212
- if ((0, _apidomNsArazzo.isArazzoSpecification1Element)(element.api)) {
213
- mediaType = _apidomNsArazzo.mediaTypes.latest();
214
- }
215
- if (element.hasMetaProperty('retrievalURI')) {
216
- baseURI = (0, _apidomCore.toValue)(element.meta.get('retrievalURI'));
217
- } else if (!baseURI) {
218
- throw new _DereferenceError.default('baseURI option is required when dereferencing a ParseResultElement without retrievalURI metadata');
219
- }
220
- } else if ((0, _apidomDatamodel.isParseResultElement)(mergedOptions.dereference?.strategyOpts?.parseResult)) {
221
- // dereferencing child element requires refSet for component resolution
222
- const {
223
- parseResult
224
- } = mergedOptions.dereference.strategyOpts;
225
- let rootURI;
226
- if ((0, _apidomNsArazzo.isArazzoSpecification1Element)(parseResult.api)) {
227
- mediaType = _apidomNsArazzo.mediaTypes.latest();
228
- }
229
- if (parseResult.hasMetaProperty('retrievalURI')) {
230
- rootURI = (0, _apidomCore.toValue)(parseResult.meta.get('retrievalURI'));
231
- } else if (baseURI) {
232
- rootURI = baseURI;
233
- } else {
234
- throw new _DereferenceError.default('baseURI option is required when dereferencing a child element without retrievalURI metadata');
235
- }
236
- const elementReference = new _empty.Reference({
237
- uri: `${rootURI}#fragment`,
238
- value: new _apidomDatamodel.ParseResultElement([element])
239
- });
240
- const rootReference = new _empty.Reference({
241
- uri: rootURI,
242
- value: parseResult
243
- });
244
- refSet.add(elementReference).add(rootReference);
245
- baseURI = rootURI;
246
- }
247
- }
248
- try {
249
- return await (0, _empty.dereferenceApiDOM)(element, (0, _empty.mergeOptions)(mergedOptions, {
250
- resolve: {
251
- baseURI
252
- },
253
- parse: {
254
- mediaType
255
- },
256
- dereference: {
257
- refSet
258
- }
259
- }));
260
- } catch (error) {
261
- throw new _DereferenceError.default('Failed to dereference Arazzo Document', {
262
- cause: error
263
- });
264
- }
265
- }
@@ -1,257 +0,0 @@
1
- import { isParseResultElement, ParseResultElement } from '@speclynx/apidom-datamodel';
2
- import { dereference as dereferenceURI, dereferenceApiDOM as dereferenceApiDOMElement, mergeOptions, ReferenceSet, Reference, UnmatchedDereferenceStrategyError } from '@speclynx/apidom-reference/configuration/empty';
3
- import Arazzo1DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/arazzo-1';
4
- import OpenAPI2DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-2';
5
- import OpenAPI30DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-3-0';
6
- import OpenAPI31DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-3-1';
7
- import ArazzoJSON1Parser from '@speclynx/apidom-reference/parse/parsers/arazzo-json-1';
8
- import ArazzoYAML1Parser from '@speclynx/apidom-reference/parse/parsers/arazzo-yaml-1';
9
- import OpenAPIJSON20Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-2';
10
- import OpenAPIYAML20Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-2';
11
- import OpenAPIJSON30Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-0';
12
- import OpenAPIYAML30Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-0';
13
- import OpenAPIJSON31Parser from '@speclynx/apidom-reference/parse/parsers/openapi-json-3-1';
14
- import OpenAPIYAML31Parser from '@speclynx/apidom-reference/parse/parsers/openapi-yaml-3-1';
15
- import JSONParser from '@speclynx/apidom-reference/parse/parsers/json';
16
- import YAMLParser from '@speclynx/apidom-reference/parse/parsers/yaml-1-2';
17
- import BinaryParser from '@speclynx/apidom-reference/parse/parsers/binary';
18
- import FileResolver from '@speclynx/apidom-reference/resolve/resolvers/file';
19
- import HTTPResolverAxios from '@speclynx/apidom-reference/resolve/resolvers/http-axios';
20
- import { isArazzoSpecification1Element, mediaTypes } from '@speclynx/apidom-ns-arazzo-1';
21
- import { toValue } from '@speclynx/apidom-core';
22
- import DereferenceError from "../errors/DereferenceError.mjs";
23
- /**
24
- * Options for dereferencing Arazzo Documents.
25
- * @public
26
- */
27
- /**
28
- * Default reference options for dereferencing Arazzo Documents.
29
- * @public
30
- */
31
- export const defaultOptions = {
32
- resolve: {
33
- resolvers: [new FileResolver({
34
- fileAllowList: ['*.json', '*.yaml', '*.yml']
35
- }), new HTTPResolverAxios({
36
- timeout: 5000,
37
- redirects: 5,
38
- withCredentials: false
39
- })]
40
- },
41
- parse: {
42
- parsers: [new ArazzoJSON1Parser({
43
- allowEmpty: false
44
- }), new ArazzoYAML1Parser({
45
- allowEmpty: false
46
- }), new OpenAPIJSON20Parser({
47
- allowEmpty: false
48
- }), new OpenAPIYAML20Parser({
49
- allowEmpty: false
50
- }), new OpenAPIJSON30Parser({
51
- allowEmpty: false
52
- }), new OpenAPIYAML30Parser({
53
- allowEmpty: false
54
- }), new OpenAPIJSON31Parser({
55
- allowEmpty: false
56
- }), new OpenAPIYAML31Parser({
57
- allowEmpty: false
58
- }), new JSONParser({
59
- allowEmpty: false
60
- }), new YAMLParser({
61
- allowEmpty: false
62
- }), new BinaryParser({
63
- allowEmpty: false
64
- })],
65
- parserOpts: {
66
- sourceMap: false,
67
- style: false,
68
- strict: true
69
- }
70
- },
71
- dereference: {
72
- strategies: [new Arazzo1DereferenceStrategy(), new OpenAPI2DereferenceStrategy(), new OpenAPI30DereferenceStrategy(), new OpenAPI31DereferenceStrategy()],
73
- strategyOpts: {
74
- sourceDescriptions: false
75
- }
76
- }
77
- };
78
-
79
- /**
80
- * Dereferences an Arazzo Document from a file system path or HTTP(S) URL.
81
- *
82
- * This function resolves all JSON References ($ref) and Reusable Object references
83
- * ($components.*) in the Arazzo Document.
84
- *
85
- * Source descriptions can optionally be dereferenced using strategy options:
86
- * - `sourceDescriptions`: `true` (all) or `['name1', 'name2']` (specific names only)
87
- * - `sourceDescriptionsMaxDepth`: Maximum recursion depth for nested Arazzo source descriptions (default: `+Infinity`)
88
- *
89
- * Options can be passed globally via `strategyOpts` or strategy-specific via `strategyOpts['arazzo-1']`.
90
- *
91
- * @param uri - A file system path or HTTP(S) URL to the Arazzo Document
92
- * @param options - Reference options (uses defaultOptions when not provided)
93
- * @returns A promise that resolves to the dereferenced Arazzo Document as ApiDOM element
94
- * @throws DereferenceError - When dereferencing fails or document is not an Arazzo specification. The original error is available via the `cause` property.
95
- *
96
- * @example
97
- * // Dereference from file
98
- * const result = await dereferenceArazzo('/path/to/arazzo.json');
99
- *
100
- * @example
101
- * // Dereference from URL
102
- * const result = await dereferenceArazzo('https://example.com/arazzo.yaml');
103
- *
104
- * @example
105
- * Dereference with source descriptions
106
- * ```typescript
107
- * const result = await dereferenceArazzo('/path/to/arazzo.json', {
108
- * dereference: { strategyOpts: { sourceDescriptions: true } },
109
- * });
110
- * ```
111
- *
112
- * @example
113
- * // Dereference with custom options
114
- * const result = await dereferenceArazzo('/path/to/arazzo.json', customReferenceOptions);
115
- * @public
116
- */
117
- export async function dereference(uri, options = {}) {
118
- const mergedOptions = mergeOptions(defaultOptions, options);
119
- try {
120
- const parseResult = await dereferenceURI(uri, mergedOptions);
121
-
122
- // validate that the dereferenced document is an Arazzo specification
123
- if (!isArazzoSpecification1Element(parseResult.api)) {
124
- throw new UnmatchedDereferenceStrategyError(`Could not find a dereference strategy that can dereference "${uri}" as an Arazzo specification`);
125
- }
126
- parseResult.meta.set('retrievalURI', uri);
127
- return parseResult;
128
- } catch (error) {
129
- throw new DereferenceError(`Failed to dereference Arazzo Document at "${uri}"`, {
130
- cause: error
131
- });
132
- }
133
- }
134
-
135
- /**
136
- * Dereferences an ApiDOM element representing an Arazzo Document.
137
- *
138
- * This function resolves all JSON References ($ref) and Reusable Object references
139
- * ($components.*) in the Arazzo Document element.
140
- *
141
- * Supported scenarios:
142
- * - ParseResultElement with retrievalURI metadata: baseURI derived automatically
143
- * - ParseResultElement without retrievalURI: requires `options.resolve.baseURI`
144
- * - Child element (e.g., WorkflowElement) with parseResult in strategyOpts:
145
- * requires `options.dereference.strategyOpts.parseResult` for component resolution,
146
- * and `options.resolve.baseURI` if parseResult lacks retrievalURI metadata
147
- *
148
- * Source descriptions can optionally be dereferenced using strategy options:
149
- * - `sourceDescriptions`: `true` (all) or `['name1', 'name2']` (specific names only)
150
- * - `sourceDescriptionsMaxDepth`: Maximum recursion depth for nested Arazzo source descriptions (default: `+Infinity`)
151
- *
152
- * Options can be passed globally via `strategyOpts` or strategy-specific via `strategyOpts['arazzo-1']`.
153
- *
154
- * @param element - An ApiDOM element (ParseResultElement or child element like WorkflowElement)
155
- * @param options - Reference options (uses defaultOptions when not provided)
156
- * @returns A promise that resolves to the dereferenced element
157
- * @throws DereferenceError - When baseURI is required but not provided, or when dereferencing fails
158
- *
159
- * @example
160
- * Dereference ParseResultElement with retrievalURI (from file parsing)
161
- * ```typescript
162
- * import { parseArazzo } from '@jentic/arazzo-parser';
163
- *
164
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
165
- * const dereferenced = await dereferenceArazzoElement(parseResult);
166
- * ```
167
- *
168
- * @example
169
- * Dereference ParseResultElement without retrievalURI (from inline parsing)
170
- * ```typescript
171
- * const parseResult = await parseArazzo({ arazzo: '1.0.1', ... });
172
- * const dereferenced = await dereferenceArazzoElement(parseResult, {
173
- * resolve: { baseURI: 'https://example.com/arazzo.json' },
174
- * });
175
- * ```
176
- *
177
- * @example
178
- * Dereference child element (e.g., WorkflowElement)
179
- * ```typescript
180
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
181
- * const workflow = parseResult.api.workflows.get(0);
182
- * const dereferenced = await dereferenceArazzoElement(workflow, {
183
- * dereference: { strategyOpts: { parseResult } },
184
- * });
185
- * ```
186
- *
187
- * @example
188
- * Dereference with source descriptions
189
- * ```typescript
190
- * const parseResult = await parseArazzo('/path/to/arazzo.json');
191
- * const dereferenced = await dereferenceArazzoElement(parseResult, {
192
- * dereference: { strategyOpts: { sourceDescriptions: true } },
193
- * });
194
- * ```
195
- * @public
196
- */
197
- export async function dereferenceElement(element, options = {}) {
198
- const mergedOptions = mergeOptions(defaultOptions, options);
199
- const refSet = mergedOptions.dereference?.refSet ?? new ReferenceSet();
200
- let baseURI = mergedOptions.resolve?.baseURI;
201
- let mediaType = 'text/plain';
202
- if (refSet.size === 0) {
203
- if (isParseResultElement(element)) {
204
- if (isArazzoSpecification1Element(element.api)) {
205
- mediaType = mediaTypes.latest();
206
- }
207
- if (element.hasMetaProperty('retrievalURI')) {
208
- baseURI = toValue(element.meta.get('retrievalURI'));
209
- } else if (!baseURI) {
210
- throw new DereferenceError('baseURI option is required when dereferencing a ParseResultElement without retrievalURI metadata');
211
- }
212
- } else if (isParseResultElement(mergedOptions.dereference?.strategyOpts?.parseResult)) {
213
- // dereferencing child element requires refSet for component resolution
214
- const {
215
- parseResult
216
- } = mergedOptions.dereference.strategyOpts;
217
- let rootURI;
218
- if (isArazzoSpecification1Element(parseResult.api)) {
219
- mediaType = mediaTypes.latest();
220
- }
221
- if (parseResult.hasMetaProperty('retrievalURI')) {
222
- rootURI = toValue(parseResult.meta.get('retrievalURI'));
223
- } else if (baseURI) {
224
- rootURI = baseURI;
225
- } else {
226
- throw new DereferenceError('baseURI option is required when dereferencing a child element without retrievalURI metadata');
227
- }
228
- const elementReference = new Reference({
229
- uri: `${rootURI}#fragment`,
230
- value: new ParseResultElement([element])
231
- });
232
- const rootReference = new Reference({
233
- uri: rootURI,
234
- value: parseResult
235
- });
236
- refSet.add(elementReference).add(rootReference);
237
- baseURI = rootURI;
238
- }
239
- }
240
- try {
241
- return await dereferenceApiDOMElement(element, mergeOptions(mergedOptions, {
242
- resolve: {
243
- baseURI
244
- },
245
- parse: {
246
- mediaType
247
- },
248
- dereference: {
249
- refSet
250
- }
251
- }));
252
- } catch (error) {
253
- throw new DereferenceError('Failed to dereference Arazzo Document', {
254
- cause: error
255
- });
256
- }
257
- }