Allow for un-captured route of config.yml (#1182)

This commit is contained in:
Tony Alves 2018-03-28 13:25:46 -07:00 committed by Shawn Erquhart
parent b2051343cc
commit d5cd79f2c4

View File

@ -75,6 +75,21 @@ function parseConfig(data) {
return config; return config;
} }
async function getConfig(file, isPreloaded) {
const response = await fetch(file, { credentials: 'same-origin' });
if (response.status !== 200) {
if (isPreloaded) return parseConfig('');
throw new Error(`Failed to load config.yml (${ response.status })`);
}
const contentType = response.headers.get('Content-Type') || 'Not-Found';
const isYaml = contentType.indexOf('yaml') !== -1;
if (!isYaml) {
console.log(`Response for ${ file } was not yaml. (Content-Type: ${ contentType })`);
if (isPreloaded) return parseConfig('');
}
return parseConfig(await response.text());
}
export function configLoaded(config) { export function configLoaded(config) {
return { return {
type: CONFIG_SUCCESS, type: CONFIG_SUCCESS,
@ -115,19 +130,12 @@ export function loadConfig() {
try { try {
const preloadedConfig = getState().config; const preloadedConfig = getState().config;
const response = await fetch('config.yml', { credentials: 'same-origin' }) const loadedConfig = await getConfig('config.yml', preloadedConfig && preloadedConfig.size > 1);
const requestSuccess = response.status === 200;
if (!preloadedConfig && !requestSuccess) {
throw new Error(`Failed to load config.yml (${ response.status })`);
}
const loadedConfig = parseConfig(requestSuccess ? await response.text() : '');
/** /**
* Merge any existing configuration so the result can be validated. * Merge any existing configuration so the result can be validated.
*/ */
const mergedConfig = mergePreloadedConfig(preloadedConfig, loadedConfig) const mergedConfig = mergePreloadedConfig(preloadedConfig, loadedConfig);
const config = flow(validateConfig, applyDefaults)(mergedConfig); const config = flow(validateConfig, applyDefaults)(mergedConfig);
dispatch(configDidLoad(config)); dispatch(configDidLoad(config));