Skip to content

Commit

Permalink
refactor(oas31): introduce selector composition mechanism (#8484)
Browse files Browse the repository at this point in the history
Refs #8474
  • Loading branch information
char0n authored Mar 20, 2023
1 parent 47e12f1 commit d709979
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 176 deletions.
50 changes: 50 additions & 0 deletions src/core/plugins/oas31/fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @prettier
*/

export const isOAS31 = (jsSpec) => {
const oasVersion = jsSpec.get("openapi")

return (
typeof oasVersion === "string" && /^3\.1\.(?:[1-9]\d*|0)$/.test(oasVersion)
)
}

/**
* Creates selector that returns value of the original
* selector when spec is OpenAPI 3.1.0., null otherwise.
*
* @param selector
* @returns {function(*, ...[*]): function(*): (*|null)}
*/
export const createOnlyOAS31Selector =
(selector) =>
(state, ...args) =>
(system) => {
if (system.getSystem().specSelectors.isOAS31()) {
const selectedValue = selector(state, ...args)
return typeof selectedValue === "function"
? selectedValue(system)
: selectedValue
} else {
return null
}
}

/**
* Creates selector that provides system as the
* second argument. This allows to create memoized
* composed selectors from different plugins.
*
* @param selector
* @returns {function(*, ...[*]): function(*): *}
*/
export const createSystemSelector =
(selector) =>
(state, ...args) =>
(system) => {
const selectedValue = selector(state, system, ...args)
return typeof selectedValue === "function"
? selectedValue(system)
: selectedValue
}
43 changes: 0 additions & 43 deletions src/core/plugins/oas31/helpers.js

This file was deleted.

67 changes: 38 additions & 29 deletions src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,48 @@ import LicenseWrapper from "./wrap-components/license"
import ContactWrapper from "./wrap-components/contact"
import InfoWrapper from "./wrap-components/info"
import {
license,
contact,
webhooks,
license as selectLicense,
contact as selectContact,
webhooks as selectWebhooks,
selectLicenseNameField,
selectLicenseUrlField,
selectLicenseIdentifierField,
selectContactNameField,
selectContactEmailField,
selectContactUrlField,
makeSelectContactUrl,
makeIsOAS31,
makeSelectLicenseUrl,
selectContactUrl,
isOAS31 as selectIsOAS31,
selectLicenseUrl,
selectInfoTitleField,
selectInfoSummaryField,
selectInfoDescriptionField,
selectInfoTermsOfServiceField,
makeSelectInfoTermsOfServiceUrl,
selectInfoTermsOfServiceUrl,
selectExternalDocsDescriptionField,
selectExternalDocsUrlField,
makeSelectExternalDocsUrl,
makeSelectWebhooksOperations,
selectExternalDocsUrl,
selectWebhooksOperations,
} from "./spec-extensions/selectors"
import {
isOAS3 as isOAS3Wrapper,
selectLicenseUrl as selectLicenseUrlWrapper,
} from "./spec-extensions/wrap-selectors"
import { makeSelectLicenseUrl as makeOAS31SelectLicenseUrl } from "./selectors"

const OAS31Plugin = () => {
return {
afterLoad(system) {
const oas31Selectors = this.statePlugins.oas31.selectors
const specSelectors = this.statePlugins.spec.selectors
import { selectLicenseUrl as selectOAS31LicenseUrl } from "./selectors"
import {
isOAS31 as isOAS31Fn,
createOnlyOAS31Selector as createOnlyOAS31SelectorFn,
createSystemSelector as createSystemSelectorFn,
} from "./fn"

specSelectors.isOAS31 = makeIsOAS31(system)
specSelectors.selectLicenseUrl = makeSelectLicenseUrl(system)
specSelectors.selectContactUrl = makeSelectContactUrl(system)
specSelectors.selectInfoTermsOfServiceUrl = makeSelectInfoTermsOfServiceUrl(system) // prettier-ignore
specSelectors.selectExternalDocsUrl = makeSelectExternalDocsUrl(system)
specSelectors.selectWebhooksOperations = makeSelectWebhooksOperations(system) // prettier-ignore
const OAS31Plugin = ({ fn }) => {
const createSystemSelector = fn.createSystemSelector || createSystemSelectorFn
const createOnlyOAS31Selector = fn.createOnlyOAS31Selector || createOnlyOAS31SelectorFn // prettier-ignore

oas31Selectors.selectLicenseUrl = makeOAS31SelectLicenseUrl(system)
return {
fn: {
isOAs31: isOAS31Fn,
createSystemSelector: createSystemSelectorFn,
createOnlyOAS31Selector: createOnlyOAS31SelectorFn,
},
components: {
Webhooks,
Expand All @@ -66,33 +66,42 @@ const OAS31Plugin = () => {
statePlugins: {
spec: {
selectors: {
license,
isOAS31: createSystemSelector(selectIsOAS31),

license: selectLicense,
selectLicenseNameField,
selectLicenseUrlField,
selectLicenseIdentifierField,
selectLicenseIdentifierField: createOnlyOAS31Selector(selectLicenseIdentifierField), // prettier-ignore
selectLicenseUrl: createSystemSelector(selectLicenseUrl),

contact,
contact: selectContact,
selectContactNameField,
selectContactEmailField,
selectContactUrlField,
selectContactUrl: createSystemSelector(selectContactUrl),

selectInfoTitleField,
selectInfoSummaryField,
selectInfoSummaryField: createOnlyOAS31Selector(selectInfoSummaryField), // prettier-ignore
selectInfoDescriptionField,
selectInfoTermsOfServiceField,
selectInfoTermsOfServiceUrl: createSystemSelector(selectInfoTermsOfServiceUrl), // prettier-ignore

selectExternalDocsDescriptionField,
selectExternalDocsUrlField,
selectExternalDocsUrl: createSystemSelector(selectExternalDocsUrl),

webhooks,
webhooks: createOnlyOAS31Selector(selectWebhooks),
selectWebhooksOperations: createOnlyOAS31Selector(createSystemSelector(selectWebhooksOperations)), // prettier-ignore
},
wrapSelectors: {
isOAS3: isOAS3Wrapper,
selectLicenseUrl: selectLicenseUrlWrapper,
},
},
oas31: {
selectors: {},
selectors: {
selectLicenseUrl: createOnlyOAS31Selector(createSystemSelector(selectOAS31LicenseUrl)), // prettier-ignore
},
},
},
}
Expand Down
34 changes: 15 additions & 19 deletions src/core/plugins/oas31/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@
import { createSelector } from "reselect"

import { safeBuildUrl } from "core/utils/url"
import { onlyOAS31 } from "./helpers"

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

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

return undefined
}
)
)
return undefined
}
)
Loading

0 comments on commit d709979

Please sign in to comment.