refactor(config): use conventional naming
This commit is contained in:
committed by
Vladimír Gorej
parent
a56643fe07
commit
fe6c1b88c3
@@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
import deepExtend from "deep-extend"
|
import deepExtend from "deep-extend"
|
||||||
|
|
||||||
const storeFactorization = (options) => {
|
const systemFactorization = (options) => {
|
||||||
const state = deepExtend(
|
const state = deepExtend(
|
||||||
{
|
{
|
||||||
layout: {
|
layout: {
|
||||||
@@ -42,4 +42,4 @@ const storeFactorization = (options) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default storeFactorization
|
export default systemFactorization
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* @prettier
|
* @prettier
|
||||||
*/
|
*/
|
||||||
export { default as inlinePluginOptionsFactorization } from "./factorization/inline-plugin"
|
export { default as inlinePluginOptionsFactorization } from "./factorization/inline-plugin"
|
||||||
export { default as storeOptionsFactorization } from "./factorization/store"
|
export { default as systemOptionsFactorization } from "./factorization/system"
|
||||||
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 optionsFromRuntime } from "./sources/runtime"
|
export { default as optionsFromRuntime } from "./sources/runtime"
|
||||||
|
|||||||
@@ -2,19 +2,24 @@
|
|||||||
* @prettier
|
* @prettier
|
||||||
* Receives options from a remote URL.
|
* Receives options from a remote URL.
|
||||||
*/
|
*/
|
||||||
|
const makeDeferred = () => {
|
||||||
|
const deferred = {}
|
||||||
|
deferred.promise = new Promise((resolve, reject) => {
|
||||||
|
deferred.resolve = resolve
|
||||||
|
deferred.reject = reject
|
||||||
|
})
|
||||||
|
return deferred
|
||||||
|
}
|
||||||
|
|
||||||
const optionsFromURL =
|
const optionsFromURL =
|
||||||
({ url, system }) =>
|
({ url, system }) =>
|
||||||
async (options) => {
|
async (options) => {
|
||||||
if (!url) return {}
|
if (!url) return {}
|
||||||
if (typeof system.configsActions?.getConfigByUrl !== "function") return {}
|
if (typeof system.configsActions?.getConfigByUrl !== "function") return {}
|
||||||
let resolve
|
const deferred = makeDeferred()
|
||||||
const deferred = new Promise((res) => {
|
|
||||||
resolve = res
|
|
||||||
})
|
|
||||||
const callback = (fetchedOptions) => {
|
const callback = (fetchedOptions) => {
|
||||||
// receives null on remote URL fetch failure
|
// receives null on remote URL fetch failure
|
||||||
resolve(fetchedOptions)
|
deferred.resolve(fetchedOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
system.configsActions.getConfigByUrl(
|
system.configsActions.getConfigByUrl(
|
||||||
@@ -27,7 +32,7 @@ const optionsFromURL =
|
|||||||
callback
|
callback
|
||||||
)
|
)
|
||||||
|
|
||||||
return deferred
|
return deferred.promise
|
||||||
}
|
}
|
||||||
|
|
||||||
export default optionsFromURL
|
export default optionsFromURL
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import {
|
|||||||
optionsFromRuntime,
|
optionsFromRuntime,
|
||||||
mergeOptions,
|
mergeOptions,
|
||||||
inlinePluginOptionsFactorization,
|
inlinePluginOptionsFactorization,
|
||||||
storeOptionsFactorization,
|
systemOptionsFactorization,
|
||||||
typeCastOptions,
|
typeCastOptions,
|
||||||
typeCastMappings,
|
typeCastMappings,
|
||||||
} from "./config"
|
} from "./config"
|
||||||
@@ -54,65 +54,68 @@ function SwaggerUI(userOptions) {
|
|||||||
userOptions,
|
userOptions,
|
||||||
queryOptions
|
queryOptions
|
||||||
)
|
)
|
||||||
const storeOptions = storeOptionsFactorization(mergedOptions)
|
const storeOptions = systemOptionsFactorization(mergedOptions)
|
||||||
const InlinePlugin = inlinePluginOptionsFactorization(mergedOptions)
|
const InlinePlugin = inlinePluginOptionsFactorization(mergedOptions)
|
||||||
|
|
||||||
const store = new System(storeOptions)
|
const system = new System(storeOptions)
|
||||||
store.register([mergedOptions.plugins, InlinePlugin])
|
system.register([mergedOptions.plugins, InlinePlugin])
|
||||||
const system = store.getSystem()
|
|
||||||
|
|
||||||
optionsFromURL({ url: mergedOptions.configUrl, system })(mergedOptions).then(
|
const boundSystem = system.getSystem()
|
||||||
(urlOptions) => {
|
|
||||||
const urlOptionsFailedToFetch = urlOptions === null
|
|
||||||
|
|
||||||
mergedOptions = SwaggerUI.config.merge(
|
optionsFromURL({ url: mergedOptions.configUrl, system: boundSystem })(
|
||||||
{},
|
mergedOptions
|
||||||
mergedOptions,
|
).then((urlOptions) => {
|
||||||
urlOptions,
|
const urlOptionsFailedToFetch = urlOptions === null
|
||||||
queryOptions
|
|
||||||
)
|
|
||||||
store.setConfigs(mergedOptions)
|
|
||||||
system.configsActions.loaded()
|
|
||||||
|
|
||||||
if (!urlOptionsFailedToFetch) {
|
mergedOptions = SwaggerUI.config.merge(
|
||||||
if (
|
{},
|
||||||
!queryOptions.url &&
|
mergedOptions,
|
||||||
typeof mergedOptions.spec === "object" &&
|
urlOptions,
|
||||||
Object.keys(mergedOptions.spec).length > 0
|
queryOptions
|
||||||
) {
|
)
|
||||||
system.specActions.updateUrl("")
|
system.setConfigs(mergedOptions)
|
||||||
system.specActions.updateLoadingStatus("success")
|
boundSystem.configsActions.loaded()
|
||||||
system.specActions.updateSpec(JSON.stringify(mergedOptions.spec))
|
|
||||||
} else if (
|
|
||||||
typeof system.specActions.download === "function" &&
|
|
||||||
mergedOptions.url &&
|
|
||||||
!mergedOptions.urls
|
|
||||||
) {
|
|
||||||
system.specActions.updateUrl(mergedOptions.url)
|
|
||||||
system.specActions.download(mergedOptions.url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mergedOptions.domNode) {
|
if (!urlOptionsFailedToFetch) {
|
||||||
system.render(mergedOptions.domNode, "App")
|
if (
|
||||||
} else if (mergedOptions.dom_id) {
|
!queryOptions.url &&
|
||||||
let domNode = document.querySelector(mergedOptions.dom_id)
|
typeof mergedOptions.spec === "object" &&
|
||||||
system.render(domNode, "App")
|
Object.keys(mergedOptions.spec).length > 0
|
||||||
} else if (
|
|
||||||
mergedOptions.dom_id === null ||
|
|
||||||
mergedOptions.domNode === null
|
|
||||||
) {
|
) {
|
||||||
// do nothing
|
boundSystem.specActions.updateUrl("")
|
||||||
// this is useful for testing that does not need to do any rendering
|
boundSystem.specActions.updateLoadingStatus("success")
|
||||||
} else {
|
boundSystem.specActions.updateSpec(JSON.stringify(mergedOptions.spec))
|
||||||
console.error(
|
} else if (
|
||||||
"Skipped rendering: no `dom_id` or `domNode` was specified"
|
typeof boundSystem.specActions.download === "function" &&
|
||||||
)
|
mergedOptions.url &&
|
||||||
|
!mergedOptions.urls
|
||||||
|
) {
|
||||||
|
boundSystem.specActions.updateUrl(mergedOptions.url)
|
||||||
|
boundSystem.specActions.download(mergedOptions.url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
return system
|
if (mergedOptions.domNode) {
|
||||||
|
boundSystem.render(mergedOptions.domNode, "App")
|
||||||
|
} else if (mergedOptions.dom_id) {
|
||||||
|
const domNode = document.querySelector(mergedOptions.dom_id)
|
||||||
|
boundSystem.render(domNode, "App")
|
||||||
|
} else if (
|
||||||
|
mergedOptions.dom_id === null ||
|
||||||
|
mergedOptions.domNode === null
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* noop
|
||||||
|
*
|
||||||
|
* SwaggerUI instance can be created without any rendering involved.
|
||||||
|
* This is also useful for lazy rendering or testing.
|
||||||
|
*/
|
||||||
|
} else {
|
||||||
|
console.error("Skipped rendering: no `dom_id` or `domNode` was specified")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return boundSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
SwaggerUI.System = System
|
SwaggerUI.System = System
|
||||||
|
|||||||
Reference in New Issue
Block a user