index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React from "react"
  2. import PropTypes from "prop-types"
  3. import swaggerUIConstructor, {presets} from "./swagger-ui"
  4. export default class SwaggerUI extends React.Component {
  5. constructor (props) {
  6. super(props)
  7. this.SwaggerUIComponent = null
  8. this.system = null
  9. }
  10. componentDidMount() {
  11. const ui = swaggerUIConstructor({
  12. plugins: this.props.plugins,
  13. spec: this.props.spec,
  14. url: this.props.url,
  15. layout: this.props.layout,
  16. defaultModelsExpandDepth: this.props.defaultModelsExpandDepth,
  17. presets: [presets.apis,...this.props.presets],
  18. requestInterceptor: this.requestInterceptor,
  19. responseInterceptor: this.responseInterceptor,
  20. onComplete: this.onComplete,
  21. docExpansion: this.props.docExpansion,
  22. supportedSubmitMethods: this.props.supportedSubmitMethods,
  23. defaultModelExpandDepth: this.props.defaultModelExpandDepth,
  24. displayOperationId: this.props.displayOperationId,
  25. showMutatedRequest: typeof this.props.showMutatedRequest === "boolean" ? this.props.showMutatedRequest : true,
  26. deepLinking: typeof this.props.deepLinking === "boolean" ? this.props.deepLinking : false,
  27. })
  28. this.system = ui
  29. this.SwaggerUIComponent = ui.getComponent("App", "root")
  30. this.forceUpdate()
  31. }
  32. render() {
  33. return this.SwaggerUIComponent ? <this.SwaggerUIComponent /> : null
  34. }
  35. componentDidUpdate(prevProps) {
  36. if(this.props.url !== prevProps.url) {
  37. // flush current content
  38. this.system.specActions.updateSpec("")
  39. if(this.props.url) {
  40. // update the internal URL
  41. this.system.specActions.updateUrl(this.props.url)
  42. // trigger remote definition fetch
  43. this.system.specActions.download(this.props.url)
  44. }
  45. }
  46. if(this.props.spec !== prevProps.spec && this.props.spec) {
  47. if(typeof this.props.spec === "object") {
  48. this.system.specActions.updateSpec(JSON.stringify(this.props.spec))
  49. } else {
  50. this.system.specActions.updateSpec(this.props.spec)
  51. }
  52. }
  53. }
  54. requestInterceptor = (req) => {
  55. if (typeof this.props.requestInterceptor === "function") {
  56. return this.props.requestInterceptor(req)
  57. }
  58. return req
  59. }
  60. responseInterceptor = (res) => {
  61. if (typeof this.props.responseInterceptor === "function") {
  62. return this.props.responseInterceptor(res)
  63. }
  64. return res
  65. }
  66. onComplete = () => {
  67. if (typeof this.props.onComplete === "function") {
  68. return this.props.onComplete(this.system)
  69. }
  70. }
  71. }
  72. SwaggerUI.propTypes = {
  73. spec: PropTypes.oneOfType([
  74. PropTypes.string,
  75. PropTypes.object,
  76. ]),
  77. url: PropTypes.string,
  78. layout: PropTypes.string,
  79. requestInterceptor: PropTypes.func,
  80. responseInterceptor: PropTypes.func,
  81. onComplete: PropTypes.func,
  82. docExpansion: PropTypes.oneOf(["list", "full", "none"]),
  83. supportedSubmitMethods: PropTypes.arrayOf(
  84. PropTypes.oneOf(["get", "put", "post", "delete", "options", "head", "patch", "trace"])
  85. ),
  86. plugins: PropTypes.arrayOf(PropTypes.object),
  87. displayOperationId: PropTypes.bool,
  88. showMutatedRequest: PropTypes.bool,
  89. defaultModelExpandDepth: PropTypes.number,
  90. defaultModelsExpandDepth: PropTypes.number,
  91. presets: PropTypes.arrayOf(PropTypes.func),
  92. deepLinking: PropTypes.bool,
  93. }
  94. SwaggerUI.defaultProps = {
  95. layout: "BaseLayout",
  96. supportedSubmitMethods: ["get", "put", "post", "delete", "options", "head", "patch", "trace"],
  97. docExpansion: "list",
  98. defaultModelsExpandDepth: 1,
  99. presets: [],
  100. deepLinking: false,
  101. }