UNPKG

ng-dynamic-mf

Version:

[![npm](https://img.shields.io/npm/v/ng-dynamic-mf?color=%2300d26a&style=for-the-badge)](https://www.npmjs.com/package/ng-dynamic-mf) [![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/LoaderB0T_ng-dynamic-mf?server=https%3A%2F%2Fsonarcloud

1 lines 5.95 kB
{"version":3,"file":"ng-dynamic-mf-environment.mjs","sources":["../../projects/ng-dynamic-mf/environment/src/environment.ts","../../projects/ng-dynamic-mf/environment/src/ng-dynamic-mf-environment.ts"],"sourcesContent":["export type Environment = {\r\n production: boolean;\r\n [key: string]: any;\r\n};\r\nlet environmentUpdateCallback: ((environment: Environment) => void) | null = null;\r\n\r\nlet win: Window | null = null;\r\ntype Win = typeof window & { __ng_dynamic_mf_env__: Environment };\r\n\r\nfunction getWindow() {\r\n return (win || window) as Win;\r\n}\r\n\r\nexport function setWindow(window: Window) {\r\n win = window;\r\n}\r\n\r\n/**\r\n * import this for all values of the environment.json file\r\n */\r\nexport const environment: Environment = { production: false };\r\n\r\nexport const envValidator = (defaultEnvironment: Environment) => {\r\n if (!environment) {\r\n console.error('Environment is not defined');\r\n }\r\n (Object.keys(defaultEnvironment) as (keyof typeof defaultEnvironment)[]).forEach(key => {\r\n if (!environment[key]) {\r\n console.error(`Environment is missing '${key}'`);\r\n }\r\n });\r\n};\r\n\r\nexport const ɵinitializeEnvironment = (\r\n env: Environment,\r\n disableParentEnvironmentReuse?: boolean\r\n) => {\r\n if (disableParentEnvironmentReuse || !ɵreuseEnvironment()) {\r\n getWindow().__ng_dynamic_mf_env__ = env;\r\n }\r\n Object.keys(env).forEach(key => {\r\n environment[key] = env[key];\r\n });\r\n environmentUpdateCallback?.(environment);\r\n};\r\n\r\nexport const ɵreuseEnvironment = () => {\r\n const existingEnv = getWindow().__ng_dynamic_mf_env__;\r\n if (!existingEnv) {\r\n return false;\r\n }\r\n Object.keys(existingEnv).forEach(key => {\r\n environment[key] = existingEnv[key];\r\n });\r\n environmentUpdateCallback?.(environment);\r\n return true;\r\n};\r\n\r\n/**\r\n * Use this to check if the ng-dynamic-mf environment is available.\r\n * Will be false if ng-dynamic-mf was not used to bootstrap the app and no environment was set manually\r\n */\r\nexport function hasEnvironment() {\r\n return !!getWindow().__ng_dynamic_mf_env__;\r\n}\r\n\r\n/**\r\n * Copies all environment variables into an iframe\r\n * Make sure to call this mehtod before the angular app in the iframe is bootstrapped\r\n * @param iframe the iframe to copy the environment into\r\n * @param environment optional environment to copy into the iframe. If not provided, the environment from the parent window is used\r\n * * @throws if the environment is not available. Check with `hasEnvironment()` before calling this method\r\n */\r\nexport function copyEnvironmentIntoIFrame(iframe: HTMLIFrameElement, environment?: Environment) {\r\n if (!hasEnvironment() && !environment) {\r\n throw new Error('ng-dynamic-mf environment is not available and no environment was provided');\r\n }\r\n const env = environment ?? getWindow().__ng_dynamic_mf_env__;\r\n if (env) {\r\n (iframe.contentWindow as Win).__ng_dynamic_mf_env__ = env;\r\n } else {\r\n console.error('Environment (__ng_dynamic_mf_env__) is not defined');\r\n }\r\n}\r\n\r\n/**\r\n * Use this to set a custom environment variable at runtime\r\n * @param key the key of the environment variable\r\n * @param value the value of the environment variable\r\n */\r\nexport const setEnvironmentValue = (key: string, value: unknown) => {\r\n environment[key] = value;\r\n getWindow().__ng_dynamic_mf_env__ ??= { production: false };\r\n getWindow().__ng_dynamic_mf_env__[key] = value;\r\n environmentUpdateCallback?.(environment);\r\n};\r\n\r\n/**\r\n * Registers a callback that is called whenever the environment is updated\r\n * @param callback the callback that is called whenever the environment is updated\r\n */\r\nexport const registerEnvironmentUpdateCallback = (callback: (environment: Environment) => void) => {\r\n environmentUpdateCallback = callback;\r\n};\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":"AAIA,IAAI,yBAAyB,GAAgD,IAAI;AAEjF,IAAI,GAAG,GAAkB,IAAI;AAG7B,SAAS,SAAS,GAAA;AAChB,IAAA,QAAQ,GAAG,IAAI,MAAM;A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