|
| 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 BrowserOnly from "@docusaurus/BrowserOnly"; |
| 11 | +import Details from "@theme/Details"; |
| 12 | +import { |
| 13 | + ExampleFromSchema, |
| 14 | + MimeExample, |
| 15 | + MimeExamples, |
| 16 | + SchemaExample, |
| 17 | + SchemaExamples, |
| 18 | +} from "@theme/Examples"; |
| 19 | +import Markdown from "@theme/Markdown"; |
| 20 | +import MimeTabs from "@theme/MimeTabs"; |
| 21 | +import SchemaNode from "@theme/Schema"; |
| 22 | +import SchemaTabs from "@theme/SchemaTabs"; |
| 23 | +import SkeletonLoader from "@theme/SkeletonLoader"; |
| 24 | +import TabItem from "@theme/TabItem"; |
| 25 | +import { MediaTypeObject } from "docusaurus-plugin-openapi-docs/lib/openapi/types"; |
| 26 | + |
| 27 | +interface Props { |
| 28 | + style?: React.CSSProperties; |
| 29 | + title: string; |
| 30 | + body: { |
| 31 | + content?: { |
| 32 | + [key: string]: MediaTypeObject; |
| 33 | + }; |
| 34 | + description?: string; |
| 35 | + required?: string[] | boolean; |
| 36 | + }; |
| 37 | + schemaType: "request" | "response"; |
| 38 | +} |
| 39 | + |
| 40 | +const BaseSchemaComponent: React.FC<Props> = ({ |
| 41 | + title, |
| 42 | + body, |
| 43 | + style, |
| 44 | + schemaType, |
| 45 | +}) => { |
| 46 | + if ( |
| 47 | + body === undefined || |
| 48 | + body.content === undefined || |
| 49 | + Object.keys(body).length === 0 || |
| 50 | + Object.keys(body.content).length === 0 |
| 51 | + ) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const mimeTypes = Object.keys(body.content); |
| 56 | + if (mimeTypes && mimeTypes.length) { |
| 57 | + return ( |
| 58 | + <MimeTabs className="openapi-tabs__mime" schemaType={schemaType}> |
| 59 | + {mimeTypes.map((mimeType: any) => { |
| 60 | + const mimeExamples = body.content![mimeType].examples; |
| 61 | + const mimeExample = body.content![mimeType].example; |
| 62 | + const schemaExamples = body.content![mimeType].schema?.examples; |
| 63 | + const schemaExample = body.content![mimeType].schema?.example; |
| 64 | + const firstBody = body.content![mimeType].schema; |
| 65 | + |
| 66 | + if ( |
| 67 | + firstBody === undefined || |
| 68 | + (firstBody.properties && |
| 69 | + Object.keys(firstBody.properties).length === 0) |
| 70 | + ) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + if (firstBody) { |
| 75 | + const tabTitle = "Schema"; |
| 76 | + return ( |
| 77 | + // @ts-ignore |
| 78 | + <TabItem key={mimeType} label={mimeType} value={mimeType}> |
| 79 | + <SchemaTabs className="openapi-tabs__schema"> |
| 80 | + {/* @ts-ignore */} |
| 81 | + <TabItem key={tabTitle} label={tabTitle} value={tabTitle}> |
| 82 | + <Details |
| 83 | + className="openapi-markdown__details mime" |
| 84 | + data-collapsed={false} |
| 85 | + open={true} |
| 86 | + style={style} |
| 87 | + summary={ |
| 88 | + <> |
| 89 | + <summary> |
| 90 | + <strong className="openapi-markdown__details-summary-header-body"> |
| 91 | + {title} |
| 92 | + {body.required && ( |
| 93 | + <strong className="openapi-schema__required"> |
| 94 | + required |
| 95 | + </strong> |
| 96 | + )} |
| 97 | + </strong> |
| 98 | + </summary> |
| 99 | + </> |
| 100 | + } |
| 101 | + > |
| 102 | + <div style={{ textAlign: "left", marginLeft: "1rem" }}> |
| 103 | + {body.description && ( |
| 104 | + <div |
| 105 | + style={{ marginTop: "1rem", marginBottom: "1rem" }} |
| 106 | + > |
| 107 | + <Markdown>{body.description}</Markdown> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + <ul style={{ marginLeft: "1rem" }}> |
| 112 | + <SchemaNode |
| 113 | + schema={firstBody} |
| 114 | + schemaType={schemaType} |
| 115 | + /> |
| 116 | + </ul> |
| 117 | + </Details> |
| 118 | + </TabItem> |
| 119 | + {firstBody && |
| 120 | + ExampleFromSchema({ |
| 121 | + schema: firstBody, |
| 122 | + mimeType, |
| 123 | + context: { type: schemaType }, |
| 124 | + })} |
| 125 | + |
| 126 | + {mimeExamples && |
| 127 | + MimeExamples({ examples: mimeExamples, mimeType })} |
| 128 | + |
| 129 | + {mimeExample && |
| 130 | + MimeExample({ example: mimeExample, mimeType })} |
| 131 | + |
| 132 | + {schemaExamples && |
| 133 | + SchemaExamples({ examples: schemaExamples, mimeType })} |
| 134 | + |
| 135 | + {schemaExample && |
| 136 | + SchemaExample({ example: schemaExample, mimeType })} |
| 137 | + </SchemaTabs> |
| 138 | + </TabItem> |
| 139 | + ); |
| 140 | + } |
| 141 | + return null; |
| 142 | + })} |
| 143 | + </MimeTabs> |
| 144 | + ); |
| 145 | + } |
| 146 | + return null; |
| 147 | +}; |
| 148 | + |
| 149 | +const BaseSchema: React.FC<Props> = (props) => { |
| 150 | + return ( |
| 151 | + <BrowserOnly fallback={<SkeletonLoader size="sm" />}> |
| 152 | + {() => { |
| 153 | + return <BaseSchemaComponent {...props} />; |
| 154 | + }} |
| 155 | + </BrowserOnly> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default BaseSchema; |
0 commit comments