From 333e5e38d6bf8f678e59f0bf9a5556c5f8c339ee Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Thu, 25 Apr 2024 16:02:15 +0200 Subject: [PATCH] fix(config): remove system config source (#9875) Refs #5148 --- docs/usage/configuration.md | 3 +- flavors/swagger-ui-react/index.jsx | 2 -- src/core/config/index.js | 1 - src/core/config/sources/system.js | 16 --------- src/core/config/sources/url.js | 4 +-- src/core/index.js | 8 +---- src/core/plugins/configs/actions.js | 36 +++++++++++++++++-- .../plugins/configs/{helpers.js => fn.js} | 2 +- src/core/plugins/configs/index.js | 14 -------- src/core/plugins/configs/spec-actions.js | 25 ------------- swagger-config.yaml | 4 --- test/unit/core/plugins/configs/actions.js | 2 +- 12 files changed, 40 insertions(+), 77 deletions(-) delete mode 100644 src/core/config/sources/system.js rename src/core/plugins/configs/{helpers.js => fn.js} (77%) delete mode 100644 src/core/plugins/configs/spec-actions.js delete mode 100644 swagger-config.yaml diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 2823fb83107..5d96636d440 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -2,10 +2,9 @@ ### How to configure -Swagger UI accepts configuration parameters in four locations. +Swagger UI accepts configuration parameters in three locations. From lowest to highest precedence: -- The `swagger-config.yaml` in the project root directory, if it exists, is baked into the application - configuration object passed as an argument to Swagger UI (`SwaggerUI({ ... })`) - configuration document fetched from a specified `configUrl` - configuration items passed as key/value pairs in the URL query string diff --git a/flavors/swagger-ui-react/index.jsx b/flavors/swagger-ui-react/index.jsx index 71ea324952f..fe611c39541 100644 --- a/flavors/swagger-ui-react/index.jsx +++ b/flavors/swagger-ui-react/index.jsx @@ -53,8 +53,6 @@ const SwaggerUI = ({ plugins, spec, url, - dom_id: null, - domNode: null, layout, defaultModelsExpandDepth, defaultModelRendering, diff --git a/src/core/config/index.js b/src/core/config/index.js index bb3058ae077..b0377a8141f 100644 --- a/src/core/config/index.js +++ b/src/core/config/index.js @@ -5,7 +5,6 @@ export { default as inlinePluginOptionsFactorization } from "./factorization/inl export { default as storeOptionsFactorization } from "./factorization/store" export { default as optionsFromQuery } from "./sources/query" export { default as optionsFromURL } from "./sources/url" -export { default as optionsFromSystem } from "./sources/system" export { default as optionsFromRuntime } from "./sources/runtime" export { default as defaultOptions } from "./defaults" export { default as mergeOptions } from "./merge" diff --git a/src/core/config/sources/system.js b/src/core/config/sources/system.js deleted file mode 100644 index 220a658b7b5..00000000000 --- a/src/core/config/sources/system.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @prettier - * - * Receives options from a System. - * These options are baked-in to the System during the compile time. - */ - -const optionsFromSystem = - ({ system }) => - () => { - if (typeof system.specSelectors?.getLocalConfig !== "function") return {} - - return system.specSelectors.getLocalConfig() - } - -export default optionsFromSystem diff --git a/src/core/config/sources/url.js b/src/core/config/sources/url.js index 962b45c7010..f40f73dfc86 100644 --- a/src/core/config/sources/url.js +++ b/src/core/config/sources/url.js @@ -7,7 +7,7 @@ const optionsFromURL = ({ url, system }) => async (options) => { if (!url) return {} - if (typeof system.specActions?.getConfigByUrl !== "function") return {} + if (typeof system.configsActions?.getConfigByUrl !== "function") return {} let resolve const deferred = new Promise((res) => { resolve = res @@ -17,7 +17,7 @@ const optionsFromURL = resolve(fetchedOptions) } - system.specActions.getConfigByUrl( + system.configsActions.getConfigByUrl( { url, loadRemoteConfig: true, diff --git a/src/core/index.js b/src/core/index.js index 70657fd5224..ec0c36d8006 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -36,7 +36,6 @@ import { defaultOptions, optionsFromQuery, optionsFromURL, - optionsFromSystem, optionsFromRuntime, mergeOptions, inlinePluginOptionsFactorization, @@ -62,18 +61,13 @@ function SwaggerUI(userOptions) { store.register([mergedOptions.plugins, InlinePlugin]) const system = store.getSystem() - const systemOptions = optionsFromSystem({ system })(mergedOptions) - optionsFromURL({ url: mergedOptions.configUrl, system })(mergedOptions).then( (urlOptions) => { const urlOptionsFailedToFetch = urlOptions === null mergedOptions = SwaggerUI.config.merge( {}, - SwaggerUI.config.defaults, - runtimeOptions, - systemOptions, - userOptions, + mergedOptions, urlOptions, queryOptions ) diff --git a/src/core/plugins/configs/actions.js b/src/core/plugins/configs/actions.js index 15c95bbc1a1..65df630e7d7 100644 --- a/src/core/plugins/configs/actions.js +++ b/src/core/plugins/configs/actions.js @@ -1,3 +1,8 @@ +/** + * @prettier + */ +import { parseConfig } from "./fn" + export const UPDATE_CONFIGS = "configs_update" export const TOGGLE_CONFIGS = "configs_toggle" @@ -6,7 +11,7 @@ export function update(configName, configValue) { return { type: UPDATE_CONFIGS, payload: { - [configName]: configValue + [configName]: configValue, }, } } @@ -19,8 +24,35 @@ export function toggle(configName) { } } - // Hook export const loaded = () => () => { // noop } + +export const downloadConfig = (req) => (system) => { + const { + fn: { fetch }, + } = system + + return fetch(req) +} + +export const getConfigByUrl = (req, cb) => (system) => { + const { specActions, configsActions } = system + + if (req) { + return configsActions.downloadConfig(req).then(next, next) + } + + function next(res) { + if (res instanceof Error || res.status >= 400) { + specActions.updateLoadingStatus("failedConfig") + specActions.updateLoadingStatus("failedConfig") + specActions.updateUrl("") + console.error(res.statusText + " " + req.url) + cb(null) + } else { + cb(parseConfig(res.text, system)) + } + } +} diff --git a/src/core/plugins/configs/helpers.js b/src/core/plugins/configs/fn.js similarity index 77% rename from src/core/plugins/configs/helpers.js rename to src/core/plugins/configs/fn.js index b0a6e881083..29a1a90da7f 100644 --- a/src/core/plugins/configs/helpers.js +++ b/src/core/plugins/configs/fn.js @@ -1,6 +1,6 @@ import YAML from "js-yaml" -export const parseYamlConfig = (yaml, system) => { +export const parseConfig = (yaml, system) => { try { return YAML.load(yaml) } catch(e) { diff --git a/src/core/plugins/configs/index.js b/src/core/plugins/configs/index.js index 5003e2e046e..b7f4d0fcbf4 100644 --- a/src/core/plugins/configs/index.js +++ b/src/core/plugins/configs/index.js @@ -1,25 +1,11 @@ -import yamlConfig from "root/swagger-config.yaml" -import { parseYamlConfig } from "./helpers" import * as actions from "./actions" -import * as specActions from "./spec-actions" import * as selectors from "./selectors" import reducers from "./reducers" -const specSelectors = { - getLocalConfig: () => { - return parseYamlConfig(yamlConfig) - } -} - - export default function configsPlugin() { return { statePlugins: { - spec: { - actions: specActions, - selectors: specSelectors, - }, configs: { reducers, actions, diff --git a/src/core/plugins/configs/spec-actions.js b/src/core/plugins/configs/spec-actions.js deleted file mode 100644 index a7ce5981292..00000000000 --- a/src/core/plugins/configs/spec-actions.js +++ /dev/null @@ -1,25 +0,0 @@ -import { parseYamlConfig } from "./helpers" - -export const downloadConfig = (req) => (system) => { - const {fn: { fetch }} = system - - return fetch(req) -} - -export const getConfigByUrl = (req, cb)=> ({ specActions }) => { - if (req) { - return specActions.downloadConfig(req).then(next, next) - } - - function next(res) { - if (res instanceof Error || res.status >= 400) { - specActions.updateLoadingStatus("failedConfig") - specActions.updateLoadingStatus("failedConfig") - specActions.updateUrl("") - console.error(res.statusText + " " + req.url) - cb(null) - } else { - cb(parseYamlConfig(res.text)) - } - } -} diff --git a/swagger-config.yaml b/swagger-config.yaml deleted file mode 100644 index c21819c7e87..00000000000 --- a/swagger-config.yaml +++ /dev/null @@ -1,4 +0,0 @@ ---- -url: "https://petstore.swagger.io/v2/swagger.json" -dom_id: "#swagger-ui" -validatorUrl: "https://validator.swagger.io/validator" diff --git a/test/unit/core/plugins/configs/actions.js b/test/unit/core/plugins/configs/actions.js index fd1c6f8704a..6fae0d08fc0 100644 --- a/test/unit/core/plugins/configs/actions.js +++ b/test/unit/core/plugins/configs/actions.js @@ -1,4 +1,4 @@ -import { downloadConfig } from "core/plugins/configs/spec-actions" +import { downloadConfig } from "core/plugins/configs/actions" describe("configs plugin - actions", () => {