docusaurus-plugin-openapi-docs
Version:
OpenAPI plugin for Docusaurus.
64 lines (59 loc) • 1.94 kB
text/typescript
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import { create } from "./utils";
import { ApiItem } from "../types";
export default function json2xml(o: any, tab: any) {
var toXml = function (v: any, name: string, ind: any) {
var xml = "";
if (v instanceof Array) {
for (var i = 0, n = v.length; i < n; i++)
xml += ind + toXml(v[i], name, ind + "\t") + "\n";
} else if (typeof v == "object") {
var hasChild = false;
xml += ind + "<" + name;
for (var m in v) {
if (m.charAt(0) === "@")
xml += " " + m.substr(1) + '="' + v[m].toString() + '"';
else hasChild = true;
}
xml += hasChild ? ">" : "/>";
if (hasChild) {
for (var m2 in v) {
if (m2 === "#text") xml += v[m2];
else if (m2 === "#cdata") xml += "<![CDATA[" + v[m2] + "]]>";
else if (m2.charAt(0) !== "@") xml += toXml(v[m2], m2, ind + "\t");
}
xml +=
(xml.charAt(xml.length - 1) === "\n" ? ind : "") +
"</" +
name +
">";
}
} else {
xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
}
return xml;
},
xml = "";
for (var m3 in o) xml += toXml(o[m3], m3, "");
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
}
interface Props {
id?: string;
label?: string;
responses: ApiItem["responses"];
}
export function createStatusCodes({ id, label, responses }: Props) {
return [
create("StatusCodes", {
id: id,
label: label,
responses: responses,
}),
"\n\n",
];
}