refactor(oas31): consolidate plugin selectors (#8477)

Refs #8474
This commit is contained in:
Vladimír Gorej
2023-03-16 17:15:27 +01:00
committed by GitHub
parent 990d1f288e
commit 91ba415632
7 changed files with 108 additions and 66 deletions

View File

@@ -4,6 +4,7 @@
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes" import ImPropTypes from "react-immutable-proptypes"
import { sanitizeUrl } from "core/utils" import { sanitizeUrl } from "core/utils"
import { safeBuildUrl } from "core/utils/url" import { safeBuildUrl } from "core/utils/url"

View File

@@ -6,9 +6,10 @@ import PropTypes from "prop-types"
import { sanitizeUrl } from "core/utils" import { sanitizeUrl } from "core/utils"
const License = ({ getComponent, oas31Selectors }) => { const License = ({ getComponent, specSelectors }) => {
const name = oas31Selectors.selectLicenseNameField() const name = specSelectors.selectLicenseNameField()
const url = oas31Selectors.selectLicenseUrl() const url = specSelectors.selectLicenseUrl()
const Link = getComponent("Link") const Link = getComponent("Link")
return ( return (
@@ -28,7 +29,7 @@ const License = ({ getComponent, oas31Selectors }) => {
License.propTypes = { License.propTypes = {
getComponent: PropTypes.func.isRequired, getComponent: PropTypes.func.isRequired,
oas31Selectors: PropTypes.shape({ specSelectors: PropTypes.shape({
selectLicenseNameField: PropTypes.func.isRequired, selectLicenseNameField: PropTypes.func.isRequired,
selectLicenseUrl: PropTypes.func.isRequired, selectLicenseUrl: PropTypes.func.isRequired,
}).isRequired, }).isRequired,

View File

@@ -8,3 +8,15 @@ export const isOAS31 = (jsSpec) => {
typeof oasVersion === "string" && /^3\.1\.(?:[1-9]\d*|0)$/.test(oasVersion) typeof oasVersion === "string" && /^3\.1\.(?:[1-9]\d*|0)$/.test(oasVersion)
) )
} }
export const onlyOAS31 =
(selector) =>
() =>
(system, ...args) => {
if (system.getSystem().specSelectors.isOAS31()) {
const result = selector(...args)
return typeof result === "function" ? result(system, ...args) : result
} else {
return null
}
}

View File

@@ -6,21 +6,30 @@ import License from "./components/license"
import Info from "./components/info" import Info from "./components/info"
import LicenseWrapper from "./wrap-components/license" import LicenseWrapper from "./wrap-components/license"
import InfoWrapper from "./wrap-components/info" import InfoWrapper from "./wrap-components/info"
import { isOAS31, license, webhooks } from "./spec-extensions/selectors"
import { isOAS3 } from "./spec-extensions/wrap-selectors"
import { import {
makeSelectLicenseUrl, license,
selectLicenseIdentifierField, webhooks,
selectLicenseNameField, selectLicenseNameField,
selectLicenseUrlField, selectLicenseUrlField,
} from "./selectors" selectLicenseIdentifierField,
makeIsOAS31,
makeSelectLicenseUrl,
} from "./spec-extensions/selectors"
import {
isOAS3 as isOAS3Wrapper,
selectLicenseUrl as selectLicenseUrlWrapper,
} from "./spec-extensions/wrap-selectors"
import { makeSelectLicenseUrl as makeOAS31SelectLicenseUrl } from "./selectors"
const OAS31Plugin = () => { const OAS31Plugin = () => {
return { return {
afterLoad(system) { afterLoad(system) {
const oas31Selectors = this.statePlugins.oas31.selectors const oas31Selectors = this.statePlugins.oas31.selectors
const specSelectors = this.statePlugins.spec.selectors
oas31Selectors.selectLicenseUrl = makeSelectLicenseUrl(system) specSelectors.selectLicenseUrl = makeSelectLicenseUrl(system)
specSelectors.isOAS31 = makeIsOAS31(system)
oas31Selectors.selectLicenseUrl = makeOAS31SelectLicenseUrl(system)
}, },
components: { components: {
Webhooks, Webhooks,
@@ -34,20 +43,19 @@ const OAS31Plugin = () => {
statePlugins: { statePlugins: {
spec: { spec: {
selectors: { selectors: {
isOAS31,
license, license,
webhooks,
},
wrapSelectors: {
isOAS3,
},
},
oas31: {
selectors: {
selectLicenseNameField, selectLicenseNameField,
selectLicenseUrlField, selectLicenseUrlField,
selectLicenseIdentifierField, selectLicenseIdentifierField,
webhooks,
}, },
wrapSelectors: {
isOAS3: isOAS3Wrapper,
selectLicenseUrl: selectLicenseUrlWrapper,
},
},
oas31: {
selectors: {},
}, },
}, },
} }

View File

@@ -4,34 +4,25 @@
import { createSelector } from "reselect" import { createSelector } from "reselect"
import { safeBuildUrl } from "core/utils/url" import { safeBuildUrl } from "core/utils/url"
import { onlyOAS31 } from "./helpers"
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) => export const makeSelectLicenseUrl = (system) =>
createSelector( onlyOAS31(
() => system.specSelectors.url(), createSelector(
() => system.oas3Selectors.selectedServer(), () => system.specSelectors.url(),
() => system.oas31Selectors.selectLicenseUrlField(), () => system.oas3Selectors.selectedServer(),
() => system.oas31Selectors.selectLicenseIdentifierField(), () => system.specSelectors.selectLicenseUrlField(),
(specUrl, selectedServer, url, identifier) => { () => system.specSelectors.selectLicenseIdentifierField(),
if (url) { (specUrl, selectedServer, url, identifier) => {
return safeBuildUrl(url, specUrl, { selectedServer }) if (url) {
} return safeBuildUrl(url, specUrl, { selectedServer })
}
if (identifier) { if (identifier) {
return `https://spdx.org/licenses/${identifier}.html` return `https://spdx.org/licenses/${identifier}.html`
} }
return undefined return undefined
} }
)
) )

View File

@@ -2,27 +2,15 @@
* @prettier * @prettier
*/ */
import { Map } from "immutable" import { Map } from "immutable"
import { createSelector } from "reselect"
import { isOAS31 as isOAS31Helper } from "../helpers" import { safeBuildUrl } from "core/utils/url"
import { isOAS31 as isOAS31Helper, onlyOAS31 } from "../helpers"
const map = Map() const map = Map()
export const isOAS31 = () => (system) => { export const makeIsOAS31 = (system) =>
const spec = system.specSelectors.specJson() createSelector(() => system.specSelectors.specJson(), isOAS31Helper)
return isOAS31Helper(spec)
}
const onlyOAS31 =
(selector) =>
() =>
(system, ...args) => {
if (system.getSystem().specSelectors.isOAS31()) {
const result = selector(...args)
return typeof result === "function" ? result(system, ...args) : result
} else {
return null
}
}
export const webhooks = onlyOAS31(() => (system) => { export const webhooks = onlyOAS31(() => (system) => {
return system.specSelectors.specJson().get("webhooks", map) return system.specSelectors.specJson().get("webhooks", map)
@@ -31,3 +19,29 @@ export const webhooks = onlyOAS31(() => (system) => {
export const license = () => (system) => { export const license = () => (system) => {
return system.specSelectors.info().get("license", map) return system.specSelectors.info().get("license", map)
} }
export const selectLicenseNameField = () => (system) => {
return system.specSelectors.license().get("name", "License")
}
export const selectLicenseUrlField = () => (system) => {
return system.specSelectors.license().get("url")
}
export const selectLicenseIdentifierField = onlyOAS31(() => (system) => {
return system.specSelectors.license().get("identifier")
})
export const makeSelectLicenseUrl = (system) =>
createSelector(
() => system.specSelectors.url(),
() => system.oas3Selectors.selectedServer(),
() => system.specSelectors.selectLicenseUrlField(),
(specUrl, selectedServer, url) => {
if (url) {
return safeBuildUrl(url, specUrl, { selectedServer })
}
return undefined
}
)

View File

@@ -1,4 +1,19 @@
export const isOAS3 = (oriSelector, system) => (state, ...args) => { /**
const isOAS31 = system.specSelectors.isOAS31() * @prettier
return isOAS31 || oriSelector(...args) */
} export const isOAS3 =
(oriSelector, system) =>
(state, ...args) => {
const isOAS31 = system.specSelectors.isOAS31()
return isOAS31 || oriSelector(...args)
}
export const selectLicenseUrl =
(oriSelector, system) =>
(state, ...args) => {
if (system.specSelectors.isOAS31()) {
return system.oas31Selectors.selectLicenseUrl()
}
return oriSelector(...args)
}