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.
This commit is contained in:
Iain Billett
2020-11-05 15:05:06 +00:00
committed by GitHub
parent 65d5e24476
commit 4aca74e089
3 changed files with 32 additions and 0 deletions
+7
View File
@@ -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.
@@ -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 },
]);
});
});
@@ -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;
};