migrate getStarted api

This commit is contained in:
Pierre Noël 2021-08-20 18:45:06 +02:00
parent bc56be3560
commit 514fc00d97
13 changed files with 94 additions and 56 deletions

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "addresses", "collectionName": "addresses",
"info": { "info": {
"name": "address", "displayName": "Address",
"singularName": "address",
"pluralName": "addresses",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "categories", "collectionName": "categories",
"info": { "info": {
"name": "category", "displayName": "Category",
"singularName": "category",
"pluralName": "categories",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "countries", "collectionName": "countries",
"info": { "info": {
"name": "country", "displayName": "Country",
"singularName": "country",
"pluralName": "countries",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,8 @@
"kind": "singleType", "kind": "singleType",
"collectionName": "homepages", "collectionName": "homepages",
"info": { "info": {
"name": "Homepage" "displayName": "Homepage",
"singularName": "homepage"
}, },
"options": { "options": {
"draftAndPublish": true "draftAndPublish": true

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "kitchensinks", "collectionName": "kitchensinks",
"info": { "info": {
"name": "kitchensink", "displayName": "Kitchen Sink",
"singularName": "kitchensink",
"pluralName": "kitchensinks",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "likes", "collectionName": "likes",
"info": { "info": {
"name": "like", "displayName": "Like",
"singularName": "like",
"pluralName": "likes",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,8 +2,10 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "menus", "collectionName": "menus",
"info": { "info": {
"name": "menu", "description": "",
"description": "" "displayName": "Menu",
"singularName": "menu",
"pluralName": "menus"
}, },
"options": { "options": {
"draftAndPublish": false, "draftAndPublish": false,

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "menusections", "collectionName": "menusections",
"info": { "info": {
"name": "menusection", "displayName": "Menu Section",
"singularName": "menusection",
"pluralName": "menusections",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "restaurants", "collectionName": "restaurants",
"info": { "info": {
"name": "restaurant", "displayName": "Restaurant",
"singularName": "restaurant",
"pluralName": "restaurants",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -1,7 +1,9 @@
{ {
"collectionName": "reviews", "collectionName": "reviews",
"info": { "info": {
"name": "review", "displayName": "Review",
"singularName": "review",
"pluralName": "reviews",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -2,7 +2,9 @@
"kind": "collectionType", "kind": "collectionType",
"collectionName": "tags", "collectionName": "tags",
"info": { "info": {
"name": "tag", "displayName": "Tag",
"singularName": "tag",
"pluralName": "tags",
"description": "" "description": ""
}, },
"options": { "options": {

View File

@ -17,6 +17,7 @@ const contentTypeSchemaValidator = yup.object().shape({
info: yup info: yup
.object() .object()
.shape({ .shape({
displayName: yup.string().required(),
singularName: yup singularName: yup
.string() .string()
.isKebabCase() .isKebabCase()
@ -25,7 +26,6 @@ const contentTypeSchemaValidator = yup.object().shape({
.string() .string()
.isKebabCase() .isKebabCase()
.required(), .required(),
displayName: yup.string().required(),
}) })
.required(), .required(),
}), }),

View File

@ -1,73 +1,90 @@
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const { toLower } = require('lodash/fp'); const { toLower, kebabCase } = require('lodash/fp');
const { getConfigUrls } = require('@strapi/utils'); const { getConfigUrls } = require('@strapi/utils');
const pluralize = require('pluralize'); const pluralize = require('pluralize');
const { createContentType } = require('../domain/content-type'); const { createContentType } = require('../domain/content-type');
const { createCoreApi } = require('../../core-api'); const { createCoreApi } = require('../../core-api');
// TODO: function to be moved next to where the api will be loaded
const validateContentTypesUnicity = schemas => {
const names = [];
schemas.forEach(schema => {
if (schema.info.singularName) {
const singularName = kebabCase(schema.info.singularName);
if (names.includes(singularName)) {
throw new Error(`The singular name "${schema.info.singularName}" should be unique`);
}
names.push(singularName);
}
if (schema.info.pluralName) {
const pluralName = kebabCase(schema.info.pluralName);
if (names.includes(pluralName)) {
throw new Error(`The plural name "${schema.info.pluralName}" should be unique`);
}
names.push(pluralName);
}
});
};
module.exports = function(strapi) { module.exports = function(strapi) {
strapi.contentTypes = {}; strapi.contentTypes = {};
// Set models. // validate Content-Types unicity
strapi.models = Object.keys(strapi.api || []).reduce((acc, apiName) => { const allApisSchemas = Object.values(strapi.api).flatMap(api => Object.values(api.models));
validateContentTypesUnicity(allApisSchemas);
// TODO: to change with new loading system
// Register api content types
for (const apiName in strapi.api) {
const api = strapi.api[apiName]; const api = strapi.api[apiName];
for (let modelName in api.models) { const v4ContentTypes = _.mapValues(api.models, (model, modelName) => {
let model = strapi.api[apiName].models[modelName]; model.info.displayName = model.info.displayName || model.info.name;
model.info.singularName = model.info.singularName || modelName;
model.info.pluralName = model.info.pluralName || pluralize(modelName);
// mutate model return {
const ct = {
schema: model, schema: model,
actions: {}, actions: {},
lifecycles: {}, lifecycles: {},
}; };
});
ct.schema.info.displayName = model.info.name; strapi.container.get('content-types').add(`api::${apiName}`, v4ContentTypes);
ct.schema.info.singularName = modelName; }
ct.schema.info.pluralName = pluralize(modelName);
strapi.container.get('content-types').add(`api::${apiName}`, { // TODO: remove v3
[ct.schema.info.singularName]: ct, // Set models.
}); strapi.models = {};
// TODO: check unicity of pluralName and singularName amongs user's CT for (const apiName in strapi.api) {
// const validateContentTypesUnicity = (...contentTypesMaps) => { const api = strapi.api[apiName];
// const names = []; for (let modelName in api.models) {
// const arrayOfAllContentTypes = flatten(contentTypesMaps.map(values)); let model = api.models[modelName];
// console.log('arrayOfAllContentTypes', arrayOfAllContentTypes.map(ct => ct.schema.info.singularName)); const contentType = strapi.container
// arrayOfAllContentTypes.forEach(ct => {
// const singularName = kebabCase(ct.schema.info.singularName);
// const pluralName = kebabCase(ct.schema.info.pluralName);
// if (names.includes(singularName)) {
// throw new Error(`The singular name "${ct.schema.info.singularName}" should be unique`);
// }
// names.push(singularName);
// if (names.includes(pluralName)) {
// throw new Error(`The plural name "${ct.schema.info.pluralName}" should be unique`);
// }
// names.push(pluralName);
// });
// };
const createdContentType = strapi.container
.get('content-types') .get('content-types')
.get(`api::${apiName}.${ct.schema.info.singularName}`); .get(`api::${apiName}.${model.info.singularName}`);
Object.assign(model, createdContentType.schema); Object.assign(model, contentType.schema);
strapi.contentTypes[model.uid] = model; strapi.contentTypes[model.uid] = contentType.schema;
strapi.models[modelName] = model;
}
}
// set default services and default controllers
for (const apiName in strapi.api) {
const api = strapi.api[apiName];
for (const modelName in api.models) {
const model = api.models[modelName];
const { service, controller } = createCoreApi({ model, api, strapi }); const { service, controller } = createCoreApi({ model, api, strapi });
_.set(strapi.api[apiName], ['services', modelName], service); _.set(strapi.api[apiName], ['services', modelName], service);
_.set(strapi.api[apiName], ['controllers', modelName], controller); _.set(strapi.api[apiName], ['controllers', modelName], controller);
acc[modelName] = model;
} }
return acc; }
}, {});
// Set controllers. // Set user's controllers.
strapi.controllers = Object.keys(strapi.api || []).reduce((acc, apiName) => { strapi.controllers = Object.keys(strapi.api || []).reduce((acc, apiName) => {
strapi.container.get('controllers').add(`api::${apiName}`, strapi.api[apiName].controllers); strapi.container.get('controllers').add(`api::${apiName}`, strapi.api[apiName].controllers);
for (let controllerName in strapi.api[apiName].controllers) { for (let controllerName in strapi.api[apiName].controllers) {
@ -78,7 +95,7 @@ module.exports = function(strapi) {
return acc; return acc;
}, {}); }, {});
// Set services. // Set user's services.
strapi.services = Object.keys(strapi.api || []).reduce((acc, apiName) => { strapi.services = Object.keys(strapi.api || []).reduce((acc, apiName) => {
strapi.container.get('services').add(`api::${apiName}`, strapi.api[apiName].services); strapi.container.get('services').add(`api::${apiName}`, strapi.api[apiName].services);
for (let serviceName in strapi.api[apiName].services) { for (let serviceName in strapi.api[apiName].services) {