feature: swagger-ui-react module (via #5207)
* swagger-ui-react alpha.0 * alpha.1 * alpha.2 * alpha.3 * begin updating README * alpha.4 * WIP: `displayOperationId` support * move loading error readouts to BaseLayout * add `url` prop * export React component as default * add interceptor support * modify docs markup * add `onComplete` prop * add `spec` prop * Update README.md * alpha.6 * remove independent manifest; build releasable exclusively from template * ensure dist is present; drop config field in manifest * drop alpha field this script is now able to release to npm! * remove unused selector references * Update README.md
This commit is contained in:
83
flavors/swagger-ui-react/index.js
Normal file
83
flavors/swagger-ui-react/index.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import React from "react"
|
||||
import PropTypes from "prop-types"
|
||||
import swaggerUIConstructor from "./swagger-ui"
|
||||
|
||||
export default class SwaggerUI extends React.Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
this.SwaggerUIComponent = null
|
||||
this.system = null
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const ui = swaggerUIConstructor({
|
||||
spec: this.props.spec,
|
||||
url: this.props.url,
|
||||
requestInterceptor: this.requestInterceptor,
|
||||
responseInterceptor: this.responseInterceptor,
|
||||
onComplete: this.onComplete,
|
||||
})
|
||||
|
||||
this.system = ui
|
||||
this.SwaggerUIComponent = ui.getComponent("App", "root")
|
||||
|
||||
this.forceUpdate()
|
||||
}
|
||||
|
||||
render() {
|
||||
return this.SwaggerUIComponent ? <this.SwaggerUIComponent /> : null
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
if(this.props.url !== prevProps.url) {
|
||||
// flush current content
|
||||
this.system.specActions.updateSpec("")
|
||||
|
||||
if(this.props.url) {
|
||||
// update the internal URL
|
||||
this.system.specActions.updateUrl(this.props.url)
|
||||
// trigger remote definition fetch
|
||||
this.system.specActions.download(this.props.url)
|
||||
}
|
||||
}
|
||||
|
||||
if(this.props.spec !== prevProps.spec && this.props.spec) {
|
||||
if(typeof this.props.spec === "object") {
|
||||
this.system.specActions.updateSpec(JSON.stringify(this.props.spec))
|
||||
} else {
|
||||
this.system.specActions.updateSpec(this.props.spec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
requestInterceptor = (req) => {
|
||||
if (typeof this.props.requestInterceptor === "function") {
|
||||
return this.props.requestInterceptor(req)
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
responseInterceptor = (res) => {
|
||||
if (typeof this.props.responseInterceptor === "function") {
|
||||
return this.props.responseInterceptor(res)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
onComplete = () => {
|
||||
if (typeof this.props.onComplete === "function") {
|
||||
return this.props.onComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SwaggerUI.propTypes = {
|
||||
spec: PropTypes.oneOf([
|
||||
PropTypes.string,
|
||||
PropTypes.object,
|
||||
]),
|
||||
url: PropTypes.string,
|
||||
requestInterceptor: PropTypes.func,
|
||||
responseInterceptor: PropTypes.func,
|
||||
onComplete: PropTypes.func,
|
||||
}
|
||||
Reference in New Issue
Block a user