diff --git a/packages/strapi-plugin-i18n/config/functions/register.js b/packages/strapi-plugin-i18n/config/functions/register.js index 24e593da5c..d923fec908 100644 --- a/packages/strapi-plugin-i18n/config/functions/register.js +++ b/packages/strapi-plugin-i18n/config/functions/register.js @@ -1,22 +1,27 @@ 'use strict'; const _ = require('lodash'); -const pluralize = require('pluralize'); + const { getService } = require('../../utils'); module.exports = () => { - Object.values(strapi.contentTypes).forEach(model => { - if (getService('content-types').isLocalized(model)) { - _.set(model.attributes, 'localizations', { + const contentTypeService = getService('content-types'); + const coreApiService = getService('core-api'); + + Object.values(strapi.contentTypes).forEach(contentType => { + if (contentTypeService.isLocalized(contentType)) { + const { attributes, modelName } = contentType; + + _.set(attributes, 'localizations', { writable: true, private: false, configurable: false, visible: false, - collection: model.modelName, + collection: modelName, populate: ['id', 'locale', 'published_at'], }); - _.set(model.attributes, 'locale', { + _.set(attributes, 'locale', { writable: true, private: false, configurable: false, @@ -24,34 +29,7 @@ module.exports = () => { type: 'string', }); - // add new route - const route = - model.kind === 'singleType' - ? _.kebabCase(model.modelName) - : _.kebabCase(pluralize(model.modelName)); - - const localizationRoutes = [ - { - method: 'POST', - path: `/${route}/:id/localizations`, - handler: `${model.modelName}.createLocalization`, - config: { - policies: [], - }, - }, - ]; - - const handler = function(ctx) { - ctx.body = 'works'; - }; - - strapi.config.routes.push(...localizationRoutes); - - _.set( - strapi, - `api.${model.apiName}.controllers.${model.modelName}.createLocalization`, - handler - ); + coreApiService.addCreateLocalizationAction(contentType); } }); diff --git a/packages/strapi-plugin-i18n/services/core-api.js b/packages/strapi-plugin-i18n/services/core-api.js new file mode 100644 index 0000000000..6c1f7e2d9e --- /dev/null +++ b/packages/strapi-plugin-i18n/services/core-api.js @@ -0,0 +1,54 @@ +'use strict'; + +const { set } = require('lodash'); +const { getContentTypeRoutePrefix } = require('strapi-utils').contentTypes; + +/** + * Returns a handler to handle localizations creation in the core api + * @param {object} contentType + * @returns ((oject) => {}) + */ +const createLocalizationHandler = contentType => { + return function(ctx) { + ctx.body = 'works'; + }; +}; + +/** + * Returns a route config to handle localizations creation in the core api + * @param {object} contentType + * @returns {{ method: string, path: string, handler: string, config: { policies: string[] }}} + */ +const createLocalizationRoute = contentType => { + const { modelName } = contentType; + + return { + method: 'POST', + path: `/${getContentTypeRoutePrefix(contentType)}/:id/localizations`, + handler: `${modelName}.createLocalization`, + config: { + policies: [], + }, + }; +}; + +/** + * Adds a route & an action to the core api controller of a content type to allow creating new localizations + * @param {object} contentType + */ +const addCreateLocalizationAction = contentType => { + const { modelName, apiName } = contentType; + + const localizationRoute = createLocalizationRoute(contentType); + + const coreApiControllerPath = `api.${apiName}.controllers.${modelName}.createLocalization`; + const handler = createLocalizationHandler(contentType); + + strapi.config.routes.push(localizationRoute); + + set(strapi, coreApiControllerPath, handler); +}; + +module.exports = { + addCreateLocalizationAction, +}; diff --git a/packages/strapi-utils/lib/content-types.js b/packages/strapi-utils/lib/content-types.js index aaef80cd19..1f08b8d846 100644 --- a/packages/strapi-utils/lib/content-types.js +++ b/packages/strapi-utils/lib/content-types.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); +const pluralize = require('pluralize'); const SINGLE_TYPE = 'singleType'; const COLLECTION_TYPE = 'collectionType'; @@ -190,6 +191,17 @@ const getGlobalId = (model, modelName, prefix) => { const isRelationalAttribute = attribute => _.has(attribute, 'model') || _.has(attribute, 'collection'); +/** + * Returns a route prefix for a contentType + * @param {object} contentType + * @returns {string} + */ +const getContentTypeRoutePrefix = contentType => { + return isSingleType(contentType) + ? _.kebabCase(contentType.modelName) + : _.kebabCase(pluralize(contentType.modelName)); +}; + module.exports = { isScalarAttribute, isMediaAttribute, @@ -211,4 +223,5 @@ module.exports = { isKind, createContentType, getGlobalId, + getContentTypeRoutePrefix, };