mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-26 18:43:43 +03:00
chore: minor refactor of example values
This commit is contained in:
parent
4550e4d61f
commit
5297fdf01a
|
@ -2,9 +2,6 @@ import * as React from 'react';
|
||||||
|
|
||||||
import { StyledPre } from '../../common-elements/samples';
|
import { StyledPre } from '../../common-elements/samples';
|
||||||
import { ExampleModel } from '../../services/models';
|
import { ExampleModel } from '../../services/models';
|
||||||
import { isJsonLike, langFromMime } from '../../utils';
|
|
||||||
import { JsonViewer } from '../JsonViewer/JsonViewer';
|
|
||||||
import { SourceCodeWithCopy } from '../SourceCode/SourceCode';
|
|
||||||
import { ExampleValue } from './ExampleValue';
|
import { ExampleValue } from './ExampleValue';
|
||||||
import { useExternalExample } from './exernalExampleHook';
|
import { useExternalExample } from './exernalExampleHook';
|
||||||
|
|
||||||
|
@ -22,14 +19,13 @@ export function Example({ example, mimeType }: ExampleProps) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ExternalExample({ example, mimeType }: ExampleProps) {
|
export function ExternalExample({ example, mimeType }: ExampleProps) {
|
||||||
let value = useExternalExample(example, mimeType);
|
const value = useExternalExample(example, mimeType);
|
||||||
|
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
return <span>Loading...</span>;
|
return <span>Loading...</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof Error) {
|
if (value instanceof Error) {
|
||||||
console.log(value);
|
|
||||||
return (
|
return (
|
||||||
<StyledPre>
|
<StyledPre>
|
||||||
Error loading external example: <br />
|
Error loading external example: <br />
|
||||||
|
@ -40,13 +36,5 @@ export function ExternalExample({ example, mimeType }: ExampleProps) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isJsonLike(mimeType)) {
|
return <ExampleValue value={value} mimeType={mimeType} />;
|
||||||
return <JsonViewer data={value} />;
|
|
||||||
} else {
|
|
||||||
if (typeof value === 'object') {
|
|
||||||
// just in case example was cached as json but used as non-json
|
|
||||||
value = JSON.stringify(value, null, 2);
|
|
||||||
}
|
|
||||||
return <SourceCodeWithCopy lang={langFromMime(mimeType)} source={value} />;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,10 @@ export function ExampleValue({ value, mimeType }: ExampleValueProps) {
|
||||||
if (isJsonLike(mimeType)) {
|
if (isJsonLike(mimeType)) {
|
||||||
return <JsonViewer data={value} />;
|
return <JsonViewer data={value} />;
|
||||||
} else {
|
} else {
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
// just in case example was cached as json but used as non-json
|
||||||
|
value = JSON.stringify(value, null, 2);
|
||||||
|
}
|
||||||
return <SourceCodeWithCopy lang={langFromMime(mimeType)} source={value} />;
|
return <SourceCodeWithCopy lang={langFromMime(mimeType)} source={value} />;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ export class ExampleModel {
|
||||||
constructor(
|
constructor(
|
||||||
parser: OpenAPIParser,
|
parser: OpenAPIParser,
|
||||||
infoOrRef: Referenced<OpenAPIExample>,
|
infoOrRef: Referenced<OpenAPIExample>,
|
||||||
mime: string,
|
public mime: string,
|
||||||
encoding?: { [field: string]: OpenAPIEncoding },
|
encoding?: { [field: string]: OpenAPIEncoding },
|
||||||
) {
|
) {
|
||||||
const example = parser.deref(infoOrRef);
|
const example = parser.deref(infoOrRef);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user