/* ============================================================================ * 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 React from "react"; import { useDoc } from "@docusaurus/plugin-content-docs/client"; import { usePrismTheme } from "@docusaurus/theme-common"; import Translate, { translate } from "@docusaurus/Translate"; import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; import ApiCodeBlock from "@theme/ApiExplorer/ApiCodeBlock"; import { useTypedDispatch, useTypedSelector } from "@theme/ApiItem/hooks"; import SchemaTabs from "@theme/SchemaTabs"; import TabItem from "@theme/TabItem"; import { OPENAPI_REQUEST, OPENAPI_RESPONSE } from "@theme/translationIds"; import clsx from "clsx"; import type { ApiItem } from "docusaurus-plugin-openapi-docs/src/types"; import type { ThemeConfig } from "docusaurus-theme-openapi-docs/src/types"; import { clearResponse, clearCode, clearHeaders } from "./slice"; // TODO: We probably shouldn't attempt to format XML... function formatXml(xml: string) { const tab = " "; let formatted = ""; let indent = ""; xml.split(/>\s*).forEach((node) => { if (node.match(/^\/\w/)) { // decrease indent by one 'tab' indent = indent.substring(tab.length); } formatted += indent + "<" + node + ">\r\n"; if (node.match(/^\w[^>]*[^/]$/)) { // increase indent indent += tab; } }); return formatted.substring(1, formatted.length - 3); } function Response({ item }: { item: ApiItem }) { const metadata = useDoc(); const { siteConfig } = useDocusaurusContext(); const themeConfig = siteConfig.themeConfig as ThemeConfig; const hideSendButton = metadata.frontMatter.hide_send_button; const proxy = metadata.frontMatter.proxy ?? themeConfig.api?.proxy; const prismTheme = usePrismTheme(); const code = useTypedSelector((state: any) => state.response.code); const headers = useTypedSelector((state: any) => state.response.headers); const response = useTypedSelector((state: any) => state.response.value); const dispatch = useTypedDispatch(); const responseStatusClass = code && "openapi-response__dot " + (parseInt(code) >= 400 ? "openapi-response__dot--danger" : parseInt(code) >= 200 && parseInt(code) < 300 ? "openapi-response__dot--success" : "openapi-response__dot--info"); if ((!item.servers && !proxy) || hideSendButton) { return null; } let prettyResponse: string = response; if (prettyResponse) { try { prettyResponse = JSON.stringify(JSON.parse(response), null, 2); } catch { if (response.startsWith("<")) { prettyResponse = formatXml(response); } } } return (