From cc95d65a3d45b65826d83529f79894f74f3463ba Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Wed, 10 Aug 2022 11:06:10 +0200 Subject: [PATCH] Move content-type builder server custom field type conversion --- .../content-type-builder/server/services/components.js | 5 ----- .../content-type-builder/server/services/content-types.js | 6 ------ .../server/services/schema-builder/component-builder.js | 7 ++++++- .../services/schema-builder/content-type-builder.js | 8 +++++++- .../utils/convert-custom-field-type.js | 0 5 files changed, 13 insertions(+), 13 deletions(-) rename packages/core/content-type-builder/server/services/{ => schema-builder}/utils/convert-custom-field-type.js (100%) diff --git a/packages/core/content-type-builder/server/services/components.js b/packages/core/content-type-builder/server/services/components.js index e01bf73a11..51151bf59f 100644 --- a/packages/core/content-type-builder/server/services/components.js +++ b/packages/core/content-type-builder/server/services/components.js @@ -4,7 +4,6 @@ const _ = require('lodash'); const { formatAttributes, replaceTemporaryUIDs } = require('../utils/attributes'); const createBuilder = require('./schema-builder'); -const convertCustomFieldType = require('./utils/convert-custom-field-type'); /** * Formats a component attributes @@ -42,11 +41,9 @@ const createComponent = async ({ component, components = [] }) => { const uidMap = builder.createNewComponentUIDMap(components); const replaceTmpUIDs = replaceTemporaryUIDs(uidMap); - convertCustomFieldType(component.attributes); const newComponent = builder.createComponent(replaceTmpUIDs(component)); components.forEach(component => { - convertCustomFieldType(component.attributes); if (!_.has(component, 'uid')) { return builder.createComponent(replaceTmpUIDs(component)); } @@ -70,14 +67,12 @@ const editComponent = async (uid, { component, components = [] }) => { const uidMap = builder.createNewComponentUIDMap(components); const replaceTmpUIDs = replaceTemporaryUIDs(uidMap); - convertCustomFieldType(component.attributes); const updatedComponent = builder.editComponent({ uid, ...replaceTmpUIDs(component), }); components.forEach(component => { - convertCustomFieldType(component.attributes); if (!_.has(component, 'uid')) { return builder.createComponent(replaceTmpUIDs(component)); } diff --git a/packages/core/content-type-builder/server/services/content-types.js b/packages/core/content-type-builder/server/services/content-types.js index e709ee14d7..997d192b6b 100644 --- a/packages/core/content-type-builder/server/services/content-types.js +++ b/packages/core/content-type-builder/server/services/content-types.js @@ -8,7 +8,6 @@ const { ApplicationError } = require('@strapi/utils').errors; const { formatAttributes, replaceTemporaryUIDs } = require('../utils/attributes'); const createBuilder = require('./schema-builder'); const { coreUids, pluginsUids } = require('./constants'); -const convertCustomFieldType = require('./utils/convert-custom-field-type'); const isContentTypeVisible = model => getOr(true, 'pluginOptions.content-type-builder.visible', model) === true; @@ -85,7 +84,6 @@ const createContentType = async ({ contentType, components = [] }, options = {}) const replaceTmpUIDs = replaceTemporaryUIDs(uidMap); - convertCustomFieldType(contentType.attributes); const newContentType = builder.createContentType(replaceTmpUIDs(contentType)); // allow components to target the new contentType @@ -101,7 +99,6 @@ const createContentType = async ({ contentType, components = [] }, options = {}) }; components.forEach(component => { - convertCustomFieldType(component.attributes); const options = replaceTmpUIDs(targetContentType(component)); if (!_.has(component, 'uid')) { @@ -173,15 +170,12 @@ const editContentType = async (uid, { contentType, components = [] }) => { const uidMap = builder.createNewComponentUIDMap(components); const replaceTmpUIDs = replaceTemporaryUIDs(uidMap); - convertCustomFieldType(contentType.attributes); const updatedContentType = builder.editContentType({ uid, ...replaceTmpUIDs(contentType), }); components.forEach(component => { - convertCustomFieldType(component.attributes); - if (!_.has(component, 'uid')) { return builder.createComponent(replaceTmpUIDs(component)); } diff --git a/packages/core/content-type-builder/server/services/schema-builder/component-builder.js b/packages/core/content-type-builder/server/services/schema-builder/component-builder.js index eb8aa7c3fd..245682964d 100644 --- a/packages/core/content-type-builder/server/services/schema-builder/component-builder.js +++ b/packages/core/content-type-builder/server/services/schema-builder/component-builder.js @@ -8,6 +8,7 @@ const { nameToSlug, nameToCollectionName } = require('@strapi/utils'); const { ApplicationError } = require('@strapi/utils').errors; const { isConfigurable } = require('../../utils/attributes'); const createSchemaHandler = require('./schema-handler'); +const convertCustomFieldType = require('./utils/convert-custom-field-type'); module.exports = function createComponentBuilder() { return { @@ -32,12 +33,15 @@ module.exports = function createComponentBuilder() { * create a component in the tmpComponent map */ createComponent(infos) { + const { attributes } = infos; const uid = this.createComponentUID(infos); if (this.components.has(uid)) { throw new ApplicationError('component.alreadyExists'); } + convertCustomFieldType(attributes); + const handler = createSchemaHandler({ dir: path.join(strapi.dirs.components, nameToSlug(infos.category)), filename: `${nameToSlug(infos.displayName)}.json`, @@ -72,12 +76,13 @@ module.exports = function createComponentBuilder() { * create a component in the tmpComponent map */ editComponent(infos) { - const { uid } = infos; + const { uid, attributes } = infos; if (!this.components.has(uid)) { throw new ApplicationError('component.notFound'); } + convertCustomFieldType(attributes); const component = this.components.get(uid); const [, nameUID] = uid.split('.'); diff --git a/packages/core/content-type-builder/server/services/schema-builder/content-type-builder.js b/packages/core/content-type-builder/server/services/schema-builder/content-type-builder.js index 311aa7a0a5..333018fe4c 100644 --- a/packages/core/content-type-builder/server/services/schema-builder/content-type-builder.js +++ b/packages/core/content-type-builder/server/services/schema-builder/content-type-builder.js @@ -8,6 +8,7 @@ const { ApplicationError } = require('@strapi/utils').errors; const { isRelation, isConfigurable } = require('../../utils/attributes'); const { typeKinds } = require('../constants'); const createSchemaHandler = require('./schema-handler'); +const convertCustomFieldType = require('./utils/convert-custom-field-type'); const reuseUnsetPreviousProperties = (newAttribute, oldAttribute) => { _.defaults( @@ -71,12 +72,15 @@ module.exports = function createComponentBuilder() { * @returns {object} new content type */ createContentType(infos) { + const { attributes } = infos; const uid = createContentTypeUID(infos); if (this.contentTypes.has(uid)) { throw new ApplicationError('contentType.alreadyExists'); } + convertCustomFieldType(attributes); + const contentType = createSchemaHandler({ modelName: infos.singularName, dir: path.join(strapi.dirs.api, infos.singularName, 'content-types', infos.singularName), @@ -124,12 +128,14 @@ module.exports = function createComponentBuilder() { }, editContentType(infos) { - const { uid } = infos; + const { uid, attributes } = infos; if (!this.contentTypes.has(uid)) { throw new ApplicationError('contentType.notFound'); } + convertCustomFieldType(attributes); + const contentType = this.contentTypes.get(uid); const oldAttributes = contentType.schema.attributes; diff --git a/packages/core/content-type-builder/server/services/utils/convert-custom-field-type.js b/packages/core/content-type-builder/server/services/schema-builder/utils/convert-custom-field-type.js similarity index 100% rename from packages/core/content-type-builder/server/services/utils/convert-custom-field-type.js rename to packages/core/content-type-builder/server/services/schema-builder/utils/convert-custom-field-type.js