From 60670d1bbd5cfad367e6ed145b1c871d19e72ffa Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Thu, 18 Apr 2024 16:40:04 +0200 Subject: [PATCH] fix(config): add options defined during runtime (#9845) Refs #9844 --- src/core/config/defaults.js | 2 +- src/core/config/index.js | 1 + src/core/config/sources/runtime.js | 18 ++++++++++++++++++ src/core/index.js | 6 ++++-- 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/core/config/sources/runtime.js diff --git a/src/core/config/defaults.js b/src/core/config/defaults.js index ef2daf7db41..2ff789852ec 100644 --- a/src/core/config/defaults.js +++ b/src/core/config/defaults.js @@ -15,7 +15,7 @@ const defaultOptions = Object.freeze({ maxDisplayedTags: -1, filter: false, validatorUrl: "https://validator.swagger.io/validator", - oauth2RedirectUrl: `${window.location.protocol}//${window.location.host}${window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/"))}/oauth2-redirect.html`, + oauth2RedirectUrl: undefined, persistAuthorization: false, configs: {}, custom: {}, diff --git a/src/core/config/index.js b/src/core/config/index.js index e0b7d94544a..a789b0b1f15 100644 --- a/src/core/config/index.js +++ b/src/core/config/index.js @@ -3,5 +3,6 @@ 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/runtime.js b/src/core/config/sources/runtime.js new file mode 100644 index 00000000000..38cb4af7c48 --- /dev/null +++ b/src/core/config/sources/runtime.js @@ -0,0 +1,18 @@ +/** + * @prettier + * + * Receives options at runtime. + */ + +/* eslint-disable no-undef */ +const optionsFromRuntime = () => () => { + const options = {} + + if (globalThis.location) { + options.oauth2RedirectUrl = `${globalThis.location.protocol}//${globalThis.location.host}${globalThis.location.pathname.substring(0, globalThis.location.pathname.lastIndexOf("/"))}/oauth2-redirect.html` + } + + return options +} + +export default optionsFromRuntime diff --git a/src/core/index.js b/src/core/index.js index 1274d332487..bb4bb933386 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -34,6 +34,7 @@ import { optionsFromQuery, optionsFromURL, optionsFromSystem, + optionsFromRuntime, mergeOptions, inlinePluginOptionsFactorization, storeOptionsFactorization @@ -42,7 +43,8 @@ import { export default function SwaggerUI(userOptions) { const queryOptions = optionsFromQuery()(userOptions) - let mergedOptions = mergeOptions({}, defaultOptions, userOptions, queryOptions) + const runtimeOptions = optionsFromRuntime()() + let mergedOptions = mergeOptions({}, defaultOptions, runtimeOptions, userOptions, queryOptions) const storeOptions = storeOptionsFactorization(mergedOptions) const InlinePlugin = inlinePluginOptionsFactorization(mergedOptions) @@ -57,7 +59,7 @@ export default function SwaggerUI(userOptions) { .then((urlOptions) => { const urlOptionsFailedToFetch = urlOptions === null - mergedOptions = mergeOptions({}, defaultOptions, systemOptions, userOptions, urlOptions, queryOptions) + mergedOptions = mergeOptions({}, defaultOptions, runtimeOptions, systemOptions, userOptions, urlOptions, queryOptions) store.setConfigs(mergedOptions) system.configsActions.loaded()