diff --git a/src/api/page/content-types/page/schema.json b/src/api/page/content-types/page/schema.json index 65861b2..86c2c79 100644 --- a/src/api/page/content-types/page/schema.json +++ b/src/api/page/content-types/page/schema.json @@ -45,7 +45,7 @@ } }, "component": "shared.hero", - "required": true + "required": false }, "content": { "pluginOptions": { diff --git a/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/src/extensions/documentation/documentation/1.0.0/full_documentation.json index 725ae85..67f778a 100644 --- a/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "x-generation-date": "2024-11-01T10:19:23.797Z" + "x-generation-date": "2024-11-01T12:34:04.379Z" }, "x-strapi-config": { "plugins": [ @@ -9083,8 +9083,7 @@ "data": { "required": [ "identifier", - "slug", - "Hero" + "slug" ], "type": "object", "properties": { @@ -9197,8 +9196,7 @@ "type": "object", "required": [ "identifier", - "slug", - "Hero" + "slug" ], "properties": { "id": { diff --git a/src/middlewares/deepPopulate.ts b/src/middlewares/deepPopulate.ts index 7eb8d7e..18abee7 100644 --- a/src/middlewares/deepPopulate.ts +++ b/src/middlewares/deepPopulate.ts @@ -1,26 +1,41 @@ /** - * `deepPopulate` middleware - */ - +*deepPopulate middleware +*/ import type { Core } from '@strapi/strapi'; import { UID } from '@strapi/types'; import { contentTypes } from '@strapi/utils'; import pluralize from 'pluralize'; - interface Options { /** - * Fields to select when populating relations - */ + * Fields to select when populating relations + */ relationalFields?: string[]; } - const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants; const extractPathSegment = (url: string) => url.match(/\/([^/?]+)(?:\?|$)/)?.[1] || ''; +const getModelUID = (path: string): UID.Schema => { + // Special handling for users-permissions routes + if (path === 'users' || path === 'me') { + return 'plugin::users-permissions.user'; + } + + // Logic for regular content types + const singular = pluralize.singular(path); + return `api::${singular}.${singular}` as UID.Schema; +}; + const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => { const model = strapi.getModel(uid); + + // Add safety check + if (!model || !model.attributes) { + console.warn(`Model not found for UID: ${uid}`); + return {}; + } + const attributes = Object.entries(model.attributes); return attributes.reduce((acc: any, [attributeName, attribute]) => { @@ -84,14 +99,17 @@ export default (config, { strapi }: { strapi: Core.Strapi }) => { if (ctx.request.url.startsWith('/api/') && ctx.request.method === 'GET' && !ctx.query.populate) { strapi.log.info('Using custom Dynamic-Zone population Middleware...'); - const contentType = extractPathSegment(ctx.request.url); - const singular = pluralize.singular(contentType) - const uid = `api::${singular}.${singular}`; + const pathSegment = extractPathSegment(ctx.request.url); + const uid = getModelUID(pathSegment); // @ts-ignores - ctx.query.populate = getDeepPopulate(uid); + try { + ctx.query.populate = getDeepPopulate(uid); + } catch (error) { + console.error(`Error in deepPopulate middleware for path ${pathSegment}:`, error); + // Continue without population if there's an error + } } await next(); }; -}; - +}; \ No newline at end of file diff --git a/types/generated/contentTypes.d.ts b/types/generated/contentTypes.d.ts index 1942951..4f5e78c 100644 --- a/types/generated/contentTypes.d.ts +++ b/types/generated/contentTypes.d.ts @@ -690,7 +690,6 @@ export interface ApiPagePage extends Struct.CollectionTypeSchema { createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & Schema.Attribute.Private; Hero: Schema.Attribute.Component<'shared.hero', false> & - Schema.Attribute.Required & Schema.Attribute.SetPluginOptions<{ i18n: { localized: true;