Files
gitbook/packages/react-openapi/src/OpenAPIResponseExample.tsx
T
Scott Cazan fc11ffdc8b Don't try to render responses if none are defined by the user (#255)
* don't try to parse responses if none are defined

* don't try to render responses if none exists

* move logic back into component
2024-03-13 14:37:21 +01:00

69 lines
1.8 KiB
TypeScript

import { InteractiveSection } from './InteractiveSection';
import { OpenAPIOperationData } from './fetchOpenAPIOperation';
import { generateSchemaExample } from './generateSchemaExample';
import { OpenAPIContextProps } from './types';
import { noReference } from './utils';
/**
* Display an example of the response content.
*/
export function OpenAPIResponseExample(props: {
data: OpenAPIOperationData;
context: OpenAPIContextProps;
}) {
const { data, context } = props;
// if there are no responses defined for the operation
if (!data.operation.responses) {
return null;
}
const responses = Object.entries(data.operation.responses);
// Sort response to get 200, and 2xx first
responses.sort(([a], [b]) => {
if (a === 'default') {
return 1;
}
if (b === 'default') {
return -1;
}
if (a === '200') {
return -1;
}
if (b === '200') {
return 1;
}
return Number(a) - Number(b);
});
// Take the first one
const response = responses[0];
if (!response) {
return null;
}
const responseObject = noReference(response[1]);
const schema = noReference(
(
responseObject.content?.['application/json'] ??
responseObject.content?.[Object.keys(responseObject.content)[0]]
)?.schema,
);
if (!schema) {
return null;
}
const example = generateSchemaExample(schema);
if (example === undefined) {
return null;
}
return (
<InteractiveSection header="Response" className="openapi-response-example">
<context.CodeBlock code={JSON.stringify(example, null, 2)} syntax="json" />
</InteractiveSection>
);
}