@gitbook/react-openapi 0.4.0 → 0.5.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 +6 -0
- package/dist/InteractiveSection.d.ts +31 -0
- package/dist/InteractiveSection.js +32 -0
- package/dist/Markdown.d.ts +5 -0
- package/dist/Markdown.js +6 -0
- package/dist/OpenAPICodeSample.d.ts +11 -0
- package/dist/OpenAPICodeSample.js +78 -0
- package/dist/OpenAPIOperation.d.ts +11 -0
- package/dist/OpenAPIOperation.js +37 -0
- package/dist/OpenAPIRequestBody.d.ts +10 -0
- package/dist/OpenAPIRequestBody.js +18 -0
- package/dist/OpenAPIResponse.d.ts +10 -0
- package/dist/OpenAPIResponse.js +32 -0
- package/dist/OpenAPIResponseExample.d.ts +10 -0
- package/dist/OpenAPIResponseExample.js +48 -0
- package/dist/OpenAPIResponses.d.ts +10 -0
- package/dist/OpenAPIResponses.js +18 -0
- package/dist/OpenAPISchema.d.ts +45 -0
- package/dist/OpenAPISchema.js +232 -0
- package/dist/OpenAPISchema.test.d.ts +1 -0
- package/dist/OpenAPISchema.test.js +91 -0
- package/dist/OpenAPISecurities.d.ts +10 -0
- package/dist/OpenAPISecurities.js +42 -0
- package/dist/OpenAPIServerURL.d.ts +12 -0
- package/dist/OpenAPIServerURL.js +51 -0
- package/dist/OpenAPIServerURLVariable.d.ts +9 -0
- package/dist/OpenAPIServerURLVariable.js +10 -0
- package/dist/OpenAPISpec.d.ts +12 -0
- package/dist/OpenAPISpec.js +70 -0
- package/dist/ScalarApiButton.d.ts +14 -0
- package/dist/ScalarApiButton.js +89 -0
- package/dist/code-samples.d.ts +14 -0
- package/dist/code-samples.js +50 -0
- package/dist/fetchOpenAPIOperation.d.ts +72 -0
- package/dist/fetchOpenAPIOperation.js +124 -0
- package/dist/fetchOpenAPIOperation.test.d.ts +1 -0
- package/dist/fetchOpenAPIOperation.test.js +152 -0
- package/dist/generateSchemaExample.d.ts +17 -0
- package/dist/generateSchemaExample.js +119 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/resolveOpenAPIPath.d.ts +7 -0
- package/dist/resolveOpenAPIPath.js +112 -0
- package/dist/resolveOpenAPIPath.test.d.ts +1 -0
- package/dist/resolveOpenAPIPath.test.js +39 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +6 -0
- package/package.json +8 -2
- package/.turbo/turbo-build.log +0 -1
- package/tsconfig.json +0 -24
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import React, { useId } from 'react';
|
|
3
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
4
|
+
import { Markdown } from './Markdown';
|
|
5
|
+
import { SYMBOL_REF_RESOLVED } from './resolveOpenAPIPath';
|
|
6
|
+
import { noReference } from './utils';
|
|
7
|
+
/**
|
|
8
|
+
* Render a property of an OpenAPI schema.
|
|
9
|
+
*/
|
|
10
|
+
export function OpenAPISchemaProperty(props) {
|
|
11
|
+
const { propertyName, required, schema, circularRefs: parentCircularRefs = new Map(), context, className, } = props;
|
|
12
|
+
const id = useId();
|
|
13
|
+
const parentCircularRef = parentCircularRefs.get(schema);
|
|
14
|
+
const circularRefs = new Map(parentCircularRefs).set(schema, id);
|
|
15
|
+
// Avoid recursing infinitely, and instead render a link to the parent schema
|
|
16
|
+
const properties = parentCircularRef ? null : getSchemaProperties(schema);
|
|
17
|
+
const alternatives = parentCircularRef
|
|
18
|
+
? null
|
|
19
|
+
: getSchemaAlternatives(schema, new Set(circularRefs.keys()));
|
|
20
|
+
const shouldDisplayExample = (schema) => {
|
|
21
|
+
return (typeof schema.example === 'string' ||
|
|
22
|
+
typeof schema.example === 'number' ||
|
|
23
|
+
typeof schema.example === 'boolean');
|
|
24
|
+
};
|
|
25
|
+
return (React.createElement(InteractiveSection, { id: id, className: classNames('openapi-schema', className), toggeable: !!properties || !!alternatives, defaultOpened: !!context.defaultInteractiveOpened, toggleOpenIcon: context.icons.chevronRight, toggleCloseIcon: context.icons.chevronDown, tabs: alternatives?.[0].map((alternative, index) => ({
|
|
26
|
+
key: `${index}`,
|
|
27
|
+
label: getSchemaTitle(alternative, alternatives[1]),
|
|
28
|
+
body: circularRefs.has(alternative) ? (React.createElement(OpenAPISchemaCircularRef, { id: circularRefs.get(alternative), schema: alternative })) : (React.createElement(OpenAPISchemaAlternative, { schema: alternative, circularRefs: circularRefs, context: context })),
|
|
29
|
+
})), header: React.createElement("div", { className: classNames('openapi-schema-presentation') },
|
|
30
|
+
React.createElement("div", { className: classNames('openapi-schema-name') },
|
|
31
|
+
propertyName ? (React.createElement("span", { className: classNames('openapi-schema-propertyname') }, propertyName)) : null,
|
|
32
|
+
required ? (React.createElement("span", { className: classNames('openapi-schema-required') }, "*")) : null,
|
|
33
|
+
React.createElement("span", { className: classNames('openapi-schema-type') }, getSchemaTitle(schema))),
|
|
34
|
+
schema.description ? (React.createElement(Markdown, { source: schema.description, className: "openapi-schema-description" })) : null,
|
|
35
|
+
shouldDisplayExample(schema) ? (React.createElement("span", { className: "openapi-schema-example" },
|
|
36
|
+
"Example: ",
|
|
37
|
+
React.createElement("code", null, JSON.stringify(schema.example)))) : null) }, (properties && properties.length > 0) ||
|
|
38
|
+
(schema.enum && schema.enum.length > 0) ||
|
|
39
|
+
parentCircularRef ? (React.createElement(React.Fragment, null,
|
|
40
|
+
properties?.length ? (React.createElement(OpenAPISchemaProperties, { properties: properties, circularRefs: circularRefs, context: context })) : null,
|
|
41
|
+
schema.enum && schema.enum.length > 0 ? (React.createElement(OpenAPISchemaEnum, { enumValues: schema.enum })) : null,
|
|
42
|
+
parentCircularRef ? (React.createElement(OpenAPISchemaCircularRef, { id: parentCircularRef, schema: schema })) : null)) : null));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Render a set of properties of an OpenAPI schema.
|
|
46
|
+
*/
|
|
47
|
+
export function OpenAPISchemaProperties(props) {
|
|
48
|
+
const { id, properties, circularRefs, context } = props;
|
|
49
|
+
if (!properties.length) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
return (React.createElement("div", { id: id, className: classNames('openapi-schema-properties') }, properties.map((property) => (React.createElement(OpenAPISchemaProperty, { key: property.propertyName, circularRefs: circularRefs, ...property, context: context })))));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Render a root schema (such as the request body or response body).
|
|
56
|
+
*/
|
|
57
|
+
export function OpenAPIRootSchema(props) {
|
|
58
|
+
const { schema, context } = props;
|
|
59
|
+
// Avoid recursing infinitely, and instead render a link to the parent schema
|
|
60
|
+
const properties = getSchemaProperties(schema);
|
|
61
|
+
if (properties && properties.length > 0) {
|
|
62
|
+
return React.createElement(OpenAPISchemaProperties, { properties: properties, context: context });
|
|
63
|
+
}
|
|
64
|
+
return (React.createElement(OpenAPISchemaProperty, { schema: schema, context: context, className: "openapi-schema-root" }));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Render a tab for an alternative schema.
|
|
68
|
+
* It renders directly the properties if relevant;
|
|
69
|
+
* for primitives, it renders the schema itself.
|
|
70
|
+
*/
|
|
71
|
+
function OpenAPISchemaAlternative(props) {
|
|
72
|
+
const { schema, circularRefs, context } = props;
|
|
73
|
+
const id = useId();
|
|
74
|
+
const subProperties = getSchemaProperties(schema);
|
|
75
|
+
return (React.createElement(OpenAPISchemaProperties, { id: id, properties: subProperties ?? [{ schema }], circularRefs: subProperties ? new Map(circularRefs).set(schema, id) : circularRefs, context: context }));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Render a circular reference to a schema.
|
|
79
|
+
*/
|
|
80
|
+
function OpenAPISchemaCircularRef(props) {
|
|
81
|
+
const { id, schema } = props;
|
|
82
|
+
return (React.createElement("div", { className: "openapi-schema-circular" },
|
|
83
|
+
"Circular reference to ",
|
|
84
|
+
React.createElement("a", { href: `#${id}` }, getSchemaTitle(schema)),
|
|
85
|
+
' ',
|
|
86
|
+
React.createElement("span", { className: "openapi-schema-circular-glyph" }, "\u21A9")));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Render the enum value for a schema.
|
|
90
|
+
*/
|
|
91
|
+
export function OpenAPISchemaEnum(props) {
|
|
92
|
+
const { enumValues } = props;
|
|
93
|
+
return (React.createElement("div", { className: "openapi-schema-enum" }, enumValues.map((value, index) => (React.createElement("span", { key: index, className: "openapi-schema-enum-value" }, `${value}`)))));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the sub-properties of a schema.
|
|
97
|
+
*/
|
|
98
|
+
function getSchemaProperties(schema) {
|
|
99
|
+
if (schema.allOf) {
|
|
100
|
+
return schema.allOf.reduce((acc, subSchema) => {
|
|
101
|
+
const properties = getSchemaProperties(noReference(subSchema)) ?? [
|
|
102
|
+
{
|
|
103
|
+
schema: noReference(subSchema),
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
return [...acc, ...properties];
|
|
107
|
+
}, []);
|
|
108
|
+
}
|
|
109
|
+
// check array AND schema.items as this is sometimes null despite what the type indicates
|
|
110
|
+
if (schema.type === 'array' && !!schema.items) {
|
|
111
|
+
const items = noReference(schema.items);
|
|
112
|
+
const itemProperties = getSchemaProperties(items);
|
|
113
|
+
if (itemProperties) {
|
|
114
|
+
return itemProperties;
|
|
115
|
+
}
|
|
116
|
+
return [
|
|
117
|
+
{
|
|
118
|
+
propertyName: 'items',
|
|
119
|
+
schema: items,
|
|
120
|
+
},
|
|
121
|
+
];
|
|
122
|
+
}
|
|
123
|
+
if (schema.type === 'object' || schema.properties) {
|
|
124
|
+
const result = [];
|
|
125
|
+
if (schema.properties) {
|
|
126
|
+
Object.entries(schema.properties).forEach(([propertyName, rawPropertySchema]) => {
|
|
127
|
+
const propertySchema = noReference(rawPropertySchema);
|
|
128
|
+
if (propertySchema.deprecated) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
result.push({
|
|
132
|
+
propertyName,
|
|
133
|
+
required: Array.isArray(schema.required)
|
|
134
|
+
? schema.required.includes(propertyName)
|
|
135
|
+
: undefined,
|
|
136
|
+
schema: propertySchema,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (schema.additionalProperties) {
|
|
141
|
+
const additionalProperties = noReference(schema.additionalProperties);
|
|
142
|
+
result.push({
|
|
143
|
+
propertyName: 'Other properties',
|
|
144
|
+
schema: additionalProperties === true ? {} : additionalProperties,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the alternatives to display for a schema.
|
|
153
|
+
*/
|
|
154
|
+
export function getSchemaAlternatives(schema, ancestors = new Set()) {
|
|
155
|
+
const downAncestors = new Set(ancestors).add(schema);
|
|
156
|
+
if (schema.anyOf) {
|
|
157
|
+
return [
|
|
158
|
+
flattenAlternatives('anyOf', schema.anyOf.map(noReference), downAncestors),
|
|
159
|
+
noReference(schema.discriminator),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
if (schema.oneOf) {
|
|
163
|
+
return [
|
|
164
|
+
flattenAlternatives('oneOf', schema.oneOf.map(noReference), downAncestors),
|
|
165
|
+
noReference(schema.discriminator),
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
if (schema.allOf) {
|
|
169
|
+
// allOf is managed in `getSchemaProperties`
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
function flattenAlternatives(alternativeType, alternatives, ancestors) {
|
|
175
|
+
return alternatives.reduce((acc, alternative) => {
|
|
176
|
+
if (!!alternative[alternativeType] && !ancestors.has(alternative)) {
|
|
177
|
+
return [...acc, ...(getSchemaAlternatives(alternative, ancestors)?.[0] || [])];
|
|
178
|
+
}
|
|
179
|
+
return [...acc, alternative];
|
|
180
|
+
}, []);
|
|
181
|
+
}
|
|
182
|
+
function getSchemaTitle(schema,
|
|
183
|
+
/** If the title is inferred in a oneOf with discriminator, we can use it to optimize the title */
|
|
184
|
+
discriminator) {
|
|
185
|
+
if (schema.title) {
|
|
186
|
+
// If the schema has a title, use it
|
|
187
|
+
return schema.title;
|
|
188
|
+
}
|
|
189
|
+
// Try using the discriminator
|
|
190
|
+
if (discriminator && schema.properties) {
|
|
191
|
+
const discriminatorProperty = noReference(schema.properties[discriminator.propertyName]);
|
|
192
|
+
if (discriminatorProperty) {
|
|
193
|
+
if (discriminatorProperty.enum) {
|
|
194
|
+
return discriminatorProperty.enum.map((value) => value.toString()).join(' | ');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// Otherwise try to infer a nice title
|
|
199
|
+
let type = 'any';
|
|
200
|
+
if (schema.enum) {
|
|
201
|
+
type = 'enum';
|
|
202
|
+
// check array AND schema.items as this is sometimes null despite what the type indicates
|
|
203
|
+
}
|
|
204
|
+
else if (schema.type === 'array' && !!schema.items) {
|
|
205
|
+
type = `array of ${getSchemaTitle(noReference(schema.items))}`;
|
|
206
|
+
}
|
|
207
|
+
else if (schema.type || schema.properties) {
|
|
208
|
+
type = schema.type ?? 'object';
|
|
209
|
+
if (schema.format) {
|
|
210
|
+
type += ` (${schema.format})`;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else if ('anyOf' in schema) {
|
|
214
|
+
type = 'any of';
|
|
215
|
+
}
|
|
216
|
+
else if ('oneOf' in schema) {
|
|
217
|
+
type = 'one of';
|
|
218
|
+
}
|
|
219
|
+
else if ('allOf' in schema) {
|
|
220
|
+
type = 'all of';
|
|
221
|
+
}
|
|
222
|
+
else if ('not' in schema) {
|
|
223
|
+
type = 'not';
|
|
224
|
+
}
|
|
225
|
+
if (SYMBOL_REF_RESOLVED in schema) {
|
|
226
|
+
type = `${schema[SYMBOL_REF_RESOLVED]} (${type})`;
|
|
227
|
+
}
|
|
228
|
+
if (schema.nullable) {
|
|
229
|
+
type = `nullable ${type}`;
|
|
230
|
+
}
|
|
231
|
+
return type;
|
|
232
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { it, describe, expect } from 'bun:test';
|
|
2
|
+
import { getSchemaAlternatives } from './OpenAPISchema';
|
|
3
|
+
describe('getSchemaAlternatives', () => {
|
|
4
|
+
it('should flatten oneOf', () => {
|
|
5
|
+
expect(getSchemaAlternatives({
|
|
6
|
+
oneOf: [
|
|
7
|
+
{
|
|
8
|
+
oneOf: [
|
|
9
|
+
{
|
|
10
|
+
type: 'number',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
type: 'boolean',
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
type: 'string',
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
})).toEqual([
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
type: 'number',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: 'string',
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
undefined,
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
it('should not flatten oneOf and allOf', () => {
|
|
37
|
+
expect(getSchemaAlternatives({
|
|
38
|
+
oneOf: [
|
|
39
|
+
{
|
|
40
|
+
allOf: [
|
|
41
|
+
{
|
|
42
|
+
type: 'number',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: 'boolean',
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: 'string',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
})).toEqual([
|
|
54
|
+
[
|
|
55
|
+
{
|
|
56
|
+
allOf: [
|
|
57
|
+
{
|
|
58
|
+
type: 'number',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
type: 'boolean',
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
type: 'string',
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
undefined,
|
|
70
|
+
]);
|
|
71
|
+
});
|
|
72
|
+
it('should stop at circular references', () => {
|
|
73
|
+
const a = {
|
|
74
|
+
anyOf: [
|
|
75
|
+
{
|
|
76
|
+
type: 'string',
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
a.anyOf.push(a);
|
|
81
|
+
expect(getSchemaAlternatives(a)).toEqual([
|
|
82
|
+
[
|
|
83
|
+
{
|
|
84
|
+
type: 'string',
|
|
85
|
+
},
|
|
86
|
+
a,
|
|
87
|
+
],
|
|
88
|
+
undefined,
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OpenAPIClientContext } from './types';
|
|
3
|
+
import { OpenAPIOperationData } from './fetchOpenAPIOperation';
|
|
4
|
+
/**
|
|
5
|
+
* Present securities authorization that can be used for this operation.
|
|
6
|
+
*/
|
|
7
|
+
export declare function OpenAPISecurities(props: {
|
|
8
|
+
securities: OpenAPIOperationData['securities'];
|
|
9
|
+
context: OpenAPIClientContext;
|
|
10
|
+
}): React.JSX.Element | null;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
3
|
+
import { Markdown } from './Markdown';
|
|
4
|
+
/**
|
|
5
|
+
* Present securities authorization that can be used for this operation.
|
|
6
|
+
*/
|
|
7
|
+
export function OpenAPISecurities(props) {
|
|
8
|
+
const { securities, context } = props;
|
|
9
|
+
if (securities.length === 0) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return (React.createElement(InteractiveSection, { header: "Authorization", className: "openapi-securities", toggeable: true, defaultOpened: false, toggleCloseIcon: context.icons.chevronDown, toggleOpenIcon: context.icons.chevronRight, tabs: securities.map(([key, security]) => {
|
|
13
|
+
return {
|
|
14
|
+
key: key,
|
|
15
|
+
label: key,
|
|
16
|
+
body: (React.createElement(React.Fragment, null,
|
|
17
|
+
React.createElement("p", { className: "openapi-securities-label" }, getLabelForType(security)),
|
|
18
|
+
security.description ? (React.createElement(Markdown, { source: security.description, className: "openapi-securities-description" })) : null)),
|
|
19
|
+
};
|
|
20
|
+
}) }));
|
|
21
|
+
}
|
|
22
|
+
function getLabelForType(security) {
|
|
23
|
+
switch (security.type) {
|
|
24
|
+
case 'apiKey':
|
|
25
|
+
return 'API Key';
|
|
26
|
+
case 'http':
|
|
27
|
+
if (security.scheme === 'basic') {
|
|
28
|
+
return 'Basic Auth';
|
|
29
|
+
}
|
|
30
|
+
if (security.scheme == 'bearer') {
|
|
31
|
+
return `Bearer Token ${security.bearerFormat ? `(${security.bearerFormat})` : ''}`;
|
|
32
|
+
}
|
|
33
|
+
return 'HTTP';
|
|
34
|
+
case 'oauth2':
|
|
35
|
+
return 'OAuth2';
|
|
36
|
+
case 'openIdConnect':
|
|
37
|
+
return 'OpenID Connect';
|
|
38
|
+
default:
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
return security.type;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
3
|
+
/**
|
|
4
|
+
* Show the url of the server with variables replaced by their default values.
|
|
5
|
+
*/
|
|
6
|
+
export declare function OpenAPIServerURL(props: {
|
|
7
|
+
servers: OpenAPIV3.ServerObject[];
|
|
8
|
+
}): React.JSX.Element;
|
|
9
|
+
/**
|
|
10
|
+
* Get the default URL for the server.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getServersURL(servers: OpenAPIV3.ServerObject[]): string;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OpenAPIServerURLVariable } from './OpenAPIServerURLVariable';
|
|
3
|
+
/**
|
|
4
|
+
* Show the url of the server with variables replaced by their default values.
|
|
5
|
+
*/
|
|
6
|
+
export function OpenAPIServerURL(props) {
|
|
7
|
+
const { servers } = props;
|
|
8
|
+
const server = servers[0];
|
|
9
|
+
const parts = parseServerURL(server?.url ?? '');
|
|
10
|
+
return (React.createElement("span", null, parts.map((part, i) => {
|
|
11
|
+
if (part.kind === 'text') {
|
|
12
|
+
return React.createElement("span", { key: i }, part.text);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
if (!server.variables?.[part.name]) {
|
|
16
|
+
return React.createElement("span", { key: i }, `{${part.name}}`);
|
|
17
|
+
}
|
|
18
|
+
return (React.createElement(OpenAPIServerURLVariable, { key: i, name: part.name, variable: server.variables[part.name] }));
|
|
19
|
+
}
|
|
20
|
+
})));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the default URL for the server.
|
|
24
|
+
*/
|
|
25
|
+
export function getServersURL(servers) {
|
|
26
|
+
const server = servers[0];
|
|
27
|
+
const parts = parseServerURL(server?.url ?? '');
|
|
28
|
+
return parts
|
|
29
|
+
.map((part) => {
|
|
30
|
+
if (part.kind === 'text') {
|
|
31
|
+
return part.text;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
.join('');
|
|
38
|
+
}
|
|
39
|
+
function parseServerURL(url) {
|
|
40
|
+
const parts = url.split(/{([^}]+)}/g);
|
|
41
|
+
const result = [];
|
|
42
|
+
for (let i = 0; i < parts.length; i++) {
|
|
43
|
+
if (i % 2 === 0) {
|
|
44
|
+
result.push({ kind: 'text', text: parts[i] });
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
result.push({ kind: 'variable', name: parts[i] });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
3
|
+
/**
|
|
4
|
+
* Interactive component to show the value of a server variable and let the user change it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function OpenAPIServerURLVariable(props: {
|
|
7
|
+
name: string;
|
|
8
|
+
variable: OpenAPIV3.ServerVariableObject;
|
|
9
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
/**
|
|
5
|
+
* Interactive component to show the value of a server variable and let the user change it.
|
|
6
|
+
*/
|
|
7
|
+
export function OpenAPIServerURLVariable(props) {
|
|
8
|
+
const { variable } = props;
|
|
9
|
+
return React.createElement("span", { className: classNames('openapi-url-var') }, variable.default);
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { OpenAPIClientContext } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Client component to render the spec for the request and response.
|
|
5
|
+
*
|
|
6
|
+
* We use a client component as rendering recursive JSON schema in the server is expensive
|
|
7
|
+
* (the entire schema is rendered at once, while the client component only renders the visible part)
|
|
8
|
+
*/
|
|
9
|
+
export declare function OpenAPISpec(props: {
|
|
10
|
+
rawData: any;
|
|
11
|
+
context: OpenAPIClientContext;
|
|
12
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { fromJSON } from './fetchOpenAPIOperation';
|
|
4
|
+
import { InteractiveSection } from './InteractiveSection';
|
|
5
|
+
import { OpenAPIRequestBody } from './OpenAPIRequestBody';
|
|
6
|
+
import { OpenAPIResponses } from './OpenAPIResponses';
|
|
7
|
+
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
|
8
|
+
import { OpenAPISecurities } from './OpenAPISecurities';
|
|
9
|
+
import { noReference } from './utils';
|
|
10
|
+
/**
|
|
11
|
+
* Client component to render the spec for the request and response.
|
|
12
|
+
*
|
|
13
|
+
* We use a client component as rendering recursive JSON schema in the server is expensive
|
|
14
|
+
* (the entire schema is rendered at once, while the client component only renders the visible part)
|
|
15
|
+
*/
|
|
16
|
+
export function OpenAPISpec(props) {
|
|
17
|
+
const { rawData, context } = props;
|
|
18
|
+
const parsedData = fromJSON(rawData);
|
|
19
|
+
const { operation, securities } = parsedData;
|
|
20
|
+
const parameterGroups = groupParameters((operation.parameters || []).map(noReference));
|
|
21
|
+
return (React.createElement(React.Fragment, null,
|
|
22
|
+
securities.length > 0 ? (React.createElement(OpenAPISecurities, { securities: securities, context: context })) : null,
|
|
23
|
+
parameterGroups.map((group) => (React.createElement(InteractiveSection, { key: group.key, className: "openapi-parameters", toggeable: true, toggleOpenIcon: context.icons.chevronRight, toggleCloseIcon: context.icons.chevronDown, header: group.label, defaultOpened: group.key === 'path' || context.defaultInteractiveOpened },
|
|
24
|
+
React.createElement(OpenAPISchemaProperties, { properties: group.parameters.map((parameter) => ({
|
|
25
|
+
propertyName: parameter.name,
|
|
26
|
+
schema: {
|
|
27
|
+
// Description of the parameter is defined at the parameter level
|
|
28
|
+
// we use display it if the schema doesn't override it
|
|
29
|
+
description: parameter.description,
|
|
30
|
+
example: parameter.example,
|
|
31
|
+
...(noReference(parameter.schema) ?? {}),
|
|
32
|
+
},
|
|
33
|
+
required: parameter.required,
|
|
34
|
+
})), context: context })))),
|
|
35
|
+
operation.requestBody ? (React.createElement(OpenAPIRequestBody, { requestBody: noReference(operation.requestBody), context: context })) : null,
|
|
36
|
+
operation.responses ? (React.createElement(OpenAPIResponses, { responses: noReference(operation.responses), context: context })) : null));
|
|
37
|
+
}
|
|
38
|
+
function groupParameters(parameters) {
|
|
39
|
+
const sorted = ['path', 'query', 'header'];
|
|
40
|
+
const groups = [];
|
|
41
|
+
parameters.forEach((parameter) => {
|
|
42
|
+
const key = parameter.in;
|
|
43
|
+
const label = getParameterGroupName(parameter.in);
|
|
44
|
+
const group = groups.find((group) => group.key === key);
|
|
45
|
+
if (group) {
|
|
46
|
+
group.parameters.push(parameter);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
groups.push({
|
|
50
|
+
key,
|
|
51
|
+
label,
|
|
52
|
+
parameters: [parameter],
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
groups.sort((a, b) => sorted.indexOf(a.key) - sorted.indexOf(b.key));
|
|
57
|
+
return groups;
|
|
58
|
+
}
|
|
59
|
+
function getParameterGroupName(paramIn) {
|
|
60
|
+
switch (paramIn) {
|
|
61
|
+
case 'path':
|
|
62
|
+
return 'Path parameters';
|
|
63
|
+
case 'query':
|
|
64
|
+
return 'Query parameters';
|
|
65
|
+
case 'header':
|
|
66
|
+
return 'Header parameters';
|
|
67
|
+
default:
|
|
68
|
+
return paramIn;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { OpenAPIOperationData } from './fetchOpenAPIOperation';
|
|
3
|
+
/**
|
|
4
|
+
* Button which launches the Scalar API Client
|
|
5
|
+
*/
|
|
6
|
+
export declare function ScalarApiButton(props: {
|
|
7
|
+
fetchOperationData: () => Promise<OpenAPIOperationData>;
|
|
8
|
+
}): React.JSX.Element;
|
|
9
|
+
/**
|
|
10
|
+
* Wrap the rendering with a context to open the scalar modal.
|
|
11
|
+
*/
|
|
12
|
+
export declare function ScalarApiClient(props: {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { getHarRequest, getParametersFromOperation, getRequestFromOperation, } from '@scalar/oas-utils';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { fromJSON } from './fetchOpenAPIOperation';
|
|
5
|
+
const ApiClientReact = React.lazy(async () => {
|
|
6
|
+
const mod = await import('@scalar/api-client-react');
|
|
7
|
+
return { default: mod.ApiClientReact };
|
|
8
|
+
});
|
|
9
|
+
const ScalarContext = React.createContext(() => { });
|
|
10
|
+
/**
|
|
11
|
+
* Button which launches the Scalar API Client
|
|
12
|
+
*/
|
|
13
|
+
export function ScalarApiButton(props) {
|
|
14
|
+
const { fetchOperationData } = props;
|
|
15
|
+
const open = React.useContext(ScalarContext);
|
|
16
|
+
return (React.createElement("div", { className: "scalar scalar-activate" },
|
|
17
|
+
React.createElement("button", { className: "scalar-activate-button", onClick: () => {
|
|
18
|
+
open(fetchOperationData);
|
|
19
|
+
} },
|
|
20
|
+
React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: "10", height: "12", fill: "none" },
|
|
21
|
+
React.createElement("path", { stroke: "currentColor", strokeWidth: "1.5", d: "M1 10.05V1.43c0-.2.2-.31.37-.22l7.26 4.08c.17.1.17.33.01.43l-7.26 4.54a.25.25 0 0 1-.38-.21Z" })),
|
|
22
|
+
"Test it")));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Wrap the rendering with a context to open the scalar modal.
|
|
26
|
+
*/
|
|
27
|
+
export function ScalarApiClient(props) {
|
|
28
|
+
const { children } = props;
|
|
29
|
+
const [active, setActive] = React.useState(null);
|
|
30
|
+
const proxy = '/~scalar/proxy';
|
|
31
|
+
const open = React.useCallback(async (fetchOperationData) => {
|
|
32
|
+
setActive({ operationData: null });
|
|
33
|
+
const operationData = fromJSON(await fetchOperationData());
|
|
34
|
+
setActive({ operationData });
|
|
35
|
+
}, []);
|
|
36
|
+
const onClose = React.useCallback(() => {
|
|
37
|
+
setActive(null);
|
|
38
|
+
}, []);
|
|
39
|
+
const request = React.useMemo(() => {
|
|
40
|
+
const operationData = active?.operationData;
|
|
41
|
+
if (!operationData) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const operationId = operationData.operation.operationId ?? operationData.method + operationData.path;
|
|
45
|
+
const operation = {
|
|
46
|
+
...operationData,
|
|
47
|
+
httpVerb: operationData.method,
|
|
48
|
+
pathParameters: operationData.operation.parameters,
|
|
49
|
+
};
|
|
50
|
+
const variables = getParametersFromOperation(operation, 'path', false);
|
|
51
|
+
const request = getHarRequest({
|
|
52
|
+
url: operationData.path,
|
|
53
|
+
}, getRequestFromOperation({
|
|
54
|
+
...operation,
|
|
55
|
+
information: {
|
|
56
|
+
requestBody: operationData.operation.requestBody,
|
|
57
|
+
},
|
|
58
|
+
}, { requiredOnly: false }));
|
|
59
|
+
return {
|
|
60
|
+
id: operationId,
|
|
61
|
+
type: operationData.method,
|
|
62
|
+
path: operationData.path,
|
|
63
|
+
variables,
|
|
64
|
+
cookies: request.cookies.map((cookie) => {
|
|
65
|
+
return { ...cookie, enabled: true };
|
|
66
|
+
}),
|
|
67
|
+
query: request.queryString.map((queryString) => {
|
|
68
|
+
const query = queryString;
|
|
69
|
+
return { ...queryString, enabled: query.required ?? true };
|
|
70
|
+
}),
|
|
71
|
+
headers: request.headers.map((header) => {
|
|
72
|
+
return { ...header, enabled: true };
|
|
73
|
+
}),
|
|
74
|
+
url: operationData.servers[0]?.url,
|
|
75
|
+
body: request.postData?.text,
|
|
76
|
+
};
|
|
77
|
+
}, [active]);
|
|
78
|
+
return (React.createElement(ScalarContext.Provider, { value: open },
|
|
79
|
+
children,
|
|
80
|
+
active ? (React.createElement("div", { className: "scalar" },
|
|
81
|
+
React.createElement("div", { className: "scalar-container" },
|
|
82
|
+
React.createElement("div", { className: "scalar-app" },
|
|
83
|
+
React.createElement(React.Suspense, { fallback: React.createElement(ScalarLoading, null) },
|
|
84
|
+
React.createElement(ApiClientReact, { close: onClose, proxy: proxy, isOpen: true, request: request }))),
|
|
85
|
+
React.createElement("div", { onClick: () => onClose(), className: "scalar-app-exit" })))) : null));
|
|
86
|
+
}
|
|
87
|
+
function ScalarLoading() {
|
|
88
|
+
return React.createElement("div", { className: "scalar-app-loading" }, "Loading...");
|
|
89
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface CodeSampleInput {
|
|
2
|
+
method: string;
|
|
3
|
+
url: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
body?: any;
|
|
6
|
+
}
|
|
7
|
+
interface CodeSampleGenerator {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
syntax: string;
|
|
11
|
+
generate: (operation: CodeSampleInput) => string;
|
|
12
|
+
}
|
|
13
|
+
export declare const codeSampleGenerators: CodeSampleGenerator[];
|
|
14
|
+
export {};
|