schema and populate
This commit is contained in:
parent
1943224f33
commit
9c8037277f
4 changed files with 35 additions and 20 deletions
|
@ -45,7 +45,7 @@
|
|||
}
|
||||
},
|
||||
"component": "shared.hero",
|
||||
"required": true
|
||||
"required": false
|
||||
},
|
||||
"content": {
|
||||
"pluginOptions": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
};
|
||||
|
||||
};
|
1
types/generated/contentTypes.d.ts
vendored
1
types/generated/contentTypes.d.ts
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue