Skip to content

Commit

Permalink
refactor(oas31): change License component into smart
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Mar 16, 2023
1 parent 8b27441 commit 13e2774
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 50 deletions.
5 changes: 3 additions & 2 deletions src/core/plugins/oas3/spec-extensions/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { isSwagger2 as isSwagger2Helper, isOAS30 as isOAS30Helper } from "../hel
* Helpers
*/

const map = Map()

export const isSwagger2 = () => (system) => {
const spec = system.getSystem().specSelectors.specJson()
return isSwagger2Helper(spec)
Expand Down Expand Up @@ -34,6 +36,5 @@ function onlyOAS3(selector) {

export const servers = onlyOAS3(() => (system) => {
const spec = system.specSelectors.specJson()
return spec.get("servers", servers.mapConst)
return spec.get("servers", map)
})
servers.mapConst = Map()
6 changes: 4 additions & 2 deletions src/core/plugins/oas3/spec-extensions/wrap-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { Map } from "immutable"
/**
* Helpers
*/

const map = Map()

function onlyOAS3(selector) {
return (ori, system) =>
(...args) => {
Expand All @@ -31,9 +34,8 @@ const OAS3NullSelector = onlyOAS3(nullSelector)
export const definitions = onlyOAS3(() => (system) => {
const spec = system.getSystem().specSelectors.specJson()
const schemas = spec.getIn(["components", "schemas"])
return Map.isMap(schemas) ? schemas : definitions.mapConst
return Map.isMap(schemas) ? schemas : map
})
definitions.mapConst = Map()

export const hasHost = onlyOAS3(() => (system) => {
const spec = system.getSystem().specSelectors.specJson()
Expand Down
65 changes: 25 additions & 40 deletions src/core/plugins/oas31/components/license.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,35 @@
*/
import React from "react"
import PropTypes from "prop-types"
import { safeBuildUrl } from "core/utils/url"
import { sanitizeUrl } from "core/utils"

class License extends React.Component {
static propTypes = {
license: PropTypes.object,
getComponent: PropTypes.func.isRequired,
selectedServer: PropTypes.string,
url: PropTypes.string.isRequired,
}

render() {
const { license, getComponent, selectedServer, url: specUrl } = this.props
const name = license.get("name", "License")
const url = sanitizeUrl(
safeBuildUrl(license.get("url"), specUrl, { selectedServer })
)
const identifier = license.get("identifier", "")
const spdxURL = sanitizeUrl(`https://spdx.org/licenses/${identifier}.html`)

const Link = getComponent("Link")
import { sanitizeUrl } from "core/utils"

return (
<div className="info__license">
{identifier && (
<div className="info__license__url">
<Link target="_blank" href={spdxURL}>
{name}
</Link>
</div>
)}
const License = ({ getComponent, oas31Selectors }) => {
const name = oas31Selectors.selectLicenseNameField()
const url = oas31Selectors.selectLicenseUrl()
const Link = getComponent("Link")

{url && !identifier && (
<div className="info__license__url">
<Link target="_blank" href={url}>
{name}
</Link>
</div>
)}
return (
<div className="info__license">
{url ? (
<div className="info__license__url">
<Link target="_blank" href={sanitizeUrl(url)}>
{name}
</Link>
</div>
) : (
<span>{name}</span>
)}
</div>
)
}

{!url && !identifier && <span>{name}</span>}
</div>
)
}
License.propTypes = {
getComponent: PropTypes.func.isRequired,
oas31Selectors: PropTypes.shape({
selectLicenseNameField: PropTypes.func.isRequired,
selectLicenseUrl: PropTypes.func.isRequired,
}).isRequired,
}

export default License
21 changes: 20 additions & 1 deletion src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import License from "./components/license"
import Info from "./components/info"
import LicenseWrapper from "./wrap-components/license"
import InfoWrapper from "./wrap-components/info"
import { isOAS31, webhooks } from "./spec-extensions/selectors"
import { isOAS31, license, webhooks } from "./spec-extensions/selectors"
import { isOAS3 } from "./spec-extensions/wrap-selectors"
import {
makeSelectLicenseUrl,
selectLicenseIdentifierField,
selectLicenseNameField,
selectLicenseUrlField,
} from "./selectors"

const OAS31Plugin = () => {
return {
afterLoad(system) {
const oas31Selectors = this.statePlugins.oas31.selectors

oas31Selectors.selectLicenseUrl = makeSelectLicenseUrl(system)
},
components: {
Webhooks,
OAS31Info: Info,
Expand All @@ -24,12 +35,20 @@ const OAS31Plugin = () => {
spec: {
selectors: {
isOAS31,
license,
webhooks,
},
wrapSelectors: {
isOAS3,
},
},
oas31: {
selectors: {
selectLicenseNameField,
selectLicenseUrlField,
selectLicenseIdentifierField,
},
},
},
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/core/plugins/oas31/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @prettier
*/
import { createSelector } from "reselect"

import { safeBuildUrl } from "core/utils/url"

export const selectLicenseNameField = () => (system) => {
return system.specSelectors.license().get("name", "License")
}

export const selectLicenseUrlField = () => (system) => {
return system.specSelectors.license().get("url")
}

export const selectLicenseIdentifierField = () => (system) => {
return system.specSelectors.license().get("identifier")
}

export const makeSelectLicenseUrl = (system) =>
createSelector(
() => system.specSelectors.url(),
() => system.oas3Selectors.selectedServer(),
() => system.oas31Selectors.selectLicenseUrlField(),
() => system.oas31Selectors.selectLicenseIdentifierField(),
(specUrl, selectedServer, url, identifier) => {
if (url) {
return safeBuildUrl(url, specUrl, { selectedServer })
}

if (identifier) {
return `https://spdx.org/licenses/${identifier}.html`
}

return undefined
}
)
10 changes: 7 additions & 3 deletions src/core/plugins/oas31/spec-extensions/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Map } from "immutable"

import { isOAS31 as isOAS31Helper } from "../helpers"

const map = Map()

export const isOAS31 = () => (system) => {
const spec = system.specSelectors.specJson()
return isOAS31Helper(spec)
Expand All @@ -23,7 +25,9 @@ const onlyOAS31 =
}

export const webhooks = onlyOAS31(() => (system) => {
const spec = system.specSelectors.specJson()
return spec.get("webhooks", webhooks.mapConst)
return system.specSelectors.specJson().get("webhooks", map)
})
webhooks.mapConst = Map()

export const license = () => (system) => {
return system.specSelectors.info().get("license", map)
}
4 changes: 2 additions & 2 deletions src/core/plugins/oas31/wrap-components/license.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import React from "react"

const LicenseWrapper = (Original, system) => (props) => {
if (system.specSelectors.isOAS31()) {
const OAS31License = system.getComponent("OAS31License")
const OAS31License = system.getComponent("OAS31License", true)

return <OAS31License {...props} />
return <OAS31License />
}

return <Original {...props} />
Expand Down

0 comments on commit 13e2774

Please sign in to comment.