2021-01-13 19:51:45 +02:00
|
|
|
import deepmerge from 'deepmerge';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { produce } from 'immer';
|
2022-10-20 11:57:30 -04:00
|
|
|
import trim from 'lodash/trim';
|
|
|
|
import trimStart from 'lodash/trimStart';
|
|
|
|
import yaml from 'yaml';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
2021-02-07 18:11:30 +02:00
|
|
|
import { resolveBackend } from '../backend';
|
2021-02-10 17:52:02 +02:00
|
|
|
import { FILES, FOLDER } from '../constants/collectionTypes';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { validateConfig } from '../constants/configSchema';
|
|
|
|
import { I18N, I18N_FIELD, I18N_STRUCTURE } from '../lib/i18n';
|
|
|
|
import { selectDefaultSortableFields } from '../lib/util/collection.util';
|
|
|
|
import { getIntegrations, selectIntegration } from '../reducers/integrations';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
|
|
|
import type { AnyAction } from 'redux';
|
2022-10-20 11:57:30 -04:00
|
|
|
import type { ThunkDispatch } from 'redux-thunk';
|
2022-10-01 13:45:01 -04:00
|
|
|
import type {
|
2022-10-26 10:57:40 -04:00
|
|
|
BaseField,
|
2022-10-20 11:57:30 -04:00
|
|
|
Collection,
|
|
|
|
Config,
|
|
|
|
Field,
|
|
|
|
I18nInfo,
|
2022-10-26 10:57:40 -04:00
|
|
|
ListField,
|
2022-10-20 11:57:30 -04:00
|
|
|
LocalBackend,
|
2022-10-26 10:57:40 -04:00
|
|
|
ObjectField,
|
2022-09-28 20:04:00 -06:00
|
|
|
} from '../interface';
|
2022-10-20 11:57:30 -04:00
|
|
|
import type { RootState } from '../store';
|
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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function isObjectField(field: Field): field is BaseField & ObjectField {
|
|
|
|
return 'fields' in (field as ObjectField);
|
2021-02-14 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function isFieldList(field: Field): field is BaseField & ListField {
|
|
|
|
return 'types' in (field as ListField) || 'field' in (field as ListField);
|
2021-02-14 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function traverseFieldsJS<F extends Field>(
|
|
|
|
fields: F[],
|
|
|
|
updater: <T extends Field>(field: T) => T,
|
|
|
|
): F[] {
|
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.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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function setDefaultPublicFolderForField<T extends Field>(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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function setI18nField<T extends Field>(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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function getI18nDefaults(collectionOrFileI18n: boolean | I18nInfo, defaultI18n: I18nInfo) {
|
2021-02-10 17:52:02 +02:00
|
|
|
if (typeof collectionOrFileI18n === 'boolean') {
|
|
|
|
return defaultI18n;
|
|
|
|
} else {
|
|
|
|
const locales = collectionOrFileI18n.locales || defaultI18n.locales;
|
2022-10-20 11:57:30 -04:00
|
|
|
const defaultLocale = collectionOrFileI18n.defaultLocale || locales[0];
|
|
|
|
const mergedI18n: I18nInfo = deepmerge(defaultI18n, collectionOrFileI18n);
|
2021-02-10 17:52:02 +02:00
|
|
|
mergedI18n.locales = locales;
|
2022-10-20 11:57:30 -04:00
|
|
|
mergedI18n.defaultLocale = defaultLocale;
|
2021-02-10 17:52:02 +02:00
|
|
|
throwOnMissingDefaultLocale(mergedI18n);
|
|
|
|
return mergedI18n;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function setI18nDefaultsForFields(collectionOrFileFields: Field[], 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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function throwOnInvalidFileCollectionStructure(i18n?: I18nInfo) {
|
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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function throwOnMissingDefaultLocale(i18n?: I18nInfo) {
|
|
|
|
if (i18n && i18n.defaultLocale && !i18n.locales.includes(i18n.defaultLocale)) {
|
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 ${
|
2022-10-20 11:57:30 -04:00
|
|
|
i18n.defaultLocale
|
2021-02-10 17:52:02 +02:00
|
|
|
}`,
|
2020-09-20 10:30:46 -07:00
|
|
|
);
|
|
|
|
}
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-09-20 10:30:46 -07:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function hasIntegration(config: Config, collection: Collection) {
|
|
|
|
const integrations = getIntegrations(config);
|
2021-02-10 17:52:02 +02:00
|
|
|
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
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function applyDefaults(originalConfig: Config) {
|
2021-02-10 17:52:02 +02:00
|
|
|
return produce(originalConfig, config => {
|
|
|
|
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) {
|
2022-10-20 11:57:30 -04:00
|
|
|
i18n.defaultLocale = i18n.defaultLocale || i18n.locales[0];
|
2021-02-10 17:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const { folder, files, view_filters, view_groups } = collection;
|
2021-02-10 17:52:02 +02:00
|
|
|
|
|
|
|
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, '/');
|
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;
|
|
|
|
|
|
|
|
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(
|
2022-10-20 11:57:30 -04:00
|
|
|
collection,
|
2022-09-28 20:04:00 -06:00
|
|
|
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]
|
|
|
|
) {
|
2022-10-20 11:57:30 -04:00
|
|
|
const configKeys = Object.keys(config[window.CMS_ENV]) as ReadonlyArray<keyof Config>;
|
2021-02-14 18:41:02 +01:00
|
|
|
for (const key of configKeys) {
|
2022-10-20 11:57:30 -04:00
|
|
|
config[key] = config[window.CMS_ENV][key] as Config[keyof Config];
|
2021-02-14 18:41:02 +01:00
|
|
|
}
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
2022-10-20 11:57:30 -04:00
|
|
|
return config as Config;
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
async function getConfigYaml(file: string): Promise<Config> {
|
2021-02-14 18:41:02 +01:00
|
|
|
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
|
|
|
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
|
|
|
}
|
2022-10-20 11:57:30 -04:00
|
|
|
const contentType = response.headers.get('Content-Type') ?? 'Not-Found';
|
2018-03-28 13:25:46 -07:00
|
|
|
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})`);
|
2018-03-28 13:25:46 -07:00
|
|
|
}
|
|
|
|
return parseConfig(await response.text());
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function configLoaded(config: Config) {
|
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
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export async function detectProxyServer(localBackend?: boolean | LocalBackend) {
|
2021-02-14 18:41:02 +01:00
|
|
|
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-10-02 20:06:20 -04:00
|
|
|
console.info(`Looking for Static 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' }),
|
|
|
|
});
|
2022-10-01 13:45:01 -04:00
|
|
|
const { repo, type } = (await res.json()) as {
|
2021-02-14 18:41:02 +01:00
|
|
|
repo?: string;
|
|
|
|
type?: string;
|
|
|
|
};
|
2022-10-01 13:45:01 -04:00
|
|
|
if (typeof repo === 'string' && typeof type === 'string') {
|
2022-10-02 20:06:20 -04:00
|
|
|
console.info(`Detected Static CMS Proxy Server at '${proxyUrl}' with repo: '${repo}'`);
|
2022-10-01 13:45:01 -04:00
|
|
|
return { proxyUrl, type };
|
2021-02-14 18:41:02 +01:00
|
|
|
} else {
|
2022-10-02 20:06:20 -04:00
|
|
|
console.info(`Static 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-10-02 20:06:20 -04:00
|
|
|
console.info(`Static 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
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export async function handleLocalBackend(originalConfig: Config) {
|
2021-02-14 18:41:02 +01:00
|
|
|
if (!originalConfig.local_backend) {
|
|
|
|
return originalConfig;
|
2021-01-13 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const { proxyUrl } = 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;
|
|
|
|
});
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
export function loadConfig(manualConfig: Config | undefined, 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
|
|
|
}
|
2022-10-20 11:57:30 -04:00
|
|
|
return async (dispatch: ThunkDispatch<RootState, {}, 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();
|
2022-10-20 11:57:30 -04:00
|
|
|
const mergedConfig = manualConfig ? manualConfig : await getConfigYaml(configUrl);
|
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);
|
2022-10-26 10:57:40 -04:00
|
|
|
const config = applyDefaults(withLocalBackend);
|
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-10-20 11:57:30 -04:00
|
|
|
} catch (error: unknown) {
|
|
|
|
console.error(error);
|
|
|
|
if (error instanceof Error) {
|
|
|
|
dispatch(configFailed(error));
|
|
|
|
}
|
|
|
|
throw error;
|
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
|
|
|
|
>;
|