Skip to content

Commit

Permalink
Allow for un-captured route of config.yml (decaporg#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
talves authored and brianlmacdonald committed May 23, 2018
1 parent d1bb7a1 commit dadfa0c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ function parseConfig(data) {
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) {
return {
type: CONFIG_SUCCESS,
Expand Down Expand Up @@ -115,19 +130,12 @@ export function loadConfig() {

try {
const preloadedConfig = getState().config;
const response = await fetch('config.yml', { credentials: 'same-origin' })
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() : '');
const loadedConfig = await getConfig('config.yml', preloadedConfig && preloadedConfig.size > 1);

/**
* 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);

dispatch(configDidLoad(config));
Expand Down

0 comments on commit dadfa0c

Please sign in to comment.