Response examples with status code tabs (#2448)

This commit is contained in:
Brett Jephson
2024-09-06 09:35:43 +01:00
committed by GitHub
parent 51fa3ab2c8
commit 51955dae3b
2 changed files with 38 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Adds tabs to Response Example section e.g. for status code examples
@@ -36,13 +36,9 @@ export function OpenAPIResponseExample(props: {
} }
return Number(a) - Number(b); return Number(a) - Number(b);
}); });
// Take the first one
const response = responses[0];
if (!response) {
return null;
}
const examples = responses
.map((response) => {
const responseObject = noReference(response[1]); const responseObject = noReference(response[1]);
const schema = noReference( const schema = noReference(
@@ -57,16 +53,30 @@ export function OpenAPIResponseExample(props: {
} }
const example = generateSchemaExample(schema); const example = generateSchemaExample(schema);
if (example === undefined) { return {
key: `${response[0]}`,
label: `${response[0]}`,
body: (
<context.CodeBlock
code={
typeof example === 'string' ? example : JSON.stringify(example, null, 2)
}
syntax="json"
/>
),
};
})
.filter((val): val is { key: string; label: string; body: any } => Boolean(val));
if (examples.length === 0) {
return null; return null;
} }
return ( return (
<InteractiveSection header="Response" className="openapi-response-example"> <InteractiveSection
<context.CodeBlock header="Response"
code={typeof example === 'string' ? example : JSON.stringify(example, null, 2)} className="openapi-response-example"
syntax="json" tabs={examples}
/> />
</InteractiveSection>
); );
} }