Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React from "react"
  2. import PropTypes from "prop-types"
  3. import swaggerUIConstructor 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. spec: this.props.spec,
  13. url: this.props.url,
  14. requestInterceptor: this.requestInterceptor,
  15. responseInterceptor: this.responseInterceptor,
  16. onComplete: this.onComplete,
  17. docExpansion: this.props.docExpansion,
  18. })
  19. this.system = ui
  20. this.SwaggerUIComponent = ui.getComponent("App", "root")
  21. this.forceUpdate()
  22. }
  23. render() {
  24. return this.SwaggerUIComponent ? <this.SwaggerUIComponent /> : null
  25. }
  26. componentDidUpdate(prevProps) {
  27. if(this.props.url !== prevProps.url) {
  28. // flush current content
  29. this.system.specActions.updateSpec("")
  30. if(this.props.url) {
  31. // update the internal URL
  32. this.system.specActions.updateUrl(this.props.url)
  33. // trigger remote definition fetch
  34. this.system.specActions.download(this.props.url)
  35. }
  36. }
  37. if(this.props.spec !== prevProps.spec && this.props.spec) {
  38. if(typeof this.props.spec === "object") {
  39. this.system.specActions.updateSpec(JSON.stringify(this.props.spec))
  40. } else {
  41. this.system.specActions.updateSpec(this.props.spec)
  42. }
  43. }
  44. }
  45. requestInterceptor = (req) => {
  46. if (typeof this.props.requestInterceptor === "function") {
  47. return this.props.requestInterceptor(req)
  48. }
  49. return req
  50. }
  51. responseInterceptor = (res) => {
  52. if (typeof this.props.responseInterceptor === "function") {
  53. return this.props.responseInterceptor(res)
  54. }
  55. return res
  56. }
  57. onComplete = () => {
  58. if (typeof this.props.onComplete === "function") {
  59. return this.props.onComplete(this.system)
  60. }
  61. }
  62. }
  63. SwaggerUI.propTypes = {
  64. spec: PropTypes.oneOf([
  65. PropTypes.string,
  66. PropTypes.object,
  67. ]),
  68. url: PropTypes.string,
  69. requestInterceptor: PropTypes.func,
  70. responseInterceptor: PropTypes.func,
  71. onComplete: PropTypes.func,
  72. docExpansion: PropTypes.oneOf(['list', 'full', 'none']),
  73. }