api
This commit is contained in:
92
frontend/web/api-doc/docs/customization/custom-layout.md
Normal file
92
frontend/web/api-doc/docs/customization/custom-layout.md
Normal file
@@ -0,0 +1,92 @@
|
||||
# Creating a custom layout
|
||||
|
||||
**Layouts** are a special type of component that Swagger UI uses as the root component for the entire application. You can define custom layouts in order to have high-level control over what ends up on the page.
|
||||
|
||||
By default, Swagger UI uses `BaseLayout`, which is built into the application. You can specify a different layout to be used by passing the layout's name as the `layout` parameter to Swagger UI. Be sure to provide your custom layout as a component to Swagger UI.
|
||||
|
||||
<br>
|
||||
|
||||
For example, if you wanted to create a custom layout that only displayed operations, you could define an `OperationsLayout`:
|
||||
|
||||
```js
|
||||
import React from "react"
|
||||
|
||||
// Create the layout component
|
||||
class OperationsLayout extends React.Component {
|
||||
render() {
|
||||
const {
|
||||
getComponent
|
||||
} = this.props
|
||||
|
||||
const Operations = getComponent("operations", true)
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Operations />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Create the plugin that provides our layout component
|
||||
const OperationsLayoutPlugin = () => {
|
||||
return {
|
||||
components: {
|
||||
OperationsLayout: OperationsLayout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Provide the plugin to Swagger-UI, and select OperationsLayout
|
||||
// as the layout for Swagger-UI
|
||||
SwaggerUI({
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
plugins: [ OperationsLayoutPlugin ],
|
||||
layout: "OperationsLayout"
|
||||
})
|
||||
```
|
||||
|
||||
### Augmenting the default layout
|
||||
|
||||
If you'd like to build around the `BaseLayout` instead of replacing it, you can pull the `BaseLayout` into your custom layout and use it:
|
||||
|
||||
```js
|
||||
import React from "react"
|
||||
|
||||
// Create the layout component
|
||||
class AugmentingLayout extends React.Component {
|
||||
render() {
|
||||
const {
|
||||
getComponent
|
||||
} = this.props
|
||||
|
||||
const BaseLayout = getComponent("BaseLayout", true)
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="myCustomHeader">
|
||||
<h1>I have a custom header above Swagger-UI!</h1>
|
||||
</div>
|
||||
<BaseLayout />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Create the plugin that provides our layout component
|
||||
const AugmentingLayoutPlugin = () => {
|
||||
return {
|
||||
components: {
|
||||
AugmentingLayout: AugmentingLayout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Provide the plugin to Swagger-UI, and select AugmentingLayout
|
||||
// as the layout for Swagger-UI
|
||||
SwaggerUI({
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
plugins: [ AugmentingLayoutPlugin ],
|
||||
layout: "AugmentingLayout"
|
||||
})
|
||||
```
|
71
frontend/web/api-doc/docs/customization/overview.md
Normal file
71
frontend/web/api-doc/docs/customization/overview.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Plugin system overview
|
||||
|
||||
### Prior art
|
||||
|
||||
Swagger UI leans heavily on concepts and patterns found in React and Redux.
|
||||
|
||||
If you aren't already familiar, here's some suggested reading:
|
||||
|
||||
- [React: Quick Start (reactjs.org)](https://reactjs.org/docs/hello-world.html)
|
||||
- [Redux README (redux.js.org)](https://redux.js.org/)
|
||||
|
||||
In the following documentation, we won't take the time to define the fundamentals covered in the resources above.
|
||||
|
||||
> **Note**: Some of the examples in this section contain JSX, which is a syntax extension to JavaScript that is useful for writing React components.
|
||||
>
|
||||
> If you don't want to set up a build pipeline capable of translating JSX to JavaScript, take a look at [React without JSX (reactjs.org)](https://reactjs.org/docs/react-without-jsx.html). You can use our `system.React` reference to leverage React without needing to pull a copy into your project.
|
||||
|
||||
### The System
|
||||
|
||||
The _system_ is the heart of the Swagger UI application. At runtime, it's a JavaScript object that holds many things:
|
||||
|
||||
- React components
|
||||
- Bound Redux actions and reducers
|
||||
- Bound Reselect state selectors
|
||||
- System-wide collection of available components
|
||||
- Built-in helpers like `getComponent`, `makeMappedContainer`, and `getStore`
|
||||
- References to the React and Immutable.js libraries (`system.React`, `system.Im`)
|
||||
- User-defined helper functions
|
||||
|
||||
The system is built up when Swagger UI is called by iterating through ("compiling") each plugin that Swagger UI has been given, through the `presets` and `plugins` configuration options.
|
||||
|
||||
### Presets
|
||||
|
||||
Presets are arrays of plugins, which are provided to Swagger UI through the `presets` configuration option. All plugins within presets are compiled before any plugins provided via the `plugins` configuration option. Consider the following example:
|
||||
|
||||
```javascript
|
||||
const MyPreset = [FirstPlugin, SecondPlugin, ThirdPlugin]
|
||||
|
||||
SwaggerUI({
|
||||
presets: [
|
||||
MyPreset
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
By default, Swagger UI includes the internal `ApisPreset`, which contains a set of plugins that provide baseline functionality for Swagger UI. If you specify your own `presets` option, you need to add the ApisPreset manually, like so:
|
||||
|
||||
```javascript
|
||||
SwaggerUI({
|
||||
presets: [
|
||||
SwaggerUI.presets.apis,
|
||||
MyAmazingCustomPreset
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
The need to provide the `apis` preset when adding other presets is an artifact of Swagger UI's original design, and will likely be removed in the next major version.
|
||||
|
||||
### getComponent
|
||||
|
||||
`getComponent` is a helper function injected into every container component, which is used to get references to components provided by the plugin system.
|
||||
|
||||
All components should be loaded through `getComponent`, since it allows other plugins to modify the component. It is preferred over a conventional `import` statement.
|
||||
|
||||
Container components in Swagger UI can be loaded by passing `true` as the second argument to `getComponent`, like so:
|
||||
|
||||
```javascript
|
||||
getComponent("ContainerComponentName", true)
|
||||
```
|
||||
|
||||
This will map the current system as props to the component.
|
415
frontend/web/api-doc/docs/customization/plug-points.md
Normal file
415
frontend/web/api-doc/docs/customization/plug-points.md
Normal file
@@ -0,0 +1,415 @@
|
||||
# Plug points
|
||||
|
||||
Swagger UI exposes most of its internal logic through the plugin system.
|
||||
|
||||
Often, it is beneficial to override the core internals to achieve custom behavior.
|
||||
|
||||
### Note: Semantic Versioning
|
||||
|
||||
Swagger UI's internal APIs are _not_ part of our public contract, which means that they can change without the major version change.
|
||||
|
||||
If your custom plugins wrap, extend, override, or consume any internal core APIs, we recommend specifying a specific minor version of Swagger UI to use in your application, because they will _not_ change between patch versions.
|
||||
|
||||
If you're installing Swagger UI via NPM, for example, you can do this by using a tilde:
|
||||
|
||||
```js
|
||||
{
|
||||
"dependencies": {
|
||||
"swagger-ui": "~3.11.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### `fn.opsFilter`
|
||||
|
||||
When using the `filter` option, tag names will be filtered by the user-provided value. If you'd like to customize this behavior, you can override the default `opsFilter` function.
|
||||
|
||||
For example, you can implement a multiple-phrase filter:
|
||||
|
||||
```js
|
||||
const MultiplePhraseFilterPlugin = function() {
|
||||
return {
|
||||
fn: {
|
||||
opsFilter: (taggedOps, phrase) => {
|
||||
const phrases = phrase.split(", ")
|
||||
|
||||
return taggedOps.filter((val, key) => {
|
||||
return phrases.some(item => key.indexOf(item) > -1)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
### Logo component
|
||||
While using the Standalone Preset the SwaggerUI logo is rendered in the Top Bar.
|
||||
The logo can be exchanged by replacing the `Logo` component via the plugin api:
|
||||
|
||||
```jsx
|
||||
import React from "react";
|
||||
const MyLogoPlugin = {
|
||||
components: {
|
||||
Logo: () => (
|
||||
<img alt="My Logo" height="40" src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTM3IiBoZWlnaHQ9IjEzNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KCiA8Zz4KICA8dGl0bGU+TGF5ZXIgMTwvdGl0bGU+CiAgPHRleHQgdHJhbnNmb3JtPSJtYXRyaXgoMy40Nzc2OSAwIDAgMy4yNjA2NyAtNjczLjEyOCAtNjkxLjk5MykiIHN0cm9rZT0iIzAwMCIgZm9udC1zdHlsZT0ibm9ybWFsIiBmb250LXdlaWdodD0ibm9ybWFsIiB4bWw6c3BhY2U9InByZXNlcnZlIiB0ZXh0LWFuY2hvcj0ic3RhcnQiIGZvbnQtZmFtaWx5PSInT3BlbiBTYW5zIEV4dHJhQm9sZCciIGZvbnQtc2l6ZT0iMjQiIGlkPSJzdmdfMSIgeT0iMjQxLjIyMTkyIiB4PSIxOTYuOTY5MjEiIHN0cm9rZS13aWR0aD0iMCIgZmlsbD0iIzYyYTAzZiI+TXkgTG9nbzwvdGV4dD4KICA8cGF0aCBpZD0ic3ZnXzIiIGQ9Im0zOTUuNjAyNSw1MS4xODM1OWw1My44Nzc3MSwwbDE2LjY0ODYzLC01MS4xODM1OGwxNi42NDg2NCw1MS4xODM1OGw1My44Nzc3LDBsLTQzLjU4NzksMzEuNjMyODNsMTYuNjQ5NDksNTEuMTgzNThsLTQzLjU4NzkyLC0zMS42MzM2OWwtNDMuNTg3OTEsMzEuNjMzNjlsMTYuNjQ5NDksLTUxLjE4MzU4bC00My41ODc5MiwtMzEuNjMyODN6IiBzdHJva2Utd2lkdGg9IjAiIHN0cm9rZT0iIzAwMCIgZmlsbD0iIzYyYTAzZiIvPgogPC9nPgo8L3N2Zz4="/>
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### JSON Schema components
|
||||
In swagger there are so called JSON Schema components. These are used to render inputs for parameters and components of request bodies with `application/x-www-form-urlencoded` or `multipart/*` media-type.
|
||||
|
||||
Internally swagger uses following mapping to find the JSON Schema component from OpenAPI Specification schema information:
|
||||
|
||||
For each schema’s type(eg. `string`, `array`, …) and if defined schema’s format (eg. ‘date’, ‘uuid’, …) there is a corresponding component mapping:
|
||||
|
||||
**If format defined:**
|
||||
```js
|
||||
`JsonSchema_${type}_${format}`
|
||||
```
|
||||
|
||||
**Fallback if `JsonSchema_${type}_${format}` component does not exist or format not defined:**
|
||||
```js
|
||||
`JsonSchema_${type}`
|
||||
```
|
||||
|
||||
**Default:**
|
||||
```js
|
||||
`JsonSchema_string`
|
||||
```
|
||||
|
||||
With this, one can define custom input components or override existing.
|
||||
|
||||
#### Example Date-Picker plugin
|
||||
|
||||
If one would like to input date values you could provide a custom plugin to integrate [react-datepicker](https://www.npmjs.com/package/react-datepicker) into swagger-ui.
|
||||
All you need to do is to create a component to wrap [react-datepicker](https://www.npmjs.com/package/react-datepicker) accordingly to the format.
|
||||
|
||||
**There are two cases:**
|
||||
- ```yaml
|
||||
type: string
|
||||
format: date
|
||||
```
|
||||
The resulting name for mapping to succeed: `JsonSchema_string_date`
|
||||
- ```yaml
|
||||
type: string
|
||||
format: date-time
|
||||
```
|
||||
The resulting name for mapping to succeed: `JsonSchema_string_date-time`
|
||||
|
||||
This creates the need for two components and simple logic to strip any time input in case the format is date:
|
||||
```js
|
||||
import React from "react";
|
||||
import DatePicker from "react-datepicker";
|
||||
import "react-datepicker/dist/react-datepicker.css";
|
||||
|
||||
const JsonSchema_string_date = (props) => {
|
||||
const dateNumber = Date.parse(props.value);
|
||||
const date = dateNumber
|
||||
? new Date(dateNumber)
|
||||
: new Date();
|
||||
|
||||
return (
|
||||
<DatePicker
|
||||
selected={date}
|
||||
onChange={d => props.onChange(d.toISOString().substring(0, 10))}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const JsonSchema_string_date_time = (props) => {
|
||||
const dateNumber = Date.parse(props.value);
|
||||
const date = dateNumber
|
||||
? new Date(dateNumber)
|
||||
: new Date();
|
||||
|
||||
return (
|
||||
<DatePicker
|
||||
selected={date}
|
||||
onChange={d => props.onChange(d.toISOString())}
|
||||
showTimeSelect
|
||||
timeFormat="p"
|
||||
dateFormat="Pp"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export const DateTimeSwaggerPlugin = {
|
||||
components: {
|
||||
JsonSchema_string_date: JsonSchema_string_date,
|
||||
"JsonSchema_string_date-time": JsonSchema_string_date_time
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Request Snippets
|
||||
|
||||
SwaggerUI can be configured with the `requestSnippetsEnabled: true` option to activate Request Snippets.
|
||||
Instead of the generic curl that is generated upon doing a request. It gives you more granular options:
|
||||
- curl for bash
|
||||
- curl for cmd
|
||||
- curl for powershell
|
||||
|
||||
There might be the case where you want to provide your own snipped generator. This can be done by using the plugin api.
|
||||
A Request Snipped generator consists of the configuration and a `fn`,
|
||||
which takes the internal request object and transforms it to the desired snippet.
|
||||
|
||||
```js
|
||||
// Add config to Request Snippets Configuration with an unique key like "node_native"
|
||||
const snippetConfig = {
|
||||
requestSnippetsEnabled: true,
|
||||
requestSnippets: {
|
||||
generators: {
|
||||
"node_native": {
|
||||
title: "NodeJs Native",
|
||||
syntax: "javascript"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SnippedGeneratorNodeJsPlugin = {
|
||||
fn: {
|
||||
// use `requestSnippetGenerator_` + key from config (node_native) for generator fn
|
||||
requestSnippetGenerator_node_native: (request) => {
|
||||
const url = new Url(request.get("url"))
|
||||
let isMultipartFormDataRequest = false
|
||||
const headers = request.get("headers")
|
||||
if(headers && headers.size) {
|
||||
request.get("headers").map((val, key) => {
|
||||
isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(key) && /^multipart\/form-data$/i.test(val)
|
||||
})
|
||||
}
|
||||
const packageStr = url.protocol === "https:" ? "https" : "http"
|
||||
let reqBody = request.get("body")
|
||||
if (request.get("body")) {
|
||||
if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
|
||||
return "throw new Error(\"Currently unsupported content-type: /^multipart\\/form-data$/i\");"
|
||||
} else {
|
||||
if (!Map.isMap(reqBody)) {
|
||||
if (typeof reqBody !== "string") {
|
||||
reqBody = JSON.stringify(reqBody)
|
||||
}
|
||||
} else {
|
||||
reqBody = getStringBodyOfMap(request)
|
||||
}
|
||||
}
|
||||
} else if (!request.get("body") && request.get("method") === "POST") {
|
||||
reqBody = ""
|
||||
}
|
||||
|
||||
const stringBody = "`" + (reqBody || "")
|
||||
.replace(/\\n/g, "\n")
|
||||
.replace(/`/g, "\\`")
|
||||
+ "`"
|
||||
|
||||
return `const http = require("${packageStr}");
|
||||
const options = {
|
||||
"method": "${request.get("method")}",
|
||||
"hostname": "${url.host}",
|
||||
"port": ${url.port || "null"},
|
||||
"path": "${url.pathname}"${headers && headers.size ? `,
|
||||
"headers": {
|
||||
${request.get("headers").map((val, key) => `"${key}": "${val}"`).valueSeq().join(",\n ")}
|
||||
}` : ""}
|
||||
};
|
||||
const req = http.request(options, function (res) {
|
||||
const chunks = [];
|
||||
res.on("data", function (chunk) {
|
||||
chunks.push(chunk);
|
||||
});
|
||||
res.on("end", function () {
|
||||
const body = Buffer.concat(chunks);
|
||||
console.log(body.toString());
|
||||
});
|
||||
});
|
||||
${reqBody ? `\nreq.write(${stringBody});` : ""}
|
||||
req.end();`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ui = SwaggerUIBundle({
|
||||
"dom_id": "#swagger-ui",
|
||||
deepLinking: true,
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [
|
||||
SwaggerUIBundle.plugins.DownloadUrl,
|
||||
SnippedGeneratorNodeJsPlugin
|
||||
],
|
||||
layout: "StandaloneLayout",
|
||||
validatorUrl: "https://validator.swagger.io/validator",
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
...snippetConfig,
|
||||
})
|
||||
```
|
||||
|
||||
### Error handling
|
||||
|
||||
SwaggerUI comes with a `safe-render` plugin that handles error handling allows plugging into error handling system and modify it.
|
||||
|
||||
The plugin accepts a list of component names that should be protected by error boundaries.
|
||||
|
||||
Its public API looks like this:
|
||||
|
||||
```js
|
||||
{
|
||||
fn: {
|
||||
componentDidCatch,
|
||||
withErrorBoundary: withErrorBoundary(getSystem),
|
||||
},
|
||||
components: {
|
||||
ErrorBoundary,
|
||||
Fallback,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
safe-render plugin is automatically utilized by [base](https://github.com/swagger-api/swagger-ui/blob/78f62c300a6d137e65fd027d850981b010009970/src/core/presets/base.js) and [standalone](https://github.com/swagger-api/swagger-ui/tree/78f62c300a6d137e65fd027d850981b010009970/src/standalone) SwaggerUI presets and
|
||||
should always be used as the last plugin, after all the components are already known to the SwaggerUI.
|
||||
The plugin defines a default list of components that should be protected by error boundaries:
|
||||
|
||||
```js
|
||||
[
|
||||
"App",
|
||||
"BaseLayout",
|
||||
"VersionPragmaFilter",
|
||||
"InfoContainer",
|
||||
"ServersContainer",
|
||||
"SchemesContainer",
|
||||
"AuthorizeBtnContainer",
|
||||
"FilterContainer",
|
||||
"Operations",
|
||||
"OperationContainer",
|
||||
"parameters",
|
||||
"responses",
|
||||
"OperationServers",
|
||||
"Models",
|
||||
"ModelWrapper",
|
||||
"Topbar",
|
||||
"StandaloneLayout",
|
||||
"onlineValidatorBadge"
|
||||
]
|
||||
```
|
||||
|
||||
As demonstrated below, additional components can be protected by utilizing the safe-render plugin
|
||||
with configuration options. This gets really handy if you are a SwaggerUI integrator and you maintain a number of
|
||||
plugins with additional custom components.
|
||||
|
||||
```js
|
||||
const swaggerUI = SwaggerUI({
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
dom_id: '#swagger-ui',
|
||||
plugins: [
|
||||
() => ({
|
||||
components: {
|
||||
MyCustomComponent1: () => 'my custom component',
|
||||
},
|
||||
}),
|
||||
SwaggerUI.plugins.SafeRender({
|
||||
fullOverride: true, // only the component list defined here will apply (not the default list)
|
||||
componentList: [
|
||||
"MyCustomComponent1",
|
||||
],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
##### componentDidCatch
|
||||
|
||||
This static function is invoked after a component has thrown an error.
|
||||
It receives two parameters:
|
||||
|
||||
1. `error` - The error that was thrown.
|
||||
2. `info` - An object with a componentStack key containing [information about which component threw the error](https://reactjs.org/docs/error-boundaries.html#component-stack-traces).
|
||||
|
||||
It has precisely the same signature as error boundaries [componentDidCatch lifecycle method](https://reactjs.org/docs/react-component.html#componentdidcatch),
|
||||
except it's a static function and not a class method.
|
||||
|
||||
Default implement of componentDidCatch uses `console.error` to display the received error:
|
||||
|
||||
```js
|
||||
export const componentDidCatch = console.error;
|
||||
```
|
||||
|
||||
To utilize your own error handling logic (e.g. [bugsnag](https://www.bugsnag.com/)), create new SwaggerUI plugin that overrides componentDidCatch:
|
||||
|
||||
{% highlight js linenos %}
|
||||
const BugsnagErrorHandlerPlugin = () => {
|
||||
// init bugsnag
|
||||
|
||||
return {
|
||||
fn: {
|
||||
componentDidCatch = (error, info) => {
|
||||
Bugsnag.notify(error);
|
||||
Bugsnag.notify(info);
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
{% endhighlight %}
|
||||
|
||||
##### withErrorBoundary
|
||||
|
||||
This function is HOC (Higher Order Component). It wraps a particular component into the `ErrorBoundary` component.
|
||||
It can be overridden via a plugin system to control how components are wrapped by the ErrorBoundary component.
|
||||
In 99.9% of situations, you won't need to override this function, but if you do, please read the source code of this function first.
|
||||
|
||||
##### Fallback
|
||||
|
||||
The component is displayed when the error boundary catches an error. It can be overridden via a plugin system.
|
||||
Its default implementation is trivial:
|
||||
|
||||
```js
|
||||
import React from "react"
|
||||
import PropTypes from "prop-types"
|
||||
|
||||
const Fallback = ({ name }) => (
|
||||
<div className="fallback">
|
||||
😱 <i>Could not render { name === "t" ? "this component" : name }, see the console.</i>
|
||||
</div>
|
||||
)
|
||||
Fallback.propTypes = {
|
||||
name: PropTypes.string.isRequired,
|
||||
}
|
||||
export default Fallback
|
||||
```
|
||||
|
||||
Feel free to override it to match your look & feel:
|
||||
|
||||
```js
|
||||
const CustomFallbackPlugin = () => ({
|
||||
components: {
|
||||
Fallback: ({ name } ) => `This is my custom fallback. ${name} failed to render`,
|
||||
},
|
||||
});
|
||||
|
||||
const swaggerUI = SwaggerUI({
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
dom_id: '#swagger-ui',
|
||||
plugins: [
|
||||
CustomFallbackPlugin,
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
##### ErrorBoundary
|
||||
|
||||
This is the component that implements React error boundaries. Uses `componentDidCatch` and `Fallback`
|
||||
under the hood. In 99.9% of situations, you won't need to override this component, but if you do,
|
||||
please read the source code of this component first.
|
||||
|
||||
|
||||
##### Change in behavior
|
||||
|
||||
In prior releases of SwaggerUI (before v4.3.0), almost all components have been protected, and when thrown error,
|
||||
`Fallback` component was displayed. This changes with SwaggerUI v4.3.0. Only components defined
|
||||
by the `safe-render` plugin are now protected and display fallback. If a small component somewhere within
|
||||
SwaggerUI React component tree fails to render and throws an error. The error bubbles up to the closest
|
||||
error boundary, and that error boundary displays the `Fallback` component and invokes `componentDidCatch`.
|
459
frontend/web/api-doc/docs/customization/plugin-api.md
Normal file
459
frontend/web/api-doc/docs/customization/plugin-api.md
Normal file
@@ -0,0 +1,459 @@
|
||||
# Plugins
|
||||
|
||||
A plugin is a function that returns an object - more specifically, the object may contain functions and components that augment and modify Swagger UI's functionality.
|
||||
|
||||
### Note: Semantic Versioning
|
||||
|
||||
Swagger UI's internal APIs are _not_ part of our public contract, which means that they can change without the major version change.
|
||||
|
||||
If your custom plugins wrap, extend, override, or consume any internal core APIs, we recommend specifying a specific minor version of Swagger UI to use in your application, because they will _not_ change between patch versions.
|
||||
|
||||
If you're installing Swagger UI via NPM, for example, you can do this by using a tilde:
|
||||
|
||||
```js
|
||||
{
|
||||
"dependencies": {
|
||||
"swagger-ui": "~3.11.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Format
|
||||
|
||||
A plugin return value may contain any of these keys, where `stateKey` is a name for a piece of state:
|
||||
|
||||
```javascript
|
||||
{
|
||||
statePlugins: {
|
||||
[stateKey]: {
|
||||
actions,
|
||||
reducers,
|
||||
selectors,
|
||||
wrapActions,
|
||||
wrapSelectors
|
||||
}
|
||||
},
|
||||
components: {},
|
||||
wrapComponents: {},
|
||||
rootInjects: {},
|
||||
afterLoad: (system) => {},
|
||||
fn: {},
|
||||
}
|
||||
```
|
||||
|
||||
### System is provided to plugins
|
||||
|
||||
Let's assume we have a plugin, `NormalPlugin`, that exposes a `doStuff` action under the `normal` state namespace.
|
||||
|
||||
```javascript
|
||||
const ExtendingPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
extending: {
|
||||
actions: {
|
||||
doExtendedThings: function(...args) {
|
||||
// you can do other things in here if you want
|
||||
return system.normalActions.doStuff(...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
As you can see, each plugin is passed a reference to the `system` being built up. As long as `NormalPlugin` is compiled before `ExtendingPlugin`, this will work without any issues.
|
||||
|
||||
There is no dependency management built into the plugin system, so if you create a plugin that relies on another, it is your responsibility to make sure that the dependent plugin is loaded _after_ the plugin being depended on.
|
||||
|
||||
### Interfaces
|
||||
|
||||
#### Actions
|
||||
|
||||
```javascript
|
||||
const MyActionPlugin = () => {
|
||||
return {
|
||||
statePlugins: {
|
||||
example: {
|
||||
actions: {
|
||||
updateFavoriteColor: (str) => {
|
||||
return {
|
||||
type: "EXAMPLE_SET_FAV_COLOR",
|
||||
payload: str
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Once an action has been defined, you can use it anywhere that you can get a system reference:
|
||||
|
||||
```javascript
|
||||
// elsewhere
|
||||
system.exampleActions.updateFavoriteColor("blue")
|
||||
```
|
||||
|
||||
The Action interface enables the creation of new Redux action creators within a piece of state in the Swagger UI system.
|
||||
|
||||
This action creator function will be exposed to container components as `exampleActions.updateFavoriteColor`. When this action creator is called, the return value (which should be a [Flux Standard Action](https://github.com/acdlite/flux-standard-action)) will be passed to the `example` reducer, which we'll define in the next section.
|
||||
|
||||
For more information about the concept of actions in Redux, see the [Redux Actions documentation](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow#actions).
|
||||
|
||||
#### Reducers
|
||||
|
||||
Reducers take a state (which is an [Immutable.js map](https://facebook.github.io/immutable-js/docs/#/Map)) and an action, then returns a new state.
|
||||
|
||||
Reducers must be provided to the system under the name of the action type that they handle, in this case, `EXAMPLE_SET_FAV_COLOR`.
|
||||
|
||||
```javascript
|
||||
const MyReducerPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
example: {
|
||||
reducers: {
|
||||
"EXAMPLE_SET_FAV_COLOR": (state, action) => {
|
||||
// you're only working with the state under the namespace, in this case "example".
|
||||
// So you can do what you want, without worrying about /other/ namespaces
|
||||
return state.set("favColor", action.payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Selectors
|
||||
|
||||
Selectors reach into their namespace's state to retrieve or derive data from the state.
|
||||
|
||||
They're an easy way to keep logic in one place, and are preferred over passing state data directly into components.
|
||||
|
||||
|
||||
```javascript
|
||||
const MySelectorPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
example: {
|
||||
selectors: {
|
||||
myFavoriteColor: (state) => state.get("favColor")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can also use the Reselect library to memoize your selectors, which is recommended for any selectors that will see heavy use, since Reselect automatically memoizes selector calls for you:
|
||||
|
||||
```javascript
|
||||
import { createSelector } from "reselect"
|
||||
|
||||
const MySelectorPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
example: {
|
||||
selectors: {
|
||||
// this selector will be memoized after it is run once for a
|
||||
// value of `state`
|
||||
myFavoriteColor: createSelector((state) => state.get("favColor"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Once a selector has been defined, you can use it anywhere that you can get a system reference:
|
||||
```javascript
|
||||
system.exampleSelectors.myFavoriteColor() // gets `favColor` in state for you
|
||||
```
|
||||
|
||||
#### Components
|
||||
|
||||
You can provide a map of components to be integrated into the system.
|
||||
|
||||
Be mindful of the key names for the components you provide, as you'll need to use those names to refer to the components elsewhere.
|
||||
|
||||
```javascript
|
||||
class HelloWorldClass extends React.Component {
|
||||
render() {
|
||||
return <h1>Hello World!</h1>
|
||||
}
|
||||
}
|
||||
|
||||
const MyComponentPlugin = function(system) {
|
||||
return {
|
||||
components: {
|
||||
HelloWorldClass: HelloWorldClass
|
||||
// components can just be functions, these are called "stateless components"
|
||||
HelloWorldStateless: () => <h1>Hello World!</h1>,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```javascript
|
||||
// elsewhere
|
||||
const HelloWorldStateless = system.getComponent("HelloWorldStateless")
|
||||
const HelloWorldClass = system.getComponent("HelloWorldClass")
|
||||
```
|
||||
|
||||
You can also "cancel out" any components that you don't want by creating a stateless component that always returns `null`:
|
||||
|
||||
```javascript
|
||||
const NeverShowInfoPlugin = function(system) {
|
||||
return {
|
||||
components: {
|
||||
info: () => null
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can use `config.failSilently` if you don't want a warning when a component doesn't exist in the system.
|
||||
|
||||
Be mindful of `getComponent` arguments order. In the example below, the boolean `false` refers to presence of a container, and the 3rd argument is the config object used to suppress the missing component warning.
|
||||
```javascript
|
||||
const thisVariableWillBeNull = getComponent("not_real", false, { failSilently: true })
|
||||
```
|
||||
|
||||
#### Wrap-Actions
|
||||
|
||||
Wrap Actions allow you to override the behavior of an action in the system.
|
||||
|
||||
They are function factories with the signature `(oriAction, system) => (...args) => result`.
|
||||
|
||||
A Wrap Action's first argument is `oriAction`, which is the action being wrapped. It is your responsibility to call the `oriAction` - if you don't, the original action will not fire!
|
||||
|
||||
This mechanism is useful for conditionally overriding built-in behaviors, or listening to actions.
|
||||
|
||||
```javascript
|
||||
// FYI: in an actual Swagger UI, `updateSpec` is already defined in the core code
|
||||
// it's just here for clarity on what's behind the scenes
|
||||
const MySpecPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: {
|
||||
actions: {
|
||||
updateSpec: (str) => {
|
||||
return {
|
||||
type: "SPEC_UPDATE_SPEC",
|
||||
payload: str
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this plugin allows you to watch changes to the spec that is in memory
|
||||
const MyWrapActionPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: {
|
||||
wrapActions: {
|
||||
updateSpec: (oriAction, system) => (str) => {
|
||||
// here, you can hand the value to some function that exists outside of Swagger UI
|
||||
console.log("Here is my API definition", str)
|
||||
return oriAction(str) // don't forget! otherwise, Swagger UI won't update
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Wrap-Selectors
|
||||
|
||||
Wrap Selectors allow you to override the behavior of a selector in the system.
|
||||
|
||||
They are function factories with the signature `(oriSelector, system) => (state, ...args) => result`.
|
||||
|
||||
This interface is useful for controlling what data flows into components. We use this in the core code to disable selectors based on the API definition's version.
|
||||
|
||||
```javascript
|
||||
import { createSelector } from 'reselect'
|
||||
|
||||
// FYI: in an actual Swagger UI, the `url` spec selector is already defined
|
||||
// it's just here for clarity on what's behind the scenes
|
||||
const MySpecPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: {
|
||||
selectors: {
|
||||
url: createSelector(
|
||||
state => state.get("url")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const MyWrapSelectorsPlugin = function(system) {
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: {
|
||||
wrapSelectors: {
|
||||
url: (oriSelector, system) => (state, ...args) => {
|
||||
console.log('someone asked for the spec url!!! it is', state.get('url'))
|
||||
// you can return other values here...
|
||||
// but let's just enable the default behavior
|
||||
return oriSelector(state, ...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Wrap-Components
|
||||
|
||||
Wrap Components allow you to override a component registered within the system.
|
||||
|
||||
Wrap Components are function factories with the signature `(OriginalComponent, system) => props => ReactElement`. If you'd prefer to provide a React component class, `(OriginalComponent, system) => ReactClass` works as well.
|
||||
|
||||
```javascript
|
||||
const MyWrapBuiltinComponentPlugin = function(system) {
|
||||
return {
|
||||
wrapComponents: {
|
||||
info: (Original, system) => (props) => {
|
||||
return <div>
|
||||
<h3>Hello world! I am above the Info component.</h3>
|
||||
<Original {...props} />
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Here's another example that includes a code sample of a component that will be wrapped:
|
||||
|
||||
```javascript
|
||||
///// Overriding a component from a plugin
|
||||
|
||||
// Here's our normal, unmodified component.
|
||||
const MyNumberDisplayPlugin = function(system) {
|
||||
return {
|
||||
components: {
|
||||
NumberDisplay: ({ number }) => <span>{number}</span>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Here's a component wrapper defined as a function.
|
||||
const MyWrapComponentPlugin = function(system) {
|
||||
return {
|
||||
wrapComponents: {
|
||||
NumberDisplay: (Original, system) => (props) => {
|
||||
if(props.number > 10) {
|
||||
return <div>
|
||||
<h3>Warning! Big number ahead.</h3>
|
||||
<Original {...props} />
|
||||
</div>
|
||||
} else {
|
||||
return <Original {...props} />
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alternatively, here's the same component wrapper defined as a class.
|
||||
const MyWrapComponentPlugin = function(system) {
|
||||
return {
|
||||
wrapComponents: {
|
||||
NumberDisplay: (Original, system) => class WrappedNumberDisplay extends React.component {
|
||||
render() {
|
||||
if(props.number > 10) {
|
||||
return <div>
|
||||
<h3>Warning! Big number ahead.</h3>
|
||||
<Original {...props} />
|
||||
</div>
|
||||
} else {
|
||||
return <Original {...props} />
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Note:**
|
||||
|
||||
If you have multiple plugins wrapping the same component, you may want to change the [`pluginsOptions.pluginLoadType`](/docs/usage/configuration.md#Plugins-options) parameter to `chain`.
|
||||
|
||||
#### `rootInjects`
|
||||
|
||||
The `rootInjects` interface allows you to inject values at the top level of the system.
|
||||
|
||||
This interface takes an object, which will be merged in with the top-level system object at runtime.
|
||||
|
||||
```js
|
||||
const MyRootInjectsPlugin = function(system) {
|
||||
return {
|
||||
rootInjects: {
|
||||
myConstant: 123,
|
||||
myMethod: (...params) => console.log(...params)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### `afterLoad`
|
||||
|
||||
The `afterLoad` plugin method allows you to get a reference to the system after your plugin has been registered.
|
||||
|
||||
This interface is used in the core code to attach methods that are driven by bound selectors or actions. You can also use it to execute logic that requires your plugin to already be ready, for example fetching initial data from a remote endpoint and passing it to an action your plugin creates.
|
||||
|
||||
The plugin context, which is bound to `this`, is undocumented, but below is an example of how to attach a bound action as a top-level method:
|
||||
|
||||
```javascript
|
||||
const MyMethodProvidingPlugin = function() {
|
||||
return {
|
||||
afterLoad(system) {
|
||||
// at this point in time, your actions have been bound into the system
|
||||
// so you can do things with them
|
||||
this.rootInjects = this.rootInjects || {}
|
||||
this.rootInjects.myMethod = system.exampleActions.updateFavoriteColor
|
||||
},
|
||||
statePlugins: {
|
||||
example: {
|
||||
actions: {
|
||||
updateFavoriteColor: (str) => {
|
||||
return {
|
||||
type: "EXAMPLE_SET_FAV_COLOR",
|
||||
payload: str
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### fn
|
||||
|
||||
The fn interface allows you to add helper functions to the system for use elsewhere.
|
||||
|
||||
```javascript
|
||||
import leftPad from "left-pad"
|
||||
|
||||
const MyFnPlugin = function(system) {
|
||||
return {
|
||||
fn: {
|
||||
leftPad: leftPad
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user