|
| 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; |
0 commit comments