You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin-api.md 12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # Plugins
  2. 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.
  3. ### Note: Semantic Versioning
  4. Swagger-UI's internal APIs are _not_ part of our public contract, which means that they can change without the major version changing.
  5. 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.
  6. If you're installing Swagger-UI via NPM, for example, you can do this by using a tilde:
  7. ```js
  8. {
  9. "dependencies": {
  10. "swagger-ui": "~3.11.0"
  11. }
  12. }
  13. ```
  14. ### Format
  15. A plugin return value may contain any of these keys, where `stateKey` is a name for a piece of state:
  16. ```javascript
  17. {
  18. statePlugins: {
  19. [stateKey]: {
  20. actions,
  21. reducers,
  22. selectors,
  23. wrapActions,
  24. wrapSelectors
  25. }
  26. },
  27. components: {},
  28. wrapComponents: {},
  29. rootInjects: {},
  30. afterLoad: (system) => {},
  31. fn: {},
  32. }
  33. ```
  34. ### System is provided to plugins
  35. Let's assume we have a plugin, `NormalPlugin`, that exposes a `doStuff` action under the `normal` state namespace.
  36. ```javascript
  37. const ExtendingPlugin = function(system) {
  38. return {
  39. statePlugins: {
  40. extending: {
  41. actions: {
  42. doExtendedThings: function(...args) {
  43. // you can do other things in here if you want
  44. return system.normalActions.doStuff(...args)
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. ```
  52. 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.
  53. 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.
  54. ### Interfaces
  55. #### Actions
  56. ```javascript
  57. const MyActionPlugin = () => {
  58. return {
  59. statePlugins: {
  60. example: {
  61. actions: {
  62. updateFavoriteColor: (str) => {
  63. return {
  64. type: "EXAMPLE_SET_FAV_COLOR",
  65. payload: str
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. ```
  74. Once an action has been defined, you can use it anywhere that you can get a system reference:
  75. ```javascript
  76. // elsewhere
  77. system.exampleActions.updateFavoriteColor("blue")
  78. ```
  79. The Action interface enables the creation of new Redux action creators within a piece of state in the Swagger-UI system.
  80. 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.
  81. For more information about the concept of actions in Redux, see the [Redux Actions documentation](http://redux.js.org/docs/basics/Actions.html).
  82. #### Reducers
  83. Reducers take a state (which is an [Immutable.js map](https://facebook.github.io/immutable-js/docs/#/Map)) and an action, and return a new state.
  84. Reducers must be provided to the system under the name of the action type that they handle, in this case, `EXAMPLE_SET_FAV_COLOR`.
  85. ```javascript
  86. const MyReducerPlugin = function(system) {
  87. return {
  88. statePlugins: {
  89. example: {
  90. reducers: {
  91. "EXAMPLE_SET_FAV_COLOR": (state, action) => {
  92. // you're only working with the state under the namespace, in this case "example".
  93. // So you can do what you want, without worrying about /other/ namespaces
  94. return state.set("favColor", action.payload)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. ```
  102. #### Selectors
  103. Selectors reach into their namespace's state to retrieve or derive data from the state.
  104. They're an easy way to keep logic in one place, and is preferred over passing state data directly into components.
  105. ```javascript
  106. const MySelectorPlugin = function(system) {
  107. return {
  108. statePlugins: {
  109. example: {
  110. selectors: {
  111. myFavoriteColor: (state) => state.get("favColor")
  112. }
  113. }
  114. }
  115. }
  116. }
  117. ```
  118. 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:
  119. ```javascript
  120. import { createSelector } from "reselect"
  121. const MySelectorPlugin = function(system) {
  122. return {
  123. statePlugins: {
  124. example: {
  125. selectors: {
  126. // this selector will be memoized after it is run once for a
  127. // value of `state`
  128. myFavoriteColor: createSelector((state) => state.get("favColor"))
  129. }
  130. }
  131. }
  132. }
  133. }
  134. ```
  135. Once a selector has been defined, you can use it anywhere that you can get a system reference:
  136. ```javascript
  137. system.exampleSelectors.myFavoriteColor() // gets `favColor` in state for you
  138. ```
  139. #### Components
  140. You can provide a map of components to be integrated into the system.
  141. 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.
  142. ```javascript
  143. class HelloWorldClass extends React.Component {
  144. render() {
  145. return <h1>Hello World!</h1>
  146. }
  147. }
  148. const MyComponentPlugin = function(system) {
  149. return {
  150. components: {
  151. HelloWorldClass: HelloWorldClass
  152. // components can just be functions, these are called "stateless components"
  153. HelloWorldStateless: () => <h1>Hello World!</h1>,
  154. }
  155. }
  156. }
  157. ```
  158. ```javascript
  159. // elsewhere
  160. const HelloWorldStateless = system.getComponent("HelloWorldStateless")
  161. const HelloWorldClass = system.getComponent("HelloWorldClass")
  162. ```
  163. You can also "cancel out" any components that you don't want by creating a stateless component that always returns `null`:
  164. ```javascript
  165. const NeverShowInfoPlugin = function(system) {
  166. return {
  167. components: {
  168. info: () => null
  169. }
  170. }
  171. }
  172. ```
  173. #### Wrap-Actions
  174. Wrap Actions allow you to override the behavior of an action in the system.
  175. They are function factories with the signature `(oriAction, system) => (...args) => result`.
  176. 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!
  177. This mechanism is useful for conditionally overriding built-in behaviors, or listening to actions.
  178. ```javascript
  179. // FYI: in an actual Swagger-UI, `updateSpec` is already defined in the core code
  180. // it's just here for clarity on what's behind the scenes
  181. const MySpecPlugin = function(system) {
  182. return {
  183. statePlugins: {
  184. spec: {
  185. actions: {
  186. updateSpec: (str) => {
  187. return {
  188. type: "SPEC_UPDATE_SPEC",
  189. payload: str
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // this plugin allows you to watch changes to the spec that is in memory
  198. const MyWrapActionPlugin = function(system) {
  199. return {
  200. statePlugins: {
  201. spec: {
  202. wrapActions: {
  203. updateSpec: (oriAction, system) => (str) => {
  204. // here, you can hand the value to some function that exists outside of Swagger-UI
  205. console.log("Here is my API definition", str)
  206. return oriAction(str) // don't forget! otherwise, Swagger-UI won't update
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. ```
  214. #### Wrap-Selectors
  215. Wrap Selectors allow you to override the behavior of a selector in the system.
  216. They are function factories with the signature `(oriSelector, system) => (state, ...args) => result`.
  217. 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.
  218. ```javascript
  219. import { createSelector } from 'reselect'
  220. // FYI: in an actual Swagger-UI, the `url` spec selector is already defined
  221. // it's just here for clarity on what's behind the scenes
  222. const MySpecPlugin = function(system) {
  223. return {
  224. statePlugins: {
  225. spec: {
  226. selectors: {
  227. url: createSelector(
  228. state => state.get("url")
  229. )
  230. }
  231. }
  232. }
  233. }
  234. }
  235. const MyWrapSelectorsPlugin = function(system) {
  236. return {
  237. statePlugins: {
  238. spec: {
  239. wrapSelectors: {
  240. url: (oriSelector, system) => (state, ...args) => {
  241. console.log('someone asked for the spec url!!! it is', state.get('url'))
  242. // you can return other values here...
  243. // but let's just enable the default behavior
  244. return oriSelector(state, ...args)
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. ```
  252. #### Wrap-Components
  253. Wrap Components allow you to override a component registered within the system.
  254. 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.
  255. ```javascript
  256. const MyWrapBuiltinComponentPlugin = function(system) {
  257. return {
  258. wrapComponents: {
  259. info: (Original, system) => (props) => {
  260. return <div>
  261. <h3>Hello world! I am above the Info component.</h3>
  262. <Original {...props} />
  263. </div>
  264. }
  265. }
  266. }
  267. }
  268. ```
  269. Here's another example that includes a code sample of a component that will be wrapped:
  270. ```javascript
  271. ///// Overriding a component from a plugin
  272. // Here's our normal, unmodified component.
  273. const MyNumberDisplayPlugin = function(system) {
  274. return {
  275. components: {
  276. NumberDisplay: ({ number }) => <span>{number}</span>
  277. }
  278. }
  279. }
  280. // Here's a component wrapper defined as a function.
  281. const MyWrapComponentPlugin = function(system) {
  282. return {
  283. wrapComponents: {
  284. NumberDisplay: (Original, system) => (props) => {
  285. if(props.number > 10) {
  286. return <div>
  287. <h3>Warning! Big number ahead.</h3>
  288. <Original {...props} />
  289. </div>
  290. } else {
  291. return <Original {...props} />
  292. }
  293. }
  294. }
  295. }
  296. }
  297. // Alternatively, here's the same component wrapper defined as a class.
  298. const MyWrapComponentPlugin = function(system) {
  299. return {
  300. wrapComponents: {
  301. NumberDisplay: (Original, system) => class WrappedNumberDisplay extends React.component {
  302. render() {
  303. if(props.number > 10) {
  304. return <div>
  305. <h3>Warning! Big number ahead.</h3>
  306. <Original {...props} />
  307. </div>
  308. } else {
  309. return <Original {...props} />
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. ```
  317. #### `rootInjects`
  318. The `rootInjects` interface allows you to inject values at the top level of the system.
  319. This interface takes an object, which will be merged in with the top-level system object at runtime.
  320. ```js
  321. const MyRootInjectsPlugin = function(system) {
  322. return {
  323. rootInjects: {
  324. myConstant: 123,
  325. myMethod: (...params) => console.log(...params)
  326. }
  327. }
  328. }
  329. ```
  330. #### `afterLoad`
  331. The `afterLoad` plugin method allows you to get a reference to the system after your plugin has been registered.
  332. 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.
  333. 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:
  334. ```javascript
  335. const MyMethodProvidingPlugin = function() {
  336. return {
  337. afterLoad(system) {
  338. // at this point in time, your actions have been bound into the system
  339. // so you can do things with them
  340. this.rootInjects = this.rootInjects || {}
  341. this.rootInjects.myMethod = system.exampleActions.updateFavoriteColor
  342. },
  343. statePlugins: {
  344. example: {
  345. actions: {
  346. updateFavoriteColor: (str) => {
  347. return {
  348. type: "EXAMPLE_SET_FAV_COLOR",
  349. payload: str
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. ```
  358. #### fn
  359. The fn interface allows you to add helper functions to the system for use elsewhere.
  360. ```javascript
  361. import leftPad from "left-pad"
  362. const MyFnPlugin = function(system) {
  363. return {
  364. fn: {
  365. leftPad: leftPad
  366. }
  367. }
  368. }
  369. ```