Skip to content

Commit

Permalink
refactor: consolidate configuration code into config
Browse files Browse the repository at this point in the history
Refs #9806
  • Loading branch information
glowcloud committed Apr 12, 2024
1 parent 675d94d commit 5f977bf
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 140 deletions.
144 changes: 144 additions & 0 deletions src/core/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* @prettier
*/
import deepExtend from "deep-extend"

import ApisPreset from "core/presets/apis"
import { parseSearch } from "core/utils"

const defaultConfig = {
// eslint-disable-next-line camelcase
domNode: null,
spec: {},
url: "",
urls: null,
layout: "BaseLayout",
docExpansion: "list",
maxDisplayedTags: null,
filter: null,
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`,
persistAuthorization: false,
configs: {},
custom: {},
displayOperationId: false,
displayRequestDuration: false,
deepLinking: false,
tryItOutEnabled: false,
requestInterceptor: (a) => a,
responseInterceptor: (a) => a,
showMutatedRequest: true,
defaultModelRendering: "example",
defaultModelExpandDepth: 1,
defaultModelsExpandDepth: 1,
showExtensions: false,
showCommonExtensions: false,
withCredentials: undefined,
requestSnippetsEnabled: false,
requestSnippets: {
generators: {
// eslint-disable-next-line camelcase
curl_bash: {
title: "cURL (bash)",
syntax: "bash",
},
// eslint-disable-next-line camelcase
curl_powershell: {
title: "cURL (PowerShell)",
syntax: "powershell",
},
// eslint-disable-next-line camelcase
curl_cmd: {
title: "cURL (CMD)",
syntax: "bash",
},
},
defaultExpanded: true,
languages: null, // e.g. only show curl bash = ["curl_bash"]
},
supportedSubmitMethods: [
"get",
"put",
"post",
"delete",
"options",
"head",
"patch",
"trace",
],
queryConfigEnabled: false,

// Initial set of plugins ( TODO rename this, or refactor - we don't need presets _and_ plugins. Its just there for performance.
// Instead, we can compile the first plugin ( it can be a collection of plugins ), then batch the rest.
presets: [ApisPreset],

// Plugins; ( loaded after presets )
plugins: [],

pluginsOptions: {
// Behavior during plugin registration. Can be :
// - legacy (default) : the current behavior for backward compatibility – last plugin takes precedence over the others
// - chain : chain wrapComponents when targeting the same core component
pluginLoadType: "legacy",
},

// Initial state
initialState: {},

// Inline Plugin
fn: {},
components: {},

syntaxHighlight: {
activated: true,
theme: "agate",
},
}

export const getConfigs = (opts) => {
const queryConfig = opts.queryConfigEnabled ? parseSearch() : {}

const combinedConfig = deepExtend({}, defaultConfig, opts, queryConfig)

const storeConfig = {
system: {
configs: combinedConfig.configs,
},
plugins: combinedConfig.presets,
pluginsOptions: combinedConfig.pluginsOptions,
state: deepExtend(
{
layout: {
layout: combinedConfig.layout,
filter: combinedConfig.filter,
},
spec: {
spec: "",
// support Relative References
url: combinedConfig.url,
},
requestSnippets: combinedConfig.requestSnippets,
},
combinedConfig.initialState
),
}

if (combinedConfig.initialState) {
// if the user sets a key as `undefined`, that signals to us that we
// should delete the key entirely.
// known usage: Swagger-Editor validate plugin tests
for (var key in combinedConfig.initialState) {
if (
Object.prototype.hasOwnProperty.call(
combinedConfig.initialState,
key
) &&
combinedConfig.initialState[key] === undefined
) {
delete storeConfig.state[key]
}
}
}

return { queryConfig, combinedConfig, storeConfig }
}
154 changes: 14 additions & 140 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import DownloadUrlPlugin from "./plugins/download-url"
import SyntaxHighlightingPlugin from "core/plugins/syntax-highlighting"
import SafeRenderPlugin from "./plugins/safe-render"

import { parseSearch } from "./utils"
import { getConfigs } from "./config"
import win from "./window"

// eslint-disable-next-line no-undef
Expand All @@ -46,153 +46,27 @@ export default function SwaggerUI(opts) {
buildTimestamp: BUILD_TIME,
}

const defaults = {
// Some general settings, that we floated to the top
dom_id: null, // eslint-disable-line camelcase
domNode: null,
spec: {},
url: "",
urls: null,
layout: "BaseLayout",
docExpansion: "list",
maxDisplayedTags: null,
filter: null,
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`,
persistAuthorization: false,
configs: {},
custom: {},
displayOperationId: false,
displayRequestDuration: false,
deepLinking: false,
tryItOutEnabled: false,
requestInterceptor: (a => a),
responseInterceptor: (a => a),
showMutatedRequest: true,
defaultModelRendering: "example",
defaultModelExpandDepth: 1,
defaultModelsExpandDepth: 1,
showExtensions: false,
showCommonExtensions: false,
withCredentials: undefined,
requestSnippetsEnabled: false,
requestSnippets: {
generators: {
"curl_bash": {
title: "cURL (bash)",
syntax: "bash"
},
"curl_powershell": {
title: "cURL (PowerShell)",
syntax: "powershell"
},
"curl_cmd": {
title: "cURL (CMD)",
syntax: "bash"
},
},
defaultExpanded: true,
languages: null, // e.g. only show curl bash = ["curl_bash"]
},
supportedSubmitMethods: [
"get",
"put",
"post",
"delete",
"options",
"head",
"patch",
"trace"
],
queryConfigEnabled: false,

// Initial set of plugins ( TODO rename this, or refactor - we don't need presets _and_ plugins. Its just there for performance.
// Instead, we can compile the first plugin ( it can be a collection of plugins ), then batch the rest.
presets: [
ApisPreset
],

// Plugins; ( loaded after presets )
plugins: [
],

pluginsOptions: {
// Behavior during plugin registration. Can be :
// - legacy (default) : the current behavior for backward compatibility – last plugin takes precedence over the others
// - chain : chain wrapComponents when targeting the same core component
pluginLoadType: "legacy"
},

// Initial state
initialState: { },

// Inline Plugin
fn: { },
components: { },

syntaxHighlight: {
activated: true,
theme: "agate"
}
}

let queryConfig = opts.queryConfigEnabled ? parseSearch() : {}

const domNode = opts.domNode
delete opts.domNode

const constructorConfig = deepExtend({}, defaults, opts, queryConfig)

const storeConfigs = {
system: {
configs: constructorConfig.configs
},
plugins: constructorConfig.presets,
pluginsOptions: constructorConfig.pluginsOptions,
state: deepExtend({
layout: {
layout: constructorConfig.layout,
filter: constructorConfig.filter
},
spec: {
spec: "",
// support Relative References
url: constructorConfig.url,
},
requestSnippets: constructorConfig.requestSnippets
}, constructorConfig.initialState)
}

if(constructorConfig.initialState) {
// if the user sets a key as `undefined`, that signals to us that we
// should delete the key entirely.
// known usage: Swagger-Editor validate plugin tests
for (var key in constructorConfig.initialState) {
if(
Object.prototype.hasOwnProperty.call(constructorConfig.initialState, key)
&& constructorConfig.initialState[key] === undefined
) {
delete storeConfigs.state[key]
}
}
}
const { queryConfig, combinedConfig, storeConfig } = getConfigs(opts)

let inlinePlugin = ()=> {
const inlinePlugin = ()=> {
return {
fn: constructorConfig.fn,
components: constructorConfig.components,
state: constructorConfig.state,
fn: combinedConfig.fn,
components: combinedConfig.components,
state: combinedConfig.state,
}
}

var store = new System(storeConfigs)
store.register([constructorConfig.plugins, inlinePlugin])
const store = new System(storeConfig)
store.register([combinedConfig.plugins, inlinePlugin])

var system = store.getSystem()
const system = store.getSystem()

const downloadSpec = (fetchedConfig) => {
let localConfig = system.specSelectors.getLocalConfig ? system.specSelectors.getLocalConfig() : {}
let mergedConfig = deepExtend({}, localConfig, constructorConfig, fetchedConfig || {}, queryConfig)
const localConfig = system.specSelectors.getLocalConfig ? system.specSelectors.getLocalConfig() : {}
const mergedConfig = deepExtend({}, localConfig, combinedConfig, fetchedConfig || {}, queryConfig)

// deep extend mangles domNode, we need to set it manually
if(domNode) {
Expand Down Expand Up @@ -228,14 +102,14 @@ export default function SwaggerUI(opts) {
return system
}

const configUrl = queryConfig.config || constructorConfig.configUrl
const configUrl = combinedConfig.configUrl

if (configUrl && system.specActions && system.specActions.getConfigByUrl) {
system.specActions.getConfigByUrl({
url: configUrl,
loadRemoteConfig: true,
requestInterceptor: constructorConfig.requestInterceptor,
responseInterceptor: constructorConfig.responseInterceptor,
requestInterceptor: combinedConfig.requestInterceptor,
responseInterceptor: combinedConfig.responseInterceptor,
}, downloadSpec)
} else {
return downloadSpec()
Expand Down

0 comments on commit 5f977bf

Please sign in to comment.