-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathOpenAPICodeSample.tsx
240 lines (208 loc) · 7.54 KB
/
OpenAPICodeSample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
import { ScalarApiButton } from './ScalarApiButton';
import { StaticSection } from './StaticSection';
import { type CodeSampleInput, codeSampleGenerators } from './code-samples';
import { generateMediaTypeExamples, generateSchemaExample } from './generateSchemaExample';
import { stringifyOpenAPI } from './stringifyOpenAPI';
import type { OpenAPIContextProps, OpenAPIOperationData } from './types';
import { getDefaultServerURL } from './util/server';
import { checkIsReference, createStateKey } from './utils';
const CUSTOM_CODE_SAMPLES_KEYS = ['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const;
/**
* Display code samples to execute the operation.
* It supports the Redocly custom syntax as well (https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples/)
*/
export function OpenAPICodeSample(props: {
data: OpenAPIOperationData;
context: OpenAPIContextProps;
}) {
const { data } = props;
// If code samples are disabled at operation level, we don't display the code samples.
if (data.operation['x-codeSamples'] === false) {
return null;
}
const customCodeSamples = getCustomCodeSamples(props);
// If code samples are disabled at the top-level and not custom code samples are defined,
// we don't display the code samples.
if (data['x-codeSamples'] === false && !customCodeSamples) {
return null;
}
const samples = customCodeSamples ?? generateCodeSamples(props);
if (samples.length === 0) {
return null;
}
return (
<OpenAPITabs stateKey={createStateKey('codesample')} items={samples}>
<StaticSection header={<OpenAPITabsList />} className="openapi-codesample">
<OpenAPITabsPanels />
</StaticSection>
</OpenAPITabs>
);
}
function OpenAPICodeSampleFooter(props: {
data: OpenAPIOperationData;
context: OpenAPIContextProps;
}) {
const { data, context } = props;
const { method, path } = data;
const { specUrl } = context;
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
if (hideTryItPanel) {
return null;
}
if (!validateHttpMethod(method)) {
return null;
}
return (
<div className="openapi-codesample-footer">
<ScalarApiButton method={method} path={path} specUrl={specUrl} />
</div>
);
}
/**
* Generate code samples for the operation.
*/
function generateCodeSamples(props: {
data: OpenAPIOperationData;
context: OpenAPIContextProps;
}) {
const { data, context } = props;
const searchParams = new URLSearchParams();
const headersObject: { [k: string]: string } = {};
data.operation.parameters?.forEach((param) => {
if (!param) {
return;
}
if (param.in === 'header' && param.required) {
const example = param.schema
? generateSchemaExample(param.schema, { mode: 'write' })
: undefined;
if (example !== undefined && param.name) {
headersObject[param.name] =
typeof example !== 'string' ? stringifyOpenAPI(example) : example;
}
} else if (param.in === 'query' && param.required) {
const example = param.schema
? generateSchemaExample(param.schema, { mode: 'write' })
: undefined;
if (example !== undefined && param.name) {
searchParams.append(
param.name,
String(Array.isArray(example) ? example[0] : example)
);
}
}
});
const requestBody = !checkIsReference(data.operation.requestBody)
? data.operation.requestBody
: undefined;
const requestBodyContentEntries = requestBody?.content
? Object.entries(requestBody.content)
: undefined;
const requestBodyContent = requestBodyContentEntries?.[0];
const requestBodyExamples = requestBodyContent
? generateMediaTypeExamples(requestBodyContent[1])
: [];
const input: CodeSampleInput = {
url:
getDefaultServerURL(data.servers) +
data.path +
(searchParams.size ? `?${searchParams.toString()}` : ''),
method: data.method,
body: requestBodyExamples[0]?.value,
headers: {
...getSecurityHeaders(data.securities),
...headersObject,
...(requestBodyContent
? {
'Content-Type': requestBodyContent[0],
}
: undefined),
},
};
return codeSampleGenerators.map((generator) => ({
key: `default-${generator.id}`,
label: generator.label,
body: context.renderCodeBlock({
code: generator.generate(input),
syntax: generator.syntax,
}),
footer: <OpenAPICodeSampleFooter data={data} context={context} />,
}));
}
/**
* Get custom code samples for the operation.
*/
function getCustomCodeSamples(props: {
data: OpenAPIOperationData;
context: OpenAPIContextProps;
}) {
const { data, context } = props;
let customCodeSamples: null | Array<{
key: string;
label: string;
body: React.ReactNode;
}> = null;
CUSTOM_CODE_SAMPLES_KEYS.forEach((key) => {
const customSamples = data.operation[key];
if (customSamples && Array.isArray(customSamples)) {
customCodeSamples = customSamples
.filter((sample) => {
return (
typeof sample.label === 'string' &&
typeof sample.source === 'string' &&
typeof sample.lang === 'string'
);
})
.map((sample, index) => ({
key: `custom-sample-${sample.lang}-${index}`,
label: sample.label,
body: context.renderCodeBlock({
code: sample.source,
syntax: sample.lang,
}),
footer: <OpenAPICodeSampleFooter data={data} context={context} />,
}));
}
});
return customCodeSamples;
}
function getSecurityHeaders(securities: OpenAPIOperationData['securities']): {
[key: string]: string;
} {
const security = securities[0];
if (!security) {
return {};
}
switch (security[1].type) {
case 'http': {
let scheme = security[1].scheme;
let format = security[1].bearerFormat ?? 'YOUR_SECRET_TOKEN';
if (scheme?.includes('bearer')) {
scheme = 'Bearer';
} else if (scheme?.includes('basic')) {
scheme = 'Basic';
format = 'username:password';
} else if (scheme?.includes('token')) {
scheme = 'Token';
}
return {
Authorization: `${scheme} ${format}`,
};
}
case 'apiKey': {
if (security[1].in !== 'header') return {};
const name = security[1].name ?? 'Authorization';
return {
[name]: 'YOUR_API_KEY',
};
}
default: {
return {};
}
}
}
function validateHttpMethod(method: string): method is OpenAPIV3.HttpMethods {
return ['get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace'].includes(method);
}