content-manager: list content types on kind

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-01-20 17:35:04 +01:00
parent dce6bd432c
commit 90408d5119
3 changed files with 50 additions and 5 deletions

View File

@ -1,12 +1,25 @@
'use strict';
const { createModelConfigurationSchema } = require('./validation');
const _ = require('lodash');
const {
createModelConfigurationSchema,
validateKind,
} = require('./validation');
module.exports = {
/**
* Returns the list of available content types
*/
listContentTypes(ctx) {
async listContentTypes(ctx) {
const { kind } = ctx.query;
try {
await validateKind(kind);
} catch (error) {
return ctx.send({ error }, 400);
}
const service = strapi.plugins['content-manager'].services.contenttypes;
const contentTypes = Object.keys(strapi.contentTypes)
@ -14,6 +27,13 @@ module.exports = {
if (uid.startsWith('strapi::')) return false;
if (uid === 'plugins::upload.file') return false;
if (
kind &&
_.get(strapi.contentTypes[uid], 'kind', 'collectionType') !== kind
) {
return false;
}
return true;
})
.map(uid => {

View File

@ -1,3 +1,24 @@
module.exports = {
createModelConfigurationSchema: require('./model-configuration'),
'use strict';
const yup = require('yup');
const createModelConfigurationSchema = require('./model-configuration');
const TYPES = ['singleType', 'collectionType'];
/**
* Validates type kind
*/
const validateKind = kind => {
return yup
.string()
.oneOf(TYPES)
.nullable()
.validate(kind);
// .catch(error => Promise.reject(formatYupErrors(error)));
};
module.exports = {
createModelConfigurationSchema,
validateKind,
};

View File

@ -49,12 +49,16 @@ const deleteConfiguration = uid => {
const formatContentType = contentType => {
return {
uid: contentType.uid,
name: _.get(contentType, ['info', 'name']),
label: formatContentTypeLabel(
_.get(contentType, ['info', 'name'], contentType.modelName)
),
isDisplayed: HIDDEN_CONTENT_TYPES.includes(contentType.uid) ? false : true,
schema: formatContentTypeSchema(contentType),
schema: {
...formatContentTypeSchema(contentType),
kind: contentType.kind || 'collectionType',
},
};
};