mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
use format schema, actions, lifecycles
This commit is contained in:
parent
323bf21f43
commit
dee087a9be
@ -335,8 +335,13 @@ class Strapi {
|
||||
}
|
||||
});
|
||||
|
||||
const pluginProvider = await createPluginProvider(this);
|
||||
this.plugin = pluginProvider;
|
||||
try {
|
||||
const pluginProvider = await createPluginProvider(this);
|
||||
this.plugin = pluginProvider;
|
||||
console.log(this.plugin('i18n').contentType.getAll());
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
// const modules = await loadModules(this);
|
||||
|
||||
|
@ -4,37 +4,37 @@ const { cloneDeep } = require('lodash/fp');
|
||||
const { validateContentTypeDefinition } = require('./validator');
|
||||
|
||||
const createContentType = (definition, { apiName, pluginName } = {}) => {
|
||||
const createdContentType = cloneDeep(definition);
|
||||
|
||||
validateContentTypeDefinition(definition);
|
||||
|
||||
const createdContentType = cloneDeep(definition);
|
||||
|
||||
if (apiName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `application::${apiName}.${definition.info.singularName}`,
|
||||
Object.assign(createdContentType.schema, {
|
||||
uid: `application::${apiName}.${definition.schema.info.singularName}`,
|
||||
apiName,
|
||||
collectionName: definition.collectionName || definition.info.singularName,
|
||||
collectionName: definition.schema.collectionName || definition.schema.info.singularName,
|
||||
});
|
||||
} else if (pluginName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `plugins::${pluginName}.${definition.info.singularName}`,
|
||||
Object.assign(createdContentType.schema, {
|
||||
uid: `plugins::${pluginName}.${definition.schema.info.singularName}`,
|
||||
plugin: pluginName,
|
||||
collectionName:
|
||||
createdContentType.collectionName ||
|
||||
`${pluginName}_${definition.info.singularName}`.toLowerCase(),
|
||||
createdContentType.schema.collectionName ||
|
||||
`${pluginName}_${definition.schema.info.singularName}`.toLowerCase(),
|
||||
});
|
||||
} else {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `strapi::${definition.info.singularName}`,
|
||||
Object.assign(createdContentType.schema, {
|
||||
uid: `strapi::${definition.schema.info.singularName}`,
|
||||
plugin: 'admin',
|
||||
});
|
||||
}
|
||||
|
||||
Object.assign(createdContentType, {
|
||||
kind: createdContentType.kind || 'collectionType',
|
||||
Object.assign(createdContentType.schema, {
|
||||
kind: createdContentType.schema.kind || 'collectionType',
|
||||
});
|
||||
Object.defineProperty(createdContentType, 'privateAttributes', {
|
||||
Object.defineProperty(createdContentType.schema, 'privateAttributes', {
|
||||
get() {
|
||||
return strapi.getModel(createdContentType.uid).privateAttributes;
|
||||
return strapi.getModel(createdContentType.schema.uid).privateAttributes;
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -1,22 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
const { keyBy, mapValues } = require('lodash');
|
||||
const { yup } = require('@strapi/utils');
|
||||
|
||||
// To replace by directly using implemented lifecycles
|
||||
const lifecycles = ['beforeCreate', 'afterCreate'];
|
||||
const lifecyclesShape = mapValues(keyBy(lifecycles), () => yup.mixed().isFunction());
|
||||
|
||||
const contentTypeSchemaValidator = yup.object().shape({
|
||||
info: yup
|
||||
schema: yup.object().shape({
|
||||
info: yup
|
||||
.object()
|
||||
.shape({
|
||||
singularName: yup
|
||||
.string()
|
||||
.isCamelCase()
|
||||
.required(),
|
||||
pluralName: yup
|
||||
.string()
|
||||
.isCamelCase()
|
||||
.required(),
|
||||
displayName: yup.string().required(),
|
||||
})
|
||||
.required(),
|
||||
}),
|
||||
actions: yup.object().onlyContainsFunctions(),
|
||||
lifecycles: yup
|
||||
.object()
|
||||
.shape({
|
||||
singularName: yup
|
||||
.string()
|
||||
.isCamelCase()
|
||||
.required(),
|
||||
pluralName: yup
|
||||
.string()
|
||||
.isCamelCase()
|
||||
.required(),
|
||||
displayName: yup.string().required(),
|
||||
})
|
||||
.required(),
|
||||
.shape(lifecyclesShape)
|
||||
.noUnknown(),
|
||||
});
|
||||
|
||||
const validateContentTypeDefinition = data => {
|
||||
|
@ -5,8 +5,8 @@ const { createContentType } = require('../domain/content-type');
|
||||
module.exports = (pluginName, contentTypeDefinitions) => {
|
||||
const contentTypes = contentTypeDefinitions.map(ct => createContentType(ct, { pluginName }));
|
||||
const contentTypesMap = contentTypes.reduce((map, ct) => {
|
||||
map[ct.info.singularName] = ct;
|
||||
map[ct.info.pluralName] = ct;
|
||||
map[ct.schema.info.singularName] = ct;
|
||||
map[ct.schema.info.pluralName] = ct;
|
||||
return map;
|
||||
}, {});
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { yup, kebabCase } = require('@strapi/utils');
|
||||
const { kebabCase } = require('lodash/fp');
|
||||
const { yup } = require('@strapi/utils');
|
||||
|
||||
const strapiServerSchema = yup
|
||||
.object()
|
||||
@ -38,14 +39,14 @@ const validateStrapiServer = data => {
|
||||
const validateContentTypesUnicity = contentTypes => {
|
||||
const names = [];
|
||||
contentTypes.forEach(ct => {
|
||||
const singularName = kebabCase(ct.info.singularName);
|
||||
const pluralName = kebabCase(ct.info.pluralName);
|
||||
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.info.singularName}" should be unique`);
|
||||
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.info.pluralName}" should be unique`);
|
||||
throw new Error(`The plural name "${ct.schema.info.pluralName}" should be unique`);
|
||||
}
|
||||
names.push(pluralName);
|
||||
});
|
||||
|
@ -28,11 +28,20 @@ function isCamelCase(message = '${path} is not in camel case (anExampleOfCamelCa
|
||||
return this.test('is in camelCase', message, value => value === _.camelCase(value));
|
||||
}
|
||||
|
||||
function onlyContainsFunctions(message = '${path} contains values that are not functions') {
|
||||
return this.test(
|
||||
'only contains functions',
|
||||
message,
|
||||
value => value && Object.values(value).every(_.isFunction)
|
||||
);
|
||||
}
|
||||
|
||||
yup.addMethod(yup.mixed, 'notNil', isNotNill);
|
||||
yup.addMethod(yup.mixed, 'notNull', isNotNull);
|
||||
yup.addMethod(yup.array, 'requiredAllowEmpty', arrayRequiredAllowEmpty);
|
||||
yup.addMethod(yup.mixed, 'isFunction', isFunction);
|
||||
yup.addMethod(yup.string, 'isCamelCase', isCamelCase);
|
||||
yup.addMethod(yup.object, 'onlyContainsFunctions', onlyContainsFunctions);
|
||||
|
||||
class StrapiIDSchema extends MixedSchemaType {
|
||||
constructor() {
|
||||
|
14
packages/plugins/i18n/server/content-types/locale/actions.js
Normal file
14
packages/plugins/i18n/server/content-types/locale/actions.js
Normal file
@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
// find() {},
|
||||
// create() {},
|
||||
// findPage() {},
|
||||
// findWithRelationCounts() {},
|
||||
// findOne() {},
|
||||
// update() {},
|
||||
// delete() {},
|
||||
// search() {},
|
||||
// searchWithRelationCounts() {},
|
||||
// searchPage() {},
|
||||
};
|
11
packages/plugins/i18n/server/content-types/locale/index.js
Normal file
11
packages/plugins/i18n/server/content-types/locale/index.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const schema = require('./schema');
|
||||
const actions = require('./actions');
|
||||
const lifecycles = require('./lifecycles');
|
||||
|
||||
module.exports = {
|
||||
schema,
|
||||
actions,
|
||||
lifecycles,
|
||||
};
|
@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
module.exports = {
|
||||
beforeCreate() {},
|
||||
afterCreate() {},
|
||||
};
|
@ -1,64 +0,0 @@
|
||||
{
|
||||
"routes": [
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/iso-locales",
|
||||
"handler": "iso-locales.listIsoLocales",
|
||||
"config": {
|
||||
"policies": [
|
||||
"admin::isAuthenticatedAdmin",
|
||||
["plugins::content-manager.hasPermissions", ["plugins::i18n.locale.read"]]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/locales",
|
||||
"handler": "locales.listLocales",
|
||||
"config": {
|
||||
"policies": ["admin::isAuthenticatedAdmin"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/locales",
|
||||
"handler": "locales.createLocale",
|
||||
"config": {
|
||||
"policies": [
|
||||
"admin::isAuthenticatedAdmin",
|
||||
["plugins::content-manager.hasPermissions", ["plugins::i18n.locale.create"]]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "PUT",
|
||||
"path": "/locales/:id",
|
||||
"handler": "locales.updateLocale",
|
||||
"config": {
|
||||
"policies": [
|
||||
"admin::isAuthenticatedAdmin",
|
||||
["plugins::content-manager.hasPermissions", ["plugins::i18n.locale.update"]]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "DELETE",
|
||||
"path": "/locales/:id",
|
||||
"handler": "locales.deleteLocale",
|
||||
"config": {
|
||||
"policies": [
|
||||
"admin::isAuthenticatedAdmin",
|
||||
["plugins::content-manager.hasPermissions", ["plugins::i18n.locale.delete"]]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/content-manager/actions/get-non-localized-fields",
|
||||
"handler": "content-types.getNonLocalizedAttributes",
|
||||
"config": {
|
||||
"policies": ["admin::isAuthenticatedAdmin"]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
64
packages/plugins/i18n/server/routes/index.js
Normal file
64
packages/plugins/i18n/server/routes/index.js
Normal file
@ -0,0 +1,64 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/iso-locales',
|
||||
handler: 'iso-locales.listIsoLocales',
|
||||
config: {
|
||||
policies: [
|
||||
'admin::isAuthenticatedAdmin',
|
||||
['plugins::content-manager.hasPermissions', ['plugins::i18n.locale.read']],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/locales',
|
||||
handler: 'locales.listLocales',
|
||||
config: {
|
||||
policies: ['admin::isAuthenticatedAdmin'],
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/locales',
|
||||
handler: 'locales.createLocale',
|
||||
config: {
|
||||
policies: [
|
||||
'admin::isAuthenticatedAdmin',
|
||||
['plugins::content-manager.hasPermissions', ['plugins::i18n.locale.create']],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'PUT',
|
||||
path: '/locales/:id',
|
||||
handler: 'locales.updateLocale',
|
||||
config: {
|
||||
policies: [
|
||||
'admin::isAuthenticatedAdmin',
|
||||
['plugins::content-manager.hasPermissions', ['plugins::i18n.locale.update']],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'DELETE',
|
||||
path: '/locales/:id',
|
||||
handler: 'locales.deleteLocale',
|
||||
config: {
|
||||
policies: [
|
||||
'admin::isAuthenticatedAdmin',
|
||||
['plugins::content-manager.hasPermissions', ['plugins::i18n.locale.delete']],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/content-manager/actions/get-non-localized-fields',
|
||||
handler: 'content-types.getNonLocalizedAttributes',
|
||||
config: {
|
||||
policies: ['admin::isAuthenticatedAdmin'],
|
||||
},
|
||||
},
|
||||
];
|
@ -4,27 +4,46 @@ const bootstrap = require('./server/bootstrap');
|
||||
const contentTypes = require('./server/content-types');
|
||||
const policies = require('./server/policies');
|
||||
const services = require('./server/services');
|
||||
// const routes = require('./server/routes');
|
||||
|
||||
// object or function. If function then pass strapi.
|
||||
module.exports = () => {
|
||||
return {
|
||||
bootstrap,
|
||||
// register,
|
||||
destroy: () => console.log('i18n DESTROY'),
|
||||
config: {
|
||||
default: () => ({
|
||||
olala: 'olala',
|
||||
pouet: 'pouet',
|
||||
featureA: true,
|
||||
}),
|
||||
validator: () => {},
|
||||
register: () => {
|
||||
// extend entityService
|
||||
// route.add('/giveBestCountries', { action: giveBestCountries });
|
||||
// route.add('/giveBestCountries', [policies.get('plugins::users-permissions.permissions')],
|
||||
// handler: giveBestCountries,
|
||||
// });
|
||||
// route.add('/giveBestCountries', (ctx, { }) => {
|
||||
// ctx.entityService('countries').giveBestCountries();
|
||||
// });
|
||||
//
|
||||
// addQuery('giveBestCountries', {
|
||||
// args: ,
|
||||
// resolve: ,
|
||||
// type: ,
|
||||
// });
|
||||
//
|
||||
// registerRoute('/countries', {
|
||||
// method: 'get',
|
||||
// handler: () => {},
|
||||
// })
|
||||
},
|
||||
routes: [],
|
||||
controllers: {},
|
||||
bootstrap,
|
||||
// routes,
|
||||
// controllers: {},
|
||||
middlewares: {},
|
||||
contentTypes,
|
||||
policies,
|
||||
services,
|
||||
hooks: {},
|
||||
// middlewares,
|
||||
};
|
||||
};
|
||||
|
||||
// create, update, delete, read
|
||||
|
||||
// modifier une route existance CRUD
|
||||
// Ajouter des nouvelles routes / query graphql
|
||||
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user