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.

index.js 3.2 KiB

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