mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add required query parameters to the code sample (#2474)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/react-openapi': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add required query parameters to the code sample
|
||||||
@@ -19,18 +19,33 @@ export function OpenAPICodeSample(props: {
|
|||||||
}) {
|
}) {
|
||||||
const { data, context } = props;
|
const { data, context } = props;
|
||||||
|
|
||||||
const requiredHeaders = data.operation.parameters
|
const searchParams = new URLSearchParams();
|
||||||
?.map(noReference)
|
|
||||||
.filter((param) => param.in === 'header' && param.required);
|
|
||||||
|
|
||||||
const headersObject: { [k: string]: string } = {};
|
const headersObject: { [k: string]: string } = {};
|
||||||
requiredHeaders?.forEach((header) => {
|
|
||||||
const example = header.schema
|
data.operation.parameters?.forEach((rawParam) => {
|
||||||
? generateSchemaExample(noReference(header.schema))
|
const param = noReference(rawParam);
|
||||||
: undefined;
|
if (!param) {
|
||||||
if (example !== undefined) {
|
return;
|
||||||
headersObject[header.name] =
|
}
|
||||||
typeof example !== 'string' ? JSON.stringify(example) : example;
|
|
||||||
|
if (param.in === 'header' && param.required) {
|
||||||
|
const example = param.schema
|
||||||
|
? generateSchemaExample(noReference(param.schema))
|
||||||
|
: undefined;
|
||||||
|
if (example !== undefined) {
|
||||||
|
headersObject[param.name] =
|
||||||
|
typeof example !== 'string' ? JSON.stringify(example) : example;
|
||||||
|
}
|
||||||
|
} else if (param.in === 'query' && param.required) {
|
||||||
|
const example = param.schema
|
||||||
|
? generateSchemaExample(noReference(param.schema))
|
||||||
|
: undefined;
|
||||||
|
if (example !== undefined) {
|
||||||
|
searchParams.append(
|
||||||
|
param.name,
|
||||||
|
String(Array.isArray(example) ? example[0] : example),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -38,7 +53,10 @@ export function OpenAPICodeSample(props: {
|
|||||||
const requestBodyContent = requestBody ? Object.entries(requestBody.content)[0] : undefined;
|
const requestBodyContent = requestBody ? Object.entries(requestBody.content)[0] : undefined;
|
||||||
|
|
||||||
const input: CodeSampleInput = {
|
const input: CodeSampleInput = {
|
||||||
url: getServersURL(data.servers) + data.path,
|
url:
|
||||||
|
getServersURL(data.servers) +
|
||||||
|
data.path +
|
||||||
|
(searchParams.size ? `?${searchParams.toString()}` : ''),
|
||||||
method: data.method,
|
method: data.method,
|
||||||
body: requestBodyContent
|
body: requestBodyContent
|
||||||
? generateMediaTypeExample(requestBodyContent[1], { onlyRequired: true })
|
? generateMediaTypeExample(requestBodyContent[1], { onlyRequired: true })
|
||||||
|
|||||||
Reference in New Issue
Block a user