ng-dynamic-mf
Version:
[](https://www.npmjs.com/package/ng-dynamic-mf) [ • 3.34 kB
JavaScript
let environmentUpdateCallback = null;
let win = null;
function getWindow() {
return (win || window);
}
function setWindow(window) {
win = window;
}
/**
* import this for all values of the environment.json file
*/
const environment = { production: false };
const envValidator = (defaultEnvironment) => {
if (!environment) {
console.error('Environment is not defined');
}
Object.keys(defaultEnvironment).forEach(key => {
if (!environment[key]) {
console.error(`Environment is missing '${key}'`);
}
});
};
const ɵinitializeEnvironment = (env, disableParentEnvironmentReuse) => {
if (disableParentEnvironmentReuse || !ɵreuseEnvironment()) {
getWindow().__ng_dynamic_mf_env__ = env;
}
Object.keys(env).forEach(key => {
environment[key] = env[key];
});
environmentUpdateCallback?.(environment);
};
const ɵreuseEnvironment = () => {
const existingEnv = getWindow().__ng_dynamic_mf_env__;
if (!existingEnv) {
return false;
}
Object.keys(existingEnv).forEach(key => {
environment[key] = existingEnv[key];
});
environmentUpdateCallback?.(environment);
return true;
};
/**
* Use this to check if the ng-dynamic-mf environment is available.
* Will be false if ng-dynamic-mf was not used to bootstrap the app and no environment was set manually
*/
function hasEnvironment() {
return !!getWindow().__ng_dynamic_mf_env__;
}
/**
* Copies all environment variables into an iframe
* Make sure to call this mehtod before the angular app in the iframe is bootstrapped
* @param iframe the iframe to copy the environment into
* @param environment optional environment to copy into the iframe. If not provided, the environment from the parent window is used
* * @throws if the environment is not available. Check with `hasEnvironment()` before calling this method
*/
function copyEnvironmentIntoIFrame(iframe, environment) {
if (!hasEnvironment() && !environment) {
throw new Error('ng-dynamic-mf environment is not available and no environment was provided');
}
const env = environment ?? getWindow().__ng_dynamic_mf_env__;
if (env) {
iframe.contentWindow.__ng_dynamic_mf_env__ = env;
}
else {
console.error('Environment (__ng_dynamic_mf_env__) is not defined');
}
}
/**
* Use this to set a custom environment variable at runtime
* @param key the key of the environment variable
* @param value the value of the environment variable
*/
const setEnvironmentValue = (key, value) => {
environment[key] = value;
getWindow().__ng_dynamic_mf_env__ ??= { production: false };
getWindow().__ng_dynamic_mf_env__[key] = value;
environmentUpdateCallback?.(environment);
};
/**
* Registers a callback that is called whenever the environment is updated
* @param callback the callback that is called whenever the environment is updated
*/
const registerEnvironmentUpdateCallback = (callback) => {
environmentUpdateCallback = callback;
};
/**
* Generated bundle index. Do not edit.
*/
export { copyEnvironmentIntoIFrame, envValidator, environment, hasEnvironment, registerEnvironmentUpdateCallback, setEnvironmentValue, setWindow, ɵinitializeEnvironment, ɵreuseEnvironment };
//# sourceMappingURL=ng-dynamic-mf-environment.mjs.map