カスタムレイアウトの作成
レイアウトは、Swagger UI がアプリケーション全体のルートコンポーネントとして使用する特別なタイプのコンポーネントです。カスタムレイアウトを定義することで、ページに表示される内容を高度に制御できます。
デフォルトでは、Swagger UI はアプリケーションに組み込まれている BaseLayout
を使用します。layout
パラメーターとしてレイアウト名を Swagger UI に渡すことで、別のレイアウトを指定できます。カスタムレイアウトを Swagger UI のコンポーネントとして必ず提供してください。
たとえば、操作のみを表示するカスタムレイアウトを作成したい場合は、OperationsLayout
を定義できます。
1import React from "react"2
3// Create the layout component4class OperationsLayout extends React.Component {5 render() {6 const {7 getComponent8 } = this.props9
10 const Operations = getComponent("operations", true)11
12 return (13 <div className="swagger-ui">14 <Operations />15 </div>16 )17 }18}19
20// Create the plugin that provides our layout component21const OperationsLayoutPlugin = () => {22 return {23 components: {24 OperationsLayout: OperationsLayout25 }26 }27}28
29// Provide the plugin to Swagger-UI, and select OperationsLayout30// as the layout for Swagger-UI31SwaggerUI({32 url: "https://petstore.swagger.io/v2/swagger.json",33 plugins: [ OperationsLayoutPlugin ],34 layout: "OperationsLayout"35})
デフォルトレイアウトの拡張
BaseLayout
を置き換えるのではなく、BaseLayout
をカスタムレイアウトに組み込んで使用することもできます。
1import React from "react"2
3// Create the layout component4class AugmentingLayout extends React.Component {5 render() {6 const {7 getComponent8 } = this.props9
10 const BaseLayout = getComponent("BaseLayout", true)11
12 return (13 <div>14 <div className="myCustomHeader">15 <h1>I have a custom header above Swagger-UI!</h1>16 </div>17 <BaseLayout />18 </div>19 )20 }21}22
23// Create the plugin that provides our layout component24const AugmentingLayoutPlugin = () => {25 return {26 components: {27 AugmentingLayout: AugmentingLayout28 }29 }30}31
32// Provide the plugin to Swagger-UI, and select AugmentingLayout33// as the layout for Swagger-UI34SwaggerUI({35 url: "https://petstore.swagger.io/v2/swagger.json",36 plugins: [ AugmentingLayoutPlugin ],37 layout: "AugmentingLayout"38})