2020-04-07 15:00:06 +03:00
|
|
|
import yaml from 'yaml';
|
2018-08-07 14:46:54 -06:00
|
|
|
import { Map, fromJS } from 'immutable';
|
2021-01-13 19:51:45 +02:00
|
|
|
import deepmerge from 'deepmerge';
|
|
|
|
import { trimStart, trim, get, isPlainObject, isEmpty } from 'lodash';
|
2021-02-07 18:11:30 +02:00
|
|
|
import { SIMPLE as SIMPLE_PUBLISH_MODE } from '../constants/publishModes';
|
|
|
|
import { validateConfig } from '../constants/configSchema';
|
2020-04-01 17:18:56 +03:00
|
|
|
import { selectDefaultSortableFields, traverseFields } from '../reducers/collections';
|
2021-01-19 06:59:00 -08:00
|
|
|
import { getIntegrations, selectIntegration } from '../reducers/integrations';
|
2021-02-07 18:11:30 +02:00
|
|
|
import { resolveBackend } from '../backend';
|
2020-11-26 02:23:53 -08:00
|
|
|
import { I18N, I18N_FIELD, I18N_STRUCTURE } from '../lib/i18n';
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
export const CONFIG_REQUEST = 'CONFIG_REQUEST';
|
|
|
|
export const CONFIG_SUCCESS = 'CONFIG_SUCCESS';
|
|
|
|
export const CONFIG_FAILURE = 'CONFIG_FAILURE';
|
2018-04-10 16:48:04 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function traverseFieldsJS(fields, updater) {
|
2021-02-07 18:11:30 +02:00
|
|
|
return fields.map(field => {
|
|
|
|
let newField = updater(field);
|
|
|
|
if (newField.fields) {
|
|
|
|
newField = { ...newField, fields: traverseFieldsJS(newField.fields, updater) };
|
|
|
|
} else if (newField.field) {
|
|
|
|
newField = { ...newField, field: traverseFieldsJS([newField.field], updater)[0] };
|
|
|
|
} else if (newField.types) {
|
|
|
|
newField = { ...newField, types: traverseFieldsJS(newField.types, updater) };
|
|
|
|
}
|
|
|
|
|
|
|
|
return newField;
|
|
|
|
});
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2021-02-07 18:11:30 +02:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function getConfigUrl() {
|
2018-04-10 16:48:04 -04:00
|
|
|
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml' };
|
|
|
|
const configLinkEl = document.querySelector('link[rel="cms-config-url"]');
|
|
|
|
const isValidLink = configLinkEl && validTypes[configLinkEl.type] && get(configLinkEl, 'href');
|
|
|
|
if (isValidLink) {
|
|
|
|
const link = get(configLinkEl, 'href');
|
|
|
|
console.log(`Using config file path: "${link}"`);
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
return 'config.yml';
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2018-04-10 16:48:04 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function setDefaultPublicFolder(map) {
|
2020-04-01 17:18:56 +03:00
|
|
|
if (map.has('media_folder') && !map.has('public_folder')) {
|
|
|
|
map = map.set('public_folder', map.get('media_folder'));
|
|
|
|
}
|
|
|
|
return map;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-04-01 17:18:56 +03:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
// Mapping between existing camelCase and its snake_case counterpart
|
|
|
|
const WIDGET_KEY_MAP = {
|
|
|
|
dateFormat: 'date_format',
|
|
|
|
timeFormat: 'time_format',
|
|
|
|
pickerUtc: 'picker_utc',
|
|
|
|
editorComponents: 'editor_components',
|
|
|
|
valueType: 'value_type',
|
|
|
|
valueField: 'value_field',
|
|
|
|
searchFields: 'search_fields',
|
|
|
|
displayFields: 'display_fields',
|
|
|
|
optionsLength: 'options_length',
|
|
|
|
};
|
2020-08-31 19:25:48 +08:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function setSnakeCaseConfig(field) {
|
2021-02-07 18:11:30 +02:00
|
|
|
const deprecatedKeys = Object.keys(WIDGET_KEY_MAP).filter(camel => camel in field);
|
|
|
|
const snakeValues = deprecatedKeys.map(camel => {
|
|
|
|
const snake = WIDGET_KEY_MAP[camel];
|
|
|
|
console.warn(
|
|
|
|
`Field ${field.name} is using a deprecated configuration '${camel}'. Please use '${snake}'`,
|
|
|
|
);
|
|
|
|
return { [snake]: field[camel] };
|
2020-08-31 19:25:48 +08:00
|
|
|
});
|
2021-02-07 18:11:30 +02:00
|
|
|
|
|
|
|
return Object.assign({}, field, ...snakeValues);
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-08-31 19:25:48 +08:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function setI18nField(field) {
|
2020-09-20 10:30:46 -07:00
|
|
|
if (field.get(I18N) === true) {
|
|
|
|
field = field.set(I18N, I18N_FIELD.TRANSLATE);
|
|
|
|
} else if (field.get(I18N) === false || !field.has(I18N)) {
|
|
|
|
field = field.set(I18N, I18N_FIELD.NONE);
|
|
|
|
}
|
|
|
|
return field;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function setI18nDefaults(defaultI18n, collectionOrFile) {
|
2020-11-26 02:23:53 -08:00
|
|
|
if (defaultI18n && collectionOrFile.has(I18N)) {
|
|
|
|
const collectionOrFileI18n = collectionOrFile.get(I18N);
|
|
|
|
if (collectionOrFileI18n === true) {
|
|
|
|
collectionOrFile = collectionOrFile.set(I18N, defaultI18n);
|
|
|
|
} else if (collectionOrFileI18n === false) {
|
|
|
|
collectionOrFile = collectionOrFile.delete(I18N);
|
2020-09-20 10:30:46 -07:00
|
|
|
} else {
|
2020-11-26 02:23:53 -08:00
|
|
|
const locales = collectionOrFileI18n.get('locales', defaultI18n.get('locales'));
|
|
|
|
const defaultLocale = collectionOrFileI18n.get(
|
2020-09-20 10:30:46 -07:00
|
|
|
'default_locale',
|
2020-11-26 02:23:53 -08:00
|
|
|
collectionOrFileI18n.has('locales') ? locales.first() : defaultI18n.get('default_locale'),
|
2020-09-20 10:30:46 -07:00
|
|
|
);
|
2020-11-26 02:23:53 -08:00
|
|
|
collectionOrFile = collectionOrFile.set(I18N, defaultI18n.merge(collectionOrFileI18n));
|
|
|
|
collectionOrFile = collectionOrFile.setIn([I18N, 'locales'], locales);
|
|
|
|
collectionOrFile = collectionOrFile.setIn([I18N, 'default_locale'], defaultLocale);
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2020-11-26 02:23:53 -08:00
|
|
|
throwOnMissingDefaultLocale(collectionOrFile.get(I18N));
|
2020-09-20 10:30:46 -07:00
|
|
|
}
|
|
|
|
|
2020-11-26 02:23:53 -08:00
|
|
|
if (collectionOrFileI18n !== false) {
|
2020-09-20 10:30:46 -07:00
|
|
|
// set default values for i18n fields
|
2020-11-26 02:23:53 -08:00
|
|
|
if (collectionOrFile.has('fields')) {
|
|
|
|
collectionOrFile = collectionOrFile.set(
|
|
|
|
'fields',
|
|
|
|
traverseFields(collectionOrFile.get('fields'), setI18nField),
|
|
|
|
);
|
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-26 02:23:53 -08:00
|
|
|
collectionOrFile = collectionOrFile.delete(I18N);
|
|
|
|
if (collectionOrFile.has('fields')) {
|
|
|
|
collectionOrFile = collectionOrFile.set(
|
|
|
|
'fields',
|
|
|
|
traverseFields(collectionOrFile.get('fields'), field => field.delete(I18N)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return collectionOrFile;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-11-26 02:23:53 -08:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function throwOnInvalidFileCollectionStructure(i18n) {
|
2020-11-26 02:23:53 -08:00
|
|
|
if (i18n && i18n.get('structure') !== I18N_STRUCTURE.SINGLE_FILE) {
|
|
|
|
throw new Error(
|
|
|
|
`i18n configuration for files collections is limited to ${I18N_STRUCTURE.SINGLE_FILE} structure`,
|
2020-09-20 10:30:46 -07:00
|
|
|
);
|
|
|
|
}
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function throwOnMissingDefaultLocale(i18n) {
|
2020-09-20 10:30:46 -07:00
|
|
|
if (i18n && !i18n.get('locales').includes(i18n.get('default_locale'))) {
|
|
|
|
throw new Error(
|
|
|
|
`i18n locales '${i18n.get('locales').join(', ')}' are missing the default locale ${i18n.get(
|
|
|
|
'default_locale',
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
}
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function setViewPatternsDefaults(key, collection) {
|
2020-11-08 17:33:09 +01:00
|
|
|
if (!collection.has(key)) {
|
|
|
|
collection = collection.set(key, fromJS([]));
|
|
|
|
} else {
|
|
|
|
collection = collection.set(
|
|
|
|
key,
|
|
|
|
collection.get(key).map(v => v.set('id', `${v.get('field')}__${v.get('pattern')}`)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-11-08 17:33:09 +01:00
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
const defaults = {
|
2021-02-07 18:11:30 +02:00
|
|
|
publish_mode: SIMPLE_PUBLISH_MODE,
|
2016-11-11 12:17:01 +01:00
|
|
|
};
|
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function hasIntegration(config, collection) {
|
2021-01-19 06:59:00 -08:00
|
|
|
const integrations = getIntegrations(config);
|
|
|
|
const integration = selectIntegration(integrations, collection.get('name'), 'listEntries');
|
|
|
|
return !!integration;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2021-01-19 06:59:00 -08:00
|
|
|
|
2020-08-31 19:25:48 +08:00
|
|
|
export function normalizeConfig(config) {
|
2021-02-07 18:11:30 +02:00
|
|
|
const { collections = [] } = config;
|
|
|
|
|
|
|
|
const normalizedCollections = collections.map(collection => {
|
|
|
|
const { fields, files } = collection;
|
|
|
|
|
|
|
|
let normalizedCollection = collection;
|
|
|
|
if (fields) {
|
|
|
|
const normalizedFields = traverseFieldsJS(fields, setSnakeCaseConfig);
|
|
|
|
normalizedCollection = { ...normalizedCollection, fields: normalizedFields };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files) {
|
|
|
|
const normalizedFiles = files.map(file => {
|
|
|
|
const normalizedFileFields = traverseFieldsJS(file.fields, setSnakeCaseConfig);
|
|
|
|
return { ...file, fields: normalizedFileFields };
|
|
|
|
});
|
|
|
|
normalizedCollection = { ...normalizedCollection, files: normalizedFiles };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (normalizedCollection.sortableFields) {
|
|
|
|
const { sortableFields, ...rest } = normalizedCollection;
|
|
|
|
normalizedCollection = { ...rest, sortable_fields: sortableFields };
|
|
|
|
|
|
|
|
console.warn(
|
|
|
|
`Collection ${collection.name} is using a deprecated configuration 'sortableFields'. Please use 'sortable_fields'`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizedCollection;
|
2020-08-31 19:25:48 +08:00
|
|
|
});
|
2021-02-07 18:11:30 +02:00
|
|
|
|
|
|
|
return { ...config, collections: normalizedCollections };
|
2020-08-31 19:25:48 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
export function applyDefaults(config) {
|
2018-02-28 15:45:16 -05:00
|
|
|
return Map(defaults)
|
|
|
|
.mergeDeep(config)
|
|
|
|
.withMutations(map => {
|
2019-03-01 09:45:23 -05:00
|
|
|
// Use `site_url` as default `display_url`.
|
2019-02-08 12:26:59 -05:00
|
|
|
if (!map.get('display_url') && map.get('site_url')) {
|
|
|
|
map.set('display_url', map.get('site_url'));
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
|
|
|
// Use media_folder as default public_folder.
|
2018-02-28 15:45:16 -05:00
|
|
|
const defaultPublicFolder = `/${trimStart(map.get('media_folder'), '/')}`;
|
2020-04-01 13:16:30 +03:00
|
|
|
if (!map.has('public_folder')) {
|
2018-02-28 15:45:16 -05:00
|
|
|
map.set('public_folder', defaultPublicFolder);
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
// default values for the slug config
|
|
|
|
if (!map.getIn(['slug', 'encoding'])) {
|
|
|
|
map.setIn(['slug', 'encoding'], 'unicode');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map.getIn(['slug', 'clean_accents'])) {
|
|
|
|
map.setIn(['slug', 'clean_accents'], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map.getIn(['slug', 'sanitize_replacement'])) {
|
|
|
|
map.setIn(['slug', 'sanitize_replacement'], '-');
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:30:46 -07:00
|
|
|
let i18n = config.get(I18N);
|
|
|
|
i18n = i18n?.set('default_locale', i18n.get('default_locale', i18n.get('locales').first()));
|
|
|
|
throwOnMissingDefaultLocale(i18n);
|
|
|
|
|
2019-03-01 09:45:23 -05:00
|
|
|
// Strip leading slash from collection folders and files
|
|
|
|
map.set(
|
|
|
|
'collections',
|
|
|
|
map.get('collections').map(collection => {
|
2020-03-23 12:01:37 +02:00
|
|
|
if (!collection.has('publish')) {
|
|
|
|
collection = collection.set('publish', true);
|
|
|
|
}
|
|
|
|
|
2020-11-26 02:23:53 -08:00
|
|
|
collection = setI18nDefaults(i18n, collection);
|
|
|
|
|
2019-03-01 09:45:23 -05:00
|
|
|
const folder = collection.get('folder');
|
|
|
|
if (folder) {
|
2019-12-18 18:16:02 +02:00
|
|
|
if (collection.has('path') && !collection.has('media_folder')) {
|
|
|
|
// default value for media folder when using the path config
|
|
|
|
collection = collection.set('media_folder', '');
|
|
|
|
}
|
2020-04-01 17:18:56 +03:00
|
|
|
collection = setDefaultPublicFolder(collection);
|
|
|
|
collection = collection.set(
|
|
|
|
'fields',
|
|
|
|
traverseFields(collection.get('fields'), setDefaultPublicFolder),
|
|
|
|
);
|
2020-06-18 10:11:37 +03:00
|
|
|
collection = collection.set('folder', trim(folder, '/'));
|
|
|
|
if (collection.has('meta')) {
|
|
|
|
const fields = collection.get('fields');
|
|
|
|
const metaFields = [];
|
|
|
|
collection.get('meta').forEach((value, key) => {
|
|
|
|
const field = value.withMutations(map => {
|
|
|
|
map.set('name', key);
|
|
|
|
map.set('meta', true);
|
|
|
|
map.set('required', true);
|
|
|
|
});
|
|
|
|
metaFields.push(field);
|
|
|
|
});
|
|
|
|
collection = collection.set('fields', fromJS([]).concat(metaFields, fields));
|
|
|
|
} else {
|
|
|
|
collection = collection.set('meta', Map());
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const files = collection.get('files');
|
|
|
|
if (files) {
|
2020-11-26 02:23:53 -08:00
|
|
|
const collectionI18n = collection.get(I18N);
|
|
|
|
throwOnInvalidFileCollectionStructure(collectionI18n);
|
|
|
|
|
2020-06-18 10:11:37 +03:00
|
|
|
collection = collection.delete('nested');
|
|
|
|
collection = collection.delete('meta');
|
2020-04-01 06:13:27 +03:00
|
|
|
collection = collection.set(
|
2019-03-01 09:45:23 -05:00
|
|
|
'files',
|
|
|
|
files.map(file => {
|
2020-04-01 17:18:56 +03:00
|
|
|
file = file.set('file', trimStart(file.get('file'), '/'));
|
|
|
|
file = setDefaultPublicFolder(file);
|
|
|
|
file = file.set(
|
|
|
|
'fields',
|
|
|
|
traverseFields(file.get('fields'), setDefaultPublicFolder),
|
|
|
|
);
|
2020-11-26 02:23:53 -08:00
|
|
|
file = setI18nDefaults(collectionI18n, file);
|
|
|
|
throwOnInvalidFileCollectionStructure(file.get(I18N));
|
2020-04-01 17:18:56 +03:00
|
|
|
return file;
|
2019-03-01 09:45:23 -05:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
|
2020-08-31 19:25:48 +08:00
|
|
|
if (!collection.has('sortable_fields')) {
|
2020-04-01 14:05:02 +03:00
|
|
|
const backend = resolveBackend(config);
|
2021-01-19 06:59:00 -08:00
|
|
|
const defaultSortable = selectDefaultSortableFields(
|
|
|
|
collection,
|
|
|
|
backend,
|
|
|
|
hasIntegration(map, collection),
|
|
|
|
);
|
2020-08-31 19:25:48 +08:00
|
|
|
collection = collection.set('sortable_fields', fromJS(defaultSortable));
|
2020-04-01 06:13:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:33:09 +01:00
|
|
|
collection = setViewPatternsDefaults('view_filters', collection);
|
|
|
|
collection = setViewPatternsDefaults('view_groups', collection);
|
2020-05-24 17:37:08 +00:00
|
|
|
|
2020-10-12 11:59:03 +02:00
|
|
|
if (map.hasIn(['editor', 'preview']) && !collection.has('editor')) {
|
|
|
|
collection = collection.setIn(['editor', 'preview'], map.getIn(['editor', 'preview']));
|
|
|
|
}
|
|
|
|
|
2020-04-01 06:13:27 +03:00
|
|
|
return collection;
|
2019-03-01 09:45:23 -05:00
|
|
|
}),
|
|
|
|
);
|
2018-02-28 15:45:16 -05:00
|
|
|
});
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:40:44 +03:00
|
|
|
export function parseConfig(data) {
|
2020-04-10 22:50:05 +01:00
|
|
|
const config = yaml.parse(data, { maxAliasCount: -1, prettyErrors: true, merge: true });
|
2018-08-07 14:46:54 -06:00
|
|
|
if (typeof CMS_ENV === 'string' && config[CMS_ENV]) {
|
|
|
|
Object.keys(config[CMS_ENV]).forEach(key => {
|
2017-04-14 22:36:41 +01:00
|
|
|
config[key] = config[CMS_ENV][key];
|
|
|
|
});
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2021-01-13 19:51:45 +02:00
|
|
|
async function getConfigYaml(file, hasManualConfig) {
|
2018-10-09 13:53:12 -04:00
|
|
|
const response = await fetch(file, { credentials: 'same-origin' }).catch(err => err);
|
|
|
|
if (response instanceof Error || response.status !== 200) {
|
2021-01-13 19:51:45 +02:00
|
|
|
if (hasManualConfig) return parseConfig('');
|
2018-10-09 13:53:12 -04:00
|
|
|
throw new Error(`Failed to load config.yml (${response.status || response})`);
|
2018-03-28 13:25:46 -07:00
|
|
|
}
|
|
|
|
const contentType = response.headers.get('Content-Type') || 'Not-Found';
|
|
|
|
const isYaml = contentType.indexOf('yaml') !== -1;
|
|
|
|
if (!isYaml) {
|
2018-08-07 14:46:54 -06:00
|
|
|
console.log(`Response for ${file} was not yaml. (Content-Type: ${contentType})`);
|
2021-01-13 19:51:45 +02:00
|
|
|
if (hasManualConfig) return parseConfig('');
|
2018-03-28 13:25:46 -07:00
|
|
|
}
|
|
|
|
return parseConfig(await response.text());
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
export function configLoaded(config) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_SUCCESS,
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: config,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configLoading() {
|
|
|
|
return {
|
2016-11-11 12:17:01 +01:00
|
|
|
type: CONFIG_REQUEST,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configFailed(err) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_FAILURE,
|
2018-08-07 14:46:54 -06:00
|
|
|
error: 'Error loading config',
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: err,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:56:11 +02:00
|
|
|
export async function detectProxyServer(localBackend) {
|
2020-05-26 11:50:09 +03:00
|
|
|
const allowedHosts = ['localhost', '127.0.0.1', ...(localBackend?.allowed_hosts || [])];
|
|
|
|
if (allowedHosts.includes(location.hostname)) {
|
2020-02-05 17:56:11 +02:00
|
|
|
let proxyUrl;
|
2020-05-26 11:50:09 +03:00
|
|
|
const defaultUrl = 'http://localhost:8081/api/v1';
|
2020-02-05 17:56:11 +02:00
|
|
|
if (localBackend === true) {
|
2020-05-26 11:50:09 +03:00
|
|
|
proxyUrl = defaultUrl;
|
2020-02-05 17:56:11 +02:00
|
|
|
} else if (isPlainObject(localBackend)) {
|
2020-05-26 11:50:09 +03:00
|
|
|
proxyUrl = localBackend.url || defaultUrl.replace('localhost', location.hostname);
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
console.log(`Looking for Netlify CMS Proxy Server at '${proxyUrl}'`);
|
2020-02-10 18:07:52 +02:00
|
|
|
const { repo, publish_modes, type } = await fetch(`${proxyUrl}`, {
|
2020-02-05 17:56:11 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify({ action: 'info' }),
|
|
|
|
}).then(res => res.json());
|
2020-02-10 18:07:52 +02:00
|
|
|
if (typeof repo === 'string' && Array.isArray(publish_modes) && typeof type === 'string') {
|
2020-02-05 17:56:11 +02:00
|
|
|
console.log(`Detected Netlify CMS Proxy Server at '${proxyUrl}' with repo: '${repo}'`);
|
2020-02-10 18:07:52 +02:00
|
|
|
return { proxyUrl, publish_modes, type };
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
console.log(`Netlify CMS Proxy Server not detected at '${proxyUrl}'`);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 18:07:52 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function getPublishMode(config, publishModes, backendType) {
|
2021-02-07 18:11:30 +02:00
|
|
|
if (config.publish_mode && publishModes && !publishModes.includes(config.publish_mode)) {
|
|
|
|
const newPublishMode = publishModes[0];
|
|
|
|
console.log(
|
|
|
|
`'${config.publish_mode}' is not supported by '${backendType}' backend, switching to '${newPublishMode}'`,
|
|
|
|
);
|
|
|
|
return newPublishMode;
|
2021-01-13 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
return config.publish_mode;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2021-01-13 19:51:45 +02:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
export async function handleLocalBackend(config) {
|
2021-02-07 18:11:30 +02:00
|
|
|
if (!config.local_backend) {
|
|
|
|
return config;
|
2021-01-13 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
const { proxyUrl, publish_modes: publishModes, type: backendType } = await detectProxyServer(
|
|
|
|
config.local_backend,
|
|
|
|
);
|
2021-01-13 19:51:45 +02:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
if (!proxyUrl) {
|
|
|
|
return config;
|
2020-02-10 18:07:52 +02:00
|
|
|
}
|
2021-02-07 18:11:30 +02:00
|
|
|
|
|
|
|
const publishMode = getPublishMode(config, publishModes, backendType);
|
|
|
|
return {
|
|
|
|
...config,
|
|
|
|
...(publishMode && { publish_mode: publishMode }),
|
|
|
|
backend: { ...config.backend, name: 'proxy', proxy_url: proxyUrl },
|
|
|
|
};
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2021-01-18 11:37:12 +02:00
|
|
|
export function loadConfig(manualConfig = {}, onLoad) {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (window.CMS_CONFIG) {
|
2021-01-13 19:51:45 +02:00
|
|
|
return configLoaded(fromJS(window.CMS_CONFIG));
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2021-01-13 19:51:45 +02:00
|
|
|
return async dispatch => {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configLoading());
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
try {
|
2018-04-10 16:48:04 -04:00
|
|
|
const configUrl = getConfigUrl();
|
2021-01-13 19:51:45 +02:00
|
|
|
const hasManualConfig = !isEmpty(manualConfig);
|
|
|
|
const configYaml =
|
|
|
|
manualConfig.load_config_file === false
|
2019-02-03 14:48:40 -08:00
|
|
|
? {}
|
2021-01-13 19:51:45 +02:00
|
|
|
: await getConfigYaml(configUrl, hasManualConfig);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2021-01-13 19:51:45 +02:00
|
|
|
// Merge manual config into the config.yml one
|
2021-02-07 18:11:30 +02:00
|
|
|
const mergedConfig = deepmerge(configYaml, manualConfig);
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2021-01-13 19:51:45 +02:00
|
|
|
validateConfig(mergedConfig);
|
2018-08-07 10:27:15 -06:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
const withLocalBackend = await handleLocalBackend(mergedConfig);
|
|
|
|
const normalizedConfig = normalizeConfig(withLocalBackend);
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
const config = applyDefaults(fromJS(normalizedConfig));
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2021-01-13 19:51:45 +02:00
|
|
|
dispatch(configLoaded(config));
|
2021-01-18 11:37:12 +02:00
|
|
|
|
|
|
|
if (typeof onLoad === 'function') {
|
|
|
|
onLoad();
|
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
} catch (err) {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configFailed(err));
|
2018-08-07 14:46:54 -06:00
|
|
|
throw err;
|
2018-02-28 15:45:16 -05:00
|
|
|
}
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|