Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(system): allow wrapping components both from presets and plugins #9919

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/customization/plugin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ const MyWrapComponentPlugin = function(system) {
}
```

**Note:**

If you have multiple plugins wrapping the same component, you may want to change the [`pluginsOptions.pluginLoadType`](/docs/usage/configuration.md#Plugins-options) parameter to `chain`.

#### `rootInjects`

The `rootInjects` interface allows you to inject values at the top level of the system.
Expand Down
7 changes: 0 additions & 7 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ Read more about the plugin system in the [Customization documentation](/docs/cus
Parameter name | Docker variable | Description
--- | --- | -----
<a name="layout"></a>`layout` | _Unavailable_ | `String="BaseLayout"`. The name of a component available via the plugin system to use as the top-level layout for Swagger UI.
<a name="pluginsOptions"></a>`pluginsOptions` | _Unavailable_ | `Object`. A Javascript object to configure plugin integration and behaviors (see below).
<a name="plugins"></a>`plugins` | _Unavailable_ | `Array=[]`. An array of plugin functions to use in Swagger UI.
<a name="presets"></a>`presets` | _Unavailable_ | `Array=[SwaggerUI.presets.ApisPreset]`. An array of presets to use in Swagger UI. Usually, you'll want to include `ApisPreset` if you use this option.

##### Plugins options

Parameter name | Docker variable | Description
--- | --- | -----
<a name="pluginLoadType"></a>`pluginLoadType` | _Unavailable_ | `String=["legacy", "chain"]`. Control behavior of plugins when targeting the same component with wrapComponent.<br/>- `legacy` (default) : last plugin takes precedence over the others<br/>- `chain` : chain wrapComponents when targeting the same core component, allowing multiple plugins to wrap the same component

##### Display

<table role="table">
Expand Down
7 changes: 0 additions & 7 deletions src/core/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ const defaultOptions = Object.freeze({
// 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",
},

initialState: {},

// Inline Plugin
Expand Down
1 change: 0 additions & 1 deletion src/core/config/factorization/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const systemFactorization = (options) => {
configs: options.configs,
},
plugins: options.presets,
pluginsOptions: options.pluginsOptions,
state,
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/core/config/type-cast/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ const mappings = {
typeCaster: arrayTypeCaster,
defaultValue: defaultOptions.plugins,
},
pluginsOptions: {
typeCaster: objectTypeCaster,
pluginsOptions: defaultOptions.pluginsOptions,
},
"pluginsOptions.pluginsLoadType": { typeCaster: stringTypeCaster },
presets: {
typeCaster: arrayTypeCaster,
defaultValue: defaultOptions.presets,
Expand Down
13 changes: 5 additions & 8 deletions src/core/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export default class Store {
deepExtend(this, {
state: {},
plugins: [],
pluginsOptions: {},
system: {
configs: {},
fn: {},
Expand Down Expand Up @@ -64,7 +63,7 @@ export default class Store {
}

register(plugins, rebuild=true) {
var pluginSystem = combinePlugins(plugins, this.getSystem(), this.pluginsOptions)
var pluginSystem = combinePlugins(plugins, this.getSystem())
systemExtend(this.system, pluginSystem)
if(rebuild) {
this.buildSystem()
Expand Down Expand Up @@ -311,21 +310,19 @@ export default class Store {

}

function combinePlugins(plugins, toolbox, pluginOptions) {
function combinePlugins(plugins, toolbox) {
if(isObject(plugins) && !isArray(plugins)) {
return merge({}, plugins)
}

if(isFunc(plugins)) {
return combinePlugins(plugins(toolbox), toolbox, pluginOptions)
return combinePlugins(plugins(toolbox), toolbox)
}

if(isArray(plugins)) {
const dest = pluginOptions.pluginLoadType === "chain" ? toolbox.getComponents() : {}

return plugins
.map(plugin => combinePlugins(plugin, toolbox, pluginOptions))
.reduce(systemExtend, dest)
.map(plugin => combinePlugins(plugin, toolbox))
.reduce(systemExtend, { components: { ...toolbox.getComponents() } })
}

return {}
Expand Down
5 changes: 1 addition & 4 deletions test/unit/core/system/wrapComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,11 @@ describe("wrapComponents", () => {
expect(children.eq(1).text()).toEqual("WOW much data")
})

it("should wrap correctly when registering multiple plugins targeting the same component", function () {
it("should wrap component correctly when performing subsequent plugin registering targeting the same component", function () {

// Given

const mySystem = new System({
pluginsOptions: {
pluginLoadType: "chain"
},
plugins: [
() => {
return {
Expand Down