strapi-layout-plugin 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "strapi-layout-plugin",
3
+ "version": "1.0.0",
4
+ "description": "Strapi layout engine plugin for Sitecore-like routing",
5
+ "strapi": {
6
+ "name": "strapi-layout-plugin",
7
+ "description": "Strapi layout engine plugin for Sitecore-like routing",
8
+ "kind": "plugin"
9
+ },
10
+ "dependencies": {},
11
+ "engines": {
12
+ "node": ">=18.0.0",
13
+ "npm": ">=6.0.0"
14
+ },
15
+ "main": "./strapi-server.js"
16
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const layout = require('./layout');
4
+
5
+ module.exports = {
6
+ layout,
7
+ };
@@ -0,0 +1,292 @@
1
+ 'use strict';
2
+
3
+ const getComponentPopulate = (strapi, componentUid, depth = 0) => {
4
+ if (depth > 2) return '*';
5
+ const component = strapi.components[componentUid];
6
+ if (!component) return '*';
7
+
8
+ const populate = {};
9
+ const attributes = component.attributes || {};
10
+
11
+ for (const [attrName, attrDef] of Object.entries(attributes)) {
12
+ const type = attrDef.type;
13
+ if (type === 'media') {
14
+ populate[attrName] = true;
15
+ } else if (type === 'component') {
16
+ const targetComp = attrDef.component;
17
+ populate[attrName] = {
18
+ populate: getComponentPopulate(strapi, targetComp, depth + 1)
19
+ };
20
+ } else if (type === 'relation') {
21
+ populate[attrName] = { populate: '*' };
22
+ }
23
+ }
24
+ return Object.keys(populate).length > 0 ? populate : '*';
25
+ };
26
+
27
+ const getDynamicSectionsPopulate = (strapi) => {
28
+ const populate = {};
29
+ const components = strapi.components;
30
+
31
+ for (const [uid, schema] of Object.entries(components)) {
32
+ if (uid.startsWith('velox.')) {
33
+ const attributes = schema.attributes || {};
34
+ const populateMap = {};
35
+
36
+ for (const [attrName, attrDef] of Object.entries(attributes)) {
37
+ const type = attrDef.type;
38
+
39
+ if (type === 'media') {
40
+ populateMap[attrName] = true;
41
+ } else if (type === 'component') {
42
+ const targetComp = attrDef.component;
43
+ populateMap[attrName] = {
44
+ populate: getComponentPopulate(strapi, targetComp)
45
+ };
46
+ } else if (type === 'dynamiczone') {
47
+ populateMap[attrName] = { populate: '*' };
48
+ } else if (type === 'relation' && attrDef.target) {
49
+ const targetUid = attrDef.target;
50
+ const targetModel = strapi.contentTypes[targetUid];
51
+
52
+ if (targetModel) {
53
+ const targetAttributes = targetModel.attributes || {};
54
+ const targetPopulate = {};
55
+ let hasComplexFields = false;
56
+
57
+ for (const [targetAttrName, targetAttrDef] of Object.entries(targetAttributes)) {
58
+ if (targetAttrDef.type === 'component') {
59
+ const targetComp = targetAttrDef.component;
60
+ targetPopulate[targetAttrName] = {
61
+ populate: getComponentPopulate(strapi, targetComp)
62
+ };
63
+ hasComplexFields = true;
64
+ } else if (targetAttrDef.type === 'dynamiczone') {
65
+ targetPopulate[targetAttrName] = { populate: '*' };
66
+ hasComplexFields = true;
67
+ } else if (targetAttrDef.type === 'media') {
68
+ targetPopulate[targetAttrName] = true;
69
+ hasComplexFields = true;
70
+ }
71
+ }
72
+
73
+ if (hasComplexFields) {
74
+ populateMap[attrName] = { populate: targetPopulate };
75
+ } else {
76
+ populateMap[attrName] = { populate: '*' };
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ if (Object.keys(populateMap).length > 0) {
83
+ populate[uid] = { populate: populateMap };
84
+ } else {
85
+ populate[uid] = { populate: '*' };
86
+ }
87
+ }
88
+ }
89
+ return populate;
90
+ };
91
+
92
+ module.exports = ({ strapi }) => ({
93
+ async getLayout(ctx) {
94
+ const { slug } = ctx.params;
95
+
96
+ try {
97
+ const requestedLocale = ctx.query.locale || 'en';
98
+ let availableLocales = [];
99
+ try {
100
+ if (strapi.plugin('i18n')) {
101
+ availableLocales = await strapi.plugin('i18n').service('locales').find();
102
+ }
103
+ } catch (e) {
104
+ console.warn('Layout Plugin: Could not fetch locales', e);
105
+ }
106
+
107
+ console.log('Layout Plugin: Processing slug:', slug);
108
+
109
+ let slugVariations = [
110
+ slug,
111
+ slug.startsWith('/') ? slug.substring(1) : `/${slug}`,
112
+ slug.endsWith('/') ? slug.slice(0, -1) : slug
113
+ ].filter(Boolean);
114
+
115
+ let page = null;
116
+ const pages = await strapi.entityService.findMany('api::page.page', {
117
+ locale: requestedLocale,
118
+ filters: {
119
+ slug: {
120
+ $in: slugVariations
121
+ }
122
+ },
123
+ populate: {
124
+ seo: { populate: '*' },
125
+ sections: {
126
+ on: getDynamicSectionsPopulate(strapi)
127
+ },
128
+ },
129
+ });
130
+
131
+ if (pages && pages.length > 0) {
132
+ page = pages[0];
133
+ }
134
+
135
+ if (!page) {
136
+ let childSlug = slug;
137
+ let parentSlug = '';
138
+
139
+ if (slug.includes('/')) {
140
+ const parts = slug.split('/');
141
+ childSlug = parts.pop();
142
+ parentSlug = parts.join('/');
143
+ }
144
+
145
+ let allowedContentType = null;
146
+
147
+ if (parentSlug) {
148
+ const parentPage = await strapi.entityService.findMany('api::page.page', {
149
+ filters: { slug: parentSlug },
150
+ limit: 1,
151
+ populate: { sections: true }
152
+ });
153
+
154
+ if (parentPage && parentPage.length > 0) {
155
+ const p = parentPage[0];
156
+ if (p.relatedContentType) {
157
+ allowedContentType = p.relatedContentType;
158
+ } else if (p.sections && Array.isArray(p.sections)) {
159
+ for (const section of p.sections) {
160
+ const compName = section.__component;
161
+ if (compName && compName.endsWith('-list')) {
162
+ const parts = compName.split('.');
163
+ if (parts.length > 1) {
164
+ const featureName = parts[1].replace(/-list$/, '');
165
+ const candidateUid = `api::${featureName}.${featureName}`;
166
+ if (strapi.contentTypes[candidateUid]) {
167
+ allowedContentType = candidateUid;
168
+ break;
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ if (!allowedContentType) {
176
+ for (const [uid, ct] of Object.entries(strapi.contentTypes)) {
177
+ if (!uid.startsWith('api::')) continue;
178
+ const info = ct.info || {};
179
+ if (info.pluralName === parentSlug || ct.collectionName === parentSlug || info.singularName === parentSlug) {
180
+ allowedContentType = uid;
181
+ break;
182
+ }
183
+ }
184
+ }
185
+ }
186
+ }
187
+
188
+ let routableTypes = [];
189
+ if (allowedContentType) {
190
+ routableTypes = [allowedContentType];
191
+ } else if (parentSlug) {
192
+ routableTypes = [];
193
+ }
194
+
195
+ for (const uid of routableTypes) {
196
+ if (!strapi.contentTypes[uid]) continue;
197
+
198
+ const entities = await strapi.entityService.findMany(uid, {
199
+ locale: requestedLocale,
200
+ filters: { slug: childSlug },
201
+ populate: (function() {
202
+ const model = strapi.contentTypes[uid];
203
+ const attributes = model?.attributes || {};
204
+ const populate = {};
205
+ const candidates = ['section', 'sections', 'seo', 'image', 'banner', 'blocks'];
206
+ for (const candidate of candidates) {
207
+ if (attributes[candidate]) {
208
+ if (attributes[candidate].type === 'dynamiczone' || attributes[candidate].type === 'component') {
209
+ populate[candidate] = { populate: '*' };
210
+ } else if (attributes[candidate].type === 'media') {
211
+ populate[candidate] = true;
212
+ } else if (attributes[candidate].type === 'relation') {
213
+ populate[candidate] = { populate: '*' };
214
+ }
215
+ }
216
+ }
217
+ return populate;
218
+ })()
219
+ });
220
+
221
+ if (entities && entities.length > 0) {
222
+ const entity = entities[0];
223
+ const typeName = uid.split('.')[1];
224
+
225
+ let pageSections = [];
226
+ if (Array.isArray(entity.section)) {
227
+ pageSections = entity.section;
228
+ } else if (Array.isArray(entity.sections)) {
229
+ pageSections = entity.sections;
230
+ } else if (Array.isArray(entity.blocks)) {
231
+ pageSections = entity.blocks;
232
+ } else {
233
+ pageSections = [{
234
+ __component: `velox.${typeName}-detail`,
235
+ id: 0,
236
+ ...entity
237
+ }];
238
+ }
239
+
240
+ page = {
241
+ id: `${typeName}-${entity.id}`,
242
+ title: entity.title || entity.name || 'Untitled',
243
+ slug: slug,
244
+ sections: pageSections
245
+ };
246
+ break;
247
+ }
248
+ }
249
+ }
250
+
251
+ if (!page) {
252
+ return ctx.notFound('Page/Article not found');
253
+ }
254
+
255
+ const globalLayout = await strapi.entityService.findMany('api::layout.layout', {
256
+ locale: requestedLocale,
257
+ populate: {
258
+ defaultSeo: { populate: '*' },
259
+ header: { on: getDynamicSectionsPopulate(strapi) },
260
+ footer: { on: getDynamicSectionsPopulate(strapi) }
261
+ }
262
+ });
263
+
264
+ const siteName = globalLayout?.siteName || "Velox";
265
+ const language = page.locale || requestedLocale;
266
+ const { sections } = page;
267
+
268
+ ctx.body = {
269
+ strapi: {
270
+ context: {
271
+ pageEditing: false,
272
+ site: { name: siteName },
273
+ language: language,
274
+ locales: availableLocales
275
+ },
276
+ route: {
277
+ name: slug,
278
+ displayName: page.title,
279
+ placeholders: {
280
+ header: globalLayout?.header || [],
281
+ main: sections || [],
282
+ footer: globalLayout?.footer || []
283
+ }
284
+ },
285
+ },
286
+ };
287
+ } catch (err) {
288
+ console.error('Layout Plugin Error:', err);
289
+ return ctx.internalServerError('Internal Server Error', err);
290
+ }
291
+ },
292
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const controllers = require('./controllers');
4
+ const routes = require('./routes');
5
+
6
+ module.exports = {
7
+ controllers,
8
+ routes,
9
+ };
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ strapi }) => {
4
+ const extensionService = strapi.plugin('graphql').service('extension');
5
+
6
+ if (!extensionService) {
7
+ return;
8
+ }
9
+
10
+ extensionService.use(({ strapi }) => ({
11
+ typeDefs: `
12
+ type LayoutPlaceholder {
13
+ header: JSON
14
+ main: JSON
15
+ footer: JSON
16
+ }
17
+
18
+ type LayoutRoute {
19
+ name: String
20
+ displayName: String
21
+ placeholders: LayoutPlaceholder
22
+ }
23
+
24
+ type LayoutContext {
25
+ pageEditing: Boolean
26
+ site: JSON
27
+ language: String
28
+ locales: JSON
29
+ }
30
+
31
+ type LayoutResponse {
32
+ context: LayoutContext
33
+ route: LayoutRoute
34
+ }
35
+
36
+ extend type Query {
37
+ getLayout(slug: String!, locale: String): LayoutResponse
38
+ }
39
+ `,
40
+ resolvers: {
41
+ Query: {
42
+ getLayout: async (parent, args, context) => {
43
+ const { slug, locale } = args;
44
+
45
+ // Call the plugin's layout controller directly
46
+ const controller = strapi.plugin('strapi-layout-plugin').controller('layout');
47
+
48
+ // Mock Koa ctx for the controller
49
+ const mockCtx = {
50
+ params: { slug },
51
+ query: { locale: locale || 'en' },
52
+ notFound: (msg) => { throw new Error(msg || 'Not Found'); },
53
+ internalServerError: (msg, err) => { throw new Error(msg || 'Internal Server Error'); }
54
+ };
55
+
56
+ await controller.getLayout(mockCtx);
57
+
58
+ if (mockCtx.body && mockCtx.body.strapi) {
59
+ return mockCtx.body.strapi;
60
+ }
61
+
62
+ throw new Error('Failed to fetch layout via GraphQL');
63
+ },
64
+ },
65
+ },
66
+ resolversConfig: {
67
+ 'Query.getLayout': {
68
+ auth: false,
69
+ },
70
+ },
71
+ }));
72
+ };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const layout = require('./layout');
4
+
5
+ module.exports = {
6
+ layout,
7
+ };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ type: 'content-api',
5
+ routes: [
6
+ {
7
+ method: 'GET',
8
+ path: '/:slug(.*)',
9
+ handler: 'layout.getLayout',
10
+ config: {
11
+ auth: false,
12
+ },
13
+ },
14
+ ],
15
+ };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const register = require('./server/register');
4
+ const server = require('./server');
5
+
6
+ module.exports = {
7
+ register,
8
+ ...server,
9
+ };