[Docs]: Is it possible to create a custom build; with custom defaults?
ceesvanegmond opened this issue · comments
What is the improvement/update you wish to see?
Hi all,
I first have to say! What a great library! It's so diverse and usable. Thank you!
I do have one question. At the moment; we can use the run()
method. The run
method expects an config json and this also inits the consent.
My question; is it possible to set defaults before calling the run()
method? Maybe already in the build (custom)?
We create lots of website and for the cookiebar it's 80% the same except the type of cookies and colors; so I want to see if we can somehow create a build with defaults that match our config; and override the config that's specific to the client.
Is this possible?
Link the related docs page, if it exists.
No response
@ceesvanegmond you can do it, but you need to create your own wrapper and a proper merge function to merge the 2 configs. Here is a proof of concept using v3:
index.js
import * as CookieConsent from "vanilla-cookieconsent"
import defaultConfig from "./defaultConfig";
const mergeConfigs = (defaultConfig, userConfig) => {
const mergedConfig = { ...defaultConfig };
for (const key in userConfig) {
if (userConfig.hasOwnProperty(key)) {
if (typeof userConfig[key] === 'object' && userConfig[key] !== null && defaultConfig[key]) {
mergedConfig[key] = mergeConfigs(defaultConfig[key], userConfig[key]);
} else {
mergedConfig[key] = userConfig[key];
}
}
}
return mergedConfig;
}
// Export your custom run method
export const run = async (userConfig = {}) => {
await CookieConsent.run(mergeConfigs(defaultConfig, userConfig));
};
export const acceptService = CookieConsent.acceptCategory;
export const acceptedCategory = CookieConsent.acceptedCategory;
export const acceptedService = CookieConsent.acceptedService;
// TODO: export the other methods
@orestbida Thanks! Works!