Skip to content

Commit

Permalink
chore: improve ts typing in sanitization functions (#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodMFlesch authored Aug 17, 2023
1 parent f5f2332 commit 294fb5e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
51 changes: 25 additions & 26 deletions src/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,53 @@ import checkDuplicateCollections from '../utilities/checkDuplicateCollections';
import { defaults } from './defaults';
import getDefaultBundler from '../bundlers/webpack/bundler';

const sanitizeAdmin = (config: SanitizedConfig): SanitizedConfig['admin'] => {
const adminConfig = config.admin;
const sanitizeAdminConfig = (configToSanitize: Config): Partial<SanitizedConfig> => {
const sanitizedConfig = { ...configToSanitize };

// add default user collection if none provided
if (!adminConfig?.user) {
const firstCollectionWithAuth = config.collections.find(({ auth }) => Boolean(auth));
if (!sanitizedConfig?.admin?.user) {
const firstCollectionWithAuth = sanitizedConfig.collections.find(({ auth }) => Boolean(auth));
if (firstCollectionWithAuth) {
adminConfig.user = firstCollectionWithAuth.slug;
sanitizedConfig.admin.user = firstCollectionWithAuth.slug;
} else {
adminConfig.user = 'users';
const sanitizedDefaultUser = sanitizeCollection(config, defaultUserCollection);
config.collections.push(sanitizedDefaultUser);
sanitizedConfig.admin.user = defaultUserCollection.slug;
sanitizedConfig.collections.push(defaultUserCollection);
}
}

if (!config.collections.find(({ slug }) => slug === adminConfig.user)) {
throw new InvalidConfiguration(`${config.admin.user} is not a valid admin user collection`);
if (!sanitizedConfig.collections.find(({ slug }) => slug === sanitizedConfig.admin.user)) {
throw new InvalidConfiguration(`${sanitizedConfig.admin.user} is not a valid admin user collection`);
}

// add default bundler if none provided
if (!adminConfig.bundler) {
adminConfig.bundler = getDefaultBundler();
if (!sanitizedConfig.admin.bundler) {
sanitizedConfig.admin.bundler = getDefaultBundler();
}

return adminConfig;
return sanitizedConfig as Partial<SanitizedConfig>;
};

export const sanitizeConfig = (config: Config): SanitizedConfig => {
const sanitizedConfig: Config = merge(defaults, config, {
export const sanitizeConfig = (incomingConfig: Config): SanitizedConfig => {
const configWithDefaults: Config = merge(defaults, incomingConfig, {
isMergeableObject: isPlainObject,
}) as Config;
});

sanitizedConfig.admin = sanitizeAdmin(sanitizedConfig as SanitizedConfig);
const config: Partial<SanitizedConfig> = sanitizeAdminConfig(configWithDefaults);
config.collections = config.collections.map((collection) => sanitizeCollection(configWithDefaults, collection));

sanitizedConfig.collections = sanitizedConfig.collections.map((collection) => sanitizeCollection(sanitizedConfig, collection));
checkDuplicateCollections(sanitizedConfig.collections);
checkDuplicateCollections(config.collections);

if (sanitizedConfig.globals.length > 0) {
sanitizedConfig.globals = sanitizeGlobals(sanitizedConfig.collections, sanitizedConfig.globals);
if (config.globals.length > 0) {
config.globals = sanitizeGlobals(config.collections, config.globals);
}

if (typeof sanitizedConfig.serverURL === 'undefined') {
sanitizedConfig.serverURL = '';
if (typeof config.serverURL === 'undefined') {
config.serverURL = '';
}

if (sanitizedConfig.serverURL !== '') {
sanitizedConfig.csrf.push(sanitizedConfig.serverURL);
if (config.serverURL !== '') {
config.csrf.push(config.serverURL);
}

return sanitizedConfig as SanitizedConfig;
return config as SanitizedConfig;
};
4 changes: 2 additions & 2 deletions src/utilities/checkDuplicateCollections.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DuplicateCollection } from '../errors';
import { CollectionConfig } from '../collections/config/types';
import { SanitizedCollectionConfig } from '../collections/config/types';

const getDuplicates = (arr: string[]) => arr.filter((item, index) => arr.indexOf(item) !== index);

const checkDuplicateCollections = (collections: CollectionConfig[]): void => {
const checkDuplicateCollections = (collections: SanitizedCollectionConfig[]): void => {
const duplicateSlugs = getDuplicates(collections.map((c) => c.slug));
if (duplicateSlugs.length > 0) {
throw new DuplicateCollection('slug', duplicateSlugs);
Expand Down

0 comments on commit 294fb5e

Please sign in to comment.