Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/configinlink #1146

Merged
merged 17 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: slims down code per review
  • Loading branch information
brianlmacdonald authored and tech4him1 committed Apr 10, 2018
commit adcc171c3d9b629ccfb89714b65dc2ec7d86e3e2
14 changes: 4 additions & 10 deletions src/actions/__tests__/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fromJS } from 'immutable';
import { applyDefaults, validateConfig, getConfigUrl } from '../config';
import { get } from 'lodash';
import { applyDefaults, validateConfig } from '../config';

describe('config', () => {
describe('applyDefaults', () => {
Expand Down Expand Up @@ -133,17 +134,10 @@ describe('config', () => {
it('should should return a default url if there is no <link> in <head>.', () => {
const testChild = document.getElementById('test');
document.head.removeChild(testChild);
expect(getConfig()).toEqual('config.yml');
expect(get(document.querySelector('head link[rel="cms-config-url"]'), 'href') || 'config.yml').toEqual('config.yml');
});
it('should return the <link> href if one is provided.', () => {
expect(getConfig()).toEqual('the/test/works');
});
it('should throw an error if an incorrect type is provided.', () => {
const testChild = document.getElementById('test');
testChild.setAttribute('type', 'failing/type');
expect(() => {
getConfig();
}).toThrowError(`The configuration type must be "text/yaml" or "application/x-yaml"`);
expect(get(document.querySelector('head link[rel="cms-config-url"]'), 'href') || 'config.yml').toEqual('the/test/works');
});
});
});
28 changes: 4 additions & 24 deletions src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,10 @@ export const CONFIG_SUCCESS = "CONFIG_SUCCESS";
export const CONFIG_FAILURE = "CONFIG_FAILURE";
export const CONFIG_MERGE = "CONFIG_MERGE";

const validTypes = [ "text/yaml", "application/x-yaml" ];
const configLink = document.querySelector('link[rel="cms-config-url"]');
const isValidType = link => link && validTypes.includes(link.type);
const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml';

export function getConfigUrl() {
let url = 'config.yml';
// set default as 'config.yml'
// look in DOM head for a cms config link.
document.head.childNodes.forEach((child) => {
if (child.rel === "cms-config-url") {
if (child.type !== "text/yaml" &&
child.type !== "application/x-yaml") {
// check that the type is allowed;
throw new Error(`The configuration type must be "text/yaml" or "application/x-yaml"`);
}
url = child.href;
}
// overwrite default if link is found
// keep default otherwise.
});
return url;
}
const configUrl = get(
document.querySelector('head link[rel="cms-config-url"]'),
'href'
) || 'config.yml';

const defaults = {
publish_mode: publishModes.SIMPLE,
Expand Down Expand Up @@ -154,7 +135,6 @@ export function loadConfig() {

try {
const preloadedConfig = getState().config;
const configUrl = getConfigUrl();
const loadedConfig = await getConfig(configUrl, preloadedConfig && preloadedConfig.size > 1);

/**
Expand Down