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-02-14 18:41:02 +01:00
|
|
|
import { trimStart, trim, isEmpty } from 'lodash';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
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';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
|
|
|
import type { ThunkDispatch } from 'redux-thunk';
|
|
|
|
import type { AnyAction } from 'redux';
|
2022-09-28 20:04:00 -06:00
|
|
|
import type { State } from '../types/redux';
|
|
|
|
import {
|
2021-02-14 18:41:02 +01:00
|
|
|
CmsConfig,
|
|
|
|
CmsField,
|
|
|
|
CmsFieldBase,
|
|
|
|
CmsFieldObject,
|
|
|
|
CmsFieldList,
|
|
|
|
CmsI18nConfig,
|
|
|
|
CmsPublishMode,
|
|
|
|
CmsLocalBackend,
|
2022-09-28 20:04:00 -06:00
|
|
|
CmsCollection,
|
|
|
|
} from '../interface';
|
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-14 18:41:02 +01:00
|
|
|
function isObjectField(field: CmsField): field is CmsFieldBase & CmsFieldObject {
|
|
|
|
return 'fields' in (field as CmsFieldObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isFieldList(field: CmsField): field is CmsFieldBase & CmsFieldList {
|
|
|
|
return 'types' in (field as CmsFieldList) || 'field' in (field as CmsFieldList);
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverseFieldsJS<Field extends CmsField>(
|
|
|
|
fields: Field[],
|
|
|
|
updater: <T extends CmsField>(field: T) => T,
|
|
|
|
): Field[] {
|
2021-02-07 18:11:30 +02:00
|
|
|
return fields.map(field => {
|
2021-02-14 18:41:02 +01:00
|
|
|
const newField = updater(field);
|
|
|
|
if (isObjectField(newField)) {
|
|
|
|
return { ...newField, fields: traverseFieldsJS(newField.fields, updater) };
|
|
|
|
} else if (isFieldList(newField) && newField.field) {
|
|
|
|
return { ...newField, field: traverseFieldsJS([newField.field], updater)[0] };
|
|
|
|
} else if (isFieldList(newField) && newField.types) {
|
|
|
|
return { ...newField, types: traverseFieldsJS(newField.types, updater) };
|
2021-02-07 18:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2021-02-14 18:41:02 +01:00
|
|
|
const validTypes: { [type: string]: string } = {
|
|
|
|
'text/yaml': 'yaml',
|
|
|
|
'application/x-yaml': 'yaml',
|
|
|
|
};
|
|
|
|
const configLinkEl = document.querySelector<HTMLLinkElement>('link[rel="cms-config-url"]');
|
|
|
|
if (configLinkEl && validTypes[configLinkEl.type] && configLinkEl.href) {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Using config file path: "${configLinkEl.href}"`);
|
2021-02-14 18:41:02 +01:00
|
|
|
return configLinkEl.href;
|
2018-04-10 16:48:04 -04:00
|
|
|
}
|
|
|
|
return 'config.yml';
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2018-04-10 16:48:04 -04:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
function setDefaultPublicFolderForField<T extends CmsField>(field: T) {
|
2021-05-16 05:31:15 -07:00
|
|
|
if ('media_folder' in field && !('public_folder' in field)) {
|
2021-02-10 17:52:02 +02:00
|
|
|
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',
|
2021-02-14 18:41:02 +01:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
function setSnakeCaseConfig<T extends CmsField>(field: T) {
|
|
|
|
const deprecatedKeys = Object.keys(WIDGET_KEY_MAP).filter(
|
|
|
|
camel => camel in field,
|
|
|
|
) as ReadonlyArray<keyof typeof WIDGET_KEY_MAP>;
|
2020-08-31 19:25:48 +08:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
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}'`,
|
|
|
|
);
|
2022-09-28 20:04:00 -06:00
|
|
|
return { [snake]: (field as unknown as Record<string, unknown>)[camel] };
|
2020-08-31 19:25:48 +08:00
|
|
|
});
|
2021-02-07 18:11:30 +02:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
return Object.assign({}, field, ...snakeValues) as T;
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-08-31 19:25:48 +08:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
function setI18nField<T extends CmsField>(field: T) {
|
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-14 18:41:02 +01:00
|
|
|
function getI18nDefaults(
|
|
|
|
collectionOrFileI18n: boolean | CmsI18nConfig,
|
|
|
|
defaultI18n: CmsI18nConfig,
|
|
|
|
) {
|
2021-02-10 17:52:02 +02:00
|
|
|
if (typeof collectionOrFileI18n === 'boolean') {
|
|
|
|
return defaultI18n;
|
|
|
|
} else {
|
|
|
|
const locales = collectionOrFileI18n.locales || defaultI18n.locales;
|
|
|
|
const defaultLocale = collectionOrFileI18n.default_locale || locales[0];
|
2021-02-14 18:41:02 +01:00
|
|
|
const mergedI18n: CmsI18nConfig = deepmerge(defaultI18n, collectionOrFileI18n);
|
2021-02-10 17:52:02 +02:00
|
|
|
mergedI18n.locales = locales;
|
|
|
|
mergedI18n.default_locale = defaultLocale;
|
|
|
|
throwOnMissingDefaultLocale(mergedI18n);
|
|
|
|
return mergedI18n;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
function setI18nDefaultsForFields(collectionOrFileFields: CmsField[], hasI18n: boolean) {
|
2021-02-10 17:52:02 +02:00
|
|
|
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-14 18:41:02 +01:00
|
|
|
function throwOnInvalidFileCollectionStructure(i18n?: CmsI18nConfig) {
|
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-14 18:41:02 +01:00
|
|
|
function throwOnMissingDefaultLocale(i18n?: CmsI18nConfig) {
|
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-14 18:41:02 +01:00
|
|
|
function hasIntegration(config: CmsConfig, collection: CmsCollection) {
|
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
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export function normalizeConfig(config: CmsConfig) {
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 18:41:02 +01:00
|
|
|
export function applyDefaults(originalConfig: CmsConfig) {
|
2021-02-10 17:52:02 +02:00
|
|
|
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];
|
2021-02-14 18:41:02 +01:00
|
|
|
|
|
|
|
if (i18n) {
|
2021-02-10 17:52:02 +02:00
|
|
|
i18n.default_locale = i18n.default_locale || i18n.locales[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
throwOnMissingDefaultLocale(i18n);
|
|
|
|
|
2021-03-11 12:08:46 +02:00
|
|
|
const backend = resolveBackend(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-14 18:41:02 +01:00
|
|
|
let collectionI18n = collection[I18N];
|
|
|
|
|
|
|
|
if (i18n && collectionI18n) {
|
|
|
|
collectionI18n = getI18nDefaults(collectionI18n, i18n);
|
|
|
|
collection[I18N] = collectionI18n;
|
2021-02-10 17:52:02 +02:00
|
|
|
} else {
|
2021-02-14 18:41:02 +01:00
|
|
|
collectionI18n = undefined;
|
2021-02-10 17:52:02 +02:00
|
|
|
delete collection[I18N];
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (collection.fields) {
|
2021-02-14 18:41:02 +01:00
|
|
|
collection.fields = setI18nDefaultsForFields(collection.fields, Boolean(collectionI18n));
|
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 = '';
|
|
|
|
}
|
|
|
|
|
2021-05-16 05:31:15 -07:00
|
|
|
if ('media_folder' in collection && !('public_folder' in collection)) {
|
2021-02-10 17:52:02 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
throwOnInvalidFileCollectionStructure(collectionI18n);
|
|
|
|
|
|
|
|
delete collection.nested;
|
|
|
|
delete collection.meta;
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
file.file = trimStart(file.file, '/');
|
|
|
|
|
2021-05-16 05:31:15 -07:00
|
|
|
if ('media_folder' in file && !('public_folder' in file)) {
|
2021-02-10 17:52:02 +02:00
|
|
|
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-14 18:41:02 +01:00
|
|
|
let fileI18n = file[I18N];
|
2020-11-26 02:23:53 -08:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
if (fileI18n && collectionI18n) {
|
|
|
|
fileI18n = getI18nDefaults(fileI18n, collectionI18n);
|
|
|
|
file[I18N] = fileI18n;
|
2021-02-10 17:52:02 +02:00
|
|
|
} else {
|
2021-02-14 18:41:02 +01:00
|
|
|
fileI18n = undefined;
|
2021-02-10 17:52:02 +02:00
|
|
|
delete file[I18N];
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
throwOnInvalidFileCollectionStructure(fileI18n);
|
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (file.fields) {
|
2021-02-14 18:41:02 +01:00
|
|
|
file.fields = setI18nDefaultsForFields(file.fields, Boolean(fileI18n));
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
2021-02-10 17:52:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
|
2021-02-10 17:52:02 +02:00
|
|
|
if (!collection.sortable_fields) {
|
2022-09-28 20:04:00 -06:00
|
|
|
collection.sortable_fields = {
|
|
|
|
fields: selectDefaultSortableFields(
|
|
|
|
// TODO remove fromJS when Immutable is removed from the collections state slice
|
|
|
|
fromJS(collection),
|
|
|
|
backend,
|
|
|
|
hasIntegration(config, collection),
|
|
|
|
),
|
|
|
|
};
|
2021-02-10 17:52:02 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export function parseConfig(data: string) {
|
2020-04-10 22:50:05 +01:00
|
|
|
const config = yaml.parse(data, { maxAliasCount: -1, prettyErrors: true, merge: true });
|
2021-02-14 18:41:02 +01:00
|
|
|
if (
|
|
|
|
typeof window !== 'undefined' &&
|
|
|
|
typeof window.CMS_ENV === 'string' &&
|
|
|
|
config[window.CMS_ENV]
|
|
|
|
) {
|
|
|
|
const configKeys = Object.keys(config[window.CMS_ENV]) as ReadonlyArray<keyof CmsConfig>;
|
|
|
|
for (const key of configKeys) {
|
|
|
|
config[key] = config[window.CMS_ENV][key] as CmsConfig[keyof CmsConfig];
|
|
|
|
}
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
2021-02-14 18:41:02 +01:00
|
|
|
return config as Partial<CmsConfig>;
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
async function getConfigYaml(file: string, hasManualConfig: boolean) {
|
|
|
|
const response = await fetch(file, { credentials: 'same-origin' }).catch(error => error as Error);
|
2018-10-09 13:53:12 -04:00
|
|
|
if (response instanceof Error || response.status !== 200) {
|
2021-02-14 18:41:02 +01:00
|
|
|
if (hasManualConfig) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const message = response instanceof Error ? response.message : response.status;
|
|
|
|
throw new Error(`Failed to load config.yml (${message})`);
|
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) {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Response for ${file} was not yaml. (Content-Type: ${contentType})`);
|
2021-02-14 18:41:02 +01:00
|
|
|
if (hasManualConfig) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-28 13:25:46 -07:00
|
|
|
}
|
|
|
|
return parseConfig(await response.text());
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export function configLoaded(config: CmsConfig) {
|
2016-02-25 00:45:56 -08:00
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_SUCCESS,
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: config,
|
2021-03-11 12:08:46 +02:00
|
|
|
} as const;
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function configLoading() {
|
|
|
|
return {
|
2016-11-11 12:17:01 +01:00
|
|
|
type: CONFIG_REQUEST,
|
2021-03-11 12:08:46 +02:00
|
|
|
} as const;
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export function configFailed(err: Error) {
|
2016-02-25 00:45:56 -08:00
|
|
|
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,
|
2021-03-11 12:08:46 +02:00
|
|
|
} as const;
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export async function detectProxyServer(localBackend?: boolean | CmsLocalBackend) {
|
|
|
|
const allowedHosts = [
|
|
|
|
'localhost',
|
|
|
|
'127.0.0.1',
|
|
|
|
...(typeof localBackend === 'boolean' ? [] : localBackend?.allowed_hosts || []),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!allowedHosts.includes(location.hostname) || !localBackend) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultUrl = 'http://localhost:8081/api/v1';
|
|
|
|
const proxyUrl =
|
|
|
|
localBackend === true
|
|
|
|
? defaultUrl
|
|
|
|
: localBackend.url || defaultUrl.replace('localhost', location.hostname);
|
|
|
|
|
|
|
|
try {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Looking for Netlify CMS Proxy Server at '${proxyUrl}'`);
|
2021-02-14 18:41:02 +01:00
|
|
|
const res = await fetch(`${proxyUrl}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify({ action: 'info' }),
|
|
|
|
});
|
|
|
|
const { repo, publish_modes, type } = (await res.json()) as {
|
|
|
|
repo?: string;
|
|
|
|
publish_modes?: CmsPublishMode[];
|
|
|
|
type?: string;
|
|
|
|
};
|
|
|
|
if (typeof repo === 'string' && Array.isArray(publish_modes) && typeof type === 'string') {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Detected Netlify CMS Proxy Server at '${proxyUrl}' with repo: '${repo}'`);
|
2021-02-14 18:41:02 +01:00
|
|
|
return { proxyUrl, publish_modes, type };
|
|
|
|
} else {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Netlify CMS Proxy Server not detected at '${proxyUrl}'`);
|
2021-02-14 18:41:02 +01:00
|
|
|
return {};
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
2021-02-14 18:41:02 +01:00
|
|
|
} catch {
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(`Netlify CMS Proxy Server not detected at '${proxyUrl}'`);
|
2021-02-14 18:41:02 +01:00
|
|
|
return {};
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
2020-02-10 18:07:52 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
function getPublishMode(config: CmsConfig, publishModes?: CmsPublishMode[], backendType?: string) {
|
2021-02-07 18:11:30 +02:00
|
|
|
if (config.publish_mode && publishModes && !publishModes.includes(config.publish_mode)) {
|
|
|
|
const newPublishMode = publishModes[0];
|
2022-09-28 20:04:00 -06:00
|
|
|
console.info(
|
2021-02-07 18:11:30 +02:00
|
|
|
`'${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-14 18:41:02 +01:00
|
|
|
export async function handleLocalBackend(originalConfig: CmsConfig) {
|
|
|
|
if (!originalConfig.local_backend) {
|
|
|
|
return originalConfig;
|
2021-01-13 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 14:39:35 +02:00
|
|
|
const {
|
|
|
|
proxyUrl,
|
|
|
|
publish_modes: publishModes,
|
|
|
|
type: backendType,
|
|
|
|
} = await detectProxyServer(originalConfig.local_backend);
|
2021-01-13 19:51:45 +02:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
if (!proxyUrl) {
|
2021-02-14 18:41:02 +01:00
|
|
|
return originalConfig;
|
2020-02-10 18:07:52 +02:00
|
|
|
}
|
2021-02-07 18:11:30 +02:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
return produce(originalConfig, config => {
|
|
|
|
config.backend.name = 'proxy';
|
|
|
|
config.backend.proxy_url = proxyUrl;
|
|
|
|
|
|
|
|
if (config.publish_mode) {
|
|
|
|
config.publish_mode = getPublishMode(config, publishModes, backendType);
|
|
|
|
}
|
|
|
|
});
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2021-02-14 18:41:02 +01:00
|
|
|
export function loadConfig(manualConfig: Partial<CmsConfig> = {}, onLoad: () => unknown) {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (window.CMS_CONFIG) {
|
2021-03-11 12:08:46 +02:00
|
|
|
return configLoaded(window.CMS_CONFIG);
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2021-02-14 18:41:02 +01:00
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>) => {
|
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-03-11 12:08:46 +02:00
|
|
|
dispatch(configLoaded(config));
|
2021-01-18 11:37:12 +02:00
|
|
|
|
|
|
|
if (typeof onLoad === 'function') {
|
|
|
|
onLoad();
|
|
|
|
}
|
2022-09-28 20:04:00 -06:00
|
|
|
} catch (err: any) {
|
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
|
|
|
};
|
|
|
|
}
|
2021-03-11 12:08:46 +02:00
|
|
|
|
|
|
|
export type ConfigAction = ReturnType<
|
|
|
|
typeof configLoading | typeof configLoaded | typeof configFailed
|
|
|
|
>;
|