-
References:
DescriptionWe need a setting for merge strategy when adding/overwriting API defaults in
Requirements:
Propositioncocreated with @SimonMarx Global settingExample: module.exports = {
apiDefaults: {
_mergeStrategy: "extend" // Alowed: extend | replace. Default: replace
useCms: {
limit: 8,
includes: {
product: ["manufacturer"]
},
}
} props:
cons:
Per key setting/// API TBD pros:
cons:
What do you think? Let's discuss it :) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I think the overwhelming majority will use the I think because the defaultParams should be as minimal as possible anyways, most of the folks just want to add some fields on top. |
Beta Was this translation helpful? Give feedback.
-
First of all, how @niklaswolf already mentioned most of us only want to add fields on top. To reach a minimal apiDefaults config, themes must be able to provide apiDefaults (themes are the ones, who have requirements to the api, collect data and send data). But how we already discuss in Discord, this is another topic. tl;dr The case where you may want to remove apiDefaultsMaybe this are thoughts of me, which are not necessary at all, but could make a config file highly configurable while merging different layers of config files (project - theme - shopware-pwa - storefront) Lets take the CrossSelling Component as example. Case 1: Global Settings
Will maybe result in:
Questions for the global option:
Case 2: Per Key options Per key options are more complex in the config structure to write, but makes it possible to configure your theme/project defaults specific for your needs and let developers create configuration files where other developers directly understand what is going on while merging multiple default definitions (once they understood the configuration structure). An example could be: // Api Defaults (e.g. by theme or shopware-pwa)
const apiDefaults = {
useCms: {
limit: 10,
associations: {
crossSellings: {
associations: {
assignedProducts: {
associations: {
product: {
associations: {
media: {},
cover: {},
seoUrls: {}
}
}
}
}
}
},
manufacturer: {
associations: {
media: {}
}
},
},
includes: {
media: ["thumbnails", "width", "height", "url"]
}
}
}
// Your project configs
module.exports = {
// other configs
defaultMergeStrategy: 'extend', // Allowed: extend | replace Default: extend (Yes its breaking, but makes per-key configuration much more easier and less writing effort)
// Example 1 - prevent nesting of keys, instead using dot notation for key/field access
mergeStrategies: {
apiDefaults: {
useCms: {
associations: [
// exclude single keys
{
field: 'crossSelling.associations.assignedProducts.product.associations.media',
strategy: 'remove' // Allowed: remove | extend | replace
},
{
field: 'manufacturer.association',
strategy: 'replace'
}
],
includes: [
{field: 'media', strategy: 'extend'}
]
}
}
},
// Example 2 - same as Example 1, but with nested keys instead dot notation
mergeStrategies: {
apiDefaults: {
associations: {
crossSellings: {
associations: {
assignedProducts: {
associations: {
product: {
associations: {
media: 'remove',
}
}
}
}
}
},
manufacturer: {
associations: 'replace'
}
},
}
},
// projects api defaults
apiDefaults: {
useCms: {
associations: {
manufacturer: {
associations: {
someAssociation: {}
}
}
},
includes: {
media: ["cdn"]
}
}
}
}
// result
const finalApiDefaults = {
useCms: {
limit: 10,
associations: {
crossSellings: {
associations: {
assignedProducts: {
associations: {
product: {
associations: {
// removed the "media: {}" associations
cover: {},
seoUrls: {}
}
}
}
}
}
},
manufacturer: {
associations: {
someAssociation: {} // replaced the whole object, removed "media: {}" and placed "someAssociation: {}" in it.
}
},
},
includes: {
media: ["thumbnails", "width", "height", "url", "cdn"] // extend "cdn" to the media include
}
}
} Another advantage of per-key configuration is, that updates of theme / shopware-pwa are a lot easier to make compatible with your project, in case a theme e.g. adds a new associations / includes in their apiDefaults within the update. |
Beta Was this translation helpful? Give feedback.
-
Hey folks, I've come with another proposition to solve our case, I believe it's pretty clean and straightforward :) Api defaults builderYou could be able to use it in The base: import defaultsConfigBuilder from "@shopware-pwa/nuxt-module/api-defaults"
// or in CJS
const defaultsConfigBuilder =
require("@shopware-pwa/nuxt-module/api-defaults").default
console.info('defaultsConfigBuilder', defaultsConfigBuilder().get()); // will print default config Now we could have many abilities to edit the config, I'll leave the API with examples Adding new value// merge object (including arrays), in this case add one value to array
defaultsConfigBuilder().add("useCms.includes.cms_page_slot", "someCustomValue")
// result for defaultsConfigBuilder().get()["useCms"]["includes"]["cms_page_slot"]
[
'id',
'type',
'slot',
'blockId',
'config',
'data',
'backgroundMediaMode',
'backgroundMedia',
'someCustomValue',
] Removing value// merge object (including arrays), in this case add one value to array
defaultsConfigBuilder().remove("useCms.includes.cms_page_slot", "type")
// result for defaultsConfigBuilder().get()["useCms"]["includes"]["cms_page_slot"]
[
'id',
'slot',
'blockId',
'config',
'data',
'backgroundMediaMode',
'backgroundMedia',
] Replacing valuedefaultsConfigBuilder().replace("useCms.includes.cms_page_slot", ["id", "myValue"])
// another examples
defaultsConfigBuilder().replace("useCms", { limit: 3 }) // replace whole useCms setting
defaultsConfigBuilder().replace("useCms.limit", 5) Implementation details for some cases
Let me know what do you think! |
Beta Was this translation helpful? Give feedback.
Hey folks, I've come with another proposition to solve our case, I believe it's pretty clean and straightforward :)
Api defaults builder
You could be able to use it in
nuxt
config sile,shopware-pwa
config file or move logic to dedicated file.The base:
Now we could have many abilities to edit the config, I'll leave the API with examples
Adding new value
// merge object (including arrays), in this case add one value…