Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

overview.md 2.9 KiB

7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
7 lat temu
6 lat temu
7 lat temu
6 lat temu
7 lat temu
7 lat temu
6 lat temu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Plugin system overview
  2. ### Prior art
  3. Swagger UI leans heavily on concepts and patterns found in React and Redux.
  4. If you aren't already familiar, here's some suggested reading:
  5. - [React: Quick Start (reactjs.org)](https://reactjs.org/docs/hello-world.html)
  6. - [Redux README (redux.js.org)](http://redux.js.org/)
  7. In the following documentation, we won't take the time to define the fundamentals covered in the resources above.
  8. > **Note**: Some of the examples in this section contain JSX, which is a syntax extension to JavaScript that is useful for writing React components.
  9. >
  10. > 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.
  11. ### The System
  12. The _system_ is the heart of the Swagger UI application. At runtime, it's a JavaScript object that holds many things:
  13. - React components
  14. - Bound Redux actions and reducers
  15. - Bound Reselect state selectors
  16. - System-wide collection of available components
  17. - Built-in helpers like `getComponent`, `makeMappedContainer`, and `getStore`
  18. - References to the React and Immutable.js libraries (`system.React`, `system.Im`)
  19. - User-defined helper functions
  20. 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.
  21. ### Presets
  22. 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:
  23. ```javascript
  24. const MyPreset = [FirstPlugin, SecondPlugin, ThirdPlugin]
  25. SwaggerUI({
  26. presets: [
  27. MyPreset
  28. ]
  29. })
  30. ```
  31. 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:
  32. ```javascript
  33. SwaggerUI({
  34. presets: [
  35. SwaggerUI.presets.apis,
  36. MyAmazingCustomPreset
  37. ]
  38. })
  39. ```
  40. 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.
  41. ### getComponent
  42. `getComponent` is a helper function injected into every container component, which is used to get references to components provided by the plugin system.
  43. All components should be loaded through `getComponent`, since it allows other plugins to modify the component. It is preferred over a conventional `import` statement.
  44. Container components in Swagger UI can be loaded by passing `true` as the second argument to `getComponent`, like so:
  45. ```javascript
  46. getComponent("ContainerComponentName", true)
  47. ```
  48. This will map the current system as props to the component.