source: node_modules/@swagger-api/apidom-ns-openapi-3-0/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: 9.4 KB
Line 
1# @swagger-api/apidom-ns-openapi-3-0
2
3`@swagger-api/apidom-ns-openapi-3-0` contains ApiDOM namespace supports following OpenAPI specification versions:
4
5- [OpenAPI 3.0.3 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md)
6- [OpenAPI 3.0.2 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md)
7- [OpenAPI 3.0.1 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.1.md)
8- [OpenAPI 3.0.0 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md)
9
10## Installation
11
12You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
13
14```sh
15 $ npm install @swagger-api/apidom-ns-openapi-3-0
16```
17
18## OpenAPI 3.0.x namespace
19
20OpenAPI 3.0.x namespace consists of [number of elements](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-openapi-3-0/src/elements) implemented on top
21of [primitive ones](https://github.com/refractproject/minim/tree/master/lib/primitives).
22
23```js
24import { createNamespace } from '@swagger-api/apidom-core';
25import openApi3_0Namespace from '@swagger-api/apidom-ns-openapi-3-0';
26
27const namespace = createNamespace(openApi3_0Namespace);
28
29const objectElement = new namespace.elements.Object();
30const openApiElement = new namespace.elements.OpenApi3_0();
31```
32
33When namespace instance is created in this way, it will extend the base namespace
34with the namespace provided as an argument.
35
36Elements from the namespace can also be used directly by importing them.
37
38```js
39import { OpenApi3_0Element, InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
40
41const infoElement = new InfoElement();
42const openApiElement = new OpenApi3_0Element();
43```
44
45## Predicates
46
47This package exposes [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/predicates.ts)
48for all higher order elements that are part of this namespace.
49
50```js
51import { isOpenApi3_0Element, OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0';
52
53const openApiElement = new OpenApi3_0Element();
54
55isOpenApi3_0Element(openApiElement); // => true
56```
57
58## Traversal
59
60Traversing ApiDOM in this namespace is possible by using `visit` function from `apidom` package.
61This package comes with its own [keyMap](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/traversal/visitor.ts#L11) and [nodeTypeGetter](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/traversal/visitor.ts#L4).
62To learn more about these `visit` configuration options please refer to [@swagger-api/apidom-ast documentation](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/README.md#visit).
63
64```js
65import { visit } from '@swagger-api/apidom-core';
66import { OpenApi3_0Element, keyMap, getNodeType } from '@swagger-api/apidom-ns-openapi-3-0';
67
68const element = new OpenApi3_0Element();
69
70const visitor = {
71 OpenApi3_0Element(openApiElement) {
72 console.dir(openApiElement);
73 },
74};
75
76visit(element, visitor, { keyMap, nodeTypeGetter: getNodeType });
77```
78
79## Refractors
80
81Refractor is a special layer inside the namespace that can transform either JavaScript structures
82or generic ApiDOM structures into structures built from elements of this namespace.
83
84**Refracting JavaScript structures**:
85
86```js
87import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
88
89const object = {
90 title: 'my title',
91 description: 'my description',
92 version: '0.1.0',
93};
94
95InfoElement.refract(object); // => InfoElement({ title, description, version })
96```
97
98**Refracting generic ApiDOM structures**:
99
100```js
101import { ObjectElement } from '@swagger-api/apidom-core';
102import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
103
104const objectElement = new ObjectElement({
105 title: 'my title',
106 description: 'my description',
107 version: '0.1.0',
108});
109
110InfoElement.refract(objectElement); // => InfoElement({ title = 'my title', description = 'my description', version = '0.1.0' })
111```
112
113### Refractor plugins
114
115Refractors can accept plugins as a second argument of refract static method.
116
117```js
118import { ObjectElement } from '@swagger-api/apidom-core';
119import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
120
121const objectElement = new ObjectElement({
122 title: 'my title',
123 description: 'my description',
124 version: '0.1.0',
125});
126
127const plugin = ({ predicates, namespace }) => ({
128 name: 'plugin',
129 pre() {
130 console.dir('runs before traversal');
131 },
132 visitor: {
133 InfoElement(infoElement) {
134 infoElement.version = '2.0.0';
135 },
136 },
137 post() {
138 console.dir('runs after traversal');
139 },
140});
141
142InfoElement.refract(objectElement, { plugins: [plugin] }); // => InfoElement({ title = 'my title', description = 'my description', version = '2.0.0' })
143```
144
145You can define as many plugins as needed to enhance the resulting namespaced ApiDOM structure.
146If multiple plugins with the same visitor method are defined, they run in parallel (just like in Babel).
147
148#### Replace Empty Element plugin
149
150This plugin is specific to YAML 1.2 format, which allows defining key-value pairs with empty key,
151empty value, or both. If the value is not provided in YAML format, this plugin compensates for
152this missing value with the most appropriate semantic element type.
153
154```js
155import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
156import { refractorPluginReplaceEmptyElement, OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0';
157
158const yamlDefinition = `
159openapi: 3.0.3
160info:
161`;
162const apiDOM = await parse(yamlDefinition);
163const openApiElement = OpenApi3_0Element.refract(apiDOM.result, {
164 plugins: [refractorPluginReplaceEmptyElement()],
165});
166
167// =>
168// (OpenApi3_0Element
169// (MemberElement
170// (StringElement)
171// (OpenapiElement))
172// (MemberElement
173// (StringElement)
174// (InfoElement)))
175
176// => without the plugin the result would be as follows:
177// (OpenApi3_0Element
178// (MemberElement
179// (StringElement)
180// (OpenapiElement))
181// (MemberElement
182// (StringElement)
183// (StringElement)))
184```
185
186## Implementation progress
187
188Only fully implemented specification objects should be checked here.
189
190- [x] [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oasObject)
191- [x] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#infoObject)
192- [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contactObject)
193- [x] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#licenseObject)
194- [x] [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject)
195- [x] [Server Variable Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverVariableObject)
196- [x] [Components](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsObject)
197- [x] [Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathsObject)
198- [x] [Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathItemObject)
199- [x] [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operationObject)
200- [x] [External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#externalDocumentationObject)
201- [x] [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameterObject)
202- [x] [Request Body Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#requestBodyObject)
203- [x] [Media Type Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#mediaTypeObject)
204- [x] [Encoding Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#encodingObject)
205- [x] [Responses Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#responsesObject)
206- [x] [Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#callbackObject)
207- [x] [Example Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#exampleObject)
208- [x] [Link Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#linkObject)
209- [x] [Header Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#headerObject)
210- [x] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#tagObject)
211- [x] [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#referenceObject)
212- [x] [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaObject)
213- [x] [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminatorObject)
214- [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#xmlObject)
215- [x] [Security Scheme Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securitySchemeObject)
216- [x] [OAuth Flows Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauthFlowsObject)
217- [x] [OAuth Flow Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauthFlowObject)
218- [x] [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securityRequirementObject)
219- [x] [Specification extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions)
220
Note: See TracBrowser for help on using the repository browser.