Skip to content

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:

1
{
2
"dependencies": {
3
"swagger-ui": "~3.11.0"
4
}
5
}

Format

A plugin return value may contain any of these keys, where stateKey is a name for a piece of state:

1
{
2
statePlugins: {
3
[stateKey]: {
4
actions,
5
reducers,
6
selectors,
7
wrapActions,
8
wrapSelectors
9
}
10
},
11
components: {},
12
wrapComponents: {},
13
rootInjects: {},
14
afterLoad: (system) => {},
15
fn: {},
16
}

System is provided to plugins

Let’s assume we have a plugin, NormalPlugin, that exposes a doStuff action under the normal state namespace.

1
const ExtendingPlugin = function(system) {
2
return {
3
statePlugins: {
4
extending: {
5
actions: {
6
doExtendedThings: function(...args) {
7
// you can do other things in here if you want
8
return system.normalActions.doStuff(...args)
9
}
10
}
11
}
12
}
13
}
14
}

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

1
const MyActionPlugin = () => {
2
return {
3
statePlugins: {
4
example: {
5
actions: {
6
updateFavoriteColor: (str) => {
7
return {
8
type: "EXAMPLE_SET_FAV_COLOR",
9
payload: str
10
}
11
}
12
}
13
}
14
}
15
}
16
}

Once an action has been defined, you can use it anywhere that you can get a system reference:

1
// elsewhere
2
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) 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.

Reducers

Reducers take a state (which is an Immutable.js 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.

1
const MyReducerPlugin = function(system) {
2
return {
3
statePlugins: {
4
example: {
5
reducers: {
6
"EXAMPLE_SET_FAV_COLOR": (state, action) => {
7
// you're only working with the state under the namespace, in this case "example".
8
// So you can do what you want, without worrying about /other/ namespaces
9
return state.set("favColor", action.payload)
10
}
11
}
12
}
13
}
14
}
15
}

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.

1
const MySelectorPlugin = function(system) {
2
return {
3
statePlugins: {
4
example: {
5
selectors: {
6
myFavoriteColor: (state) => state.get("favColor")
7
}
8
}
9
}
10
}
11
}

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:

1
import { createSelector } from "reselect"
2
3
const MySelectorPlugin = function(system) {
4
return {
5
statePlugins: {
6
example: {
7
selectors: {
8
// this selector will be memoized after it is run once for a
9
// value of `state`
10
myFavoriteColor: createSelector((state) => state.get("favColor"))
11
}
12
}
13
}
14
}
15
}

Once a selector has been defined, you can use it anywhere that you can get a system reference:

1
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.

1
class HelloWorldClass extends React.Component {
2
render() {
3
return <h1>Hello World!</h1>
4
}
5
}
6
7
const MyComponentPlugin = function(system) {
8
return {
9
components: {
10
HelloWorldClass: HelloWorldClass
11
// components can just be functions, these are called "stateless components"
12
HelloWorldStateless: () => <h1>Hello World!</h1>,
13
}
14
}
15
}
1
// elsewhere
2
const HelloWorldStateless = system.getComponent("HelloWorldStateless")
3
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:

1
const NeverShowInfoPlugin = function(system) {
2
return {
3
components: {
4
info: () => null
5
}
6
}
7
}

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.

1
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.

1
// FYI: in an actual Swagger UI, `updateSpec` is already defined in the core code
2
// it's just here for clarity on what's behind the scenes
3
const MySpecPlugin = function(system) {
4
return {
5
statePlugins: {
6
spec: {
7
actions: {
8
updateSpec: (str) => {
9
return {
10
type: "SPEC_UPDATE_SPEC",
11
payload: str
12
}
13
}
14
}
15
}
16
}
17
}
18
}
19
20
// this plugin allows you to watch changes to the spec that is in memory
21
const MyWrapActionPlugin = function(system) {
22
return {
23
statePlugins: {
24
spec: {
25
wrapActions: {
26
updateSpec: (oriAction, system) => (str) => {
27
// here, you can hand the value to some function that exists outside of Swagger UI
28
console.log("Here is my API definition", str)
29
return oriAction(str) // don't forget! otherwise, Swagger UI won't update
30
}
31
}
32
}
33
}
34
}
35
}

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.

1
import { createSelector } from 'reselect'
2
3
// FYI: in an actual Swagger UI, the `url` spec selector is already defined
4
// it's just here for clarity on what's behind the scenes
5
const MySpecPlugin = function(system) {
6
return {
7
statePlugins: {
8
spec: {
9
selectors: {
10
url: createSelector(
11
state => state.get("url")
12
)
13
}
14
}
15
}
16
}
17
}
18
19
const MyWrapSelectorsPlugin = function(system) {
20
return {
21
statePlugins: {
22
spec: {
23
wrapSelectors: {
24
url: (oriSelector, system) => (state, ...args) => {
25
console.log('someone asked for the spec url!!! it is', state.get('url'))
26
// you can return other values here...
27
// but let's just enable the default behavior
28
return oriSelector(state, ...args)
29
}
30
}
31
}
32
}
33
}
34
}

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.

1
const MyWrapBuiltinComponentPlugin = function(system) {
2
return {
3
wrapComponents: {
4
info: (Original, system) => (props) => {
5
return <div>
6
<h3>Hello world! I am above the Info component.</h3>
7
<Original {...props} />
8
</div>
9
}
10
}
11
}
12
}

Here’s another example that includes a code sample of a component that will be wrapped:

1
///// Overriding a component from a plugin
2
3
// Here's our normal, unmodified component.
4
const MyNumberDisplayPlugin = function(system) {
5
return {
6
components: {
7
NumberDisplay: ({ number }) => <span>{number}</span>
8
}
9
}
10
}
11
12
// Here's a component wrapper defined as a function.
13
const MyWrapComponentPlugin = function(system) {
14
return {
15
wrapComponents: {
16
NumberDisplay: (Original, system) => (props) => {
17
if(props.number > 10) {
18
return <div>
19
<h3>Warning! Big number ahead.</h3>
20
<Original {...props} />
21
</div>
22
} else {
23
return <Original {...props} />
24
}
25
}
26
}
27
}
28
}
29
30
// Alternatively, here's the same component wrapper defined as a class.
31
const MyWrapComponentPlugin = function(system) {
32
return {
33
wrapComponents: {
34
NumberDisplay: (Original, system) => class WrappedNumberDisplay extends React.component {
35
render() {
36
if(props.number > 10) {
37
return <div>
38
<h3>Warning! Big number ahead.</h3>
39
<Original {...props} />
40
</div>
41
} else {
42
return <Original {...props} />
43
}
44
}
45
}
46
}
47
}
48
}

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.

1
const MyRootInjectsPlugin = function(system) {
2
return {
3
rootInjects: {
4
myConstant: 123,
5
myMethod: (...params) => console.log(...params)
6
}
7
}
8
}

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:

1
const MyMethodProvidingPlugin = function() {
2
return {
3
afterLoad(system) {
4
// at this point in time, your actions have been bound into the system
5
// so you can do things with them
6
this.rootInjects = this.rootInjects || {}
7
this.rootInjects.myMethod = system.exampleActions.updateFavoriteColor
8
},
9
statePlugins: {
10
example: {
11
actions: {
12
updateFavoriteColor: (str) => {
13
return {
14
type: "EXAMPLE_SET_FAV_COLOR",
15
payload: str
16
}
17
}
18
}
19
}
20
}
21
}
22
}

fn

The fn interface allows you to add helper functions to the system for use elsewhere.

1
import leftPad from "left-pad"
2
3
const MyFnPlugin = function(system) {
4
return {
5
fn: {
6
leftPad: leftPad
7
}
8
}
9
}