From 4aca74e089be29fc3b8495f1c8168daa092f3760 Mon Sep 17 00:00:00 2001 From: Iain Billett Date: Thu, 5 Nov 2020 15:05:06 +0000 Subject: [PATCH] Load config from window in default config loader (#3238) Support setting config through the window object. The config should be set in window.__APP_CONFIG__ which should be an object. --- .changeset/3194.md | 7 +++++++ .../core/src/api-wrappers/createApp.test.tsx | 18 ++++++++++++++++++ packages/core/src/api-wrappers/createApp.tsx | 7 +++++++ 3 files changed, 32 insertions(+) create mode 100644 .changeset/3194.md diff --git a/.changeset/3194.md b/.changeset/3194.md new file mode 100644 index 0000000000..2c15d27eed --- /dev/null +++ b/.changeset/3194.md @@ -0,0 +1,7 @@ +--- +'@backstage/core': patch +--- + +Extend default config loader to read config from the window object. + +Config will be read from `window.__APP_CONFIG__` which should be an object. diff --git a/packages/core/src/api-wrappers/createApp.test.tsx b/packages/core/src/api-wrappers/createApp.test.tsx index a1655b88dd..31f584b68c 100644 --- a/packages/core/src/api-wrappers/createApp.test.tsx +++ b/packages/core/src/api-wrappers/createApp.test.tsx @@ -18,10 +18,12 @@ import { defaultConfigLoader } from './createApp'; (process as any).env = { NODE_ENV: 'test' }; const anyEnv = process.env as any; +const anyWindow = window as any; describe('defaultConfigLoader', () => { afterEach(() => { delete anyEnv.APP_CONFIG; + delete anyWindow.__APP_CONFIG__; }); it('loads static config', async () => { @@ -73,4 +75,20 @@ describe('defaultConfigLoader', () => { 'Failed to load runtime configuration, SyntaxError: Unexpected token } in JSON at position 0', ); }); + + it('loads config from window.__APP_CONFIG__', async () => { + anyEnv.APP_CONFIG = [ + { data: { my: 'config' }, context: 'a' }, + { data: { my: 'override-config' }, context: 'b' }, + ]; + const windowConfig = { app: { configKey: 'config-value' } }; + anyWindow.__APP_CONFIG__ = windowConfig; + + const configs = await defaultConfigLoader(); + + expect(configs).toEqual([ + ...anyEnv.APP_CONFIG, + { context: 'window', data: windowConfig }, + ]); + }); }); diff --git a/packages/core/src/api-wrappers/createApp.tsx b/packages/core/src/api-wrappers/createApp.tsx index c7d0f5ce01..592a2417ca 100644 --- a/packages/core/src/api-wrappers/createApp.tsx +++ b/packages/core/src/api-wrappers/createApp.tsx @@ -67,6 +67,13 @@ export const defaultConfigLoader: AppConfigLoader = async ( } } + const windowAppConfig = (window as any).__APP_CONFIG__; + if (windowAppConfig) { + configs.push({ + context: 'window', + data: windowAppConfig, + }); + } return configs; };