2020-04-07 15:00:06 +03:00
|
|
|
import yaml from 'yaml';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { fromJS } from 'immutable';
|
2021-01-13 19:51:45 +02:00
|
|
|
import deepmerge from 'deepmerge';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { produce } from 'immer';
|
2021-01-13 19:51:45 +02:00
|
|
|
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';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { selectDefaultSortableFields } 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';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { FILES, FOLDER } from '../constants/collectionTypes';
|
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-10 17:52:02 +02:00
|
|
|
function setDefaultPublicFolderForField(field) {
|
|
|
|
if ('media_folder' in field && !field.public_folder) {
|
|
|
|
return { ...field, public_folder: field.media_folder };
|
2020-04-01 17:18:56 +03:00
|
|
|
}
|
2021-02-10 17:52:02 +02:00
|
|
|
return field;
|
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) {
|
2021-02-10 17:52:02 +02:00
|
|
|
if (field[I18N] === true) {
|
|
|
|
return { ...field, [I18N]: I18N_FIELD.TRANSLATE };
|
|
|
|
} else if (field[I18N] === false || !field[I18N]) {
|
|
|
|
return { ...field, [I18N]: I18N_FIELD.NONE };
|
2020-09-20 10:30:46 -07:00
|
|
|
}
|
|
|
|
return field;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
function getI18nDefaults(collectionOrFileI18n, defaultI18n) {
|
|
|
|
if (typeof collectionOrFileI18n === 'boolean') {
|
|
|
|
return defaultI18n;
|
|
|
|
} else {
|
|
|
|
const locales = collectionOrFileI18n.locales || defaultI18n.locales;
|
|
|
|
const defaultLocale = collectionOrFileI18n.default_locale || locales[0];
|
|
|
|
const mergedI18n = deepmerge(defaultI18n, collectionOrFileI18n);
|
|
|
|
mergedI18n.locales = locales;
|
|
|
|
mergedI18n.default_locale = defaultLocale;
|
|
|
|
throwOnMissingDefaultLocale(mergedI18n);
|
|
|
|
return mergedI18n;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
function setI18nDefaultsForFields(collectionOrFileFields, hasI18n) {
|
|
|
|
if (hasI18n) {
|
|
|
|
return traverseFieldsJS(collectionOrFileFields, setI18nField);
|
2020-09-20 10:30:46 -07:00
|
|
|
} else {
|
2021-02-10 17:52:02 +02:00
|
|
|
return traverseFieldsJS(collectionOrFileFields, field => {
|
|
|
|
const newField = { ...field };
|
|
|
|
delete newField[I18N];
|
|
|
|
return newField;
|
|
|
|
});
|
2020-11-26 02:23:53 -08:00
|
|
|
}
|
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) {
|
2021-02-10 17:52:02 +02:00
|
|
|
if (i18n && i18n.structure !== I18N_STRUCTURE.SINGLE_FILE) {
|
2020-11-26 02:23:53 -08:00
|
|
|
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) {
|
2021-02-10 17:52:02 +02:00
|
|
|
if (i18n && i18n.default_locale && !i18n.locales.includes(i18n.default_locale)) {
|
2020-09-20 10:30:46 -07:00
|
|
|
throw new Error(
|
2021-02-10 17:52:02 +02:00
|
|
|
`i18n locales '${i18n.locales.join(', ')}' are missing the default locale ${
|
|
|
|
i18n.default_locale
|
|
|
|
}`,
|
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 hasIntegration(config, collection) {
|
2021-02-10 17:52:02 +02:00
|
|
|
// TODO remove fromJS when Immutable is removed from the integrations state slice
|
|
|
|
const integrations = getIntegrations(fromJS(config));
|
|
|
|
const integration = selectIntegration(integrations, collection.name, 'listEntries');
|
2021-01-19 06:59:00 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
export function applyDefaults(originalConfig) {
|
|
|
|
return produce(originalConfig, config => {
|
|
|
|
config.publish_mode = config.publish_mode || SIMPLE_PUBLISH_MODE;
|
|
|
|
config.slug = config.slug || {};
|
|
|
|
config.collections = config.collections || [];
|
|
|
|
|
|
|
|
// Use `site_url` as default `display_url`.
|
|
|
|
if (!config.display_url && config.site_url) {
|
|
|
|
config.display_url = config.site_url;
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
// Use media_folder as default public_folder.
|
|
|
|
const defaultPublicFolder = `/${trimStart(config.media_folder, '/')}`;
|
|
|
|
if (!('public_folder' in config)) {
|
|
|
|
config.public_folder = defaultPublicFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default values for the slug config
|
|
|
|
if (!('encoding' in config.slug)) {
|
|
|
|
config.slug.encoding = 'unicode';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!('clean_accents' in config.slug)) {
|
|
|
|
config.slug.clean_accents = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!('sanitize_replacement' in config.slug)) {
|
|
|
|
config.slug.sanitize_replacement = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
const i18n = config[I18N];
|
|
|
|
const hasI18n = Boolean(i18n);
|
|
|
|
if (hasI18n) {
|
|
|
|
i18n.default_locale = i18n.default_locale || i18n.locales[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
throwOnMissingDefaultLocale(i18n);
|
|
|
|
|
2021-02-10 09:16:08 -08:00
|
|
|
const backend = resolveBackend(fromJS(config));
|
2021-02-10 17:52:02 +02:00
|
|
|
|
|
|
|
for (const collection of config.collections) {
|
|
|
|
if (!('publish' in collection)) {
|
|
|
|
collection.publish = true;
|
2018-02-28 15:45:16 -05:00
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
const collectionHasI18n = Boolean(collection[I18N]);
|
|
|
|
if (hasI18n && collectionHasI18n) {
|
|
|
|
collection[I18N] = getI18nDefaults(collection[I18N], i18n);
|
|
|
|
} else {
|
|
|
|
delete collection[I18N];
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (collection.fields) {
|
|
|
|
collection.fields = setI18nDefaultsForFields(collection.fields, collectionHasI18n);
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
const { folder, files, view_filters, view_groups, meta } = collection;
|
|
|
|
|
|
|
|
if (folder) {
|
|
|
|
collection.type = FOLDER;
|
|
|
|
|
|
|
|
if (collection.path && !collection.media_folder) {
|
|
|
|
// default value for media folder when using the path config
|
|
|
|
collection.media_folder = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('media_folder' in collection && !collection.public_folder) {
|
|
|
|
collection.public_folder = collection.media_folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection.fields) {
|
|
|
|
collection.fields = traverseFieldsJS(collection.fields, setDefaultPublicFolderForField);
|
|
|
|
}
|
|
|
|
|
|
|
|
collection.folder = trim(folder, '/');
|
|
|
|
|
|
|
|
if (meta && meta.path) {
|
|
|
|
const metaField = {
|
|
|
|
name: 'path',
|
|
|
|
meta: true,
|
|
|
|
required: true,
|
|
|
|
...meta.path,
|
|
|
|
};
|
|
|
|
collection.fields = [metaField, ...(collection.fields || [])];
|
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (files) {
|
|
|
|
collection.type = FILES;
|
|
|
|
|
|
|
|
// after we invoked setI18nDefaults,
|
|
|
|
// i18n property can't be boolean anymore
|
|
|
|
const collectionI18n = collection[I18N];
|
|
|
|
throwOnInvalidFileCollectionStructure(collectionI18n);
|
|
|
|
|
|
|
|
delete collection.nested;
|
|
|
|
delete collection.meta;
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
file.file = trimStart(file.file, '/');
|
|
|
|
|
|
|
|
if ('media_folder' in file && !file.public_folder) {
|
|
|
|
file.public_folder = file.media_folder;
|
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (file.fields) {
|
|
|
|
file.fields = traverseFieldsJS(file.fields, setDefaultPublicFolderForField);
|
2020-03-23 12:01:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
const fileHasI18n = Boolean(file[I18N]);
|
2020-11-26 02:23:53 -08:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (fileHasI18n) {
|
|
|
|
if (collectionI18n) {
|
|
|
|
file[I18N] = getI18nDefaults(file[I18N], collectionI18n);
|
2020-06-18 10:11:37 +03:00
|
|
|
}
|
2021-02-10 17:52:02 +02:00
|
|
|
} else {
|
|
|
|
delete file[I18N];
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (file.fields) {
|
|
|
|
file.fields = setI18nDefaultsForFields(file.fields, fileHasI18n);
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
// after we invoked setI18nDefaults,
|
|
|
|
// i18n property can't be boolean anymore
|
|
|
|
const fileI18n = file[I18N];
|
|
|
|
throwOnInvalidFileCollectionStructure(fileI18n);
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (!collection.sortable_fields) {
|
|
|
|
collection.sortable_fields = selectDefaultSortableFields(
|
|
|
|
// TODO remove fromJS when Immutable is removed from the collections state slice
|
|
|
|
fromJS(collection),
|
|
|
|
backend,
|
|
|
|
hasIntegration(config, collection),
|
|
|
|
);
|
|
|
|
}
|
2020-05-24 17:37:08 +00:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
collection.view_filters = (view_filters || []).map(filter => {
|
|
|
|
return {
|
|
|
|
...filter,
|
|
|
|
id: `${filter.field}__${filter.pattern}`,
|
|
|
|
};
|
|
|
|
});
|
2020-10-12 11:59:03 +02:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
collection.view_groups = (view_groups || []).map(group => {
|
|
|
|
return {
|
|
|
|
...group,
|
|
|
|
id: `${group.field}__${group.pattern}`,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (config.editor && !collection.editor) {
|
|
|
|
collection.editor = { preview: config.editor.preview };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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-10 17:52:02 +02:00
|
|
|
const config = applyDefaults(normalizedConfig);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
dispatch(configLoaded(fromJS(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
|
|
|
};
|
|
|
|
}
|