Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

100 строки
2.8 KiB

  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. plugins: this.props.plugins,
  13. spec: this.props.spec,
  14. url: this.props.url,
  15. requestInterceptor: this.requestInterceptor,
  16. responseInterceptor: this.responseInterceptor,
  17. onComplete: this.onComplete,
  18. docExpansion: this.props.docExpansion,
  19. supportedSubmitMethods: this.props.supportedSubmitMethods,
  20. defaultModelExpandDepth: this.props.defaultModelExpandDepth,
  21. displayOperationId: this.props.displayOperationId,
  22. })
  23. this.system = ui
  24. this.SwaggerUIComponent = ui.getComponent("App", "root")
  25. this.forceUpdate()
  26. }
  27. render() {
  28. return this.SwaggerUIComponent ? <this.SwaggerUIComponent /> : null
  29. }
  30. componentDidUpdate(prevProps) {
  31. if(this.props.url !== prevProps.url) {
  32. // flush current content
  33. this.system.specActions.updateSpec("")
  34. if(this.props.url) {
  35. // update the internal URL
  36. this.system.specActions.updateUrl(this.props.url)
  37. // trigger remote definition fetch
  38. this.system.specActions.download(this.props.url)
  39. }
  40. }
  41. if(this.props.spec !== prevProps.spec && this.props.spec) {
  42. if(typeof this.props.spec === "object") {
  43. this.system.specActions.updateSpec(JSON.stringify(this.props.spec))
  44. } else {
  45. this.system.specActions.updateSpec(this.props.spec)
  46. }
  47. }
  48. }
  49. requestInterceptor = (req) => {
  50. if (typeof this.props.requestInterceptor === "function") {
  51. return this.props.requestInterceptor(req)
  52. }
  53. return req
  54. }
  55. responseInterceptor = (res) => {
  56. if (typeof this.props.responseInterceptor === "function") {
  57. return this.props.responseInterceptor(res)
  58. }
  59. return res
  60. }
  61. onComplete = () => {
  62. if (typeof this.props.onComplete === "function") {
  63. return this.props.onComplete(this.system)
  64. }
  65. }
  66. }
  67. SwaggerUI.propTypes = {
  68. spec: PropTypes.oneOf([
  69. PropTypes.string,
  70. PropTypes.object,
  71. ]),
  72. url: PropTypes.string,
  73. requestInterceptor: PropTypes.func,
  74. responseInterceptor: PropTypes.func,
  75. onComplete: PropTypes.func,
  76. docExpansion: PropTypes.oneOf(['list', 'full', 'none']),
  77. supportedSubmitMethods: PropTypes.arrayOf(
  78. PropTypes.oneOf(['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'])
  79. ),
  80. defaultModelExpandDepth: PropTypes.number,
  81. plugins: PropTypes.arrayOf(PropTypes.object),
  82. displayOperationId: PropTypes.bool,
  83. }
  84. SwaggerUI.defaultProps = {
  85. supportedSubmitMethods: ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'],
  86. }