Skip to content

Commit

Permalink
chore: minor refactor of example values
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed May 12, 2019
1 parent 4550e4d commit 5297fdf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
16 changes: 2 additions & 14 deletions src/components/PayloadSamples/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import * as React from 'react';

import { StyledPre } from '../../common-elements/samples';
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 { useExternalExample } from './exernalExampleHook';

Expand All @@ -22,14 +19,13 @@ export function Example({ example, mimeType }: ExampleProps) {
}

export function ExternalExample({ example, mimeType }: ExampleProps) {
let value = useExternalExample(example, mimeType);
const value = useExternalExample(example, mimeType);

if (value === undefined) {
return <span>Loading...</span>;
}

if (value instanceof Error) {
console.log(value);
return (
<StyledPre>
Error loading external example: <br />
Expand All @@ -40,13 +36,5 @@ export function ExternalExample({ example, mimeType }: ExampleProps) {
);
}

if (isJsonLike(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} />;
}
return <ExampleValue value={value} mimeType={mimeType} />;
}
4 changes: 4 additions & 0 deletions src/components/PayloadSamples/ExampleValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function ExampleValue({ value, mimeType }: ExampleValueProps) {
if (isJsonLike(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} />;
}
}
2 changes: 1 addition & 1 deletion src/services/models/Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ExampleModel {
constructor(
parser: OpenAPIParser,
infoOrRef: Referenced<OpenAPIExample>,
mime: string,
public mime: string,
encoding?: { [field: string]: OpenAPIEncoding },
) {
const example = parser.deref(infoOrRef);
Expand Down

0 comments on commit 5297fdf

Please sign in to comment.