@jentic/arazzo-resolver 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.
@@ -0,0 +1,224 @@
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 _openapi = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-2"));
11
+ var _openapi2 = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-3-0"));
12
+ var _openapi3 = _interopRequireDefault(require("@speclynx/apidom-reference/dereference/strategies/openapi-3-1"));
13
+ var _json = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/json"));
14
+ var _yaml = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/yaml-1-2"));
15
+ var _binary = _interopRequireDefault(require("@speclynx/apidom-reference/parse/parsers/binary"));
16
+ var _apidomNsOpenapi = require("@speclynx/apidom-ns-openapi-2");
17
+ var _apidomNsOpenapi2 = require("@speclynx/apidom-ns-openapi-3-0");
18
+ var _apidomNsOpenapi3 = require("@speclynx/apidom-ns-openapi-3-1");
19
+ var _arazzoParser = require("@jentic/arazzo-parser");
20
+ var _DereferenceError = _interopRequireDefault(require("../errors/DereferenceError.cjs"));
21
+ /**
22
+ * Options for dereferencing OpenAPI Documents.
23
+ * @public
24
+ */
25
+
26
+ /**
27
+ * Default reference options for dereferencing OpenAPI Documents.
28
+ * @public
29
+ */
30
+ const defaultOptions = exports.defaultOptions = {
31
+ resolve: {
32
+ resolvers: [..._arazzoParser.defaultParseOpenAPIOptions.resolve.resolvers]
33
+ },
34
+ parse: {
35
+ parsers: [..._arazzoParser.defaultParseOpenAPIOptions.parse.parsers, new _json.default({
36
+ allowEmpty: false
37
+ }), new _yaml.default({
38
+ allowEmpty: false
39
+ }), new _binary.default({
40
+ allowEmpty: false
41
+ })],
42
+ parserOpts: {
43
+ ..._arazzoParser.defaultParseOpenAPIOptions.parse.parserOpts
44
+ }
45
+ },
46
+ dereference: {
47
+ strategies: [new _openapi.default(), new _openapi2.default(), new _openapi3.default()],
48
+ strategyOpts: {}
49
+ }
50
+ };
51
+
52
+ /**
53
+ * Dereferences an OpenAPI Document from a file system path or HTTP(S) URL.
54
+ *
55
+ * This function resolves all JSON References ($ref) in the OpenAPI Document.
56
+ *
57
+ * Supports OpenAPI 2.0 (Swagger), OpenAPI 3.0.x, and OpenAPI 3.1.x.
58
+ *
59
+ * @param uri - A file system path or HTTP(S) URL to the OpenAPI Document
60
+ * @param options - Reference options (uses defaultOptions when not provided)
61
+ * @returns A promise that resolves to the dereferenced OpenAPI Document as ApiDOM element
62
+ * @throws DereferenceError - When dereferencing fails or document is not an OpenAPI specification. The original error is available via the `cause` property.
63
+ *
64
+ * @example
65
+ * // Dereference from file
66
+ * const result = await dereferenceOpenAPI('/path/to/openapi.json');
67
+ *
68
+ * @example
69
+ * // Dereference from URL
70
+ * const result = await dereferenceOpenAPI('https://example.com/openapi.yaml');
71
+ *
72
+ * @example
73
+ * // Dereference with custom options
74
+ * const result = await dereferenceOpenAPI('/path/to/openapi.json', customReferenceOptions);
75
+ * @public
76
+ */
77
+ async function dereference(uri, options = {}) {
78
+ const mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
79
+ try {
80
+ const parseResult = await (0, _empty.dereference)(uri, mergedOptions);
81
+
82
+ // validate that the dereferenced document is an OpenAPI specification
83
+ if (!isOpenApiElement(parseResult.api)) {
84
+ throw new _empty.UnmatchedDereferenceStrategyError(`Could not find a dereference strategy that can dereference "${uri}" as an OpenAPI specification`);
85
+ }
86
+ parseResult.meta.set('retrievalURI', uri);
87
+ return parseResult;
88
+ } catch (error) {
89
+ throw new _DereferenceError.default(`Failed to dereference OpenAPI Document at "${uri}"`, {
90
+ cause: error
91
+ });
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Dereferences an ApiDOM element representing an OpenAPI Document.
97
+ *
98
+ * This function resolves all JSON References ($ref) in the OpenAPI Document element.
99
+ *
100
+ * Supported scenarios:
101
+ * - ParseResultElement with retrievalURI metadata: baseURI derived automatically
102
+ * - ParseResultElement without retrievalURI: requires `options.resolve.baseURI`
103
+ * - Child element (e.g., PathItemElement) with parseResult in strategyOpts:
104
+ * requires `options.dereference.strategyOpts.parseResult` for component resolution,
105
+ * and `options.resolve.baseURI` if parseResult lacks retrievalURI metadata
106
+ *
107
+ * @param element - An ApiDOM element (ParseResultElement or child element like PathItemElement)
108
+ * @param options - Reference options (uses defaultOptions when not provided)
109
+ * @returns A promise that resolves to the dereferenced element
110
+ * @throws DereferenceError - When baseURI is required but not provided, or when dereferencing fails
111
+ *
112
+ * @example
113
+ * Dereference ParseResultElement with retrievalURI (from file parsing)
114
+ * ```typescript
115
+ * import { parseOpenAPI } from '@jentic/arazzo-parser';
116
+ *
117
+ * const parseResult = await parseOpenAPI('/path/to/openapi.json');
118
+ * const dereferenced = await dereferenceOpenAPIElement(parseResult);
119
+ * ```
120
+ *
121
+ * @example
122
+ * Dereference ParseResultElement without retrievalURI (from inline parsing)
123
+ * ```typescript
124
+ * const parseResult = await parseOpenAPI({ openapi: '3.1.0', ... });
125
+ * const dereferenced = await dereferenceOpenAPIElement(parseResult, {
126
+ * resolve: { baseURI: 'https://example.com/openapi.json' },
127
+ * });
128
+ * ```
129
+ *
130
+ * @example
131
+ * Dereference child element (e.g., PathItemElement)
132
+ * ```typescript
133
+ * const parseResult = await parseOpenAPI('/path/to/openapi.json');
134
+ * const pathItem = parseResult.api.paths.get('/users');
135
+ * const dereferenced = await dereferenceOpenAPIElement(pathItem, {
136
+ * dereference: { strategyOpts: { parseResult } },
137
+ * });
138
+ * ```
139
+ * @public
140
+ */
141
+ async function dereferenceElement(element, options = {}) {
142
+ const mergedOptions = (0, _empty.mergeOptions)(defaultOptions, options);
143
+ const refSet = mergedOptions.dereference?.refSet ?? new _empty.ReferenceSet();
144
+ let baseURI = mergedOptions.resolve?.baseURI;
145
+ let mediaType = 'text/plain';
146
+ if (refSet.size === 0) {
147
+ if ((0, _apidomDatamodel.isParseResultElement)(element)) {
148
+ if (isOpenApiElement(element.api)) {
149
+ mediaType = inferOpenApiMediaType(element.api);
150
+ }
151
+ if (element.hasMetaProperty('retrievalURI')) {
152
+ baseURI = element.meta.get('retrievalURI');
153
+ } else if (!baseURI) {
154
+ throw new _DereferenceError.default('baseURI option is required when dereferencing a ParseResultElement without retrievalURI metadata');
155
+ }
156
+ } else if ((0, _apidomDatamodel.isParseResultElement)(mergedOptions.dereference?.strategyOpts?.parseResult)) {
157
+ // dereferencing child element requires refSet for component resolution
158
+ const {
159
+ parseResult
160
+ } = mergedOptions.dereference.strategyOpts;
161
+ let rootURI;
162
+ if (isOpenApiElement(parseResult.api)) {
163
+ mediaType = inferOpenApiMediaType(parseResult.api);
164
+ }
165
+ if (parseResult.hasMetaProperty('retrievalURI')) {
166
+ rootURI = parseResult.meta.get('retrievalURI');
167
+ } else if (baseURI) {
168
+ rootURI = baseURI;
169
+ } else {
170
+ throw new _DereferenceError.default('baseURI option is required when dereferencing a child element without retrievalURI metadata');
171
+ }
172
+ const elementReference = new _empty.Reference({
173
+ uri: `${rootURI}#fragment`,
174
+ value: new _apidomDatamodel.ParseResultElement([element])
175
+ });
176
+ const rootReference = new _empty.Reference({
177
+ uri: rootURI,
178
+ value: parseResult
179
+ });
180
+ refSet.add(elementReference).add(rootReference);
181
+ baseURI = rootURI;
182
+ }
183
+ }
184
+ try {
185
+ return await (0, _empty.dereferenceApiDOM)(element, (0, _empty.mergeOptions)(mergedOptions, {
186
+ resolve: {
187
+ baseURI
188
+ },
189
+ parse: {
190
+ mediaType
191
+ },
192
+ dereference: {
193
+ refSet
194
+ }
195
+ }));
196
+ } catch (error) {
197
+ throw new _DereferenceError.default('Failed to dereference OpenAPI Document', {
198
+ cause: error
199
+ });
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Checks if the element is a valid OpenAPI specification element.
205
+ */
206
+ function isOpenApiElement(element) {
207
+ return (0, _apidomNsOpenapi.isSwaggerElement)(element) || (0, _apidomNsOpenapi2.isOpenApi3_0Element)(element) || (0, _apidomNsOpenapi3.isOpenApi3_1Element)(element);
208
+ }
209
+
210
+ /**
211
+ * Gets the appropriate mediaType for an OpenAPI element.
212
+ */
213
+ function inferOpenApiMediaType(element) {
214
+ if ((0, _apidomNsOpenapi.isSwaggerElement)(element)) {
215
+ return _apidomNsOpenapi.mediaTypes.latest();
216
+ }
217
+ if ((0, _apidomNsOpenapi2.isOpenApi3_0Element)(element)) {
218
+ return _apidomNsOpenapi2.mediaTypes.latest();
219
+ }
220
+ if ((0, _apidomNsOpenapi3.isOpenApi3_1Element)(element)) {
221
+ return _apidomNsOpenapi3.mediaTypes.latest();
222
+ }
223
+ return 'text/plain';
224
+ }
@@ -0,0 +1,216 @@
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 OpenAPI2DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-2';
4
+ import OpenAPI3_0DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-3-0';
5
+ import OpenAPI3_1DereferenceStrategy from '@speclynx/apidom-reference/dereference/strategies/openapi-3-1';
6
+ import JSONParser from '@speclynx/apidom-reference/parse/parsers/json';
7
+ import YAMLParser from '@speclynx/apidom-reference/parse/parsers/yaml-1-2';
8
+ import BinaryParser from '@speclynx/apidom-reference/parse/parsers/binary';
9
+ import { isSwaggerElement, mediaTypes as openApi2MediaTypes } from '@speclynx/apidom-ns-openapi-2';
10
+ import { isOpenApi3_0Element, mediaTypes as openApi3_0MediaTypes } from '@speclynx/apidom-ns-openapi-3-0';
11
+ import { isOpenApi3_1Element, mediaTypes as openApi3_1MediaTypes } from '@speclynx/apidom-ns-openapi-3-1';
12
+ import { defaultParseOpenAPIOptions as parserDefaultOptions } from '@jentic/arazzo-parser';
13
+ import DereferenceError from "../errors/DereferenceError.mjs";
14
+ /**
15
+ * Options for dereferencing OpenAPI Documents.
16
+ * @public
17
+ */
18
+ /**
19
+ * Default reference options for dereferencing OpenAPI Documents.
20
+ * @public
21
+ */
22
+ export const defaultOptions = {
23
+ resolve: {
24
+ resolvers: [...parserDefaultOptions.resolve.resolvers]
25
+ },
26
+ parse: {
27
+ parsers: [...parserDefaultOptions.parse.parsers, new JSONParser({
28
+ allowEmpty: false
29
+ }), new YAMLParser({
30
+ allowEmpty: false
31
+ }), new BinaryParser({
32
+ allowEmpty: false
33
+ })],
34
+ parserOpts: {
35
+ ...parserDefaultOptions.parse.parserOpts
36
+ }
37
+ },
38
+ dereference: {
39
+ strategies: [new OpenAPI2DereferenceStrategy(), new OpenAPI3_0DereferenceStrategy(), new OpenAPI3_1DereferenceStrategy()],
40
+ strategyOpts: {}
41
+ }
42
+ };
43
+
44
+ /**
45
+ * Dereferences an OpenAPI Document from a file system path or HTTP(S) URL.
46
+ *
47
+ * This function resolves all JSON References ($ref) in the OpenAPI Document.
48
+ *
49
+ * Supports OpenAPI 2.0 (Swagger), OpenAPI 3.0.x, and OpenAPI 3.1.x.
50
+ *
51
+ * @param uri - A file system path or HTTP(S) URL to the OpenAPI Document
52
+ * @param options - Reference options (uses defaultOptions when not provided)
53
+ * @returns A promise that resolves to the dereferenced OpenAPI Document as ApiDOM element
54
+ * @throws DereferenceError - When dereferencing fails or document is not an OpenAPI specification. The original error is available via the `cause` property.
55
+ *
56
+ * @example
57
+ * // Dereference from file
58
+ * const result = await dereferenceOpenAPI('/path/to/openapi.json');
59
+ *
60
+ * @example
61
+ * // Dereference from URL
62
+ * const result = await dereferenceOpenAPI('https://example.com/openapi.yaml');
63
+ *
64
+ * @example
65
+ * // Dereference with custom options
66
+ * const result = await dereferenceOpenAPI('/path/to/openapi.json', customReferenceOptions);
67
+ * @public
68
+ */
69
+ export async function dereference(uri, options = {}) {
70
+ const mergedOptions = mergeOptions(defaultOptions, options);
71
+ try {
72
+ const parseResult = await dereferenceURI(uri, mergedOptions);
73
+
74
+ // validate that the dereferenced document is an OpenAPI specification
75
+ if (!isOpenApiElement(parseResult.api)) {
76
+ throw new UnmatchedDereferenceStrategyError(`Could not find a dereference strategy that can dereference "${uri}" as an OpenAPI specification`);
77
+ }
78
+ parseResult.meta.set('retrievalURI', uri);
79
+ return parseResult;
80
+ } catch (error) {
81
+ throw new DereferenceError(`Failed to dereference OpenAPI Document at "${uri}"`, {
82
+ cause: error
83
+ });
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Dereferences an ApiDOM element representing an OpenAPI Document.
89
+ *
90
+ * This function resolves all JSON References ($ref) in the OpenAPI Document element.
91
+ *
92
+ * Supported scenarios:
93
+ * - ParseResultElement with retrievalURI metadata: baseURI derived automatically
94
+ * - ParseResultElement without retrievalURI: requires `options.resolve.baseURI`
95
+ * - Child element (e.g., PathItemElement) with parseResult in strategyOpts:
96
+ * requires `options.dereference.strategyOpts.parseResult` for component resolution,
97
+ * and `options.resolve.baseURI` if parseResult lacks retrievalURI metadata
98
+ *
99
+ * @param element - An ApiDOM element (ParseResultElement or child element like PathItemElement)
100
+ * @param options - Reference options (uses defaultOptions when not provided)
101
+ * @returns A promise that resolves to the dereferenced element
102
+ * @throws DereferenceError - When baseURI is required but not provided, or when dereferencing fails
103
+ *
104
+ * @example
105
+ * Dereference ParseResultElement with retrievalURI (from file parsing)
106
+ * ```typescript
107
+ * import { parseOpenAPI } from '@jentic/arazzo-parser';
108
+ *
109
+ * const parseResult = await parseOpenAPI('/path/to/openapi.json');
110
+ * const dereferenced = await dereferenceOpenAPIElement(parseResult);
111
+ * ```
112
+ *
113
+ * @example
114
+ * Dereference ParseResultElement without retrievalURI (from inline parsing)
115
+ * ```typescript
116
+ * const parseResult = await parseOpenAPI({ openapi: '3.1.0', ... });
117
+ * const dereferenced = await dereferenceOpenAPIElement(parseResult, {
118
+ * resolve: { baseURI: 'https://example.com/openapi.json' },
119
+ * });
120
+ * ```
121
+ *
122
+ * @example
123
+ * Dereference child element (e.g., PathItemElement)
124
+ * ```typescript
125
+ * const parseResult = await parseOpenAPI('/path/to/openapi.json');
126
+ * const pathItem = parseResult.api.paths.get('/users');
127
+ * const dereferenced = await dereferenceOpenAPIElement(pathItem, {
128
+ * dereference: { strategyOpts: { parseResult } },
129
+ * });
130
+ * ```
131
+ * @public
132
+ */
133
+ export async function dereferenceElement(element, options = {}) {
134
+ const mergedOptions = mergeOptions(defaultOptions, options);
135
+ const refSet = mergedOptions.dereference?.refSet ?? new ReferenceSet();
136
+ let baseURI = mergedOptions.resolve?.baseURI;
137
+ let mediaType = 'text/plain';
138
+ if (refSet.size === 0) {
139
+ if (isParseResultElement(element)) {
140
+ if (isOpenApiElement(element.api)) {
141
+ mediaType = inferOpenApiMediaType(element.api);
142
+ }
143
+ if (element.hasMetaProperty('retrievalURI')) {
144
+ baseURI = element.meta.get('retrievalURI');
145
+ } else if (!baseURI) {
146
+ throw new DereferenceError('baseURI option is required when dereferencing a ParseResultElement without retrievalURI metadata');
147
+ }
148
+ } else if (isParseResultElement(mergedOptions.dereference?.strategyOpts?.parseResult)) {
149
+ // dereferencing child element requires refSet for component resolution
150
+ const {
151
+ parseResult
152
+ } = mergedOptions.dereference.strategyOpts;
153
+ let rootURI;
154
+ if (isOpenApiElement(parseResult.api)) {
155
+ mediaType = inferOpenApiMediaType(parseResult.api);
156
+ }
157
+ if (parseResult.hasMetaProperty('retrievalURI')) {
158
+ rootURI = parseResult.meta.get('retrievalURI');
159
+ } else if (baseURI) {
160
+ rootURI = baseURI;
161
+ } else {
162
+ throw new DereferenceError('baseURI option is required when dereferencing a child element without retrievalURI metadata');
163
+ }
164
+ const elementReference = new Reference({
165
+ uri: `${rootURI}#fragment`,
166
+ value: new ParseResultElement([element])
167
+ });
168
+ const rootReference = new Reference({
169
+ uri: rootURI,
170
+ value: parseResult
171
+ });
172
+ refSet.add(elementReference).add(rootReference);
173
+ baseURI = rootURI;
174
+ }
175
+ }
176
+ try {
177
+ return await dereferenceApiDOMElement(element, mergeOptions(mergedOptions, {
178
+ resolve: {
179
+ baseURI
180
+ },
181
+ parse: {
182
+ mediaType
183
+ },
184
+ dereference: {
185
+ refSet
186
+ }
187
+ }));
188
+ } catch (error) {
189
+ throw new DereferenceError('Failed to dereference OpenAPI Document', {
190
+ cause: error
191
+ });
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Checks if the element is a valid OpenAPI specification element.
197
+ */
198
+ function isOpenApiElement(element) {
199
+ return isSwaggerElement(element) || isOpenApi3_0Element(element) || isOpenApi3_1Element(element);
200
+ }
201
+
202
+ /**
203
+ * Gets the appropriate mediaType for an OpenAPI element.
204
+ */
205
+ function inferOpenApiMediaType(element) {
206
+ if (isSwaggerElement(element)) {
207
+ return openApi2MediaTypes.latest();
208
+ }
209
+ if (isOpenApi3_0Element(element)) {
210
+ return openApi3_0MediaTypes.latest();
211
+ }
212
+ if (isOpenApi3_1Element(element)) {
213
+ return openApi3_1MediaTypes.latest();
214
+ }
215
+ return 'text/plain';
216
+ }
package/src/index.cjs CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
4
  exports.__esModule = true;
5
- exports.dereferenceElement = exports.dereferenceArazzoElement = exports.dereferenceArazzo = exports.dereference = exports.defaultDereferenceOptions = exports.defaultDereferenceArazzoOptions = exports.DereferenceError = void 0;
6
- var _dereference = require("./dereference.cjs");
7
- exports.defaultDereferenceOptions = _dereference.defaultOptions;
8
- exports.defaultDereferenceArazzoOptions = _dereference.defaultOptions;
9
- exports.dereference = _dereference.dereference;
10
- exports.dereferenceElement = _dereference.dereferenceElement;
11
- exports.dereferenceArazzo = _dereference.dereference;
12
- exports.dereferenceArazzoElement = _dereference.dereferenceElement;
5
+ exports.dereferenceOpenAPIElement = exports.dereferenceOpenAPI = exports.dereferenceArazzoElement = exports.dereferenceArazzo = exports.defaultDereferenceOpenAPIOptions = exports.defaultDereferenceArazzoOptions = exports.DereferenceError = void 0;
6
+ var _arazzo = require("./dereference/arazzo.cjs");
7
+ exports.defaultDereferenceArazzoOptions = _arazzo.defaultOptions;
8
+ exports.dereferenceArazzo = _arazzo.dereference;
9
+ exports.dereferenceArazzoElement = _arazzo.dereferenceElement;
10
+ var _openapi = require("./dereference/openapi.cjs");
11
+ exports.defaultDereferenceOpenAPIOptions = _openapi.defaultOptions;
12
+ exports.dereferenceOpenAPI = _openapi.dereference;
13
+ exports.dereferenceOpenAPIElement = _openapi.dereferenceElement;
13
14
  var _DereferenceError = _interopRequireDefault(require("./errors/DereferenceError.cjs"));
14
15
  exports.DereferenceError = _DereferenceError.default;
package/src/index.mjs CHANGED
@@ -1,2 +1,3 @@
1
- export { defaultOptions as defaultDereferenceOptions, defaultOptions as defaultDereferenceArazzoOptions, dereference, dereferenceElement, dereference as dereferenceArazzo, dereferenceElement as dereferenceArazzoElement } from "./dereference.mjs";
1
+ export { defaultOptions as defaultDereferenceArazzoOptions, dereference as dereferenceArazzo, dereferenceElement as dereferenceArazzoElement } from "./dereference/arazzo.mjs";
2
+ export { defaultOptions as defaultDereferenceOpenAPIOptions, dereference as dereferenceOpenAPI, dereferenceElement as dereferenceOpenAPIElement } from "./dereference/openapi.mjs";
2
3
  export { default as DereferenceError } from "./errors/DereferenceError.mjs";