Skip to content

Commit b4d0dbc

Browse files
committed
add build
1 parent 7a261c0 commit b4d0dbc

File tree

6 files changed

+267
-5
lines changed

6 files changed

+267
-5
lines changed

packages/docusaurus-plugin-openapi-docs/lib/openapi/createSchemaExample.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,25 @@ function primitive(schema = {}) {
182182
if (type === undefined) {
183183
return;
184184
}
185-
let fn = schema.default ? () => schema.default : primitives[type].default;
186-
if (format !== undefined) {
187-
fn = primitives[type][format] || fn;
185+
// If type is an array, use the first type
186+
if (Array.isArray(type)) {
187+
type = type[0];
188+
if (type === undefined) {
189+
return;
190+
}
191+
}
192+
// Use schema default if available, otherwise use type default
193+
if (schema.default !== undefined) {
194+
return schema.default;
188195
}
189-
if (fn) {
190-
return fn(schema);
196+
const typeConfig = primitives[type];
197+
if (typeConfig) {
198+
if (format !== undefined && typeConfig[format] !== undefined) {
199+
return typeConfig[format](schema);
200+
}
201+
if (typeConfig.default !== undefined) {
202+
return typeConfig.default(schema);
203+
}
191204
}
192205
return "Unknown Type: " + schema.type;
193206
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
export declare function json2xml(o: Record<string, any>, tab: string): string;
3+
interface ResponseExamplesProps {
4+
responseExamples: any;
5+
mimeType: string;
6+
}
7+
export declare const ResponseExamples: React.FC<ResponseExamplesProps>;
8+
interface ResponseExampleProps {
9+
responseExample: any;
10+
mimeType: string;
11+
}
12+
export declare const ResponseExample: React.FC<ResponseExampleProps>;
13+
interface ExampleFromSchemaProps {
14+
schema: any;
15+
mimeType: string;
16+
}
17+
export declare const ExampleFromSchema: React.FC<ExampleFromSchemaProps>;
18+
export {};
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"use strict";
2+
/* ============================================================================
3+
* Copyright (c) Palo Alto Networks
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
* ========================================================================== */
8+
var __importDefault =
9+
(this && this.__importDefault) ||
10+
function (mod) {
11+
return mod && mod.__esModule ? mod : { default: mod };
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
exports.ExampleFromSchema =
15+
exports.ResponseExample =
16+
exports.ResponseExamples =
17+
void 0;
18+
exports.json2xml = json2xml;
19+
const react_1 = __importDefault(require("react"));
20+
const Markdown_1 = __importDefault(require("@theme/Markdown"));
21+
const ResponseSamples_1 = __importDefault(require("@theme/ResponseSamples"));
22+
const TabItem_1 = __importDefault(require("@theme/TabItem"));
23+
const createResponseExample_1 = require("docusaurus-plugin-openapi-docs/lib/openapi/createResponseExample");
24+
const xml_formatter_1 = __importDefault(require("xml-formatter"));
25+
function json2xml(o, tab) {
26+
const toXml = (v, name, ind) => {
27+
let xml = "";
28+
if (v instanceof Array) {
29+
for (let i = 0, n = v.length; i < n; i++) {
30+
xml += ind + toXml(v[i], name, ind + "\t") + "\n";
31+
}
32+
} else if (typeof v === "object") {
33+
let hasChild = false;
34+
xml += ind + "<" + name;
35+
for (const m in v) {
36+
if (m.charAt(0) === "@") {
37+
xml += " " + m.substr(1) + '="' + v[m].toString() + '"';
38+
} else {
39+
hasChild = true;
40+
}
41+
}
42+
xml += hasChild ? ">" : "/>";
43+
if (hasChild) {
44+
for (const m2 in v) {
45+
if (m2 === "#text") xml += v[m2];
46+
else if (m2 === "#cdata") xml += "<![CDATA[" + v[m2] + "]]>";
47+
else if (m2.charAt(0) !== "@") xml += toXml(v[m2], m2, ind + "\t");
48+
}
49+
xml +=
50+
(xml.charAt(xml.length - 1) === "\n" ? ind : "") + "</" + name + ">";
51+
}
52+
} else {
53+
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
54+
}
55+
return xml;
56+
};
57+
let xml = "";
58+
for (const m3 in o) xml += toXml(o[m3], m3, "");
59+
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
60+
}
61+
const ResponseExamples = ({ responseExamples, mimeType }) => {
62+
let language = "shell";
63+
if (mimeType.endsWith("json")) language = "json";
64+
if (mimeType.endsWith("xml")) language = "xml";
65+
// Map response examples to an array of TabItem elements
66+
const examplesArray = Object.entries(responseExamples).map(
67+
([exampleName, exampleValue]) => {
68+
const isObject = typeof exampleValue.value === "object";
69+
const responseExample = isObject
70+
? JSON.stringify(exampleValue.value, null, 2)
71+
: exampleValue.value;
72+
return (
73+
// @ts-ignore
74+
react_1.default.createElement(
75+
TabItem_1.default,
76+
{ label: exampleName, value: exampleName, key: exampleName },
77+
exampleValue.summary &&
78+
react_1.default.createElement(
79+
Markdown_1.default,
80+
{ className: "openapi-example__summary" },
81+
exampleValue.summary
82+
),
83+
react_1.default.createElement(ResponseSamples_1.default, {
84+
responseExample: responseExample,
85+
language: language,
86+
})
87+
)
88+
);
89+
}
90+
);
91+
return examplesArray;
92+
};
93+
exports.ResponseExamples = ResponseExamples;
94+
const ResponseExample = ({ responseExample, mimeType }) => {
95+
let language = "shell";
96+
if (mimeType.endsWith("json")) {
97+
language = "json";
98+
}
99+
if (mimeType.endsWith("xml")) {
100+
language = "xml";
101+
}
102+
const isObject = typeof responseExample === "object";
103+
const exampleContent = isObject
104+
? JSON.stringify(responseExample, null, 2)
105+
: responseExample;
106+
return (
107+
// @ts-ignore
108+
react_1.default.createElement(
109+
TabItem_1.default,
110+
{ label: "Example", value: "Example" },
111+
responseExample.summary &&
112+
react_1.default.createElement(
113+
Markdown_1.default,
114+
{ className: "openapi-example__summary" },
115+
responseExample.summary
116+
),
117+
react_1.default.createElement(ResponseSamples_1.default, {
118+
responseExample: exampleContent,
119+
language: language,
120+
})
121+
)
122+
);
123+
};
124+
exports.ResponseExample = ResponseExample;
125+
const ExampleFromSchema = ({ schema, mimeType }) => {
126+
const responseExample = (0, createResponseExample_1.sampleResponseFromSchema)(
127+
schema
128+
);
129+
if (mimeType.endsWith("xml")) {
130+
let responseExampleObject;
131+
try {
132+
responseExampleObject = JSON.parse(JSON.stringify(responseExample));
133+
} catch {
134+
return null;
135+
}
136+
if (typeof responseExampleObject === "object") {
137+
let xmlExample;
138+
try {
139+
xmlExample = (0, xml_formatter_1.default)(
140+
json2xml(responseExampleObject, ""),
141+
{
142+
indentation: " ",
143+
lineSeparator: "\n",
144+
collapseContent: true,
145+
}
146+
);
147+
} catch {
148+
const xmlExampleWithRoot = { root: responseExampleObject };
149+
try {
150+
xmlExample = (0, xml_formatter_1.default)(
151+
json2xml(xmlExampleWithRoot, ""),
152+
{
153+
indentation: " ",
154+
lineSeparator: "\n",
155+
collapseContent: true,
156+
}
157+
);
158+
} catch {
159+
xmlExample = json2xml(responseExampleObject, "");
160+
}
161+
}
162+
return (
163+
// @ts-ignore
164+
react_1.default.createElement(
165+
TabItem_1.default,
166+
{ label: "Example (auto)", value: "Example (auto)" },
167+
react_1.default.createElement(ResponseSamples_1.default, {
168+
responseExample: xmlExample,
169+
language: "xml",
170+
})
171+
)
172+
);
173+
}
174+
}
175+
if (
176+
typeof responseExample === "object" ||
177+
typeof responseExample === "string"
178+
) {
179+
return (
180+
// @ts-ignore
181+
react_1.default.createElement(
182+
TabItem_1.default,
183+
{ label: "Example (auto)", value: "Example (auto)" },
184+
react_1.default.createElement(ResponseSamples_1.default, {
185+
responseExample: JSON.stringify(responseExample, null, 2),
186+
language: "json",
187+
})
188+
)
189+
);
190+
}
191+
return null;
192+
};
193+
exports.ExampleFromSchema = ExampleFromSchema;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.openapi-code__response-samples-container {
2+
margin-top: 2rem;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
import { Language } from "prism-react-renderer";
3+
export interface Props {
4+
readonly responseExample: string;
5+
readonly language: Language;
6+
}
7+
declare function ResponseSamples({ responseExample, language, }: Props): React.JSX.Element;
8+
export default ResponseSamples;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
/* ============================================================================
3+
* Copyright (c) Palo Alto Networks
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
* ========================================================================== */
8+
var __importDefault =
9+
(this && this.__importDefault) ||
10+
function (mod) {
11+
return mod && mod.__esModule ? mod : { default: mod };
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
const react_1 = __importDefault(require("react"));
15+
const CodeBlock_1 = __importDefault(require("@theme/CodeBlock"));
16+
function ResponseSamples({ responseExample, language }) {
17+
return react_1.default.createElement(
18+
"div",
19+
{ className: "openapi-code__response-samples-container" },
20+
react_1.default.createElement(
21+
CodeBlock_1.default,
22+
{ language: language ? language : "json" },
23+
responseExample
24+
)
25+
);
26+
}
27+
exports.default = ResponseSamples;

0 commit comments

Comments
 (0)