-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathOpenAPIExample.tsx
55 lines (46 loc) · 1.35 KB
/
OpenAPIExample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import type { OpenAPIContext, OpenAPIUniversalContext } from './context';
import { json2xml } from './json2xml';
import { stringifyOpenAPI } from './stringifyOpenAPI';
import { t } from './translate';
/**
* Display an example.
*/
export function OpenAPIExample(props: {
example: OpenAPIV3.ExampleObject;
context: OpenAPIContext;
syntax: string;
}) {
const { example, context, syntax } = props;
const code = stringifyExample({ example, xml: syntax === 'xml' });
if (code === null) {
return <OpenAPIEmptyExample context={context} />;
}
return context.renderCodeBlock({ code, syntax });
}
function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean }): string | null {
const { example, xml } = args;
if (!example.value) {
return null;
}
if (typeof example.value === 'string') {
return example.value;
}
if (xml) {
return json2xml(example.value);
}
return stringifyOpenAPI(example.value, null, 2);
}
/**
* Empty response example.
*/
export function OpenAPIEmptyExample(props: {
context: OpenAPIUniversalContext;
}) {
const { context } = props;
return (
<pre className="openapi-example-empty">
<p>{t(context.translation, 'no_content')}</p>
</pre>
);
}