feat: proxy content type in controller factory

This commit is contained in:
Marc-Roig 2023-08-24 11:41:02 +02:00
parent 307122ead0
commit 214bb4fe23
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249

View File

@ -6,10 +6,20 @@ const { createController } = require('./core-api/controller');
const { createService } = require('./core-api/service');
const { createRoutes } = require('./core-api/routes');
// Content type is proxied to allow for dynamic content type updates
const getContentTypeProxy = (strapi, uid) => {
return new Proxy(strapi.contentType(uid), {
get(target, prop) {
const contentType = strapi.contentType(uid);
return contentType[prop];
},
});
};
const createCoreController = (uid, cfg = {}) => {
return ({ strapi }) => {
const baseController = createController({
contentType: strapi.contentType(uid),
contentType: getContentTypeProxy(strapi, uid),
});
const userCtrl = typeof cfg === 'function' ? cfg({ strapi }) : cfg;
@ -28,7 +38,7 @@ const createCoreController = (uid, cfg = {}) => {
const createCoreService = (uid, cfg = {}) => {
return ({ strapi }) => {
const baseService = createService({
contentType: strapi.contentType(uid),
contentType: getContentTypeProxy(strapi, uid),
});
const userService = typeof cfg === 'function' ? cfg({ strapi }) : cfg;