mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 10:03:31 +00:00
Support response examples in API blocks (#2799)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/react-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Support response examples
|
||||||
@@ -5,6 +5,7 @@ import { generateSchemaExample } from './generateSchemaExample';
|
|||||||
import { OpenAPIContextProps } from './types';
|
import { OpenAPIContextProps } from './types';
|
||||||
import { createStateKey, noReference } from './utils';
|
import { createStateKey, noReference } from './utils';
|
||||||
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
||||||
|
import { OpenAPIV3 } from '@scalar/openapi-types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display an example of the response content.
|
* Display an example of the response content.
|
||||||
@@ -39,39 +40,60 @@ export function OpenAPIResponseExample(props: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const examples = responses
|
const examples = responses
|
||||||
.map((response) => {
|
.map(([key, value]) => {
|
||||||
const responseObject = noReference(response[1]);
|
const responseObject = noReference(value);
|
||||||
|
const mediaTypeObject = (() => {
|
||||||
|
if (!responseObject.content) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
responseObject.content['application/json'] ??
|
||||||
|
responseObject.content[Object.keys(responseObject.content)[0]]
|
||||||
|
);
|
||||||
|
})();
|
||||||
|
|
||||||
// TODO: unnecessary with https://github.com/GitbookIO/gitbook/pull/2780
|
if (!mediaTypeObject) {
|
||||||
if (!responseObject) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const schema = noReference(
|
const example: OpenAPIV3.ExampleObject | null = noReference(
|
||||||
(
|
(() => {
|
||||||
responseObject.content?.['application/json'] ??
|
const { examples, example } = mediaTypeObject;
|
||||||
responseObject.content?.[Object.keys(responseObject.content)[0]]
|
if (examples) {
|
||||||
)?.schema,
|
const firstKey = Object.keys(examples)[0];
|
||||||
|
// @TODO handle multiple examples
|
||||||
|
const firstExample = noReference(examples[firstKey]);
|
||||||
|
if (firstExample) {
|
||||||
|
return firstExample;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (example) {
|
||||||
|
return { value: example };
|
||||||
|
}
|
||||||
|
|
||||||
|
const schema = noReference(mediaTypeObject.schema);
|
||||||
|
if (!schema) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { value: generateSchemaExample(schema) };
|
||||||
|
})(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!schema) {
|
if (!example?.value) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const example = generateSchemaExample(schema);
|
|
||||||
if (example === undefined) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
key: `${response[0]}`,
|
key: key,
|
||||||
label: `${response[0]}`,
|
label: key,
|
||||||
body: (
|
body: (
|
||||||
<context.CodeBlock
|
<context.CodeBlock
|
||||||
code={
|
code={
|
||||||
typeof example === 'string'
|
typeof example.value === 'string'
|
||||||
? example
|
? example.value
|
||||||
: stringifyOpenAPI(example, null, 2)
|
: stringifyOpenAPI(example.value, null, 2)
|
||||||
}
|
}
|
||||||
syntax="json"
|
syntax="json"
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user