fix(config): perform configuration synchronously

This commit is contained in:
Vladimir Gorej
2024-05-06 17:27:29 +02:00
committed by Vladimír Gorej
parent fe6c1b88c3
commit 5fa60ce073

View File

@@ -47,7 +47,7 @@ import {
function SwaggerUI(userOptions) { function SwaggerUI(userOptions) {
const queryOptions = optionsFromQuery()(userOptions) const queryOptions = optionsFromQuery()(userOptions)
const runtimeOptions = optionsFromRuntime()() const runtimeOptions = optionsFromRuntime()()
let mergedOptions = SwaggerUI.config.merge( const mergedOptions = SwaggerUI.config.merge(
{}, {},
SwaggerUI.config.defaults, SwaggerUI.config.defaults,
runtimeOptions, runtimeOptions,
@@ -57,53 +57,39 @@ function SwaggerUI(userOptions) {
const storeOptions = systemOptionsFactorization(mergedOptions) const storeOptions = systemOptionsFactorization(mergedOptions)
const InlinePlugin = inlinePluginOptionsFactorization(mergedOptions) const InlinePlugin = inlinePluginOptionsFactorization(mergedOptions)
const system = new System(storeOptions) const unboundSystem = new System(storeOptions)
system.register([mergedOptions.plugins, InlinePlugin]) unboundSystem.register([mergedOptions.plugins, InlinePlugin])
const system = unboundSystem.getSystem()
const boundSystem = system.getSystem() const persistConfigs = (options) => {
unboundSystem.setConfigs(options)
optionsFromURL({ url: mergedOptions.configUrl, system: boundSystem })( system.configsActions.loaded()
mergedOptions }
).then((urlOptions) => { const updateSpec = (options) => {
const urlOptionsFailedToFetch = urlOptions === null if (
!queryOptions.url &&
mergedOptions = SwaggerUI.config.merge( typeof options.spec === "object" &&
{}, Object.keys(options.spec).length > 0
mergedOptions,
urlOptions,
queryOptions
)
system.setConfigs(mergedOptions)
boundSystem.configsActions.loaded()
if (!urlOptionsFailedToFetch) {
if (
!queryOptions.url &&
typeof mergedOptions.spec === "object" &&
Object.keys(mergedOptions.spec).length > 0
) {
boundSystem.specActions.updateUrl("")
boundSystem.specActions.updateLoadingStatus("success")
boundSystem.specActions.updateSpec(JSON.stringify(mergedOptions.spec))
} else if (
typeof boundSystem.specActions.download === "function" &&
mergedOptions.url &&
!mergedOptions.urls
) {
boundSystem.specActions.updateUrl(mergedOptions.url)
boundSystem.specActions.download(mergedOptions.url)
}
}
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
) { ) {
system.specActions.updateUrl("")
system.specActions.updateLoadingStatus("success")
system.specActions.updateSpec(JSON.stringify(options.spec))
} else if (
typeof system.specActions.download === "function" &&
options.url &&
!options.urls
) {
system.specActions.updateUrl(options.url)
system.specActions.download(options.url)
}
}
const render = (options) => {
if (options.domNode) {
system.render(options.domNode, "App")
} else if (options.dom_id) {
const domNode = document.querySelector(options.dom_id)
system.render(domNode, "App")
} else if (options.dom_id === null || options.domNode === null) {
/** /**
* noop * noop
* *
@@ -113,9 +99,34 @@ function SwaggerUI(userOptions) {
} else { } else {
console.error("Skipped rendering: no `dom_id` or `domNode` was specified") console.error("Skipped rendering: no `dom_id` or `domNode` was specified")
} }
}) }
return boundSystem // if no configUrl is provided, we can safely persist the configs and render
if (!mergedOptions.configUrl) {
persistConfigs(mergedOptions)
updateSpec(mergedOptions)
render(mergedOptions)
return system
}
// eslint-disable-next-line no-extra-semi
;(async () => {
const { configUrl: url } = mergedOptions
const urlOptions = await optionsFromURL({ url, system })(mergedOptions)
const urlMergedOptions = SwaggerUI.config.merge(
{},
mergedOptions,
urlOptions,
queryOptions
)
persistConfigs(urlMergedOptions)
if (urlOptions !== null) updateSpec(urlMergedOptions)
render(urlMergedOptions)
})()
return system
} }
SwaggerUI.System = System SwaggerUI.System = System