Skip to content

Commit 808e9b1

Browse files
committed
refactor Examples
1 parent b544206 commit 808e9b1

File tree

5 files changed

+310
-439
lines changed

5 files changed

+310
-439
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/* ============================================================================
2+
* Copyright (c) Palo Alto Networks
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import React from "react";
9+
10+
import Markdown from "@theme/Markdown";
11+
import ResponseSamples from "@theme/ResponseSamples";
12+
import TabItem from "@theme/TabItem";
13+
import { sampleResponseFromSchema } from "docusaurus-plugin-openapi-docs/lib/openapi/createResponseExample";
14+
import format from "xml-formatter";
15+
16+
export function json2xml(o: Record<string, any>, tab: string): string {
17+
const toXml = (v: any, name: string, ind: string): string => {
18+
let xml = "";
19+
if (v instanceof Array) {
20+
for (let i = 0, n = v.length; i < n; i++) {
21+
xml += ind + toXml(v[i], name, ind + "\t") + "\n";
22+
}
23+
} else if (typeof v === "object") {
24+
let hasChild = false;
25+
xml += ind + "<" + name;
26+
for (const m in v) {
27+
if (m.charAt(0) === "@") {
28+
xml += " " + m.substr(1) + '="' + v[m].toString() + '"';
29+
} else {
30+
hasChild = true;
31+
}
32+
}
33+
xml += hasChild ? ">" : "/>";
34+
if (hasChild) {
35+
for (const m2 in v) {
36+
if (m2 === "#text") xml += v[m2];
37+
else if (m2 === "#cdata") xml += "<![CDATA[" + v[m2] + "]]>";
38+
else if (m2.charAt(0) !== "@") xml += toXml(v[m2], m2, ind + "\t");
39+
}
40+
xml +=
41+
(xml.charAt(xml.length - 1) === "\n" ? ind : "") + "</" + name + ">";
42+
}
43+
} else {
44+
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
45+
}
46+
return xml;
47+
};
48+
let xml = "";
49+
for (const m3 in o) xml += toXml(o[m3], m3, "");
50+
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
51+
}
52+
53+
export function getLanguageFromMimeType(mimeType: string): string {
54+
let language = "shell";
55+
if (mimeType.endsWith("json")) language = "json";
56+
if (mimeType.endsWith("xml")) language = "xml";
57+
return language;
58+
}
59+
60+
export interface MimeExampleProps {
61+
example: any;
62+
mimeType: string;
63+
}
64+
65+
export const MimeExample: React.FC<MimeExampleProps> = ({
66+
example,
67+
mimeType,
68+
}) => {
69+
const language = getLanguageFromMimeType(mimeType);
70+
71+
const isObject = typeof example === "object";
72+
const exampleContent = isObject ? JSON.stringify(example, null, 2) : example;
73+
74+
return (
75+
// @ts-ignore
76+
<TabItem label="Example" value="Example">
77+
{example.summary && (
78+
<Markdown className="openapi-example__summary">
79+
{example.summary}
80+
</Markdown>
81+
)}
82+
<ResponseSamples responseExample={exampleContent} language={language} />
83+
</TabItem>
84+
);
85+
};
86+
87+
export interface MimeExamplesProps {
88+
examples: any;
89+
mimeType: string;
90+
}
91+
92+
export const MimeExamples: React.FC<MimeExamplesProps> = ({
93+
examples,
94+
mimeType,
95+
}): any => {
96+
const language = getLanguageFromMimeType(mimeType);
97+
98+
// Map examples to an array of TabItem elements
99+
const examplesArray = Object.entries(examples).map(
100+
([exampleName, exampleValue]: any) => {
101+
const isObject = typeof exampleValue.value === "object";
102+
const exampleContent = isObject
103+
? JSON.stringify(exampleValue.value, null, 2)
104+
: exampleValue.value;
105+
106+
return (
107+
// @ts-ignore
108+
<TabItem label={exampleName} value={exampleName} key={exampleName}>
109+
{exampleValue.summary && (
110+
<Markdown className="openapi-example__summary">
111+
{exampleValue.summary}
112+
</Markdown>
113+
)}
114+
<ResponseSamples
115+
responseExample={exampleContent}
116+
language={language}
117+
/>
118+
</TabItem>
119+
);
120+
}
121+
);
122+
123+
return examplesArray;
124+
};
125+
126+
export interface SchemaExampleProps {
127+
example: any;
128+
mimeType: string;
129+
}
130+
131+
export const SchemaExample: React.FC<SchemaExampleProps> = ({
132+
example,
133+
mimeType,
134+
}) => {
135+
const language = getLanguageFromMimeType(mimeType);
136+
137+
const isObject = typeof example === "object";
138+
const exampleContent = isObject ? JSON.stringify(example, null, 2) : example;
139+
140+
return (
141+
// @ts-ignore
142+
<TabItem label="Example" value="Example">
143+
{example.summary && (
144+
<Markdown className="openapi-example__summary">
145+
{example.summary}
146+
</Markdown>
147+
)}
148+
<ResponseSamples responseExample={exampleContent} language={language} />
149+
</TabItem>
150+
);
151+
};
152+
153+
export interface SchemaExamplesProps {
154+
examples: any[];
155+
mimeType: string;
156+
}
157+
158+
export const SchemaExamples: React.FC<SchemaExamplesProps> = ({
159+
examples,
160+
mimeType,
161+
}) => {
162+
const language = getLanguageFromMimeType(mimeType);
163+
164+
// Map examples to an array of TabItem elements
165+
const examplesArray = examples.map((example: any, i: number) => {
166+
const exampleName = `Example ${i + 1}`;
167+
const isObject = typeof example === "object";
168+
const exampleContent = isObject
169+
? JSON.stringify(example, null, 2)
170+
: example;
171+
172+
return (
173+
// @ts-ignore
174+
<TabItem label={exampleName} value={exampleName} key={exampleName}>
175+
<ResponseSamples responseExample={exampleContent} language={language} />
176+
</TabItem>
177+
);
178+
});
179+
180+
return examplesArray;
181+
};
182+
183+
export interface ExampleFromSchemaProps {
184+
schema: any;
185+
mimeType: string;
186+
}
187+
188+
export const ExampleFromSchema: React.FC<ExampleFromSchemaProps> = ({
189+
schema,
190+
mimeType,
191+
}) => {
192+
const example = sampleResponseFromSchema(schema);
193+
194+
if (mimeType.endsWith("xml")) {
195+
let exampleObject;
196+
try {
197+
exampleObject = JSON.parse(JSON.stringify(example));
198+
} catch {
199+
return null;
200+
}
201+
202+
if (typeof exampleObject === "object") {
203+
let xmlExample;
204+
try {
205+
xmlExample = format(json2xml(exampleObject, ""), {
206+
indentation: " ",
207+
lineSeparator: "\n",
208+
collapseContent: true,
209+
});
210+
} catch {
211+
const xmlExampleWithRoot = { root: exampleObject };
212+
try {
213+
xmlExample = format(json2xml(xmlExampleWithRoot, ""), {
214+
indentation: " ",
215+
lineSeparator: "\n",
216+
collapseContent: true,
217+
});
218+
} catch {
219+
xmlExample = json2xml(exampleObject, "");
220+
}
221+
}
222+
return (
223+
// @ts-ignore
224+
<TabItem label="Example (auto)" value="Example (auto)">
225+
<ResponseSamples responseExample={xmlExample} language="xml" />
226+
</TabItem>
227+
);
228+
}
229+
}
230+
231+
if (typeof example === "object" || typeof example === "string") {
232+
return (
233+
// @ts-ignore
234+
<TabItem label="Example (auto)" value="Example (auto)">
235+
<ResponseSamples
236+
responseExample={JSON.stringify(example, null, 2)}
237+
language="json"
238+
/>
239+
</TabItem>
240+
);
241+
}
242+
243+
return null;
244+
};

0 commit comments

Comments
 (0)