source: node_modules/@swagger-api/apidom-reference/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 67.6 KB
Line 
1# @swagger-api/apidom-reference
2
3`@swagger-api/apidom-reference` package contains advanced algorithms for semantic ApiDOM manipulations.
4This package is divided into three (3) main components:
5
6- **[Parse component](#parse-component)**
7- **[Resolve component](#resolve-component)**
8- **[Dereference component](#dereference-component)**
9- **[Bundle component](#bundle-component)**
10
11## Installation
12
13After [prerequisites](https://github.com/swagger-api/apidom/blob/main/README.md#prerequisites) for installing this package are satisfied, you can install it
14via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
15
16```sh
17 $ npm install @swagger-api/apidom-reference
18```
19
20## Configurations
21
22This package has two main exports suitable for different use-cases. **Empty** configuration and **saturated** configuration.
23
24### Empty configuration
25
26```js
27import { parse } from '@swagger-api/apidom-reference/configuration/empty';
28import OpenApiJson3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
29
30await parse('/home/user/oas.json', {
31 parse: {
32 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
33 parsers: [OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false })]
34 }
35});
36```
37
38When using this approach, `options` object is not configured with parsers, resolvers or strategies.
39This is suitable for creating **web bundles** and gives you total control of the contents of your bundles.
40
41### Saturated configuration
42
43```js
44import { parse } from '@swagger-api/apidom-reference';
45```
46or
47```js
48import { parse } from '@swagger-api/apidom-reference/configuration/saturaged';
49```
50
51Both of above imports are equivalent. This approach is suitable for **Node.js** environments.
52`options` object is pre-configured with all the parsers, resolvers and strategies.
53
54## Parse component
55
56Parse component consists of implementation of default [parser plugins](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers).
57Defaults parser plugin is a specialized wrapper that wraps one of the ApiDOM parser adapter into specialized API.
58Standard ApiDOM parser adapter can only parse strings. Parser plugins are capable of parsing local filesystem URIs and network URLs.
59
60**Parsing a file localed on local filesystem:**
61
62```js
63import { parse } from '@swagger-api/apidom-reference';
64
65await parse('/home/user/oas.json', {
66 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
67});
68```
69
70**Parsing an HTTP(S) URL located on internet:**
71
72```js
73import { parse } from '@swagger-api/apidom-reference';
74
75await parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
76 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
77})
78```
79
80Notice how we explicitly pass a `mediaType` parse option. This option is actually **not required**,
81but if not provided, the Parse component will try to identify appropriate parser plugin by file contents, and it's extension (`.json`).
82
83What actually happens if you don't provide `mediaType` parse option?
84
85```js
86import { parse } from '@swagger-api/apidom-reference';
87
88await parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json');
89```
90
91The result of this operation is going to be generic ApiDOM structure. By analyzing the name of the file
92we can identify the extension of the file as `.json`. At this point we only know
93that this file is probably going to contain JSON string, though we have no idea what data (AsyncApi/OpenApi)
94is encoded within that JSON string.
95
96In the future, we will introduce smart algorithms for looking in the contents of a file and detecting the
97`mediaType` automatically. Of course not explicitly providing `mediaType` has performance implications (running detection)
98so providing it is always a better option.
99
100### Parser plugins
101
102Parse component comes with number of default parser plugins.
103
104#### [openapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-json-2)
105
106Wraps [@swagger-api/apidom-parser-adapter-openapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-json-2) package
107and is uniquely identified by `openapi-json-2` name.
108
109Supported media types are:
110
111```js
112[
113 'application/vnd.oai.openapi;version=2.0',
114 'application/vnd.oai.openapi+json;version=2.0',
115]
116```
117
118#### [openapi-json-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-json-3-0)
119
120Wraps [@swagger-api/apidom-parser-adapter-openapi-json-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-json-3-0) package
121and is uniquely identified by `openapi-json-3-1` name.
122
123Supported media types are:
124
125```js
126[
127 'application/vnd.oai.openapi;version=3.0.0',
128 'application/vnd.oai.openapi+json;version=3.0.0',
129 'application/vnd.oai.openapi;version=3.0.1',
130 'application/vnd.oai.openapi+json;version=3.0.1',
131 'application/vnd.oai.openapi;version=3.0.2',
132 'application/vnd.oai.openapi+json;version=3.0.2',
133 'application/vnd.oai.openapi;version=3.0.3',
134 'application/vnd.oai.openapi+json;version=3.0.3',
135]
136```
137
138#### [openapi-yaml-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-yaml-2)
139
140Wraps [@swagger-api/apidom-parser-adapter-openapi-yaml-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-yaml-2) package
141and is uniquely identified by `openapi-yaml-2` name.
142
143Supported media types are:
144
145```js
146[
147 'application/vnd.oai.openapi;version=2.0',
148 'application/vnd.oai.openapi+yaml;version=2.0',
149]
150```
151
152#### [openapi-yaml-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-yaml-3-0)
153
154Wraps [@swagger-api/apidom-parser-adapter-openapi-yaml-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-yaml-3-0) package
155and is uniquely identified by `openapi-yaml-3-1` name.
156
157Supported media types are:
158
159```js
160[
161 'application/vnd.oai.openapi;version=3.0.0',
162 'application/vnd.oai.openapi+yaml;version=3.0.0',
163 'application/vnd.oai.openapi;version=3.0.1',
164 'application/vnd.oai.openapi+yaml;version=3.0.1',
165 'application/vnd.oai.openapi;version=3.0.2',
166 'application/vnd.oai.openapi+yaml;version=3.0.2',
167 'application/vnd.oai.openapi;version=3.0.3',
168 'application/vnd.oai.openapi+yaml;version=3.0.3',
169]
170```
171
172#### [openapi-json-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-json-3-1)
173
174Wraps [@swagger-api/apidom-parser-adapter-openapi-json-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-json-3-1) package
175and is uniquely identified by `openapi-json-3-1` name.
176
177Supported media types are:
178
179```js
180[
181 'application/vnd.oai.openapi;version=3.1.0',
182 'application/vnd.oai.openapi+json;version=3.1.0',
183]
184```
185
186#### [openapi-yaml-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-yaml-3-1)
187
188Wraps [@swagger-api/apidom-parser-adapter-openapi-yaml-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-yaml-3-1) package
189and is uniquely identified by `openapi-yaml-3-1` name.
190
191Supported media types are:
192
193```js
194[
195 'application/vnd.oai.openapi;version=3.1.0',
196 'application/vnd.oai.openapi+yaml;version=3.1.0',
197]
198```
199
200#### [asyncapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/asyncapi-json-2)
201
202Wraps [@swagger-api/apidom-parser-adapter-asyncapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-asyncapi-json-2) package
203and is uniquely identified by `asyncapi-json-2` name.
204
205Supported media types are:
206
207```js
208[
209 'application/vnd.aai.asyncapi;version=2.0.0',
210 'application/vnd.aai.asyncapi;version=2.1.0',
211 'application/vnd.aai.asyncapi;version=2.2.0',
212 'application/vnd.aai.asyncapi;version=2.3.0',
213 'application/vnd.aai.asyncapi;version=2.4.0',
214 'application/vnd.aai.asyncapi;version=2.5.0',
215 'application/vnd.aai.asyncapi;version=2.6.0',
216 'application/vnd.aai.asyncapi+json;version=2.0.0',
217 'application/vnd.aai.asyncapi+json;version=2.1.0',
218 'application/vnd.aai.asyncapi+json;version=2.2.0',
219 'application/vnd.aai.asyncapi+json;version=2.3.0',
220 'application/vnd.aai.asyncapi+json;version=2.4.0',
221 'application/vnd.aai.asyncapi+json;version=2.5.0',
222 'application/vnd.aai.asyncapi+json;version=2.6.0',
223]
224```
225
226#### [asyncapi-yaml-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/asyncapi-yaml-2)
227
228Wraps [@swagger-api/apidom-parser-adapter-asyncapi-yaml-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-asyncapi-yaml-2) package
229and is uniquely identified by `asyncapi-yaml-2` name.
230
231
232Supported media types are:
233
234```js
235[
236 'application/vnd.aai.asyncapi;version=2.0.0',
237 'application/vnd.aai.asyncapi;version=2.1.0',
238 'application/vnd.aai.asyncapi;version=2.2.0',
239 'application/vnd.aai.asyncapi;version=2.3.0',
240 'application/vnd.aai.asyncapi;version=2.3.0',
241 'application/vnd.aai.asyncapi;version=2.4.0',
242 'application/vnd.aai.asyncapi;version=2.5.0',
243 'application/vnd.aai.asyncapi;version=2.6.0',
244 'application/vnd.aai.asyncapi+yaml;version=2.0.0',
245 'application/vnd.aai.asyncapi+yaml;version=2.1.0',
246 'application/vnd.aai.asyncapi+yaml;version=2.2.0',
247 'application/vnd.aai.asyncapi+yaml;version=2.3.0',
248 'application/vnd.aai.asyncapi+yaml;version=2.4.0',
249 'application/vnd.aai.asyncapi+yaml;version=2.5.0',
250 'application/vnd.aai.asyncapi+yaml;version=2.6.0',
251]
252```
253
254#### [workflows-json-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/workflows-json-1)
255
256Wraps [@swagger-api/apidom-parser-adapter-workflows-json-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-workflows-json-1) package
257and is uniquely identified by `workflows-json-1` name.
258
259Supported media types are:
260
261```js
262[
263 'application/vnd.oai.workflows;version=1.0.0',
264 'application/vnd.oai.workflows+json;version=1.0.0',
265]
266```
267
268#### [workflows-yaml-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/workflows-yaml-1)
269
270Wraps [@swagger-api/apidom-parser-adapter-workflows-yaml-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-workflows-yaml-1) package
271and is uniquely identified by `workflows-yaml-1` name.
272
273Supported media types are:
274
275```js
276[
277 'application/vnd.oai.workflows;version=1.0.0',
278 'application/vnd.oai.workflows+yaml;version=1.0.0',
279]
280```
281
282#### [api-design-systems-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/api-design-systems-json)
283
284Wraps [@swagger-api/apidom-parser-adapter-api-design-systsems-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-api-design-systems-json) package
285and is uniquely identified by `api-design-systems-json` name.
286
287Supported media types are:
288
289```js
290[
291 'application/vnd.aai.apidesignsystems;version=2021-05-07',
292 'application/vnd.aai.apidesignsystems+json;version=2021-05-07'
293]
294```
295
296#### [api-design-systems-yaml](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/api-design-systems-yaml)
297
298Wraps [@swagger-api/apidom-parser-adapter-api-design-systems-yaml](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-api-design-systems-yaml) package
299and is uniquely identified by `api-design-systems-yaml` name.
300
301
302Supported media types are:
303
304```js
305[
306 'application/vnd.aai.apidesignsystems;version=2021-05-07',
307 'application/vnd.aai.apidesignsystems+yaml;version=2021-05-07'
308]
309```
310
311#### [json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/json)
312
313Wraps [@swagger-api/apidom-parser-adapter-json](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-json) package
314and is uniquely identified by `json` name.
315
316
317Supported media types are:
318
319```js
320[
321 'application/json'
322]
323```
324
325#### [yaml-1-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/yaml-1-2)
326
327Wraps [@swagger-api/apidom-parser-adapter-yaml-1-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-yaml-1-2) package
328and is uniquely identified by `yaml-1-2` name.
329
330
331Supported media types are:
332
333```js
334[
335 'text/yaml',
336 'application/yaml'
337]
338```
339
340#### [binary](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/binary)
341
342Can parse any binary or non-binary file and return it's content as `base64` encoded string.
343This parser is uniquely identified by `binary` name.
344
345
346**All** media types are supported.
347
348#### Parser plugins execution order
349
350It's important to understand that default parser plugins are run in specific order. The order is determined
351by the [options.parse.parsers](https://github.com/swagger-api/apidom/blob/ba888d711a4292e8ed0b72e343c4902a4bf0d45a/packages/apidom-reference/src/configuration/saturated.ts#L22) option.
352Every plugin is pulled from `options.parse.parsers` option, and it's `canParse` method is called to determine
353whether the plugin can parse the URI. If `canParse` returns `true`, `parse` method of plugin is called
354and result from parsing is returned. No subsequent parser plugins are run. If `canParse` returns
355`false`, next parser plugin is pulled and this process is repeated until one of the parser plugins `canParse` method
356returns `true` or until entire list of parser plugins is exhausted (throws error).
357
358```js
359[
360 OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
361 OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
362 OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
363 OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
364 OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
365 OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
366 OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
367 AsyncApiJson2Parser({ allowEmpty: true, sourceMap: false }),
368 AsyncApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
369 WorkflowsJson1Parser({ allowEmpty: true, sourceMap: false }),
370 WorkflowsYaml1Parser({ allowEmpty: true, sourceMap: false }),
371 ApiDesignSystemsJsonParser({ allowEmpty: true, sourceMap: false }),
372 ApiDesignSystemsYamlParser({ allowEmpty: true, sourceMap: false }),
373 JsonParser({ allowEmpty: true, sourceMap: false }),
374 YamlParser({ allowEmpty: true, sourceMap: false }),
375 BinaryParser({ allowEmpty: true }),
376]
377```
378Most specific parser plugins are listed first, most generic are listed last.
379
380It's possible to **change** the parser plugins **order globally** by mutating global `parse` options:
381
382```js
383import { options } from '@swagger-api/apidom-reference';
384import OpenApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-2';
385import OpenApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-2';
386import OpenApiJson3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-0';
387import OpenApiYaml3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-0'
388import OpenApiJson3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
389import OpenApiYaml3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-1'
390import AsyncApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-2';
391import AsyncApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
392import WorkflowsJson1Parser from '@swagger-api/apidom-reference/parse/parsers/workflows-json-1';
393import WorkflowsYaml1Parser from '@swagger-api/apidom-reference/parse/parsers/workflows-yaml-1';
394import ApiDesignSystemsJsonParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
395import ApiDesignSystemsYamlParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
396import JsonParser from '@swagger-api/apidom-reference/parse/parsers/json';
397import YamlParser from '@swagger-api/apidom-reference/parse/parsers/yaml';
398import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
399
400
401options.parse.parsers = [
402 OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
403 OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
404 OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
405 OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
406 OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
407 OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
408 AsyncApiJson2Parser({ allowEmpty: true, sourceMap: false }),
409 AsyncApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
410 WorkflowsJson1Parser({ allowEmpty: true, sourceMap: false }),
411 WorkflowsYaml1Parser({ allowEmpty: true, sourceMap: false }),
412 ApiDesignSystemsJsonParser({ allowEmpty: true, sourceMap: false }),
413 ApiDesignSystemsYamlParser({ allowEmpty: true, sourceMap: false }),
414 YamlParser({ allowEmpty: true, sourceMap: false }),
415 JsonParser({ allowEmpty: true, sourceMap: false }),
416 BinaryParser({ allowEmpty: true }),
417]
418```
419
420To **change** the parser plugins **order** on ad-hoc basis:
421
422```js
423import { parse } from '@swagger-api/apidom-reference';
424import OpenApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-2';
425import OpenApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-2';
426import OpenApiJson3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-0';
427import OpenApiYaml3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-0'
428import OpenApiJson3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
429import OpenApiYaml3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-1'
430import AsyncApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-2';
431import AsyncApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
432import WorkflowsJson1Parser from '@swagger-api/apidom-reference/parse/parsers/workflows-json-1';
433import WorkflowsYaml1Parser from '@swagger-api/apidom-reference/parse/parsers/workflows-yaml-1';
434import ApiDesignSystemsJsonParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
435import ApiDesignSystemsYamlParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
436import JsonParser from '@swagger-api/apidom-reference/parse/parsers/json';
437import YamlParser from '@swagger-api/apidom-reference/parse/parsers/yaml';
438import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
439
440await parse('/home/user/oas.json', {
441 parse: {
442 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
443 parsers: [
444 OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
445 OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
446 OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
447 OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
448 OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
449 OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
450 AsyncApiJson2Parser({ allowEmpty: true, sourceMap: false }),
451 AsyncApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
452 WorkflowsJson1Parser({ allowEmpty: true, sourceMap: false }),
453 WorkflowsYaml1Parser({ allowEmpty: true, sourceMap: false }),
454 ApiDesignSystemsJsonParser({ allowEmpty: true, sourceMap: false }),
455 ApiDesignSystemsYamlParser({ allowEmpty: true, sourceMap: false }),
456 YamlParser({ allowEmpty: true, sourceMap: false }),
457 JsonParser({ allowEmpty: true, sourceMap: false }),
458 BinaryParser({ allowEmpty: true }),
459 ],
460 },
461});
462```
463
464#### Parser plugin options
465
466Parser plugins accept additional options like `allowEmpty` or `sourceMap`. It's possible to **change** parser plugin
467**options globally** by mutating global `parse` options:
468
469```js
470import { options, parse } from '@swagger-api/apidom-reference';
471
472options.parser.parserOpts = {
473 allowEmpty: false,
474 sourceMap: true,
475};
476
477await parse('/home/user/oas.json', {
478 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
479});
480```
481
482To **change** the parser plugins **options** on ad-hoc basis:
483
484```js
485import { parse } from '@swagger-api/apidom-reference';
486
487await parse('/home/user/oas.json', {
488 parse: {
489 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
490 parserOpts: { allowEmpty: false, sourceMap: true },
491 },
492});
493```
494
495### Creating new parser plugin
496
497Parse component can be extended by additional parser plugins. Every parser plugin is an object that
498must conform to the following interface/shape:
499
500```typescript
501{
502 // uniquely identifies this parser plugin
503 name: string,
504
505 // this method is called to determine whether the parser plugin can parse the file
506 async canParse(file: IFile): Promise<boolean> {
507 // ...implementation...
508 },
509
510 // this method actually parses the file
511 async parse(file: IFile): Promise<ParseResultElement> {
512 // ...implementation...
513 }
514}
515```
516
517New parser plugin is then provided as an option to a `parse` function:
518
519```js
520import { parse, options } from '@swagger-api/apidom-reference';
521
522const myCustomParserPlugin = {
523 name: 'myCustomParserPlugin',
524 async canParse(file) {
525 return true;
526 },
527 async parse(file) {
528 // implementation of parsing
529 }
530};
531
532await parse('/home/user/oas.json', {
533 parse: {
534 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
535 parsers: [...options.parse.parsers, myCustomParserPlugin],
536 }
537});
538```
539
540In this particular example we're adding our custom parser plugin as the last plugin
541to the available default parser plugin list, so there's a good chance that one of the
542default parser plugins detects that it can parse the `/home/user/oas.json` file,
543parses it and returns.
544
545If you want to force execution of your custom plugin, add it as a first parser plugin:
546
547```js
548import { parse, options } from '@swagger-api/apidom-reference';
549
550const myCustomParserPlugin = {
551 name: 'myCustomParserPlugin',
552 async canParse(file) {
553 return true;
554 },
555 async parse(file) {
556 // implementation of parsing
557 }
558};
559
560await parse('/home/user/oas.json', {
561 parse: {
562 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
563 parsers: [myCustomParserPlugin, ...options.parse.parsers],
564 }
565});
566```
567
568To override the default parser plugins entirely, set `myCustomParserPlugin` plugin to be the only one available:
569
570```js
571import { parse } from '@swagger-api/apidom-reference';
572
573const myCustomParserPlugin = {
574 name: 'myCustomParserPlugin',
575 async canParse(file) {
576 return true;
577 },
578 async parse(file) {
579 // implementation of parsing
580 }
581};
582
583await parse('/home/user/oas.json', {
584 parse: {
585 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
586 parsers: [myCustomParserPlugin],
587 }
588});
589```
590
591### Manipulating parser plugins
592
593Parser plugins can be added, removed, replaced or reordered.
594
595Here are two examples of removing one of the parser plugins called `asyncapi-json-2`.
596We're using the fact that every parser plugin is uniquely identifiable by its name.
597
598**Removing** parser plugin **globally** for all subsequence `parse` calls is achieved by mutating global options:
599
600```js
601import { parse, options, mergeOptions } from '@swagger-api/apidom-reference';
602
603options.parse.parsers = options.parse.parsers.filter(parserPlugin => parserPlugin !== 'asyncapi-json-2')
604
605// here you can be sure `asyncapi-json-2` plugin was disabled
606await parse('/home/user/oas.json', {
607 parse: {
608 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
609 }
610});
611```
612
613**Removing** default parser plugin on **ad-hoc** basis:
614
615```js
616import { parse, options } from '@swagger-api/apidom-reference';
617
618await parse('/home/user/oas.json', {
619 parse: {
620 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
621 parsers: options.parse.parsers.filter(parserPlugin => parserPlugin.name !== 'asyncapi-json-2'),
622 }
623});
624```
625As you can see, these are all primitive JavaScript Array manipulation techniques.
626These techniques can be applied to replacing (use [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)) or reordering parser plugins as well.
627
628
629## Resolve component
630
631`Resolve component` consists of two (2) sub-components: **File resolution** and **External Resolution**.
632`Resolve component` is used by [Parse component](#parse-component) under the hood. `Resolve component` provides a resolved
633file contents for a Parse component to parse.
634
635### File resolution
636
637Contains implementation of default [resolver plugins](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/resolvers).
638Defaults resolver plugin is an object which knows how to obtain contents of a file represented by URI or URL.
639
640#### Resolver plugins
641
642File resolution comes with two (2) default resolver plugins.
643
644##### [FileResolver](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/resolvers/file)
645
646This resolver plugin is responsible for resolving a local file.
647It detects if the provided URI represents a filesystem path and if so,
648reads the file and provides its content.
649
650**WARNING**: use this plugin with caution, as it can read files from a local file system.
651By default, this plugin will reject to read any files from the local file system, unless
652explicitly provided by **fileAllowList** option.
653
654###### Providing file allow list
655
656File allow list can be provided **globally** as an option to `FileResolver` in form
657of array of *glob patterns* or *regular expressions*.
658
659```js
660import { options } from '@swagger-api/apidom-reference';
661import { FileResolver } from '@swagger-api/apidom-reference/resolve/resolvers/file';
662import { HttpResolverAxios } from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
663
664options.resolve.resolvers = [
665 FileResolver({
666 fileAllowList: [
667 '*.json',
668 /\.json$/,
669 ]
670 }),
671 HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
672]
673```
674
675File allow list can also be provided on ad-hoc basis:
676
677```js
678import { resolve } from '@swagger-api/apidom-reference';
679
680await resolve('/home/user/oas.json', {
681 resolve: {
682 resolverOpts: {
683 fileAllowList: [
684 '*.json',
685 /\.json$/,
686 ]
687 },
688 },
689});
690```
691
692##### [HttpResolverAxios](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/resolvers/http-axios)
693
694This resolver plugin is responsible for resolving a remote file represented by HTTP(s) URL.
695It detects if the provided URI represents an HTTP(s) URL and if so,
696fetches the file and provides its content.
697
698###### [Axios Request Config](https://axios-http.com/docs/req_config) support
699
700HttpResolverAxios plugin supports all the options available in [Axios Request Config](https://axios-http.com/docs/req_config).
701Config options can be provided in following way:
702
703```js
704import { resolve } from '@swagger-api/apidom-reference';
705
706await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
707 resolve: {
708 resolverOpts: {
709 axiosConfig: {
710 timeout: 10000,
711 withCredentials: false,
712 responseType: 'json',
713 },
714 },
715 },
716});
717```
718
719###### [Axios Interceptors](https://axios-http.com/docs/interceptors) support
720
721HttpResolverAxios plugin supports [Axios Interceptors](https://axios-http.com/docs/interceptors).
722Interceptors can be provided in following way:
723
724```js
725import { resolve } from '@swagger-api/apidom-reference';
726
727const requestInterceptor = (config) => config;
728const responseInterceptor = (response) => response;
729
730await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
731 resolve: {
732 resolverOpts: {
733 axiosConfig: {
734 interceptors: {
735 request: requestInterceptor,
736 response: responseInterceptor,
737 },
738 },
739 },
740 },
741});
742```
743
744Multiple request and response interceptors can be provided in following way:
745
746```js
747import { resolve } from '@swagger-api/apidom-reference';
748
749const requestInterceptor1 = (config) => config;
750const requestInterceptor2 = (config) => config;
751const responseInterceptor1 = (response) => response;
752const responseInterceptor2 = async (error) => Promise.reject(error);
753
754await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
755 resolve: {
756 resolverOpts: {
757 axiosConfig: {
758 interceptors: {
759 request: [requestInterceptor1, requestInterceptor2],
760 response: [responseInterceptor1, responseInterceptor2],
761 },
762 },
763 },
764 },
765});
766```
767
768**File resolution on local filesystem path**:
769
770```js
771import { readFile } from '@swagger-api/apidom-reference';
772
773await readFile('/home/user/oas.json'); // Promise<Buffer>
774```
775
776**File resolution on HTTP(s) URL:**
777
778```js
779import { readFile } from '@swagger-api/apidom-reference';
780
781await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json'); // Promise<Buffer>
782```
783File resolution always returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) containing a [Buffer](https://nodejs.org/api/buffer.html).
784It is responsibility of the API consumer to transform `Buffer` into `String` or any other type.
785
786```js
787import { readFile } from '@swagger-api/apidom-reference';
788
789const buffer = await readFile('/home/user/oas.json');
790const string = buffer.toString('utf-8');
791```
792
793##### Resolver plugins execution order
794
795It's important to understand that default resolver plugins are run in specific order. The order is determined
796by the [options.resolve.resolvers]https://github.com/swagger-api/apidom/blob/ba888d711a4292e8ed0b72e343c4902a4bf0d45a/packages/apidom-reference/src/configuration/saturated.ts#L36) option.
797Every plugin is pulled from `options.resolve.resolvers` option, and it's `canRead` method is called to determine
798whether the plugin can resolve the URI. If `canRead` returns `true`, `read` method of plugin is called
799and result from reading the file is returned. No subsequent resolver plugins are run.
800If `canRead` returns `false`, next resolver plugin is pulled and this process is repeated until one
801of the resolver plugins `canRead` method returns `true` or until entire list of resolver plugins is exhausted (throws error).
802
803```js
804[
805 FileResolver(),
806 HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
807]
808```
809
810It's possible to **change** resolver plugins **order globally** by mutating global `resolve` option:
811
812```js
813import { options } from '@swagger-api/apidom-reference';
814import { FileResolver } from '@swagger-api/apidom-reference/resolve/resolvers/file';
815import { HttpResolverAxios } from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
816
817options.resolve.resolvers = [
818 HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
819 FileResolver(),
820]
821```
822
823To **change** resolver plugins **order** on ad-hoc basis:
824
825```js
826import { readFile } from '@swagger-api/apidom-reference';
827import { FileResolver } from '@swagger-api/apidom-reference/resolve/resolvers/file';
828import { HttpResolverAxios } from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
829
830await readFile('/home/user/oas.json', {
831 resolve: {
832 resolvers: [
833 HttpResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
834 FileResolver(),
835 ],
836 },
837});
838```
839
840##### Resolver plugin options
841
842Some resolver plugins accept additional options. It's possible to **change** resolver plugin
843**options globally** by mutating global `resolve` options:
844
845```js
846import { options, readFile } from '@swagger-api/apidom-reference';
847
848options.resolve.resolverOpts = {
849 axiosConfig: {
850 timeout: 10000,
851 },
852};
853
854await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json');
855```
856
857To **change** the resolver plugins **options** on ad-hoc basis:
858
859```js
860import { readFile } from '@swagger-api/apidom-reference';
861
862await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
863 resolve: {
864 resolverOpts: {
865 axiosConfig: {
866 timeout: 10000,
867 },
868 },
869 },
870});
871```
872
873Both of above examples will be using [HttpResolverAxios](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/resolvers/http-axios) plugin
874(as we're trying to resolve HTTP(s) URL) and the `timeout` of resolution will increase from **default 3 seconds**
875to 10 seconds.
876
877##### Creating new resolver plugin
878
879Resolve component can be extended by additional resolver plugins. Every resolver plugin is an object that
880must conform to the following interface/shape:
881
882```typescript
883{
884 // uniquely identifies this plugin
885 name: string,
886
887 // this method is called to determine whether the resolver plugin can resolve the file
888 canRead(file: IFile): boolean {
889 // ...implementation...
890 },
891
892 // this method actually resolves the file
893 async read(file: IFile): Promise<Buffer> {
894 // ...implementation...
895 }
896}
897```
898
899New resolver plugin is then provided as an option to a `readFile` function:
900
901```js
902import { readFile, options } from '@swagger-api/apidom-reference';
903
904const myCustomResolverPlugin = {
905 name: 'myCustomResolverPlugin',
906 canRead(file) {
907 return true;
908 },
909 async read(file) {
910 // implementation of file resolution
911 }
912};
913
914await readFile('/home/user/oas.json', {
915 resolve: {
916 resolvers: [...options.resolve.resolvers, myCustomResolverPlugin],
917 }
918});
919```
920
921In this particular example we're adding our custom resolver plugin as the last plugin
922to the available default resolver plugin list, so there's a good chance that one of the
923default resolver plugins detects that it can resolve the `/home/user/oas.json` file,
924resolves it and returns its content.
925
926If you want to force execution of your custom plugin, add it as a first resolver plugin:
927
928```js
929import { readFile, options } from '@swagger-api/apidom-reference';
930
931const myCustomResolverPlugin = {
932 name: 'myCustomResolverPlugin',
933 canRead(file) {
934 return true;
935 },
936 async read(file) {
937 // implementation of file resolution
938 }
939};
940
941await readFile('/home/user/oas.json', {
942 resolve: {
943 resolvers: [myCustomResolverPlugin, ...options.resolve.resolvers],
944 }
945});
946```
947
948To override the default resolver plugins entirely, set `myCustomResolverPlugin` plugin to be the only one available:
949
950```js
951import { readFile } from '@swagger-api/apidom-reference';
952
953const myCustomResolverPlugin = {
954 name: 'myCustomResolverPlugin',
955 canRead(file) {
956 return true;
957 },
958 async read(file) {
959 // implementation of file resolution
960 }
961};
962
963await readFile('/home/user/oas.json', {
964 resolve: {
965 resolvers: [myCustomResolverPlugin],
966 }
967});
968```
969New resolver plugins can be based on two predefined stamps: [Resolver](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/resolvers/Resolver.ts) and [HttpResolver](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/resolvers/HttpResolver.ts).
970
971##### Manipulating resolver plugins
972
973Resolver plugins can be added, removed, replaced or reordered. We've already covered these techniques in [Manipulating parser plugins section](#manipulating-parser-plugins).
974
975### External resolution
976
977External resolution is a process of resolving all external dependencies of a particular
978document using a specific [external resolution strategy](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies). External resolution strategy is determined by
979asserting on `mediaType` option. [File Resolution](#file-resolution) (file content is read/fetched)
980and [Parse component](#parse-component) (file content is parsed) are used under the hood.
981
982**Externally resolving a file localed on a local filesystem:**
983
984```js
985import { resolve } from '@swagger-api/apidom-reference';
986
987await resolve('/home/user/oas.json', {
988 parse: { mediType: 'application/vnd.oai.openapi+json;version=3.1.0' },
989}); // Promise<ReferenceSet>
990```
991
992**Externally resolving an HTTP(S) URL located on an internet:**
993
994```js
995import { resolve } from '@swagger-api/apidom-reference';
996
997await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
998 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
999 resolve: {
1000 resolverOpts: {
1001 axiosConfig: {
1002 timeout: 10
1003 },
1004 },
1005 },
1006}); // Promise<ReferenceSet>
1007```
1008
1009**Externally resolving an ApiDOM fragment:**
1010
1011When externally resolving an ApiDOM fragment, [baseURI](https://github.com/swagger-api/apidom/blob/91763fa4ad876375a413e7049c28c2031c7bbe83/apidom/packages/apidom-reference/src/options/index.ts#L47)
1012resolve option needs to be provided to have a starting point for external dependency resolution.
1013`mediaType` parse option is unnecessary as we can directly assert the type of ApiDOM fragment.
1014
1015```js
1016import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
1017import { resolveApiDOM } from '@swagger-api/apidom-reference';
1018
1019const apidom = OpenApi3_1Element.refract({
1020 openapi: '3.1.0',
1021 components: {
1022 parameters: {
1023 externalRef: {
1024 $ref: './ex.json#/externalParameter', // file is located at /home/user/ex.json
1025 }
1026 }
1027 }
1028});
1029
1030const refSet = await resolveApiDOM(apidom, {
1031 resolve: { baseURI: '/home/user/' },
1032});
1033
1034for (const ref of refSet) {
1035 console.log(ref.uri);
1036}
1037// /home/user
1038// /home/user/ex.json
1039```
1040
1041[ReferenceSet](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/ReferenceSet.ts) is a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
1042like structure containing list of [Reference](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/Reference.ts) objects.
1043Every Reference object represents single external dependency.
1044
1045#### [External resolution strategies](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies)
1046
1047External resolution strategy determines how a document is externally resolved. Depending on document `mediaType`
1048every strategy differs significantly. Resolve component comes with two (2) default external resolution strategies.
1049
1050##### [asyncapi-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies/asyncapi-2)
1051
1052External resolution strategy for understanding and resolving external dependencies of [AsyncApi 2.x.y](https://github.com/asyncapi/spec/blob/master/spec/asyncapi.md) definitions.
1053
1054Supported media types:
1055
1056```js
1057[
1058 'application/vnd.aai.asyncapi;version=2.0.0',
1059 'application/vnd.aai.asyncapi+json;version=2.0.0',
1060 'application/vnd.aai.asyncapi+yaml;version=2.0.0',
1061 'application/vnd.aai.asyncapi;version=2.1.0',
1062 'application/vnd.aai.asyncapi+json;version=2.1.0',
1063 'application/vnd.aai.asyncapi+yaml;version=2.1.0',
1064 'application/vnd.aai.asyncapi;version=2.2.0',
1065 'application/vnd.aai.asyncapi+json;version=2.2.0',
1066 'application/vnd.aai.asyncapi+yaml;version=2.2.0',
1067 'application/vnd.aai.asyncapi;version=2.3.0',
1068 'application/vnd.aai.asyncapi+json;version=2.3.0',
1069 'application/vnd.aai.asyncapi+yaml;version=2.3.0',
1070 'application/vnd.aai.asyncapi;version=2.4.0',
1071 'application/vnd.aai.asyncapi+json;version=2.4.0',
1072 'application/vnd.aai.asyncapi+yaml;version=2.4.0',
1073 'application/vnd.aai.asyncapi;version=2.5.0',
1074 'application/vnd.aai.asyncapi+json;version=2.5.0',
1075 'application/vnd.aai.asyncapi+yaml;version=2.5.0',
1076 'application/vnd.aai.asyncapi;version=2.6.0',
1077 'application/vnd.aai.asyncapi+json;version=2.6.0',
1078 'application/vnd.aai.asyncapi+yaml;version=2.6.0',
1079]
1080```
1081
1082##### [openapi-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies/openapi-2)
1083
1084External resolution strategy for understanding and resolving external dependencies of [OpenApi 2.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md) definitions.
1085
1086Supported media types:
1087
1088```js
1089[
1090 'application/vnd.oai.openapi;version=2.0',
1091 'application/vnd.oai.openapi+json;version=2.0',
1092 'application/vnd.oai.openapi+yaml;version=2.0',
1093]
1094```
1095
1096##### [openapi-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies/openapi-3-0)
1097
1098External resolution strategy for understanding and resolving external dependencies of [OpenApi 3.0.x](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md) definitions.
1099
1100Supported media types:
1101
1102```js
1103[
1104 'application/vnd.oai.openapi;version=3.0.0',
1105 'application/vnd.oai.openapi+json;version=3.0.0',
1106 'application/vnd.oai.openapi+yaml;version=3.0.0',
1107 'application/vnd.oai.openapi;version=3.0.1',
1108 'application/vnd.oai.openapi+json;version=3.0.1',
1109 'application/vnd.oai.openapi+yaml;version=3.0.1',
1110 'application/vnd.oai.openapi;version=3.0.2',
1111 'application/vnd.oai.openapi+json;version=3.0.2',
1112 'application/vnd.oai.openapi+yaml;version=3.0.2',
1113 'application/vnd.oai.openapi;version=3.0.3',
1114 'application/vnd.oai.openapi+json;version=3.0.3',
1115 'application/vnd.oai.openapi+yaml;version=3.0.3',
1116]
1117```
1118
1119##### [openapi-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/resolve/strategies/openapi-3-1)
1120
1121External resolution strategy for understanding and resolving external dependencies of [OpenApi 3.1.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) definitions.
1122
1123Supported media types:
1124
1125```js
1126[
1127 'application/vnd.oai.openapi;version=3.1.0',
1128 'application/vnd.oai.openapi+json;version=3.1.0',
1129 'application/vnd.oai.openapi+yaml;version=3.1.0'
1130]
1131```
1132
1133##### External resolution strategies execution order
1134
1135It's important to understand that default external resolution strategies are run in specific order. The order is determined
1136by the [options.resolve.strategies](https://github.com/swagger-api/apidom/blob/ba888d711a4292e8ed0b72e343c4902a4bf0d45a/packages/apidom-reference/src/configuration/saturated.ts#L41) option.
1137Every strategy is pulled from `options.resolve.strategies` option and its `canResolve` method is called to determine
1138whether the strategy can externally resolve the URI. If `canResolve` returns `true`, `resolve` method of strategy is called
1139and result from external resolution is returned. No subsequent strategies are run. If `canResolve` returns
1140`false`, next strategy is pulled and this process is repeated until one of the strategy's `canResolve` method
1141returns `true` or until entire list of strategies is exhausted (throws error).
1142
1143```js
1144[
1145 OpenApi2ResolveStrategy(),
1146 OpenApi3_0ResolveStrategy(),
1147 OpenApi3_1ResolveStrategy(),
1148 AsyncApi2ResolveStrategy(),
1149]
1150```
1151Most specific strategies are listed first, most generic are listed last.
1152
1153It's possible to **change** strategies **order globally** by mutating global `resolve` option:
1154
1155```js
1156import { options } from '@swagger-api/apidom-reference';
1157import AsyncApi2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/asyncapi-2';
1158import OpenApi2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-2';
1159import OpenApi3_0ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-0';
1160import OpenApi3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';
1161
1162options.resolve.strategies = [
1163 OpenApi2ResolveStrategy(),
1164 OpenApi3_0ResolveStrategy(),
1165 OpenApi3_1ResolveStrategy(),
1166 AsyncApi2ResolveStrategy(),
1167];
1168```
1169
1170To **change** the strategies **order** on ad-hoc basis:
1171
1172```js
1173import { resolve } from '@swagger-api/apidom-reference';
1174import AsyncApi2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/asyncapi-2';
1175import OpenApi2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-2';
1176import OpenApi3_0ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-0';
1177import OpenApi3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';
1178
1179
1180await resolve('/home/user/oas.json', {
1181 parse: {
1182 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1183 },
1184 resolve: {
1185 strategies: [
1186 AsyncApi2ResolveStrategy(),
1187 OpenApi2ResolveStrategy(),
1188 OpenApi3_0ResolveStrategy(),
1189 OpenApi3_1ResolveStrategy(),
1190 ]
1191 }
1192});
1193```
1194##### Creating new external resolution strategy
1195
1196Resolve component can be extended by additional strategies. Every strategy is an object that
1197must conform to the following interface/shape:
1198
1199```typescript
1200{
1201 // uniquely identifies this plugin
1202 name: string,
1203
1204 // this method is called to determine whether the strategy can externally resolve the file
1205 canResolve(file: IFile): boolean {
1206 // ...implementation...
1207 },
1208
1209 // this method actually externally resolves the file
1210 async resolve(file: IFile): Promise<ReferenceSet> {
1211 // ...implementation...
1212 }
1213}
1214```
1215
1216New strategy is then provided as an option to a `resolve` function:
1217
1218```js
1219import { resolve, options } from '@swagger-api/apidom-reference';
1220
1221const myCustomResolverStrategy = {
1222 name: 'myCustomResolverStrategy',
1223 canResolve(file) {
1224 return true;
1225 },
1226 async resolve(file) {
1227 // implementation of external resolution
1228 }
1229};
1230
1231await resolve('/home/user/oas.json', {
1232 parse: {
1233 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1234 },
1235 resolve: {
1236 strategies: [...options.resolve.strategies, myCustomResolverStrategy],
1237 }
1238});
1239```
1240
1241In this particular example we're adding our custom strategy as the last strategy
1242to the available default external resolution strategy list, so there's a good chance that one of the
1243default strategies detects that it can externally resolve the `/home/user/oas.json` file,
1244resolves it and returns `ReferenceSet` object.
1245
1246If you want to force execution of your strategy, add it as a first one:
1247
1248```js
1249import { resolve, options } from '@swagger-api/apidom-reference';
1250
1251
1252const myCustomResolverStrategy = {
1253 name: 'myCustomResolverStrategy',
1254 canResolve(file) {
1255 return true;
1256 },
1257 async resolve(file) {
1258 // implementation of external resolution
1259 }
1260};
1261
1262await resolve('/home/user/oas.json', {
1263 parse: {
1264 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1265 },
1266 resolve: {
1267 strategies: [myCustomResolverStrategy, ...options.resolve.strategies],
1268 }
1269});
1270```
1271
1272To override the default strategies entirely, set `myCustomResolverStrategy` strategy to be the only one available:
1273
1274```js
1275import { resolve } from '@swagger-api/apidom-reference';
1276
1277const myCustomResolverStrategy = {
1278 name: 'myCustomResolverStrategy',
1279 canResolve(file) {
1280 return true;
1281 },
1282 async resolve(file) {
1283 // implementation of external resolution
1284 }
1285};
1286
1287await resolve('/home/user/oas.json', {
1288 parse: {
1289 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1290 },
1291 resolve: {
1292 strategies: [myCustomResolverPlugin],
1293 }
1294});
1295```
1296New strategies can be based on a predefined stamp called [ResolveStrategy](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/resolve/strategies/ResolveStrategy.ts).
1297
1298##### Manipulating external resolution strategies
1299
1300External resolution strategies can be added, removed, replaced or reordered. We've already covered these techniques in [Manipulating parser plugins section](#manipulating-parser-plugins).
1301
1302## Dereference component
1303
1304Dereferencing is a process of transcluding referencing element (internal or external) with a referenced element
1305using a specific [dereference strategy](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies). Simply put, dereferencing is a process of reference removal.
1306Dereferencing strategy is determined by asserting on `mediaType` option. [File Resolution](#file-resolution) (file content is read/fetched)
1307and [Parse component](#parse-component) (file content is parsed) are used under the hood.
1308
1309**Dereferencing a file localed on a local filesystem:**
1310
1311```js
1312import { dereference } from '@swagger-api/apidom-reference';
1313
1314await dereference('/home/user/oas.json', {
1315 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1316}); // Promise<ParseResultElement>
1317```
1318
1319**Dereferencing an HTTP(S) URL located on an internet:**
1320
1321```js
1322import { dereference } from '@swagger-api/apidom-reference';
1323
1324await dereference('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
1325 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1326 resolve: {
1327 resolverOpts: {
1328 axiosConfig: {
1329 timeout: 10
1330 },
1331 },
1332 },
1333}); // Promise<ParseResultElement>
1334```
1335
1336**Dereferencing an ApiDOM fragment:**
1337
1338When dereferencing an ApiDOM fragment, [baseURI](https://github.com/swagger-api/apidom/blob/91763fa4ad876375a413e7049c28c2031c7bbe83/apidom/packages/apidom-reference/src/options/index.ts#L47)
1339resolve option needs to be provided to have a starting point for external dependency resolution.
1340`mediaType` parse option is unnecessary as we can directly assert the type of ApiDOM fragment.
1341
1342**ex.json**
1343
1344```json
1345{
1346 "externalParameter": {
1347 "name": "param1",
1348 "in": "query"
1349 }
1350}
1351```
1352
1353```js
1354import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
1355import { dereferenceApiDOM } from '@swagger-api/apidom-reference';
1356
1357const apidom = OpenApi3_1Element.refract({
1358 openapi: '3.1.0',
1359 components: {
1360 parameters: {
1361 externalRef: {
1362 $ref: './ex.json#/externalParameter', // file is located at /home/user/ex.json
1363 }
1364 }
1365 }
1366});
1367
1368const dereferenced = await dereferenceApiDOM(apidom, {
1369 resolve: { baseURI: '/home/user/' },
1370});
1371/**
1372 * OpenApi3_1Element {
1373 * openapi: '3.1.0',
1374 * components: {
1375 * parameters: {
1376 * externalRef: {
1377 * name: param1,
1378 * in: query
1379 * }
1380 * }
1381 * }
1382 * }
1383 */
1384```
1385
1386#### [Dereference strategies](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies)
1387
1388Dereference strategy determines how a document is internally or externally dereferenced. Depending on document `mediaType` option,
1389every strategy differs significantly. `Dereference component` comes with four (4) default dereference strategies.
1390
1391##### [asyncapi-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies/asyncapi-2)
1392
1393Dereference strategy for dereferencing [AsyncApi 2.x.y](https://github.com/asyncapi/spec/blob/master/spec/asyncapi.md) definitions.
1394
1395Supported media types:
1396
1397```js
1398[
1399 'application/vnd.aai.asyncapi;version=2.0.0',
1400 'application/vnd.aai.asyncapi+json;version=2.0.0',
1401 'application/vnd.aai.asyncapi+yaml;version=2.0.0',
1402 'application/vnd.aai.asyncapi;version=2.1.0',
1403 'application/vnd.aai.asyncapi+json;version=2.1.0',
1404 'application/vnd.aai.asyncapi+yaml;version=2.1.0',
1405 'application/vnd.aai.asyncapi;version=2.2.0',
1406 'application/vnd.aai.asyncapi+json;version=2.2.0',
1407 'application/vnd.aai.asyncapi+yaml;version=2.2.0',
1408 'application/vnd.aai.asyncapi;version=2.3.0',
1409 'application/vnd.aai.asyncapi+json;version=2.3.0',
1410 'application/vnd.aai.asyncapi+yaml;version=2.3.0',
1411 'application/vnd.aai.asyncapi;version=2.4.0',
1412 'application/vnd.aai.asyncapi+json;version=2.4.0',
1413 'application/vnd.aai.asyncapi+yaml;version=2.4.0',
1414 'application/vnd.aai.asyncapi;version=2.5.0',
1415 'application/vnd.aai.asyncapi+json;version=2.5.0',
1416 'application/vnd.aai.asyncapi+yaml;version=2.5.0',
1417 'application/vnd.aai.asyncapi;version=2.6.0',
1418 'application/vnd.aai.asyncapi+json;version=2.6.0',
1419 'application/vnd.aai.asyncapi+yaml;version=2.6.0',
1420]
1421```
1422
1423##### [openapi-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies/openapi-2)
1424
1425Dereference strategy for dereferencing [OpenApi 2.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md) definitions.
1426
1427Supported media types:
1428
1429```js
1430[
1431 'application/vnd.oai.openapi;version=2.0',
1432 'application/vnd.oai.openapi+json;version=2.0',
1433 'application/vnd.oai.openapi+yaml;version=2.0',
1434]
1435```
1436
1437##### [openapi-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies/openapi-3-0)
1438
1439Dereference strategy for dereferencing [OpenApi 3.0.x](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md) definitions.
1440
1441Supported media types:
1442
1443```js
1444[
1445 'application/vnd.oai.openapi;version=3.0.0',
1446 'application/vnd.oai.openapi+json;version=3.0.0',
1447 'application/vnd.oai.openapi+yaml;version=3.0.0',
1448 'application/vnd.oai.openapi;version=3.0.1',
1449 'application/vnd.oai.openapi+json;version=3.0.1',
1450 'application/vnd.oai.openapi+yaml;version=3.0.1',
1451 'application/vnd.oai.openapi;version=3.0.2',
1452 'application/vnd.oai.openapi+json;version=3.0.2',
1453 'application/vnd.oai.openapi+yaml;version=3.0.2',
1454 'application/vnd.oai.openapi;version=3.0.3',
1455 'application/vnd.oai.openapi+json;version=3.0.3',
1456 'application/vnd.oai.openapi+yaml;version=3.0.3',
1457]
1458```
1459
1460##### [openapi-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/dereference/strategies/openapi-3-1)
1461
1462Dereference strategy for dereferencing [OpenApi 3.1.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) definitions.
1463
1464Supported media types:
1465
1466```js
1467[
1468 'application/vnd.oai.openapi;version=3.1.0',
1469 'application/vnd.oai.openapi+json;version=3.1.0',
1470 'application/vnd.oai.openapi+yaml;version=3.1.0'
1471]
1472```
1473
1474##### Dereference strategies execution order
1475
1476It's important to understand that default dereference strategies are run in specific order. The order is determined
1477by the [options.dereference.strategies](https://github.com/swagger-api/apidom/blob/b3a391481360004d3d4a56c1467cece557442ec8/apidom/packages/apidom-reference/src/options/index.ts#L88) option.
1478Every strategy is pulled from `options.dereference.strategies` option and it's `canDereference` method is called to determine
1479whether the strategy can dereference the URI. If `canDereference` returns `true`, `dereference` method of strategy is called
1480and result from dereferencing is returned. No subsequent strategies are run. If `canDereference` returns
1481`false`, next strategy is pulled and this process is repeated until one of the strategy's `canDereference` method
1482returns `true` or until entire list of strategies is exhausted (throws error).
1483
1484```js
1485[
1486 OpenApi2DereferenceStrategy(),
1487 OpenApi3_0DereferenceStrategy(),
1488 OpenApi3_1DereferenceStrategy(),
1489 AsyncApi2DereferenceStrategy(),
1490]
1491```
1492Most specific strategies are listed first, most generic are listed last.
1493
1494It's possible to **change** strategies **order globally** by mutating global `dereference` option:
1495
1496```js
1497import { options } from '@swagger-api/apidom-reference';
1498import AsyncApi2DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/asyncapi-2'
1499import OpenApi2DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-2'
1500import OpenApi3_0DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-0'
1501import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1'
1502
1503options.dereference.strategies = [
1504 OpenApi2DereferenceStrategy(),
1505 OpenApi3_0DereferenceStrategy(),
1506 OpenApi3_1DereferenceStrategy(),
1507 AsyncApi2DereferenceStrategy(),
1508];
1509```
1510
1511To **change** the strategies **order** on ad-hoc basis:
1512
1513```js
1514import { dereference } from '@swagger-api/apidom-reference';
1515import AsyncApi2DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/asyncapi-2'
1516import OpenApi2DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-2'
1517import OpenApi3_0DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-0'
1518import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1'
1519
1520await dereference('/home/user/oas.json', {
1521 parse: {
1522 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1523 },
1524 dereference: {
1525 strategies: [
1526 AsyncApi2DereferenceStrategy(),
1527 OpenApi2DereferenceStrategy(),
1528 OpenApi3_0DereferenceStrategy(),
1529 OpenApi3_1DereferenceStrategy(),
1530 ]
1531 }
1532});
1533```
1534##### Creating new dereference strategy
1535
1536Dereference component can be extended by additional strategies. Every strategy is an object that
1537must conform to the following interface/shape:
1538
1539```typescript
1540{
1541 // uniquely identifies this plugin
1542 name: string,
1543
1544 // this method is called to determine whether the strategy can dereference the file
1545 canDereference(file: IFile): boolean {
1546 // ...implementation...
1547 },
1548
1549 // this method actually dereferences the file
1550 async dereference(file: IFile, options: IReferenceOptions): Promise<ParseResultElement> {
1551 // ...implementation...
1552 }
1553}
1554```
1555
1556New strategy is then provided as an option to the `dereference` function:
1557
1558```js
1559import { dereference, options } from '@swagger-api/apidom-reference';
1560
1561const myCustomDereferenceStrategy = {
1562 name: 'myCustomDereferenceStrategy',
1563 canDereference(file) {
1564 return true;
1565 },
1566 async dereference(file, options: IReferenceOptions) {
1567 // implementation of dereferenceing
1568 }
1569};
1570
1571await dereference('/home/user/oas.json', {
1572 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1573 dereference: {
1574 strategies: [...options.dereference.strategies, myCustomDereferenceStrategy],
1575 }
1576});
1577```
1578
1579In this particular example we're adding our custom strategy as the last strategy
1580to the available default dereference strategy list, so there's a good chance that one of the
1581default strategies detects that it can dereference the `/home/user/oas.json` file,
1582dereferences it and returns a dereferenced element.
1583
1584If you want to force execution of your strategy, add it as a first one:
1585
1586```js
1587import { dereference, options } from '@swagger-api/apidom-reference';
1588
1589const myCustomDereferenceStrategy = {
1590 name: 'myCustomDereferenceStrategy',
1591 canDereference(file) {
1592 return true;
1593 },
1594 async dereference(file, options: IReferenceOptions) {
1595 // implementation of dereferenceing
1596 }
1597};
1598
1599await dereference('/home/user/oas.json', {
1600 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1601 dereference: {
1602 strategies: [myCustomDereferenceStrategy, ...options.dereference.strategies],
1603 }
1604});
1605```
1606
1607To override the default strategies entirely, set `myCustomDereferenceStrategy` strategy to be the only one available:
1608
1609```js
1610import { dereference } from '@swagger-api/apidom-reference';
1611
1612const myCustomDereferenceStrategy = {
1613 name: 'myCustomDereferenceStrategy',
1614 canDereference(file) {
1615 return true;
1616 },
1617 async dereference(file, options: IReferenceOptions) {
1618 // implementation of dereferenceing
1619 }
1620};
1621
1622await dereference('/home/user/oas.json', {
1623 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1624 dereference: {
1625 strategies: [myCustomDereferenceStrategy],
1626 }
1627});
1628```
1629
1630New strategies can be based on a predefined stamp called [DereferenceStrategy](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/dereference/strategies/DereferenceStrategy.ts).
1631
1632##### Manipulating dereference strategies
1633
1634Dereference strategies can be added, removed, replaced or reordered. We've already covered these techniques in [Manipulating parser plugins section](#manipulating-parser-plugins).
1635
1636##### Increasing speed of dereference
1637
1638Our default dereference strategies are built on asynchronous sequential traversing of ApiDOM.
1639The total time of dereferencing is the sum of `traversing` + sum of `external resolution per referencing element`.
1640By having a huge number of external dependencies in your definition file, dereferencing can get quite slow.
1641Fortunately there is solution for this by running an `external resolution` first,
1642and passing its result to dereferencing via an option. External resolution is built on asynchronous parallel traversal (on single file),
1643so it's theoretically always faster on huge amount of external dependencies than the dereferencing.
1644
1645```js
1646import { resolve, dereference } from '@swagger-api/apidom-reference';
1647
1648const refSet = await resolve('/home/user/oas.json', {
1649 parse: { mediType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1650});
1651
1652const dereferenced = await dereference('/home/user/oas.json', {
1653 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1654 dereference: { refSet },
1655});
1656```
1657
1658Total time of dereferencing is now the sum of `external resolution traversing` + `dereference traversing` + sum of `max external resolution per file`.
1659
1660
1661## Bundle component
1662
1663Bundling is a convenient way to package up resources spread across multiple files in a single file
1664(**Compound Document**) using a specific [bundle strategy](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/bundle/strategies).
1665
1666The bundling process for creating a Compound Document is defined as taking references (such as "$ref")
1667to an external Resource and embedding the referenced Resources within the referring document.
1668Bundling SHOULD be done in such a way that all URIs (used for referencing) in the base document
1669and any referenced/embedded documents do not require altering.
1670
1671Bundling strategy is determined by asserting on `mediaType` option. [File Resolution](#file-resolution) (file content is read/fetched)
1672and [Parse component](#parse-component) (file content is parsed) are used under the hood.
1673
1674**Bundling a file localed on a local filesystem:**
1675
1676```js
1677import { bundle } from '@swagger-api/apidom-reference';
1678
1679await bundle('/home/user/oas.json', {
1680 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1681}); // Promise<ParseResultElement>
1682```
1683
1684**Bundling an HTTP(S) URL located on an internet:**
1685
1686```js
1687import { bundle } from '@swagger-api/apidom-reference';
1688
1689await bundle('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
1690 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1691 resolve: {
1692 resolverOpts: {
1693 axiosConfig: {
1694 timeout: 10
1695 },
1696 },
1697 },
1698}); // Promise<ParseResultElement>
1699```
1700
1701#### [Bundle strategies](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/bundle/strategies)
1702
1703Bundle strategy determines how a document is bundled into a Compound Document. Depending on document `mediaType` option,
1704every strategy differs significantly. `Bundle component` comes with single (1) default bundle strategy.
1705
1706##### [openapi-3-1](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/bundle/strategies/openapi-3-1)
1707
1708Bundle strategy for bundling [OpenApi 3.1.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) definitions.
1709
1710Supported media types:
1711
1712```js
1713[
1714 'application/vnd.oai.openapi;version=3.1.0',
1715 'application/vnd.oai.openapi+json;version=3.1.0',
1716 'application/vnd.oai.openapi+yaml;version=3.1.0'
1717]
1718```
1719
1720##### Bundle strategies execution order
1721
1722It's important to understand that default bundle strategies are run in specific order. The order is determined
1723by the `options.bundle.strategies` option.
1724Every strategy is pulled from `options.bundle.strategies` option, and it's `canBundle` method is called to determine
1725whether the strategy can bundle the URI. If `canBundle` returns `true`, `bundle` method of strategy is called
1726and result from bundling is returned. No subsequent strategies are run. If `canBundle` returns
1727`false`, next strategy is pulled and this process is repeated until one of the strategy's `canBundle` method
1728returns `true` or until entire list of strategies is exhausted (throws error).
1729
1730```js
1731[
1732 OpenApi3_1BundleStrategy(),
1733]
1734```
1735Most specific strategies are listed first, most generic are listed last.
1736
1737It's possible to **change** strategies **order globally** by mutating global `bundle` option:
1738
1739```js
1740import { options } from '@swagger-api/apidom-reference';
1741import OpenApi3_1BundleStrategy from '@swagger-api/apidom-reference/bundle/strategies/openapi-3-1'
1742
1743options.dereference.strategies = [
1744 OpenApi3_1DereferenceStrategy(),
1745];
1746```
1747
1748To **change** the strategies **order** on ad-hoc basis:
1749
1750```js
1751import { bundle } from '@swagger-api/apidom-reference';
1752import OpenApi3_1BundleStrategy from '@swagger-api/apidom-reference/bundle/strategies/openapi-3-1'
1753
1754await bundle('/home/user/oas.json', {
1755 parse: {
1756 mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
1757 },
1758 bundle: {
1759 strategies: [
1760 OpenApi3_1BundleStrategy(),
1761 ]
1762 }
1763});
1764```
1765##### Creating new bundle strategy
1766
1767Bundle component can be extended by additional strategies. Every strategy is an object that
1768must conform to the following interface/shape:
1769
1770```typescript
1771{
1772 // uniquely identifies this plugin
1773 name: string,
1774
1775 // this method is called to determine whether the strategy can bundle the file
1776 canBundle(file: IFile): boolean {
1777 // ...implementation...
1778 },
1779
1780 // this method actually bundles the file
1781 async bundle(file: IFile, options: IReferenceOptions): Promise<ParseResultElement> {
1782 // ...implementation...
1783 }
1784}
1785```
1786
1787New strategy is then provided as an option to the `bundle` function:
1788
1789```js
1790import { bundle, options } from '@swagger-api/apidom-reference';
1791
1792const myCustomBundleStrategy = {
1793 name: 'myCustomByndleStrategy',
1794 canBundle(file) {
1795 return true;
1796 },
1797 async bundle(file, options: IReferenceOptions) {
1798 // implementation of bundling
1799 }
1800};
1801
1802await bundle('/home/user/oas.json', {
1803 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1804 bundle: {
1805 strategies: [...options.bundle.strategies, myCustomBundleStrategy],
1806 }
1807});
1808```
1809
1810In this particular example we're adding our custom strategy as the last strategy
1811to the available default bundle strategy list, so there's a good chance that one of the
1812default strategies detects that it can bundle the `/home/user/oas.json` file,
1813bundles it and returns a bundled element.
1814
1815If you want to force execution of your strategy, add it as a first one:
1816
1817```js
1818import { bundle, options } from '@swagger-api/apidom-reference';
1819
1820const myCustomBundleStrategy = {
1821 name: 'myCustomBundleStrategy',
1822 canBundle(file) {
1823 return true;
1824 },
1825 async bundle(file, options: IReferenceOptions) {
1826 // implementation of bundling
1827 }
1828};
1829
1830
1831await bundle('/home/user/oas.json', {
1832 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1833 bundle: {
1834 strategies: [myCustomBundleStrategy, ...options.bundle.strategies],
1835 }
1836});
1837```
1838
1839To override the default strategies entirely, set `myCustomBundleStrategy` strategy to be the only one available:
1840
1841```js
1842import { bundle } from '@swagger-api/apidom-reference';
1843
1844const myCustomBundleStrategy = {
1845 name: 'myCustomBundleStrategy',
1846 canBundle(file) {
1847 return true;
1848 },
1849 async bundle(file, options: IReferenceOptions) {
1850 // implementation of bundling
1851 }
1852};
1853
1854await bundle('/home/user/oas.json', {
1855 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1856 bundle: {
1857 strategies: [myCustomBundleStrategy],
1858 }
1859});
1860```
1861
1862New strategies can be based on a predefined stamp called [BundleStrategy](https://github.com/swagger-api/apidom/blob/main/packages/apidom-reference/src/bundle/strategies/BundleStrategy.ts).
1863
1864##### Manipulating bundle strategies
1865
1866Bundle strategies can be added, removed, replaced or reordered. We've already covered these techniques in [Manipulating parser plugins section](#manipulating-parser-plugins).
1867
1868##### Increasing speed of bundling
1869
1870Our default bundling strategies are built on asynchronous sequential traversing of ApiDOM.
1871The total time of bundling is the sum of `traversing` + sum of `external resolution per referencing element`.
1872By having a huge number of external dependencies in your definition file, bundling can get quite slow.
1873Fortunately there is solution for this by running an `external resolution` first,
1874and passing its result to bundling via an option. External resolution is built on asynchronous parallel traversal (on single file),
1875so it's theoretically always faster on huge amount of external dependencies than the bundling.
1876
1877```js
1878import { resolve, bundle } from '@swagger-api/apidom-reference';
1879
1880const refSet = await resolve('/home/user/oas.json', {
1881 parse: { mediType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1882});
1883
1884const bundled = await bundle('/home/user/oas.json', {
1885 parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
1886 bundle: { refSet },
1887});
1888```
1889
1890Total time of bundling is now the sum of `external resolution traversing` + `bundle traversing` + sum of `max external resolution per file`.
Note: See TracBrowser for help on using the repository browser.