fix(config): remove system config source (#9875)

Refs #5148
This commit is contained in:
Oliwia Rogala
2024-04-25 16:02:15 +02:00
committed by GitHub
parent 636c352cc8
commit 333e5e38d6
12 changed files with 40 additions and 77 deletions

View File

@@ -2,10 +2,9 @@
### How to configure ### How to configure
Swagger UI accepts configuration parameters in four locations. Swagger UI accepts configuration parameters in three locations.
From lowest to highest precedence: 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 object passed as an argument to Swagger UI (`SwaggerUI({ ... })`)
- configuration document fetched from a specified `configUrl` - configuration document fetched from a specified `configUrl`
- configuration items passed as key/value pairs in the URL query string - configuration items passed as key/value pairs in the URL query string

View File

@@ -53,8 +53,6 @@ const SwaggerUI = ({
plugins, plugins,
spec, spec,
url, url,
dom_id: null,
domNode: null,
layout, layout,
defaultModelsExpandDepth, defaultModelsExpandDepth,
defaultModelRendering, defaultModelRendering,

View File

@@ -5,7 +5,6 @@ export { default as inlinePluginOptionsFactorization } from "./factorization/inl
export { default as storeOptionsFactorization } from "./factorization/store" export { default as storeOptionsFactorization } from "./factorization/store"
export { default as optionsFromQuery } from "./sources/query" export { default as optionsFromQuery } from "./sources/query"
export { default as optionsFromURL } from "./sources/url" export { default as optionsFromURL } from "./sources/url"
export { default as optionsFromSystem } from "./sources/system"
export { default as optionsFromRuntime } from "./sources/runtime" export { default as optionsFromRuntime } from "./sources/runtime"
export { default as defaultOptions } from "./defaults" export { default as defaultOptions } from "./defaults"
export { default as mergeOptions } from "./merge" export { default as mergeOptions } from "./merge"

View File

@@ -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

View File

@@ -7,7 +7,7 @@ const optionsFromURL =
({ url, system }) => ({ url, system }) =>
async (options) => { async (options) => {
if (!url) return {} if (!url) return {}
if (typeof system.specActions?.getConfigByUrl !== "function") return {} if (typeof system.configsActions?.getConfigByUrl !== "function") return {}
let resolve let resolve
const deferred = new Promise((res) => { const deferred = new Promise((res) => {
resolve = res resolve = res
@@ -17,7 +17,7 @@ const optionsFromURL =
resolve(fetchedOptions) resolve(fetchedOptions)
} }
system.specActions.getConfigByUrl( system.configsActions.getConfigByUrl(
{ {
url, url,
loadRemoteConfig: true, loadRemoteConfig: true,

View File

@@ -36,7 +36,6 @@ import {
defaultOptions, defaultOptions,
optionsFromQuery, optionsFromQuery,
optionsFromURL, optionsFromURL,
optionsFromSystem,
optionsFromRuntime, optionsFromRuntime,
mergeOptions, mergeOptions,
inlinePluginOptionsFactorization, inlinePluginOptionsFactorization,
@@ -62,18 +61,13 @@ function SwaggerUI(userOptions) {
store.register([mergedOptions.plugins, InlinePlugin]) store.register([mergedOptions.plugins, InlinePlugin])
const system = store.getSystem() const system = store.getSystem()
const systemOptions = optionsFromSystem({ system })(mergedOptions)
optionsFromURL({ url: mergedOptions.configUrl, system })(mergedOptions).then( optionsFromURL({ url: mergedOptions.configUrl, system })(mergedOptions).then(
(urlOptions) => { (urlOptions) => {
const urlOptionsFailedToFetch = urlOptions === null const urlOptionsFailedToFetch = urlOptions === null
mergedOptions = SwaggerUI.config.merge( mergedOptions = SwaggerUI.config.merge(
{}, {},
SwaggerUI.config.defaults, mergedOptions,
runtimeOptions,
systemOptions,
userOptions,
urlOptions, urlOptions,
queryOptions queryOptions
) )

View File

@@ -1,3 +1,8 @@
/**
* @prettier
*/
import { parseConfig } from "./fn"
export const UPDATE_CONFIGS = "configs_update" export const UPDATE_CONFIGS = "configs_update"
export const TOGGLE_CONFIGS = "configs_toggle" export const TOGGLE_CONFIGS = "configs_toggle"
@@ -6,7 +11,7 @@ export function update(configName, configValue) {
return { return {
type: UPDATE_CONFIGS, type: UPDATE_CONFIGS,
payload: { payload: {
[configName]: configValue [configName]: configValue,
}, },
} }
} }
@@ -19,8 +24,35 @@ export function toggle(configName) {
} }
} }
// Hook // Hook
export const loaded = () => () => { export const loaded = () => () => {
// noop // 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))
}
}
}

View File

@@ -1,6 +1,6 @@
import YAML from "js-yaml" import YAML from "js-yaml"
export const parseYamlConfig = (yaml, system) => { export const parseConfig = (yaml, system) => {
try { try {
return YAML.load(yaml) return YAML.load(yaml)
} catch(e) { } catch(e) {

View File

@@ -1,25 +1,11 @@
import yamlConfig from "root/swagger-config.yaml"
import { parseYamlConfig } from "./helpers"
import * as actions from "./actions" import * as actions from "./actions"
import * as specActions from "./spec-actions"
import * as selectors from "./selectors" import * as selectors from "./selectors"
import reducers from "./reducers" import reducers from "./reducers"
const specSelectors = {
getLocalConfig: () => {
return parseYamlConfig(yamlConfig)
}
}
export default function configsPlugin() { export default function configsPlugin() {
return { return {
statePlugins: { statePlugins: {
spec: {
actions: specActions,
selectors: specSelectors,
},
configs: { configs: {
reducers, reducers,
actions, actions,

View File

@@ -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))
}
}
}

View File

@@ -1,4 +0,0 @@
---
url: "https://petstore.swagger.io/v2/swagger.json"
dom_id: "#swagger-ui"
validatorUrl: "https://validator.swagger.io/validator"

View File

@@ -1,4 +1,4 @@
import { downloadConfig } from "core/plugins/configs/spec-actions" import { downloadConfig } from "core/plugins/configs/actions"
describe("configs plugin - actions", () => { describe("configs plugin - actions", () => {