@gitbook/react-openapi 1.4.3 → 1.5.1
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 +27 -0
- package/dist/index.d.ts +663 -10
- package/dist/index.js +3873 -8
- package/package.json +11 -7
- package/dist/InteractiveSection.d.ts +0 -33
- package/dist/InteractiveSection.jsx +0 -61
- package/dist/Markdown.d.ts +0 -4
- package/dist/Markdown.jsx +0 -5
- package/dist/OpenAPICodeSample.d.ts +0 -19
- package/dist/OpenAPICodeSample.jsx +0 -230
- package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
- package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
- package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
- package/dist/OpenAPICodeSampleSelector.jsx +0 -44
- package/dist/OpenAPICopyButton.d.ts +0 -13
- package/dist/OpenAPICopyButton.jsx +0 -35
- package/dist/OpenAPIDisclosure.d.ts +0 -11
- package/dist/OpenAPIDisclosure.jsx +0 -30
- package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
- package/dist/OpenAPIDisclosureGroup.jsx +0 -83
- package/dist/OpenAPIExample.d.ts +0 -16
- package/dist/OpenAPIExample.jsx +0 -36
- package/dist/OpenAPIMediaType.d.ts +0 -21
- package/dist/OpenAPIMediaType.jsx +0 -61
- package/dist/OpenAPIOperation.d.ts +0 -10
- package/dist/OpenAPIOperation.jsx +0 -25
- package/dist/OpenAPIOperationContext.d.ts +0 -16
- package/dist/OpenAPIOperationContext.jsx +0 -26
- package/dist/OpenAPIOperationDescription.d.ts +0 -9
- package/dist/OpenAPIOperationDescription.jsx +0 -22
- package/dist/OpenAPIOperationStability.d.ts +0 -9
- package/dist/OpenAPIOperationStability.jsx +0 -27
- package/dist/OpenAPIPath.d.ts +0 -18
- package/dist/OpenAPIPath.jsx +0 -55
- package/dist/OpenAPIPrefillContextProvider.d.ts +0 -22
- package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
- package/dist/OpenAPIRequestBody.d.ts +0 -11
- package/dist/OpenAPIRequestBody.jsx +0 -28
- package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
- package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
- package/dist/OpenAPIResponse.d.ts +0 -10
- package/dist/OpenAPIResponse.jsx +0 -57
- package/dist/OpenAPIResponseExample.d.ts +0 -9
- package/dist/OpenAPIResponseExample.jsx +0 -105
- package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
- package/dist/OpenAPIResponseExampleContent.jsx +0 -60
- package/dist/OpenAPIResponses.d.ts +0 -9
- package/dist/OpenAPIResponses.jsx +0 -77
- package/dist/OpenAPISchema.d.ts +0 -27
- package/dist/OpenAPISchema.jsx +0 -400
- package/dist/OpenAPISchemaName.d.ts +0 -16
- package/dist/OpenAPISchemaName.jsx +0 -43
- package/dist/OpenAPISchemaServer.d.ts +0 -12
- package/dist/OpenAPISchemaServer.jsx +0 -8
- package/dist/OpenAPISecurities.d.ts +0 -9
- package/dist/OpenAPISecurities.jsx +0 -114
- package/dist/OpenAPISelect.d.ts +0 -22
- package/dist/OpenAPISelect.jsx +0 -44
- package/dist/OpenAPISpec.d.ts +0 -6
- package/dist/OpenAPISpec.jsx +0 -80
- package/dist/OpenAPITabs.d.ts +0 -26
- package/dist/OpenAPITabs.jsx +0 -109
- package/dist/OpenAPIWebhook.d.ts +0 -10
- package/dist/OpenAPIWebhook.jsx +0 -23
- package/dist/OpenAPIWebhookExample.d.ts +0 -6
- package/dist/OpenAPIWebhookExample.jsx +0 -41
- package/dist/ScalarApiButton.d.ts +0 -14
- package/dist/ScalarApiButton.jsx +0 -81
- package/dist/StaticSection.d.ts +0 -13
- package/dist/StaticSection.jsx +0 -32
- package/dist/code-samples.d.ts +0 -17
- package/dist/code-samples.js +0 -427
- package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
- package/dist/common/OpenAPIColumnSpec.jsx +0 -20
- package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
- package/dist/common/OpenAPIOperationDescription.jsx +0 -19
- package/dist/common/OpenAPIStability.d.ts +0 -4
- package/dist/common/OpenAPIStability.jsx +0 -15
- package/dist/common/OpenAPISummary.d.ts +0 -6
- package/dist/common/OpenAPISummary.jsx +0 -30
- package/dist/contentTypeChecks.d.ts +0 -10
- package/dist/contentTypeChecks.js +0 -30
- package/dist/context.d.ts +0 -75
- package/dist/context.js +0 -43
- package/dist/decycle.d.ts +0 -2
- package/dist/decycle.js +0 -70
- package/dist/dereference.d.ts +0 -5
- package/dist/dereference.js +0 -68
- package/dist/generateSchemaExample.d.ts +0 -45
- package/dist/generateSchemaExample.js +0 -342
- package/dist/getDisclosureLabel.d.ts +0 -7
- package/dist/getDisclosureLabel.js +0 -18
- package/dist/getOrCreateStoreByKey.d.ts +0 -10
- package/dist/getOrCreateStoreByKey.js +0 -19
- package/dist/json2xml.d.ts +0 -4
- package/dist/json2xml.js +0 -7
- package/dist/resolveOpenAPIOperation.d.ts +0 -11
- package/dist/resolveOpenAPIOperation.js +0 -173
- package/dist/resolveOpenAPIWebhook.d.ts +0 -11
- package/dist/resolveOpenAPIWebhook.js +0 -127
- package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
- package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
- package/dist/schemas/OpenAPISchemas.d.ts +0 -14
- package/dist/schemas/OpenAPISchemas.jsx +0 -59
- package/dist/schemas/index.d.ts +0 -2
- package/dist/schemas/index.js +0 -2
- package/dist/schemas/resolveOpenAPISchemas.d.ts +0 -10
- package/dist/schemas/resolveOpenAPISchemas.js +0 -61
- package/dist/stringifyOpenAPI.d.ts +0 -4
- package/dist/stringifyOpenAPI.js +0 -15
- package/dist/translate.d.ts +0 -10
- package/dist/translate.jsx +0 -75
- package/dist/translations/de.d.ts +0 -43
- package/dist/translations/de.js +0 -43
- package/dist/translations/en.d.ts +0 -43
- package/dist/translations/en.js +0 -43
- package/dist/translations/es.d.ts +0 -43
- package/dist/translations/es.js +0 -43
- package/dist/translations/fr.d.ts +0 -43
- package/dist/translations/fr.js +0 -43
- package/dist/translations/index.d.ts +0 -395
- package/dist/translations/index.js +0 -27
- package/dist/translations/ja.d.ts +0 -43
- package/dist/translations/ja.js +0 -43
- package/dist/translations/nl.d.ts +0 -43
- package/dist/translations/nl.js +0 -43
- package/dist/translations/no.d.ts +0 -43
- package/dist/translations/no.js +0 -43
- package/dist/translations/pt-br.d.ts +0 -43
- package/dist/translations/pt-br.js +0 -43
- package/dist/translations/types.d.ts +0 -5
- package/dist/translations/types.js +0 -1
- package/dist/translations/zh.d.ts +0 -43
- package/dist/translations/zh.js +0 -43
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- package/dist/types.d.ts +0 -31
- package/dist/types.js +0 -1
- package/dist/util/example.d.ts +0 -35
- package/dist/util/example.jsx +0 -103
- package/dist/util/server.d.ts +0 -9
- package/dist/util/server.js +0 -44
- package/dist/util/tryit-prefill.d.ts +0 -20
- package/dist/util/tryit-prefill.js +0 -129
- package/dist/utils.d.ts +0 -50
- package/dist/utils.js +0 -224
- package/src/InteractiveSection.tsx +0 -147
- package/src/Markdown.tsx +0 -12
- package/src/OpenAPICodeSample.tsx +0 -330
- package/src/OpenAPICodeSampleInteractive.tsx +0 -136
- package/src/OpenAPICodeSampleSelector.tsx +0 -94
- package/src/OpenAPICopyButton.tsx +0 -72
- package/src/OpenAPIDisclosure.tsx +0 -46
- package/src/OpenAPIDisclosureGroup.tsx +0 -158
- package/src/OpenAPIExample.tsx +0 -55
- package/src/OpenAPIMediaType.tsx +0 -139
- package/src/OpenAPIOperation.tsx +0 -35
- package/src/OpenAPIOperationContext.tsx +0 -45
- package/src/OpenAPIOperationDescription.tsx +0 -34
- package/src/OpenAPIOperationStability.tsx +0 -39
- package/src/OpenAPIPath.tsx +0 -90
- package/src/OpenAPIPrefillContextProvider.tsx +0 -40
- package/src/OpenAPIRequestBody.tsx +0 -54
- package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
- package/src/OpenAPIResponse.tsx +0 -82
- package/src/OpenAPIResponseExample.tsx +0 -151
- package/src/OpenAPIResponseExampleContent.tsx +0 -125
- package/src/OpenAPIResponses.tsx +0 -125
- package/src/OpenAPISchema.test.ts +0 -172
- package/src/OpenAPISchema.tsx +0 -654
- package/src/OpenAPISchemaName.tsx +0 -80
- package/src/OpenAPISchemaServer.tsx +0 -34
- package/src/OpenAPISecurities.tsx +0 -231
- package/src/OpenAPISelect.tsx +0 -96
- package/src/OpenAPISpec.tsx +0 -138
- package/src/OpenAPITabs.tsx +0 -147
- package/src/OpenAPIWebhook.tsx +0 -33
- package/src/OpenAPIWebhookExample.tsx +0 -60
- package/src/ScalarApiButton.tsx +0 -132
- package/src/StaticSection.tsx +0 -91
- package/src/__snapshots__/json2xml.test.ts.snap +0 -18
- package/src/code-samples.test.ts +0 -714
- package/src/code-samples.ts +0 -448
- package/src/common/OpenAPIColumnSpec.tsx +0 -31
- package/src/common/OpenAPIOperationDescription.tsx +0 -31
- package/src/common/OpenAPIStability.tsx +0 -23
- package/src/common/OpenAPISummary.tsx +0 -45
- package/src/contentTypeChecks.ts +0 -39
- package/src/context.ts +0 -99
- package/src/decycle.ts +0 -68
- package/src/dereference.ts +0 -29
- package/src/generateSchemaExample.test.ts +0 -1040
- package/src/generateSchemaExample.ts +0 -530
- package/src/getDisclosureLabel.ts +0 -25
- package/src/getOrCreateStoreByKey.ts +0 -33
- package/src/index.ts +0 -10
- package/src/json2xml.test.ts +0 -46
- package/src/json2xml.ts +0 -8
- package/src/resolveOpenAPIOperation.test.ts +0 -177
- package/src/resolveOpenAPIOperation.ts +0 -151
- package/src/resolveOpenAPIWebhook.ts +0 -99
- package/src/schemas/OpenAPISchemaItem.tsx +0 -34
- package/src/schemas/OpenAPISchemas.tsx +0 -98
- package/src/schemas/index.ts +0 -2
- package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
- package/src/schemas/resolveOpenAPISchemas.ts +0 -28
- package/src/stringifyOpenAPI.ts +0 -25
- package/src/translate.tsx +0 -80
- package/src/translations/de.ts +0 -43
- package/src/translations/en.ts +0 -43
- package/src/translations/es.ts +0 -43
- package/src/translations/fr.ts +0 -43
- package/src/translations/index.ts +0 -33
- package/src/translations/ja.ts +0 -43
- package/src/translations/nl.ts +0 -43
- package/src/translations/no.ts +0 -43
- package/src/translations/pt-br.ts +0 -43
- package/src/translations/types.ts +0 -7
- package/src/translations/zh.ts +0 -43
- package/src/types.ts +0 -46
- package/src/util/example.tsx +0 -129
- package/src/util/server.test.ts +0 -58
- package/src/util/server.ts +0 -47
- package/src/util/tryit-prefill.test.ts +0 -311
- package/src/util/tryit-prefill.ts +0 -160
- package/src/utils.ts +0 -255
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
2
|
-
import {
|
|
3
|
-
OpenAPIMediaTypeExamplesBody,
|
|
4
|
-
OpenAPIMediaTypeExamplesSelector,
|
|
5
|
-
} from './OpenAPICodeSampleInteractive';
|
|
6
|
-
import { OpenAPICodeSampleBody } from './OpenAPICodeSampleSelector';
|
|
7
|
-
import { ScalarApiButton } from './ScalarApiButton';
|
|
8
|
-
import { type CodeSampleGenerator, codeSampleGenerators } from './code-samples';
|
|
9
|
-
import { type OpenAPIContext, getOpenAPIClientContext } from './context';
|
|
10
|
-
import { generateMediaTypeExamples, generateSchemaExample } from './generateSchemaExample';
|
|
11
|
-
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
|
12
|
-
import type { OpenAPIOperationData } from './types';
|
|
13
|
-
import { getDefaultServerURL } from './util/server';
|
|
14
|
-
import { checkIsReference } from './utils';
|
|
15
|
-
|
|
16
|
-
const CUSTOM_CODE_SAMPLES_KEYS = ['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Display code samples to execute the operation.
|
|
20
|
-
* It supports the Redocly custom syntax as well (https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples/)
|
|
21
|
-
*/
|
|
22
|
-
export function OpenAPICodeSample(props: {
|
|
23
|
-
data: OpenAPIOperationData;
|
|
24
|
-
context: OpenAPIContext;
|
|
25
|
-
}) {
|
|
26
|
-
const { data, context } = props;
|
|
27
|
-
|
|
28
|
-
// If code samples are disabled at operation level, we don't display the code samples.
|
|
29
|
-
if (data.operation['x-codeSamples'] === false) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const customCodeSamples = getCustomCodeSamples(props);
|
|
34
|
-
|
|
35
|
-
// If code samples are disabled at the top-level and not custom code samples are defined,
|
|
36
|
-
// we don't display the code samples.
|
|
37
|
-
if (data['x-codeSamples'] === false && !customCodeSamples) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const samples = customCodeSamples ?? generateCodeSamples(props);
|
|
42
|
-
|
|
43
|
-
if (samples.length === 0) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<OpenAPICodeSampleBody
|
|
49
|
-
context={getOpenAPIClientContext(context)}
|
|
50
|
-
data={data}
|
|
51
|
-
items={samples}
|
|
52
|
-
selectIcon={context.icons.chevronDown}
|
|
53
|
-
/>
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Generate code samples for the operation.
|
|
59
|
-
*/
|
|
60
|
-
function generateCodeSamples(props: {
|
|
61
|
-
data: OpenAPIOperationData;
|
|
62
|
-
context: OpenAPIContext;
|
|
63
|
-
}) {
|
|
64
|
-
const { data, context } = props;
|
|
65
|
-
|
|
66
|
-
const searchParams = new URLSearchParams();
|
|
67
|
-
const headersObject: { [k: string]: string } = {};
|
|
68
|
-
|
|
69
|
-
// The parser can sometimes returns invalid parameters (an object instead of an array).
|
|
70
|
-
// It should get fixed in scalar, but in the meantime we just ignore the parameters in that case.
|
|
71
|
-
const params = Array.isArray(data.operation.parameters) ? data.operation.parameters : [];
|
|
72
|
-
|
|
73
|
-
params.forEach((param) => {
|
|
74
|
-
if (!param) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (param.in === 'header' && param.required) {
|
|
79
|
-
const example = param.schema
|
|
80
|
-
? generateSchemaExample(param.schema, { mode: 'write' })
|
|
81
|
-
: undefined;
|
|
82
|
-
if (example !== undefined && param.name) {
|
|
83
|
-
headersObject[param.name] =
|
|
84
|
-
typeof example !== 'string' ? stringifyOpenAPI(example) : example;
|
|
85
|
-
}
|
|
86
|
-
} else if (param.in === 'query' && param.required) {
|
|
87
|
-
const example = param.schema
|
|
88
|
-
? generateSchemaExample(param.schema, { mode: 'write' })
|
|
89
|
-
: undefined;
|
|
90
|
-
if (example !== undefined && param.name) {
|
|
91
|
-
searchParams.append(
|
|
92
|
-
param.name,
|
|
93
|
-
String(Array.isArray(example) ? example[0] : example)
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
const requestBody = !checkIsReference(data.operation.requestBody)
|
|
100
|
-
? data.operation.requestBody
|
|
101
|
-
: undefined;
|
|
102
|
-
|
|
103
|
-
const url =
|
|
104
|
-
getDefaultServerURL(data.servers) +
|
|
105
|
-
data.path +
|
|
106
|
-
(searchParams.size ? `?${searchParams.toString()}` : '');
|
|
107
|
-
|
|
108
|
-
const genericHeaders = {
|
|
109
|
-
...getSecurityHeaders(data.securities),
|
|
110
|
-
...headersObject,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const mediaTypeRendererFactories = Object.entries(requestBody?.content ?? {}).map(
|
|
114
|
-
([mediaType, mediaTypeObject]) => {
|
|
115
|
-
return (generator: CodeSampleGenerator) => {
|
|
116
|
-
const mediaTypeHeaders = {
|
|
117
|
-
...genericHeaders,
|
|
118
|
-
'Content-Type': mediaType,
|
|
119
|
-
};
|
|
120
|
-
return {
|
|
121
|
-
mediaType,
|
|
122
|
-
element: context.renderCodeBlock({
|
|
123
|
-
code: generator.generate({
|
|
124
|
-
url,
|
|
125
|
-
method: data.method,
|
|
126
|
-
body: undefined,
|
|
127
|
-
headers: mediaTypeHeaders,
|
|
128
|
-
}),
|
|
129
|
-
syntax: generator.syntax,
|
|
130
|
-
}),
|
|
131
|
-
examples: generateMediaTypeExamples(mediaTypeObject, {
|
|
132
|
-
mode: 'write',
|
|
133
|
-
}).map((example) => ({
|
|
134
|
-
example,
|
|
135
|
-
element: context.renderCodeBlock({
|
|
136
|
-
code: generator.generate({
|
|
137
|
-
url,
|
|
138
|
-
method: data.method,
|
|
139
|
-
body: example.value,
|
|
140
|
-
headers: mediaTypeHeaders,
|
|
141
|
-
}),
|
|
142
|
-
syntax: generator.syntax,
|
|
143
|
-
}),
|
|
144
|
-
})),
|
|
145
|
-
} satisfies MediaTypeRenderer;
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
return codeSampleGenerators.map((generator) => {
|
|
151
|
-
if (mediaTypeRendererFactories.length > 0) {
|
|
152
|
-
const renderers = mediaTypeRendererFactories.map((generate) => generate(generator));
|
|
153
|
-
return {
|
|
154
|
-
key: `default-${generator.id}`,
|
|
155
|
-
label: generator.label,
|
|
156
|
-
body: (
|
|
157
|
-
<OpenAPIMediaTypeExamplesBody
|
|
158
|
-
method={data.method}
|
|
159
|
-
path={data.path}
|
|
160
|
-
renderers={renderers}
|
|
161
|
-
blockKey={context.blockKey}
|
|
162
|
-
/>
|
|
163
|
-
),
|
|
164
|
-
footer: (
|
|
165
|
-
<OpenAPICodeSampleFooter renderers={renderers} data={data} context={context} />
|
|
166
|
-
),
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
return {
|
|
170
|
-
key: `default-${generator.id}`,
|
|
171
|
-
label: generator.label,
|
|
172
|
-
body: context.renderCodeBlock({
|
|
173
|
-
code: generator.generate({
|
|
174
|
-
url,
|
|
175
|
-
method: data.method,
|
|
176
|
-
body: undefined,
|
|
177
|
-
headers: genericHeaders,
|
|
178
|
-
}),
|
|
179
|
-
syntax: generator.syntax,
|
|
180
|
-
}),
|
|
181
|
-
footer: <OpenAPICodeSampleFooter data={data} renderers={[]} context={context} />,
|
|
182
|
-
};
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export interface MediaTypeRenderer {
|
|
187
|
-
mediaType: string;
|
|
188
|
-
element: React.ReactNode;
|
|
189
|
-
examples: Array<{
|
|
190
|
-
example: OpenAPIV3.ExampleObject;
|
|
191
|
-
element: React.ReactNode;
|
|
192
|
-
}>;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function OpenAPICodeSampleFooter(props: {
|
|
196
|
-
data: OpenAPIOperationData;
|
|
197
|
-
renderers: MediaTypeRenderer[];
|
|
198
|
-
context: OpenAPIContext;
|
|
199
|
-
}) {
|
|
200
|
-
const { data, context, renderers } = props;
|
|
201
|
-
const { method, path, securities, servers } = data;
|
|
202
|
-
const { specUrl } = context;
|
|
203
|
-
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
|
|
204
|
-
const hasMultipleMediaTypes =
|
|
205
|
-
renderers.length > 1 || renderers.some((renderer) => renderer.examples.length > 0);
|
|
206
|
-
|
|
207
|
-
if (hideTryItPanel && !hasMultipleMediaTypes) {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (!validateHttpMethod(method)) {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return (
|
|
216
|
-
<div className="openapi-codesample-footer">
|
|
217
|
-
{hasMultipleMediaTypes ? (
|
|
218
|
-
<OpenAPIMediaTypeExamplesSelector
|
|
219
|
-
method={data.method}
|
|
220
|
-
path={data.path}
|
|
221
|
-
renderers={renderers}
|
|
222
|
-
selectIcon={context.icons.chevronDown}
|
|
223
|
-
blockKey={context.blockKey}
|
|
224
|
-
/>
|
|
225
|
-
) : (
|
|
226
|
-
<span />
|
|
227
|
-
)}
|
|
228
|
-
{!hideTryItPanel && (
|
|
229
|
-
<ScalarApiButton
|
|
230
|
-
context={getOpenAPIClientContext(context)}
|
|
231
|
-
method={method}
|
|
232
|
-
path={path}
|
|
233
|
-
securities={securities}
|
|
234
|
-
servers={servers}
|
|
235
|
-
specUrl={specUrl}
|
|
236
|
-
/>
|
|
237
|
-
)}
|
|
238
|
-
</div>
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Get custom code samples for the operation.
|
|
244
|
-
*/
|
|
245
|
-
function getCustomCodeSamples(props: {
|
|
246
|
-
data: OpenAPIOperationData;
|
|
247
|
-
context: OpenAPIContext;
|
|
248
|
-
}) {
|
|
249
|
-
const { data, context } = props;
|
|
250
|
-
|
|
251
|
-
let customCodeSamples: null | Array<{
|
|
252
|
-
key: string;
|
|
253
|
-
label: string;
|
|
254
|
-
body: React.ReactNode;
|
|
255
|
-
}> = null;
|
|
256
|
-
|
|
257
|
-
CUSTOM_CODE_SAMPLES_KEYS.forEach((key) => {
|
|
258
|
-
const customSamples = data.operation[key];
|
|
259
|
-
if (customSamples && Array.isArray(customSamples)) {
|
|
260
|
-
customCodeSamples = customSamples
|
|
261
|
-
.filter((sample) => {
|
|
262
|
-
return typeof sample.source === 'string' && typeof sample.lang === 'string';
|
|
263
|
-
})
|
|
264
|
-
.map((sample, index) => ({
|
|
265
|
-
key: `custom-sample-${sample.lang}-${index}`,
|
|
266
|
-
label: sample.label || sample.lang,
|
|
267
|
-
body: context.renderCodeBlock({
|
|
268
|
-
code: sample.source,
|
|
269
|
-
syntax: sample.lang,
|
|
270
|
-
}),
|
|
271
|
-
footer: (
|
|
272
|
-
<OpenAPICodeSampleFooter renderers={[]} data={data} context={context} />
|
|
273
|
-
),
|
|
274
|
-
}));
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
return customCodeSamples;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function getSecurityHeaders(securities: OpenAPIOperationData['securities']): {
|
|
282
|
-
[key: string]: string;
|
|
283
|
-
} {
|
|
284
|
-
const security = securities[0];
|
|
285
|
-
|
|
286
|
-
if (!security) {
|
|
287
|
-
return {};
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
switch (security[1].type) {
|
|
291
|
-
case 'http': {
|
|
292
|
-
let scheme = security[1].scheme;
|
|
293
|
-
let format = security[1].bearerFormat ?? 'YOUR_SECRET_TOKEN';
|
|
294
|
-
|
|
295
|
-
if (scheme?.includes('bearer')) {
|
|
296
|
-
scheme = 'Bearer';
|
|
297
|
-
} else if (scheme?.includes('basic')) {
|
|
298
|
-
scheme = 'Basic';
|
|
299
|
-
format = 'username:password';
|
|
300
|
-
} else if (scheme?.includes('token')) {
|
|
301
|
-
scheme = 'Token';
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return {
|
|
305
|
-
Authorization: `${scheme} ${format}`,
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
case 'apiKey': {
|
|
309
|
-
if (security[1].in !== 'header') return {};
|
|
310
|
-
|
|
311
|
-
const name = security[1].name ?? 'Authorization';
|
|
312
|
-
|
|
313
|
-
return {
|
|
314
|
-
[name]: 'YOUR_API_KEY',
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
case 'oauth2': {
|
|
318
|
-
return {
|
|
319
|
-
Authorization: 'Bearer YOUR_OAUTH2_TOKEN',
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
default: {
|
|
323
|
-
return {};
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
function validateHttpMethod(method: string): method is OpenAPIV3.HttpMethods {
|
|
329
|
-
return ['get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace'].includes(method);
|
|
330
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import clsx from 'clsx';
|
|
3
|
-
import type { MediaTypeRenderer } from './OpenAPICodeSample';
|
|
4
|
-
import { OpenAPISelect, OpenAPISelectItem, useSelectState } from './OpenAPISelect';
|
|
5
|
-
import { createStateKey } from './utils';
|
|
6
|
-
|
|
7
|
-
export function OpenAPIMediaTypeExamplesSelector(props: {
|
|
8
|
-
method: string;
|
|
9
|
-
path: string;
|
|
10
|
-
renderers: MediaTypeRenderer[];
|
|
11
|
-
selectIcon?: React.ReactNode;
|
|
12
|
-
blockKey?: string;
|
|
13
|
-
}) {
|
|
14
|
-
const { method, path, renderers, selectIcon, blockKey } = props;
|
|
15
|
-
if (!renderers[0]) {
|
|
16
|
-
throw new Error('No renderers provided');
|
|
17
|
-
}
|
|
18
|
-
const stateKey = createStateKey('request-body-media-type', blockKey);
|
|
19
|
-
const state = useSelectState(stateKey, renderers[0].mediaType);
|
|
20
|
-
const selected = renderers.find((r) => r.mediaType === state.key) || renderers[0];
|
|
21
|
-
|
|
22
|
-
return (
|
|
23
|
-
<div className="openapi-codesample-selectors">
|
|
24
|
-
<MediaTypeSelector selectIcon={selectIcon} stateKey={stateKey} renderers={renderers} />
|
|
25
|
-
<ExamplesSelector
|
|
26
|
-
selectIcon={selectIcon}
|
|
27
|
-
method={method}
|
|
28
|
-
path={path}
|
|
29
|
-
renderer={selected}
|
|
30
|
-
/>
|
|
31
|
-
</div>
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function MediaTypeSelector(props: {
|
|
36
|
-
stateKey: string;
|
|
37
|
-
renderers: MediaTypeRenderer[];
|
|
38
|
-
selectIcon?: React.ReactNode;
|
|
39
|
-
}) {
|
|
40
|
-
const { renderers, stateKey, selectIcon } = props;
|
|
41
|
-
|
|
42
|
-
if (renderers.length < 2) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const items = renderers.map((renderer) => ({
|
|
47
|
-
key: renderer.mediaType,
|
|
48
|
-
label: renderer.mediaType,
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
return (
|
|
52
|
-
<OpenAPISelect
|
|
53
|
-
className={clsx('openapi-select')}
|
|
54
|
-
items={renderers.map((renderer) => ({
|
|
55
|
-
key: renderer.mediaType,
|
|
56
|
-
label: renderer.mediaType,
|
|
57
|
-
}))}
|
|
58
|
-
icon={selectIcon}
|
|
59
|
-
stateKey={stateKey}
|
|
60
|
-
placement="bottom start"
|
|
61
|
-
>
|
|
62
|
-
{items.map((item) => (
|
|
63
|
-
<OpenAPISelectItem key={item.key} id={item.key} value={item}>
|
|
64
|
-
{item.label}
|
|
65
|
-
</OpenAPISelectItem>
|
|
66
|
-
))}
|
|
67
|
-
</OpenAPISelect>
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function ExamplesSelector(props: {
|
|
72
|
-
method: string;
|
|
73
|
-
path: string;
|
|
74
|
-
renderer: MediaTypeRenderer;
|
|
75
|
-
selectIcon?: React.ReactNode;
|
|
76
|
-
}) {
|
|
77
|
-
const { method, path, renderer, selectIcon } = props;
|
|
78
|
-
if (renderer.examples.length < 2) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const items = renderer.examples.map((example, index) => ({
|
|
83
|
-
key: index,
|
|
84
|
-
label: example.example.summary || `Example ${index + 1}`,
|
|
85
|
-
}));
|
|
86
|
-
|
|
87
|
-
return (
|
|
88
|
-
<OpenAPISelect
|
|
89
|
-
items={items}
|
|
90
|
-
icon={selectIcon}
|
|
91
|
-
stateKey={`media-type-sample-${renderer.mediaType}-${method}-${path}`}
|
|
92
|
-
placement="bottom start"
|
|
93
|
-
>
|
|
94
|
-
{items.map((item) => (
|
|
95
|
-
<OpenAPISelectItem key={item.key} id={item.key} value={item}>
|
|
96
|
-
{item.label}
|
|
97
|
-
</OpenAPISelectItem>
|
|
98
|
-
))}
|
|
99
|
-
</OpenAPISelect>
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function OpenAPIMediaTypeExamplesBody(props: {
|
|
104
|
-
method: string;
|
|
105
|
-
path: string;
|
|
106
|
-
renderers: MediaTypeRenderer[];
|
|
107
|
-
blockKey?: string;
|
|
108
|
-
}) {
|
|
109
|
-
const { renderers, method, path, blockKey } = props;
|
|
110
|
-
if (!renderers[0]) {
|
|
111
|
-
throw new Error('No renderers provided');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const mediaTypeState = useSelectState(
|
|
115
|
-
createStateKey('request-body-media-type', blockKey),
|
|
116
|
-
renderers[0].mediaType
|
|
117
|
-
);
|
|
118
|
-
const selected = renderers.find((r) => r.mediaType === mediaTypeState.key) ?? renderers[0];
|
|
119
|
-
if (selected.examples.length === 0) {
|
|
120
|
-
return selected.element;
|
|
121
|
-
}
|
|
122
|
-
return <ExamplesBody method={method} path={path} renderer={selected} />;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function ExamplesBody(props: { method: string; path: string; renderer: MediaTypeRenderer }) {
|
|
126
|
-
const { method, path, renderer } = props;
|
|
127
|
-
const exampleState = useSelectState(
|
|
128
|
-
`media-type-sample-${renderer.mediaType}-${method}-${path}`,
|
|
129
|
-
renderer.mediaType
|
|
130
|
-
);
|
|
131
|
-
const example = renderer.examples[Number(exampleState.key)] ?? renderer.examples[0];
|
|
132
|
-
if (!example) {
|
|
133
|
-
throw new Error(`No example found for key ${exampleState.key}`);
|
|
134
|
-
}
|
|
135
|
-
return example.element;
|
|
136
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useCallback } from 'react';
|
|
4
|
-
import type { Key } from 'react-aria';
|
|
5
|
-
import { useStore } from 'zustand';
|
|
6
|
-
import { OpenAPIPath } from './OpenAPIPath';
|
|
7
|
-
import { OpenAPISelect, OpenAPISelectItem } from './OpenAPISelect';
|
|
8
|
-
import { StaticSection } from './StaticSection';
|
|
9
|
-
import type { OpenAPIClientContext } from './context';
|
|
10
|
-
import { getOrCreateStoreByKey } from './getOrCreateStoreByKey';
|
|
11
|
-
import type { OpenAPIOperationData } from './types';
|
|
12
|
-
|
|
13
|
-
function useCodeSampleState(initialKey: Key = 'default') {
|
|
14
|
-
const store = useStore(getOrCreateStoreByKey('codesample', initialKey));
|
|
15
|
-
return {
|
|
16
|
-
key: store.key,
|
|
17
|
-
setKey: useCallback((key: Key) => store.setKey(key), [store.setKey]),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type CodeSampleItem = OpenAPISelectItem & {
|
|
22
|
-
body: React.ReactNode;
|
|
23
|
-
footer?: React.ReactNode;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function OpenAPICodeSampleHeader(props: {
|
|
27
|
-
items: CodeSampleItem[];
|
|
28
|
-
data: OpenAPIOperationData;
|
|
29
|
-
selectIcon?: React.ReactNode;
|
|
30
|
-
context: OpenAPIClientContext;
|
|
31
|
-
}) {
|
|
32
|
-
const { data, items, selectIcon, context } = props;
|
|
33
|
-
|
|
34
|
-
return (
|
|
35
|
-
<>
|
|
36
|
-
<OpenAPIPath context={context} canCopy={false} withServer={false} data={data} />
|
|
37
|
-
{items.length > 1 ? (
|
|
38
|
-
<OpenAPISelect
|
|
39
|
-
icon={selectIcon}
|
|
40
|
-
items={items}
|
|
41
|
-
stateKey="codesample"
|
|
42
|
-
placement="bottom end"
|
|
43
|
-
>
|
|
44
|
-
{items.map((item) => (
|
|
45
|
-
<OpenAPISelectItem key={item.key} id={item.key} value={item}>
|
|
46
|
-
{item.label}
|
|
47
|
-
</OpenAPISelectItem>
|
|
48
|
-
))}
|
|
49
|
-
</OpenAPISelect>
|
|
50
|
-
) : items[0] ? (
|
|
51
|
-
<span className="openapi-codesample-label">{items[0].label}</span>
|
|
52
|
-
) : null}
|
|
53
|
-
</>
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function OpenAPICodeSampleBody(props: {
|
|
58
|
-
items: CodeSampleItem[];
|
|
59
|
-
data: OpenAPIOperationData;
|
|
60
|
-
selectIcon?: React.ReactNode;
|
|
61
|
-
context: OpenAPIClientContext;
|
|
62
|
-
}) {
|
|
63
|
-
const { items, data, selectIcon, context } = props;
|
|
64
|
-
if (!items[0]) {
|
|
65
|
-
throw new Error('No items provided');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const state = useCodeSampleState(items[0]?.key);
|
|
69
|
-
|
|
70
|
-
const selected = items.find((item) => item.key === state.key) || items[0];
|
|
71
|
-
|
|
72
|
-
if (!selected) {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return (
|
|
77
|
-
<StaticSection
|
|
78
|
-
header={
|
|
79
|
-
<OpenAPICodeSampleHeader
|
|
80
|
-
context={context}
|
|
81
|
-
selectIcon={selectIcon}
|
|
82
|
-
data={data}
|
|
83
|
-
items={items}
|
|
84
|
-
/>
|
|
85
|
-
}
|
|
86
|
-
className="openapi-codesample"
|
|
87
|
-
>
|
|
88
|
-
<div id={selected.key as string} className="openapi-codesample-panel">
|
|
89
|
-
{selected.body ? selected.body : null}
|
|
90
|
-
{selected.footer ? selected.footer : null}
|
|
91
|
-
</div>
|
|
92
|
-
</StaticSection>
|
|
93
|
-
);
|
|
94
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { Button, type ButtonProps, Tooltip, TooltipTrigger } from 'react-aria-components';
|
|
5
|
-
import type { OpenAPIClientContext } from './context';
|
|
6
|
-
import { t } from './translate';
|
|
7
|
-
|
|
8
|
-
export function OpenAPICopyButton(
|
|
9
|
-
props: ButtonProps & {
|
|
10
|
-
value: string;
|
|
11
|
-
children: React.ReactNode;
|
|
12
|
-
context: OpenAPIClientContext;
|
|
13
|
-
label?: string;
|
|
14
|
-
/**
|
|
15
|
-
* Whether to show a tooltip.
|
|
16
|
-
* @default true
|
|
17
|
-
*/
|
|
18
|
-
withTooltip?: boolean;
|
|
19
|
-
}
|
|
20
|
-
) {
|
|
21
|
-
const { value, label, children, onPress, className, context, withTooltip = true } = props;
|
|
22
|
-
const [copied, setCopied] = useState(false);
|
|
23
|
-
const [isOpen, setIsOpen] = useState(false);
|
|
24
|
-
|
|
25
|
-
const handleCopy = () => {
|
|
26
|
-
if (!value) return;
|
|
27
|
-
navigator.clipboard.writeText(value).then(() => {
|
|
28
|
-
setIsOpen(true);
|
|
29
|
-
setCopied(true);
|
|
30
|
-
|
|
31
|
-
setTimeout(() => {
|
|
32
|
-
setCopied(false);
|
|
33
|
-
setIsOpen(false);
|
|
34
|
-
}, 2000);
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<TooltipTrigger
|
|
40
|
-
isOpen={isOpen}
|
|
41
|
-
onOpenChange={setIsOpen}
|
|
42
|
-
isDisabled={!withTooltip}
|
|
43
|
-
closeDelay={200}
|
|
44
|
-
delay={200}
|
|
45
|
-
>
|
|
46
|
-
<Button
|
|
47
|
-
type="button"
|
|
48
|
-
preventFocusOnPress
|
|
49
|
-
onPress={(e) => {
|
|
50
|
-
handleCopy();
|
|
51
|
-
onPress?.(e);
|
|
52
|
-
}}
|
|
53
|
-
className={`openapi-copy-button ${className}`}
|
|
54
|
-
{...props}
|
|
55
|
-
>
|
|
56
|
-
{children}
|
|
57
|
-
</Button>
|
|
58
|
-
|
|
59
|
-
<Tooltip
|
|
60
|
-
isOpen={isOpen}
|
|
61
|
-
onOpenChange={setIsOpen}
|
|
62
|
-
placement="top"
|
|
63
|
-
offset={4}
|
|
64
|
-
className="openapi-tooltip"
|
|
65
|
-
>
|
|
66
|
-
{copied
|
|
67
|
-
? t(context.translation, 'copied')
|
|
68
|
-
: label || t(context.translation, 'copy_to_clipboard')}
|
|
69
|
-
</Tooltip>
|
|
70
|
-
</TooltipTrigger>
|
|
71
|
-
);
|
|
72
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import clsx from 'clsx';
|
|
3
|
-
import type React from 'react';
|
|
4
|
-
import { useState } from 'react';
|
|
5
|
-
import { Button, Disclosure, DisclosurePanel } from 'react-aria-components';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Display an interactive OpenAPI disclosure.
|
|
9
|
-
*/
|
|
10
|
-
export function OpenAPIDisclosure(props: {
|
|
11
|
-
icon: React.ReactNode;
|
|
12
|
-
header: React.ReactNode;
|
|
13
|
-
children: React.ReactNode;
|
|
14
|
-
label: string | ((isExpanded: boolean) => string);
|
|
15
|
-
className?: string;
|
|
16
|
-
}): React.JSX.Element {
|
|
17
|
-
const { icon, header, label, children, className } = props;
|
|
18
|
-
const [isExpanded, setIsExpanded] = useState(false);
|
|
19
|
-
|
|
20
|
-
return (
|
|
21
|
-
<Disclosure
|
|
22
|
-
className={clsx('openapi-disclosure', className)}
|
|
23
|
-
isExpanded={isExpanded}
|
|
24
|
-
onExpandedChange={setIsExpanded}
|
|
25
|
-
>
|
|
26
|
-
<Button
|
|
27
|
-
slot="trigger"
|
|
28
|
-
className="openapi-disclosure-trigger"
|
|
29
|
-
style={({ isFocusVisible }) => ({
|
|
30
|
-
outline: isFocusVisible
|
|
31
|
-
? '2px solid rgb(var(--primary-color-500) / 0.4)'
|
|
32
|
-
: 'none',
|
|
33
|
-
})}
|
|
34
|
-
>
|
|
35
|
-
{header}
|
|
36
|
-
<div className="openapi-disclosure-trigger-label">
|
|
37
|
-
<span>{typeof label === 'function' ? label(isExpanded) : label}</span>
|
|
38
|
-
{icon}
|
|
39
|
-
</div>
|
|
40
|
-
</Button>
|
|
41
|
-
<DisclosurePanel className="openapi-disclosure-panel">
|
|
42
|
-
{isExpanded ? children : null}
|
|
43
|
-
</DisclosurePanel>
|
|
44
|
-
</Disclosure>
|
|
45
|
-
);
|
|
46
|
-
}
|