@gitbook/react-openapi 0.7.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/dist/InteractiveSection.d.ts +4 -6
- package/dist/InteractiveSection.jsx +96 -0
- package/dist/Markdown.d.ts +1 -2
- package/dist/Markdown.jsx +5 -0
- package/dist/OpenAPICodeSample.d.ts +2 -4
- package/dist/OpenAPICodeSample.jsx +143 -0
- package/dist/OpenAPIDisclosure.d.ts +12 -0
- package/dist/OpenAPIDisclosure.jsx +32 -0
- package/dist/OpenAPIDisclosureGroup.d.ts +19 -0
- package/dist/OpenAPIDisclosureGroup.jsx +81 -0
- package/dist/OpenAPIOperation.d.ts +2 -4
- package/dist/OpenAPIOperation.jsx +51 -0
- package/dist/OpenAPIOperationContext.d.ts +16 -0
- package/dist/OpenAPIOperationContext.jsx +26 -0
- package/dist/OpenAPIPath.d.ts +8 -0
- package/dist/OpenAPIPath.jsx +54 -0
- package/dist/OpenAPIRequestBody.d.ts +3 -4
- package/dist/OpenAPIRequestBody.jsx +19 -0
- package/dist/OpenAPIResponse.d.ts +4 -4
- package/dist/OpenAPIResponse.jsx +49 -0
- package/dist/OpenAPIResponseExample.d.ts +2 -4
- package/dist/OpenAPIResponseExample.jsx +108 -0
- package/dist/OpenAPIResponses.d.ts +3 -4
- package/dist/OpenAPIResponses.jsx +36 -0
- package/dist/OpenAPISchema.d.ts +11 -8
- package/dist/OpenAPISchema.jsx +295 -0
- package/dist/OpenAPISchemaName.d.ts +12 -0
- package/dist/OpenAPISchemaName.jsx +15 -0
- package/dist/OpenAPISecurities.d.ts +2 -4
- package/dist/OpenAPISecurities.jsx +55 -0
- package/dist/OpenAPIServerURL.d.ts +2 -3
- package/dist/OpenAPIServerURL.jsx +67 -0
- package/dist/OpenAPIServerURLVariable.d.ts +2 -3
- package/dist/OpenAPIServerURLVariable.jsx +8 -0
- package/dist/OpenAPISpec.d.ts +3 -4
- package/dist/OpenAPISpec.jsx +91 -0
- package/dist/OpenAPITabs.d.ts +25 -0
- package/dist/OpenAPITabs.jsx +67 -0
- package/dist/ScalarApiButton.d.ts +3 -3
- package/dist/ScalarApiButton.jsx +51 -0
- package/dist/code-samples.d.ts +4 -0
- package/dist/code-samples.js +103 -38
- package/dist/fetchOpenAPIOperation.d.ts +9 -54
- package/dist/fetchOpenAPIOperation.js +178 -107
- package/dist/generateSchemaExample.d.ts +2 -2
- package/dist/generateSchemaExample.js +28 -100
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/resolveOpenAPIOperation.d.ts +11 -0
- package/dist/resolveOpenAPIOperation.js +194 -0
- package/dist/stringifyOpenAPI.d.ts +4 -0
- package/dist/stringifyOpenAPI.js +6 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +11 -12
- package/dist/utils.d.ts +6 -1
- package/dist/utils.js +15 -2
- package/package.json +11 -10
- package/src/InteractiveSection.tsx +119 -78
- package/src/Markdown.tsx +2 -3
- package/src/OpenAPICodeSample.tsx +35 -21
- package/src/OpenAPIDisclosure.tsx +50 -0
- package/src/OpenAPIDisclosureGroup.tsx +136 -0
- package/src/OpenAPIOperation.tsx +36 -42
- package/src/OpenAPIOperationContext.tsx +45 -0
- package/src/OpenAPIPath.tsx +65 -0
- package/src/OpenAPIRequestBody.tsx +3 -14
- package/src/OpenAPIResponse.tsx +39 -43
- package/src/OpenAPIResponseExample.tsx +89 -31
- package/src/OpenAPIResponses.tsx +51 -15
- package/src/OpenAPISchema.test.ts +1 -1
- package/src/OpenAPISchema.tsx +124 -92
- package/src/OpenAPISchemaName.tsx +27 -0
- package/src/OpenAPISecurities.tsx +45 -24
- package/src/OpenAPIServerURL.tsx +17 -10
- package/src/OpenAPIServerURLVariable.tsx +2 -4
- package/src/OpenAPISpec.tsx +56 -53
- package/src/OpenAPITabs.tsx +113 -0
- package/src/ScalarApiButton.tsx +84 -7
- package/src/code-samples.test.ts +51 -0
- package/src/code-samples.ts +95 -31
- package/src/generateSchemaExample.ts +25 -151
- package/src/index.ts +3 -2
- package/src/resolveOpenAPIOperation.test.ts +177 -0
- package/src/resolveOpenAPIOperation.ts +163 -0
- package/src/stringifyOpenAPI.ts +6 -0
- package/src/types.ts +17 -10
- package/src/utils.ts +17 -2
- package/dist/InteractiveSection.js +0 -47
- package/dist/Markdown.js +0 -6
- package/dist/OpenAPICodeSample.js +0 -110
- package/dist/OpenAPIOperation.js +0 -38
- package/dist/OpenAPIRequestBody.js +0 -18
- package/dist/OpenAPIResponse.js +0 -32
- package/dist/OpenAPIResponseExample.js +0 -54
- package/dist/OpenAPIResponses.js +0 -18
- package/dist/OpenAPISchema.js +0 -235
- package/dist/OpenAPISchema.test.d.ts +0 -1
- package/dist/OpenAPISchema.test.js +0 -91
- package/dist/OpenAPISecurities.js +0 -42
- package/dist/OpenAPIServerURL.js +0 -51
- package/dist/OpenAPIServerURLVariable.js +0 -10
- package/dist/OpenAPISpec.js +0 -70
- package/dist/ScalarApiButton.js +0 -14
- package/dist/fetchOpenAPIOperation.test.d.ts +0 -1
- package/dist/fetchOpenAPIOperation.test.js +0 -152
- package/dist/resolveOpenAPIPath.d.ts +0 -7
- package/dist/resolveOpenAPIPath.js +0 -112
- package/dist/resolveOpenAPIPath.test.d.ts +0 -1
- package/dist/resolveOpenAPIPath.test.js +0 -39
- package/src/fetchOpenAPIOperation.test.ts +0 -185
- package/src/fetchOpenAPIOperation.ts +0 -230
- package/src/resolveOpenAPIPath.test.ts +0 -60
- package/src/resolveOpenAPIPath.ts +0 -145
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
+
};
|
|
10
|
+
import clsx from 'clsx';
|
|
11
|
+
import { useId } from 'react';
|
|
12
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
13
|
+
import { Markdown } from './Markdown';
|
|
14
|
+
import { checkIsReference, noReference, resolveDescription } from './utils';
|
|
15
|
+
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
|
16
|
+
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
|
17
|
+
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
|
18
|
+
/**
|
|
19
|
+
* Render a property of an OpenAPI schema.
|
|
20
|
+
*/
|
|
21
|
+
export function OpenAPISchemaProperty(props) {
|
|
22
|
+
var _a;
|
|
23
|
+
var schema = props.schema, _b = props.circularRefs, parentCircularRefs = _b === void 0 ? new Map() : _b, context = props.context, className = props.className;
|
|
24
|
+
var id = useId();
|
|
25
|
+
var parentCircularRef = parentCircularRefs.get(schema);
|
|
26
|
+
var circularRefs = new Map(parentCircularRefs).set(schema, id);
|
|
27
|
+
// Avoid recursing infinitely, and instead render a link to the parent schema
|
|
28
|
+
var properties = parentCircularRef ? null : getSchemaProperties(schema);
|
|
29
|
+
var alternatives = parentCircularRef
|
|
30
|
+
? null
|
|
31
|
+
: getSchemaAlternatives(schema, new Set(circularRefs.keys()));
|
|
32
|
+
if ((properties && !!properties.length) || schema.type === 'object') {
|
|
33
|
+
return (<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
|
|
34
|
+
<OpenAPISchemaPresentation {...props}/>
|
|
35
|
+
<OpenAPIDisclosure context={context}>
|
|
36
|
+
{properties && properties.length > 0 ? (<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>) : null}
|
|
37
|
+
</OpenAPIDisclosure>
|
|
38
|
+
</InteractiveSection>);
|
|
39
|
+
}
|
|
40
|
+
if ((_a = alternatives === null || alternatives === void 0 ? void 0 : alternatives[0]) === null || _a === void 0 ? void 0 : _a.length) {
|
|
41
|
+
return (<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
|
|
42
|
+
<OpenAPISchemaPresentation {...props}/>
|
|
43
|
+
{alternatives[0].map(function (alternative, index) { return (<OpenAPISchemaAlternative key={index} schema={alternative} circularRefs={circularRefs} context={context}/>); })}
|
|
44
|
+
</InteractiveSection>);
|
|
45
|
+
}
|
|
46
|
+
return (<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
|
|
47
|
+
<OpenAPISchemaPresentation {...props}/>
|
|
48
|
+
{(properties && properties.length > 0) ||
|
|
49
|
+
(schema.enum && schema.enum.length > 0) ||
|
|
50
|
+
parentCircularRef ? (<>
|
|
51
|
+
{(properties === null || properties === void 0 ? void 0 : properties.length) ? (<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>) : null}
|
|
52
|
+
{parentCircularRef ? (<OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>) : null}
|
|
53
|
+
</>) : null}
|
|
54
|
+
</InteractiveSection>);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Render a set of properties of an OpenAPI schema.
|
|
58
|
+
*/
|
|
59
|
+
export function OpenAPISchemaProperties(props) {
|
|
60
|
+
var id = props.id, properties = props.properties, circularRefs = props.circularRefs, context = props.context;
|
|
61
|
+
if (!properties.length) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return (<div id={id} className="openapi-schema-properties">
|
|
65
|
+
{properties.map(function (property) { return (<OpenAPISchemaProperty key={property.propertyName} circularRefs={circularRefs} {...property} context={context}/>); })}
|
|
66
|
+
</div>);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Render a root schema (such as the request body or response body).
|
|
70
|
+
*/
|
|
71
|
+
export function OpenAPIRootSchema(props) {
|
|
72
|
+
var schema = props.schema, context = props.context;
|
|
73
|
+
// Avoid recursing infinitely, and instead render a link to the parent schema
|
|
74
|
+
var properties = getSchemaProperties(schema);
|
|
75
|
+
if (properties && properties.length > 0) {
|
|
76
|
+
return <OpenAPISchemaProperties properties={properties} context={context}/>;
|
|
77
|
+
}
|
|
78
|
+
return (<OpenAPISchemaProperty schema={schema} context={context} className="openapi-schema-root"/>);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Render a tab for an alternative schema.
|
|
82
|
+
* It renders directly the properties if relevant;
|
|
83
|
+
* for primitives, it renders the schema itself.
|
|
84
|
+
*/
|
|
85
|
+
function OpenAPISchemaAlternative(props) {
|
|
86
|
+
var schema = props.schema, circularRefs = props.circularRefs, context = props.context;
|
|
87
|
+
var id = useId();
|
|
88
|
+
var subProperties = getSchemaProperties(schema);
|
|
89
|
+
return (<OpenAPIDisclosure context={context}>
|
|
90
|
+
<OpenAPISchemaProperties id={id} properties={subProperties !== null && subProperties !== void 0 ? subProperties : [{ schema: schema }]} circularRefs={subProperties ? new Map(circularRefs).set(schema, id) : circularRefs} context={context}/>
|
|
91
|
+
</OpenAPIDisclosure>);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Render a circular reference to a schema.
|
|
95
|
+
*/
|
|
96
|
+
function OpenAPISchemaCircularRef(props) {
|
|
97
|
+
var id = props.id, schema = props.schema;
|
|
98
|
+
return (<div className="openapi-schema-circular">
|
|
99
|
+
Circular reference to <a href={"#".concat(id)}>{getSchemaTitle(schema)}</a>{' '}
|
|
100
|
+
<span className="openapi-schema-circular-glyph">↩</span>
|
|
101
|
+
</div>);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Render the enum value for a schema.
|
|
105
|
+
*/
|
|
106
|
+
export function OpenAPISchemaEnum(props) {
|
|
107
|
+
var enumValues = props.enumValues;
|
|
108
|
+
return (<div className="openapi-schema-enum">
|
|
109
|
+
<span>
|
|
110
|
+
Options:{' '}
|
|
111
|
+
{enumValues.map(function (value, index) { return (<span key={index} className="openapi-schema-enum-value">
|
|
112
|
+
<code>{"".concat(value)}</code>
|
|
113
|
+
{index < enumValues.length - 1 ? ', ' : ''}
|
|
114
|
+
</span>); })}
|
|
115
|
+
</span>
|
|
116
|
+
</div>);
|
|
117
|
+
}
|
|
118
|
+
export function OpenAPISchemaPresentation(props) {
|
|
119
|
+
var schema = props.schema, propertyName = props.propertyName, required = props.required;
|
|
120
|
+
var shouldDisplayExample = function (schema) {
|
|
121
|
+
return (typeof schema.example === 'string' ||
|
|
122
|
+
typeof schema.example === 'number' ||
|
|
123
|
+
typeof schema.example === 'boolean' ||
|
|
124
|
+
(Array.isArray(schema.example) && schema.example.length > 0) ||
|
|
125
|
+
(typeof schema.example === 'object' &&
|
|
126
|
+
schema.example !== null &&
|
|
127
|
+
Object.keys(schema.example).length > 0));
|
|
128
|
+
};
|
|
129
|
+
var description = resolveDescription(schema);
|
|
130
|
+
return (<div className="openapi-schema-presentation">
|
|
131
|
+
<OpenAPISchemaName type={getSchemaTitle(schema)} propertyName={propertyName} required={required} deprecated={schema.deprecated}/>
|
|
132
|
+
{schema['x-deprecated-sunset'] ? (<div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
|
|
133
|
+
Sunset date:{' '}
|
|
134
|
+
<span className="openapi-deprecated-sunset-date">
|
|
135
|
+
{schema['x-deprecated-sunset']}
|
|
136
|
+
</span>
|
|
137
|
+
</div>) : null}
|
|
138
|
+
{description ? (<Markdown source={description} className="openapi-schema-description"/>) : null}
|
|
139
|
+
{shouldDisplayExample(schema) ? (<div className="openapi-schema-example">
|
|
140
|
+
Example:{' '}
|
|
141
|
+
<code>
|
|
142
|
+
{typeof schema.example === 'string'
|
|
143
|
+
? schema.example
|
|
144
|
+
: stringifyOpenAPI(schema.example)}
|
|
145
|
+
</code>
|
|
146
|
+
</div>) : null}
|
|
147
|
+
{schema.pattern ? (<div className="openapi-schema-pattern">
|
|
148
|
+
Pattern: <code>{schema.pattern}</code>
|
|
149
|
+
</div>) : null}
|
|
150
|
+
{schema.enum && schema.enum.length > 0 ? (<OpenAPISchemaEnum enumValues={schema.enum}/>) : null}
|
|
151
|
+
</div>);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get the sub-properties of a schema.
|
|
155
|
+
*/
|
|
156
|
+
function getSchemaProperties(schema) {
|
|
157
|
+
if (schema.allOf) {
|
|
158
|
+
return schema.allOf.reduce(function (acc, subSchema) {
|
|
159
|
+
var _a;
|
|
160
|
+
var properties = (_a = getSchemaProperties(noReference(subSchema))) !== null && _a !== void 0 ? _a : [
|
|
161
|
+
{
|
|
162
|
+
schema: noReference(subSchema),
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
return __spreadArray(__spreadArray([], acc, true), properties, true);
|
|
166
|
+
}, []);
|
|
167
|
+
}
|
|
168
|
+
// check array AND schema.items as this is sometimes null despite what the type indicates
|
|
169
|
+
if (schema.type === 'array' && !!schema.items) {
|
|
170
|
+
var items = noReference(schema.items);
|
|
171
|
+
var itemProperties = getSchemaProperties(items);
|
|
172
|
+
if (itemProperties) {
|
|
173
|
+
return itemProperties;
|
|
174
|
+
}
|
|
175
|
+
return [
|
|
176
|
+
{
|
|
177
|
+
propertyName: 'items',
|
|
178
|
+
schema: items,
|
|
179
|
+
},
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
if (schema.type === 'object' || schema.properties) {
|
|
183
|
+
var result_1 = [];
|
|
184
|
+
if (schema.properties) {
|
|
185
|
+
Object.entries(schema.properties).forEach(function (_a) {
|
|
186
|
+
var propertyName = _a[0], rawPropertySchema = _a[1];
|
|
187
|
+
var isReference = checkIsReference(rawPropertySchema);
|
|
188
|
+
var propertySchema = isReference
|
|
189
|
+
? { propertyName: propertyName }
|
|
190
|
+
: rawPropertySchema;
|
|
191
|
+
result_1.push({
|
|
192
|
+
propertyName: propertyName,
|
|
193
|
+
required: Array.isArray(schema.required)
|
|
194
|
+
? schema.required.includes(propertyName)
|
|
195
|
+
: undefined,
|
|
196
|
+
schema: propertySchema,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (schema.additionalProperties) {
|
|
201
|
+
var additionalProperties = noReference(schema.additionalProperties);
|
|
202
|
+
result_1.push({
|
|
203
|
+
propertyName: 'Other properties',
|
|
204
|
+
schema: additionalProperties === true ? {} : additionalProperties,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return result_1;
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Get the alternatives to display for a schema.
|
|
213
|
+
*/
|
|
214
|
+
export function getSchemaAlternatives(schema, ancestors) {
|
|
215
|
+
if (ancestors === void 0) { ancestors = new Set(); }
|
|
216
|
+
var downAncestors = new Set(ancestors).add(schema);
|
|
217
|
+
if (schema.anyOf) {
|
|
218
|
+
return [
|
|
219
|
+
flattenAlternatives('anyOf', schema.anyOf.map(noReference), downAncestors),
|
|
220
|
+
noReference(schema.discriminator),
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
if (schema.oneOf) {
|
|
224
|
+
return [
|
|
225
|
+
flattenAlternatives('oneOf', schema.oneOf.map(noReference), downAncestors),
|
|
226
|
+
noReference(schema.discriminator),
|
|
227
|
+
];
|
|
228
|
+
}
|
|
229
|
+
if (schema.allOf) {
|
|
230
|
+
// allOf is managed in `getSchemaProperties`
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
function flattenAlternatives(alternativeType, alternatives, ancestors) {
|
|
236
|
+
return alternatives.reduce(function (acc, alternative) {
|
|
237
|
+
var _a;
|
|
238
|
+
if (!!alternative[alternativeType] && !ancestors.has(alternative)) {
|
|
239
|
+
return __spreadArray(__spreadArray([], acc, true), (((_a = getSchemaAlternatives(alternative, ancestors)) === null || _a === void 0 ? void 0 : _a[0]) || []), true);
|
|
240
|
+
}
|
|
241
|
+
return __spreadArray(__spreadArray([], acc, true), [alternative], false);
|
|
242
|
+
}, []);
|
|
243
|
+
}
|
|
244
|
+
export function getSchemaTitle(schema,
|
|
245
|
+
/** If the title is inferred in a oneOf with discriminator, we can use it to optimize the title */
|
|
246
|
+
discriminator) {
|
|
247
|
+
var _a;
|
|
248
|
+
if (schema.title) {
|
|
249
|
+
// If the schema has a title, use it
|
|
250
|
+
return schema.title;
|
|
251
|
+
}
|
|
252
|
+
// Try using the discriminator
|
|
253
|
+
if ((discriminator === null || discriminator === void 0 ? void 0 : discriminator.propertyName) && schema.properties) {
|
|
254
|
+
var discriminatorProperty = noReference(schema.properties[discriminator.propertyName]);
|
|
255
|
+
if (discriminatorProperty) {
|
|
256
|
+
if (discriminatorProperty.enum) {
|
|
257
|
+
return discriminatorProperty.enum.map(function (value) { return value.toString(); }).join(' | ');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// Otherwise try to infer a nice title
|
|
262
|
+
var type = 'any';
|
|
263
|
+
if (schema.enum) {
|
|
264
|
+
type = 'enum';
|
|
265
|
+
// check array AND schema.items as this is sometimes null despite what the type indicates
|
|
266
|
+
}
|
|
267
|
+
else if (schema.type === 'array' && !!schema.items) {
|
|
268
|
+
type = "".concat(getSchemaTitle(noReference(schema.items)), "[]");
|
|
269
|
+
}
|
|
270
|
+
else if (Array.isArray(schema.type)) {
|
|
271
|
+
type = schema.type.join(' | ');
|
|
272
|
+
}
|
|
273
|
+
else if (schema.type || schema.properties) {
|
|
274
|
+
type = (_a = schema.type) !== null && _a !== void 0 ? _a : 'object';
|
|
275
|
+
if (schema.format) {
|
|
276
|
+
type += " ".concat(schema.format);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else if ('anyOf' in schema) {
|
|
280
|
+
type = 'any of';
|
|
281
|
+
}
|
|
282
|
+
else if ('oneOf' in schema) {
|
|
283
|
+
type = 'one of';
|
|
284
|
+
}
|
|
285
|
+
else if ('allOf' in schema) {
|
|
286
|
+
type = 'all of';
|
|
287
|
+
}
|
|
288
|
+
else if ('not' in schema) {
|
|
289
|
+
type = 'not';
|
|
290
|
+
}
|
|
291
|
+
if (schema.nullable) {
|
|
292
|
+
type = "nullable ".concat(type);
|
|
293
|
+
}
|
|
294
|
+
return type;
|
|
295
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface OpenAPISchemaNameProps {
|
|
2
|
+
propertyName?: string | JSX.Element;
|
|
3
|
+
required?: boolean;
|
|
4
|
+
type?: string;
|
|
5
|
+
deprecated?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Display the schema name row.
|
|
9
|
+
* It includes the property name, type, required and deprecated status.
|
|
10
|
+
*/
|
|
11
|
+
export declare function OpenAPISchemaName(props: OpenAPISchemaNameProps): JSX.Element;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Display the schema name row.
|
|
3
|
+
* It includes the property name, type, required and deprecated status.
|
|
4
|
+
*/
|
|
5
|
+
export function OpenAPISchemaName(props) {
|
|
6
|
+
var type = props.type, propertyName = props.propertyName, required = props.required, deprecated = props.deprecated;
|
|
7
|
+
return (<div className="openapi-schema-name">
|
|
8
|
+
{propertyName ? (<span data-deprecated={deprecated} className="openapi-schema-propertyname">
|
|
9
|
+
{propertyName}
|
|
10
|
+
</span>) : null}
|
|
11
|
+
{type ? <span className="openapi-schema-type">{type}</span> : null}
|
|
12
|
+
{required ? <span className="openapi-schema-required">required</span> : null}
|
|
13
|
+
{deprecated ? <span className="openapi-deprecated">Deprecated</span> : null}
|
|
14
|
+
</div>);
|
|
15
|
+
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OpenAPIClientContext } from './types';
|
|
3
|
-
import { OpenAPIOperationData } from './fetchOpenAPIOperation';
|
|
1
|
+
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
|
4
2
|
/**
|
|
5
3
|
* Present securities authorization that can be used for this operation.
|
|
6
4
|
*/
|
|
7
5
|
export declare function OpenAPISecurities(props: {
|
|
8
6
|
securities: OpenAPIOperationData['securities'];
|
|
9
7
|
context: OpenAPIClientContext;
|
|
10
|
-
}):
|
|
8
|
+
}): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
2
|
+
import { Markdown } from './Markdown';
|
|
3
|
+
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
|
4
|
+
import { resolveDescription } from './utils';
|
|
5
|
+
/**
|
|
6
|
+
* Present securities authorization that can be used for this operation.
|
|
7
|
+
*/
|
|
8
|
+
export function OpenAPISecurities(props) {
|
|
9
|
+
var securities = props.securities, context = props.context;
|
|
10
|
+
if (securities.length === 0) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return (<InteractiveSection header="Authorizations" toggeable defaultOpened={false} toggleIcon={context.icons.chevronRight} className="openapi-securities" tabs={securities.map(function (_a) {
|
|
14
|
+
var key = _a[0], security = _a[1];
|
|
15
|
+
var description = resolveDescription(security);
|
|
16
|
+
return {
|
|
17
|
+
key: key,
|
|
18
|
+
label: key,
|
|
19
|
+
body: (<div className="openapi-schema-body">
|
|
20
|
+
<div className="openapi-schema-presentation">
|
|
21
|
+
{getLabelForType(security)}
|
|
22
|
+
|
|
23
|
+
{description ? (<Markdown source={description} className="openapi-securities-description"/>) : null}
|
|
24
|
+
</div>
|
|
25
|
+
</div>),
|
|
26
|
+
};
|
|
27
|
+
})}/>);
|
|
28
|
+
}
|
|
29
|
+
function getLabelForType(security) {
|
|
30
|
+
var _a;
|
|
31
|
+
switch (security.type) {
|
|
32
|
+
case 'apiKey':
|
|
33
|
+
return (<OpenAPISchemaName propertyName={(_a = security.name) !== null && _a !== void 0 ? _a : 'apiKey'} type="string" required/>);
|
|
34
|
+
case 'http':
|
|
35
|
+
if (security.scheme === 'basic') {
|
|
36
|
+
return <OpenAPISchemaName propertyName="Authorization" type="string" required/>;
|
|
37
|
+
}
|
|
38
|
+
if (security.scheme == 'bearer') {
|
|
39
|
+
var description = resolveDescription(security);
|
|
40
|
+
return (<>
|
|
41
|
+
<OpenAPISchemaName propertyName="Authorization" type="string" required/>
|
|
42
|
+
{/** Show a default description if none is provided */}
|
|
43
|
+
{!description ? (<Markdown source={"Bearer authentication header of the form Bearer ".concat("<token>", ".")} className="openapi-securities-description"/>) : null}
|
|
44
|
+
</>);
|
|
45
|
+
}
|
|
46
|
+
return <OpenAPISchemaName propertyName="HTTP" required/>;
|
|
47
|
+
case 'oauth2':
|
|
48
|
+
return <OpenAPISchemaName propertyName="OAuth2" required/>;
|
|
49
|
+
case 'openIdConnect':
|
|
50
|
+
return <OpenAPISchemaName propertyName="OpenID Connect" required/>;
|
|
51
|
+
default:
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
return security.type;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OpenAPIV3 } from 'openapi-types';
|
|
1
|
+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
3
2
|
/**
|
|
4
3
|
* Show the url of the server with variables replaced by their default values.
|
|
5
4
|
*/
|
|
6
5
|
export declare function OpenAPIServerURL(props: {
|
|
7
6
|
servers: OpenAPIV3.ServerObject[];
|
|
8
|
-
}):
|
|
7
|
+
}): import("react").JSX.Element | null;
|
|
9
8
|
/**
|
|
10
9
|
* Get the default URL for the server.
|
|
11
10
|
*/
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { OpenAPIServerURLVariable } from './OpenAPIServerURLVariable';
|
|
2
|
+
/**
|
|
3
|
+
* Show the url of the server with variables replaced by their default values.
|
|
4
|
+
*/
|
|
5
|
+
export function OpenAPIServerURL(props) {
|
|
6
|
+
var _a;
|
|
7
|
+
var servers = props.servers;
|
|
8
|
+
var server = servers[0];
|
|
9
|
+
if (!server) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
var parts = parseServerURL((_a = server === null || server === void 0 ? void 0 : server.url) !== null && _a !== void 0 ? _a : '');
|
|
13
|
+
return (<span>
|
|
14
|
+
{parts.map(function (part, i) {
|
|
15
|
+
var _a;
|
|
16
|
+
if (part.kind === 'text') {
|
|
17
|
+
return <span key={i}>{part.text}</span>;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
var variable = (_a = server.variables) === null || _a === void 0 ? void 0 : _a[part.name];
|
|
21
|
+
if (!variable) {
|
|
22
|
+
return <span key={i}>{"{".concat(part.name, "}")}</span>;
|
|
23
|
+
}
|
|
24
|
+
return (<OpenAPIServerURLVariable key={i} name={part.name} variable={variable}/>);
|
|
25
|
+
}
|
|
26
|
+
})}
|
|
27
|
+
</span>);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the default URL for the server.
|
|
31
|
+
*/
|
|
32
|
+
export function getServersURL(servers) {
|
|
33
|
+
var _a;
|
|
34
|
+
var server = servers[0];
|
|
35
|
+
if (!server) {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
var parts = parseServerURL((_a = server === null || server === void 0 ? void 0 : server.url) !== null && _a !== void 0 ? _a : '');
|
|
39
|
+
return parts
|
|
40
|
+
.map(function (part) {
|
|
41
|
+
var _a, _b, _c;
|
|
42
|
+
if (part.kind === 'text') {
|
|
43
|
+
return part.text;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return (_c = (_b = (_a = server.variables) === null || _a === void 0 ? void 0 : _a[part.name]) === null || _b === void 0 ? void 0 : _b.default) !== null && _c !== void 0 ? _c : "{".concat(part.name, "}");
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
.join('');
|
|
50
|
+
}
|
|
51
|
+
function parseServerURL(url) {
|
|
52
|
+
var parts = url.split(/{([^}]+)}/g);
|
|
53
|
+
var result = [];
|
|
54
|
+
for (var i = 0; i < parts.length; i++) {
|
|
55
|
+
var part = parts[i];
|
|
56
|
+
if (!part) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (i % 2 === 0) {
|
|
60
|
+
result.push({ kind: 'text', text: part });
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
result.push({ kind: 'variable', name: part });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OpenAPIV3 } from 'openapi-types';
|
|
1
|
+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
3
2
|
/**
|
|
4
3
|
* Interactive component to show the value of a server variable and let the user change it.
|
|
5
4
|
*/
|
|
6
5
|
export declare function OpenAPIServerURLVariable(props: {
|
|
7
6
|
name: string;
|
|
8
7
|
variable: OpenAPIV3.ServerVariableObject;
|
|
9
|
-
}):
|
|
8
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* Interactive component to show the value of a server variable and let the user change it.
|
|
4
|
+
*/
|
|
5
|
+
export function OpenAPIServerURLVariable(props) {
|
|
6
|
+
var variable = props.variable;
|
|
7
|
+
return <span className="openapi-url-var">{variable.default}</span>;
|
|
8
|
+
}
|
package/dist/OpenAPISpec.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OpenAPIClientContext } from './types';
|
|
1
|
+
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
|
3
2
|
/**
|
|
4
3
|
* Client component to render the spec for the request and response.
|
|
5
4
|
*
|
|
@@ -7,6 +6,6 @@ import { OpenAPIClientContext } from './types';
|
|
|
7
6
|
* (the entire schema is rendered at once, while the client component only renders the visible part)
|
|
8
7
|
*/
|
|
9
8
|
export declare function OpenAPISpec(props: {
|
|
10
|
-
|
|
9
|
+
data: OpenAPIOperationData;
|
|
11
10
|
context: OpenAPIClientContext;
|
|
12
|
-
}):
|
|
11
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
14
|
+
import { OpenAPIRequestBody } from './OpenAPIRequestBody';
|
|
15
|
+
import { OpenAPIResponses } from './OpenAPIResponses';
|
|
16
|
+
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
|
17
|
+
import { OpenAPISecurities } from './OpenAPISecurities';
|
|
18
|
+
import { noReference, resolveDescription } from './utils';
|
|
19
|
+
/**
|
|
20
|
+
* Client component to render the spec for the request and response.
|
|
21
|
+
*
|
|
22
|
+
* We use a client component as rendering recursive JSON schema in the server is expensive
|
|
23
|
+
* (the entire schema is rendered at once, while the client component only renders the visible part)
|
|
24
|
+
*/
|
|
25
|
+
export function OpenAPISpec(props) {
|
|
26
|
+
var _a;
|
|
27
|
+
var data = props.data, context = props.context;
|
|
28
|
+
var operation = data.operation, securities = data.securities;
|
|
29
|
+
var parameters = (_a = operation.parameters) !== null && _a !== void 0 ? _a : [];
|
|
30
|
+
var parameterGroups = groupParameters(parameters);
|
|
31
|
+
return (<>
|
|
32
|
+
{securities.length > 0 ? (<OpenAPISecurities securities={securities} context={context}/>) : null}
|
|
33
|
+
|
|
34
|
+
{parameterGroups.map(function (group) {
|
|
35
|
+
return (<InteractiveSection key={group.key} className="openapi-parameters" header={group.label}>
|
|
36
|
+
<OpenAPISchemaProperties properties={group.parameters.map(function (parameter) {
|
|
37
|
+
var _a;
|
|
38
|
+
var description = resolveDescription(parameter);
|
|
39
|
+
return {
|
|
40
|
+
propertyName: parameter.name,
|
|
41
|
+
schema: __assign({
|
|
42
|
+
// Description of the parameter is defined at the parameter level
|
|
43
|
+
// we use display it if the schema doesn't override it
|
|
44
|
+
description: description, example: parameter.example,
|
|
45
|
+
// Deprecated can be defined at the parameter level
|
|
46
|
+
deprecated: parameter.deprecated }, ((_a = noReference(parameter.schema)) !== null && _a !== void 0 ? _a : {})),
|
|
47
|
+
required: parameter.required,
|
|
48
|
+
};
|
|
49
|
+
})} context={context}/>
|
|
50
|
+
</InteractiveSection>);
|
|
51
|
+
})}
|
|
52
|
+
|
|
53
|
+
{operation.requestBody ? (<OpenAPIRequestBody requestBody={noReference(operation.requestBody)} context={context}/>) : null}
|
|
54
|
+
{operation.responses ? (<OpenAPIResponses responses={noReference(operation.responses)} context={context}/>) : null}
|
|
55
|
+
</>);
|
|
56
|
+
}
|
|
57
|
+
function groupParameters(parameters) {
|
|
58
|
+
var sorted = ['path', 'query', 'header'];
|
|
59
|
+
var groups = [];
|
|
60
|
+
parameters
|
|
61
|
+
.filter(function (parameter) { return parameter.in; })
|
|
62
|
+
.forEach(function (parameter) {
|
|
63
|
+
var key = parameter.in;
|
|
64
|
+
var label = getParameterGroupName(parameter.in);
|
|
65
|
+
var group = groups.find(function (group) { return group.key === key; });
|
|
66
|
+
if (group) {
|
|
67
|
+
group.parameters.push(parameter);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
groups.push({
|
|
71
|
+
key: key,
|
|
72
|
+
label: label,
|
|
73
|
+
parameters: [parameter],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
groups.sort(function (a, b) { return sorted.indexOf(a.key) - sorted.indexOf(b.key); });
|
|
78
|
+
return groups;
|
|
79
|
+
}
|
|
80
|
+
function getParameterGroupName(paramIn) {
|
|
81
|
+
switch (paramIn) {
|
|
82
|
+
case 'path':
|
|
83
|
+
return 'Path parameters';
|
|
84
|
+
case 'query':
|
|
85
|
+
return 'Query parameters';
|
|
86
|
+
case 'header':
|
|
87
|
+
return 'Header parameters';
|
|
88
|
+
default:
|
|
89
|
+
return paramIn;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Key, TabsProps } from 'react-aria-components';
|
|
2
|
+
export type Tab = {
|
|
3
|
+
key: Key;
|
|
4
|
+
label: string;
|
|
5
|
+
body: React.ReactNode;
|
|
6
|
+
description?: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The OpenAPI Tabs wrapper component.
|
|
10
|
+
*/
|
|
11
|
+
export declare function OpenAPITabs(props: React.PropsWithChildren<TabsProps & {
|
|
12
|
+
items: Tab[];
|
|
13
|
+
}>): import("react").JSX.Element;
|
|
14
|
+
/**
|
|
15
|
+
* The OpenAPI Tabs list component.
|
|
16
|
+
* This component should be used as a child of the OpenAPITabs component.
|
|
17
|
+
* It renders the list of tabs.
|
|
18
|
+
*/
|
|
19
|
+
export declare function OpenAPITabsList(): import("react").JSX.Element;
|
|
20
|
+
/**
|
|
21
|
+
* The OpenAPI Tabs panels component.
|
|
22
|
+
* This component should be used as a child of the OpenAPITabs component.
|
|
23
|
+
* It renders the content of the selected tab.
|
|
24
|
+
*/
|
|
25
|
+
export declare function OpenAPITabsPanels(): import("react").JSX.Element | null;
|