plugin-api.md 12.7 KB
Newer Older
K
Kyle Shockey 已提交
1
# Plugins
K
Kyle Shockey 已提交
2

3
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.
K
Kyle Shockey 已提交
4

K
kyle 已提交
5 6
### Note: Semantic Versioning 

7
Swagger UI's internal APIs are _not_ part of our public contract, which means that they can change without the major version change.
K
kyle 已提交
8

9
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.
K
kyle 已提交
10

11
If you're installing Swagger UI via NPM, for example, you can do this by using a tilde:
K
kyle 已提交
12 13 14 15 16 17 18 19 20

```js
{
  "dependencies": {
    "swagger-ui": "~3.11.0"
  }
}
```

K
Kyle Shockey 已提交
21 22
### Format

23
A plugin return value may contain any of these keys, where `stateKey` is a name for a piece of state:
K
Kyle Shockey 已提交
24 25

```javascript
K
Kyle Shockey 已提交
26
{
K
Kyle Shockey 已提交
27
  statePlugins: {
28
    [stateKey]: {
K
Kyle Shockey 已提交
29 30 31 32 33 34 35
      actions,
      reducers,
      selectors,
      wrapActions,
      wrapSelectors
    }
  },
K
Kyle Shockey 已提交
36 37
  components: {},
  wrapComponents: {},
38
  rootInjects: {},
39
  afterLoad: (system) => {},
K
Kyle Shockey 已提交
40
  fn: {},
K
Kyle Shockey 已提交
41 42
}
```
K
Kyle Shockey 已提交
43

K
WIP  
Kyle Shockey 已提交
44
### System is provided to plugins
K
Kyle Shockey 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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)
          }
        }
      }
    }
K
Kyle Shockey 已提交
61 62 63 64
  }
}
```

K
Kyle Shockey 已提交
65 66 67 68
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.

K
Kyle Shockey 已提交
69 70
### Interfaces

H
Helder Sepulveda 已提交
71
#### Actions
K
Kyle Shockey 已提交
72

K
Kyle Shockey 已提交
73 74 75 76 77 78
```javascript
const MyActionPlugin = () => {
  return {
    statePlugins: {
      example: {
        actions: {
K
WIP  
Kyle Shockey 已提交
79
          updateFavoriteColor: (str) => {
K
Kyle Shockey 已提交
80 81 82 83 84 85 86 87 88 89 90 91
            return {
              type: "EXAMPLE_SET_FAV_COLOR",
              payload: str
            }
          }
        }
      }
    }
  }
}
```

K
Kyle Shockey 已提交
92 93
Once an action has been defined, you can use it anywhere that you can get a system reference:

94
```javascript
K
Kyle Shockey 已提交
95
// elsewhere
K
Kyle Shockey 已提交
96
system.exampleActions.updateFavoriteColor("blue")
K
Kyle Shockey 已提交
97 98
```

99
The Action interface enables the creation of new Redux action creators within a piece of state in the Swagger UI system.
K
Kyle Shockey 已提交
100

K
Kyle Shockey 已提交
101
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.
K
Kyle Shockey 已提交
102 103 104

For more information about the concept of actions in Redux, see the [Redux Actions documentation](http://redux.js.org/docs/basics/Actions.html).

H
Helder Sepulveda 已提交
105
#### Reducers
K
Kyle Shockey 已提交
106

107
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.
K
Kyle Shockey 已提交
108

K
WIP  
Kyle Shockey 已提交
109
Reducers must be provided to the system under the name of the action type that they handle, in this case, `EXAMPLE_SET_FAV_COLOR`.
K
Kyle Shockey 已提交
110

111
```javascript
K
Kyle Shockey 已提交
112 113 114 115 116 117
const MyReducerPlugin = function(system) {
  return {
    statePlugins: {
      example: {
        reducers: {
          "EXAMPLE_SET_FAV_COLOR": (state, action) => {
K
Kyle Shockey 已提交
118
            // you're only working with the state under the namespace, in this case "example".
K
Kyle Shockey 已提交
119
            // So you can do what you want, without worrying about /other/ namespaces
K
Kyle Shockey 已提交
120
            return state.set("favColor", action.payload)
K
Kyle Shockey 已提交
121 122 123 124 125 126 127 128
          }
        }
      }
    }
  }
}
```

H
Helder Sepulveda 已提交
129
#### Selectors
K
Kyle Shockey 已提交
130

131
Selectors reach into their namespace's state to retrieve or derive data from the state.
K
Kyle Shockey 已提交
132

133
They're an easy way to keep logic in one place, and are preferred over passing state data directly into components.
K
Kyle Shockey 已提交
134

K
WIP  
Kyle Shockey 已提交
135

136
```javascript
K
WIP  
Kyle Shockey 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
const MySelectorPlugin = function(system) {
  return {
    statePlugins: {
      example: {
        selectors: {
          myFavoriteColor: (state) => state.get("favColor")
        }
      }
    }
  }
}
```

K
Kyle Shockey 已提交
150
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:
K
WIP  
Kyle Shockey 已提交
151

152
```javascript
K
WIP  
Kyle Shockey 已提交
153 154
import { createSelector } from "reselect"

K
Kyle Shockey 已提交
155 156 157
const MySelectorPlugin = function(system) {
  return {
    statePlugins: {
K
WIP  
Kyle Shockey 已提交
158
      example: {
K
Kyle Shockey 已提交
159
        selectors: {
K
WIP  
Kyle Shockey 已提交
160 161 162
          // this selector will be memoized after it is run once for a
          // value of `state`
          myFavoriteColor: createSelector((state) => state.get("favColor"))
K
Kyle Shockey 已提交
163 164 165 166 167 168 169
        }
      }
    }
  }
}
```

K
Kyle Shockey 已提交
170
Once a selector has been defined, you can use it anywhere that you can get a system reference:
171
```javascript
K
Kyle Shockey 已提交
172
system.exampleSelectors.myFavoriteColor() // gets `favColor` in state for you
K
WIP  
Kyle Shockey 已提交
173 174
```

H
Helder Sepulveda 已提交
175
#### Components
K
Kyle Shockey 已提交
176

K
Kyle Shockey 已提交
177 178 179 180
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.

181
```javascript
K
Kyle Shockey 已提交
182 183 184 185 186 187
class HelloWorldClass extends React.Component {
  render() {
    return <h1>Hello World!</h1>
  }
}

K
Kyle Shockey 已提交
188 189 190
const MyComponentPlugin = function(system) {
  return {
    components: {
K
Kyle Shockey 已提交
191 192 193
      HelloWorldClass: HelloWorldClass
      // components can just be functions, these are called "stateless components"
      HelloWorldStateless: () => <h1>Hello World!</h1>,
K
Kyle Shockey 已提交
194 195 196 197 198
    }
  }
}
```

199
```javascript
K
Kyle Shockey 已提交
200
// elsewhere
K
Kyle Shockey 已提交
201 202 203 204 205 206
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`:

207
```javascript
K
Kyle Shockey 已提交
208 209 210 211 212 213 214
const NeverShowInfoPlugin = function(system) {
  return {
    components: {
      info: () => null
    }
  }
}
K
Kyle Shockey 已提交
215 216
```

217 218 219 220 221 222 223
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 })
```

H
Helder Sepulveda 已提交
224
#### Wrap-Actions
K
Kyle Shockey 已提交
225

K
Kyle Shockey 已提交
226 227
Wrap Actions allow you to override the behavior of an action in the system.

K
Kyle Shockey 已提交
228
They are function factories with the signature `(oriAction, system) => (...args) => result`.
K
Kyle Shockey 已提交
229 230 231

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!

K
Kyle Shockey 已提交
232 233
This mechanism is useful for conditionally overriding built-in behaviors, or listening to actions.

234
```javascript
235
// FYI: in an actual Swagger UI, `updateSpec` is already defined in the core code
K
Kyle Shockey 已提交
236
// it's just here for clarity on what's behind the scenes
K
Kyle Shockey 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
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: {
K
Kyle Shockey 已提交
260
          updateSpec: (oriAction, system) => (str) => {
261
            // here, you can hand the value to some function that exists outside of Swagger UI
K
Kyle Shockey 已提交
262
            console.log("Here is my API definition", str)
263
            return oriAction(str) // don't forget! otherwise, Swagger UI won't update
K
Kyle Shockey 已提交
264 265 266 267 268 269 270 271
          }
        }
      }
    }
  }
}
```

H
Helder Sepulveda 已提交
272
#### Wrap-Selectors
K
Kyle Shockey 已提交
273

K
Kyle Shockey 已提交
274 275
Wrap Selectors allow you to override the behavior of a selector in the system.

K
Kyle Shockey 已提交
276
They are function factories with the signature `(oriSelector, system) => (state, ...args) => result`.
K
Kyle Shockey 已提交
277 278 279

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.

280
```javascript
K
Kyle Shockey 已提交
281 282
import { createSelector } from 'reselect'

283
// FYI: in an actual Swagger UI, the `url` spec selector is already defined
K
Kyle Shockey 已提交
284
// it's just here for clarity on what's behind the scenes
K
Kyle Shockey 已提交
285 286 287 288 289
const MySpecPlugin = function(system) {
  return {
    statePlugins: {
      spec: {
        selectors: {
K
Kyle Shockey 已提交
290 291
          url: createSelector(
            state => state.get("url")
K
Kyle Shockey 已提交
292 293 294 295 296 297 298 299 300 301 302 303
          )
        }
      }
    }
  }
}

const MyWrapSelectorsPlugin = function(system) {
  return {
    statePlugins: {
      spec: {
        wrapSelectors: {
K
Kyle Shockey 已提交
304 305 306
          url: (oriSelector, system) => (state, ...args) => {
            console.log('someone asked for the spec url!!! it is', state.get('url'))
            // you can return other values here...
K
Kyle Shockey 已提交
307
            // but let's just enable the default behavior
K
Kyle Shockey 已提交
308
            return oriSelector(state, ...args)
K
Kyle Shockey 已提交
309 310 311 312 313 314 315 316
          }
        }
      }
    }
  }
}
```

H
Helder Sepulveda 已提交
317
#### Wrap-Components
K
Kyle Shockey 已提交
318

K
Kyle Shockey 已提交
319 320
Wrap Components allow you to override a component registered within the system.

321
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.
K
Kyle Shockey 已提交
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
```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>
      }
    }
  }
}
```

338 339
Here's another example that includes a code sample of a component that will be wrapped:

340
```javascript
341
/////  Overriding a component from a plugin
342

343
// Here's our normal, unmodified component.
K
Kyle Shockey 已提交
344 345 346 347 348 349 350 351
const MyNumberDisplayPlugin = function(system) {
  return {
    components: {
      NumberDisplay: ({ number }) => <span>{number}</span>
    }
  }
}

352
// Here's a component wrapper defined as a function.
K
Kyle Shockey 已提交
353 354 355 356 357 358 359
const MyWrapComponentPlugin = function(system) {
  return {
    wrapComponents: {
      NumberDisplay: (Original, system) => (props) => {
        if(props.number > 10) {
          return <div>
            <h3>Warning! Big number ahead.</h3>
360
            <Original {...props} />
K
Kyle Shockey 已提交
361 362 363 364 365 366 367 368
          </div>
        } else {
          return <Original {...props} />
        }
      }
    }
  }
}
369 370 371 372 373 374 375 376 377 378

// 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>
379
              <Original {...props} />
380 381 382 383 384 385 386 387 388
            </div>
          } else {
            return <Original {...props} />
          }
        }
      }
    }
  }
}
K
Kyle Shockey 已提交
389 390
```

H
Helder Sepulveda 已提交
391
#### `rootInjects`
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

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)
    }
  }
}
```

H
Helder Sepulveda 已提交
408
#### `afterLoad`
409

410
The `afterLoad` plugin method allows you to get a reference to the system after your plugin has been registered.
K
Kyle Shockey 已提交
411

412
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.
413 414

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:
K
Kyle Shockey 已提交
415 416 417 418 419 420 421

```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
422 423
      this.rootInjects = this.rootInjects || {}
      this.rootInjects.myMethod = system.exampleActions.updateFavoriteColor
K
Kyle Shockey 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    },
    statePlugins: {
      example: {
        actions: {
          updateFavoriteColor: (str) => {
            return {
              type: "EXAMPLE_SET_FAV_COLOR",
              payload: str
            }
          }
        }
      }
    }
  }
}
```
440

H
Helder Sepulveda 已提交
441
#### fn
K
Kyle Shockey 已提交
442 443 444

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

445
```javascript
K
Kyle Shockey 已提交
446 447 448 449 450 451 452 453 454 455
import leftPad from "left-pad"

const MyFnPlugin = function(system) {
  return {
    fn: {
      leftPad: leftPad
    }
  }
}
```