From f4acc5799c780424077c010423e1574e49987c6f Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 5 Nov 2024 14:03:36 +0100 Subject: [PATCH] pagination on custom routes --- src/api/article/services/article.ts | 2 +- .../collection-lookup/schema.json | 10 +++++++ src/api/event/services/event.ts | 2 +- src/api/operation/services/operation.ts | 2 +- .../1.0.0/full_documentation.json | 28 +++++++++++++++++-- src/middlewares/deepPopulate.ts | 7 +++-- types/generated/contentTypes.d.ts | 6 ++++ 7 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/api/article/services/article.ts b/src/api/article/services/article.ts index 53ddf79..92f3978 100644 --- a/src/api/article/services/article.ts +++ b/src/api/article/services/article.ts @@ -34,7 +34,7 @@ export default factories.createCoreService('api::article.article', ({strapi}) => } }, limit: pageSize, - offset: (page - 1) * pageSize, + page: page, orderBy: { date: 'desc' }, }); diff --git a/src/api/collection-lookup/content-types/collection-lookup/schema.json b/src/api/collection-lookup/content-types/collection-lookup/schema.json index e86c6c7..80508ba 100644 --- a/src/api/collection-lookup/content-types/collection-lookup/schema.json +++ b/src/api/collection-lookup/content-types/collection-lookup/schema.json @@ -31,6 +31,16 @@ "type": "boolean", "default": false, "required": true + }, + "numbered_item": { + "type": "boolean", + "default": false, + "required": true + }, + "inverse_count": { + "type": "boolean", + "default": false, + "required": true } } } diff --git a/src/api/event/services/event.ts b/src/api/event/services/event.ts index 515d88a..e5e9b25 100644 --- a/src/api/event/services/event.ts +++ b/src/api/event/services/event.ts @@ -34,7 +34,7 @@ export default factories.createCoreService('api::event.event', ({strapi}) => ({ } }, limit: pageSize, - offset: (page - 1) * pageSize, + page: page, orderBy: { date: 'desc' }, }); diff --git a/src/api/operation/services/operation.ts b/src/api/operation/services/operation.ts index 9e3791e..12aa6b3 100644 --- a/src/api/operation/services/operation.ts +++ b/src/api/operation/services/operation.ts @@ -34,7 +34,7 @@ export default factories.createCoreService('api::operation.operation', ({strapi} } }, limit: pageSize, - offset: (page - 1) * pageSize, + page: page, orderBy: { date: 'desc' }, }); 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 96ec17e..cd21ea0 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-05T07:32:30.586Z" + "x-generation-date": "2024-11-05T13:02:13.660Z" }, "x-strapi-config": { "plugins": [ @@ -6444,7 +6444,9 @@ "reference", "collection", "image_item", - "date_list" + "date_list", + "numbered_item", + "inverse_count" ], "type": "object", "properties": { @@ -6460,6 +6462,12 @@ "date_list": { "type": "boolean" }, + "numbered_item": { + "type": "boolean" + }, + "inverse_count": { + "type": "boolean" + }, "locale": { "type": "string" }, @@ -6522,7 +6530,9 @@ "reference", "collection", "image_item", - "date_list" + "date_list", + "numbered_item", + "inverse_count" ], "properties": { "id": { @@ -6543,6 +6553,12 @@ "date_list": { "type": "boolean" }, + "numbered_item": { + "type": "boolean" + }, + "inverse_count": { + "type": "boolean" + }, "createdAt": { "type": "string", "format": "date-time" @@ -6855,6 +6871,12 @@ "date_list": { "type": "boolean" }, + "numbered_item": { + "type": "boolean" + }, + "inverse_count": { + "type": "boolean" + }, "createdAt": { "type": "string", "format": "date-time" diff --git a/src/middlewares/deepPopulate.ts b/src/middlewares/deepPopulate.ts index 597bfd7..7c1590b 100644 --- a/src/middlewares/deepPopulate.ts +++ b/src/middlewares/deepPopulate.ts @@ -4,6 +4,7 @@ import type { Core } from '@strapi/strapi'; import { UID } from '@strapi/types'; import { contentTypes } from '@strapi/utils'; +import path from 'path'; import pluralize from 'pluralize'; interface Options { @@ -94,13 +95,15 @@ 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 pathSegment = extractPathSegment(ctx.request.url); + let pathSegment = extractPathSegment(ctx.request.url); + if(ctx.request.url.startsWith('/api/custom/')) { + pathSegment = ctx.request.url.split("/")[3] + } const uid = getModelUID(pathSegment); // @ts-ignores try { ctx.query.populate = getDeepPopulate(uid); - console.log(JSON.stringify(ctx.query.populate)) } catch (error) { console.error(`Error in deepPopulate middleware for path ${pathSegment}:`, error); // Continue without population if there's an error diff --git a/types/generated/contentTypes.d.ts b/types/generated/contentTypes.d.ts index f932f66..be72687 100644 --- a/types/generated/contentTypes.d.ts +++ b/types/generated/contentTypes.d.ts @@ -429,12 +429,18 @@ export interface ApiCollectionLookupCollectionLookup image_item: Schema.Attribute.Boolean & Schema.Attribute.Required & Schema.Attribute.DefaultTo; + inverse_count: Schema.Attribute.Boolean & + Schema.Attribute.Required & + Schema.Attribute.DefaultTo; locale: Schema.Attribute.String & Schema.Attribute.Private; localizations: Schema.Attribute.Relation< 'oneToMany', 'api::collection-lookup.collection-lookup' > & Schema.Attribute.Private; + numbered_item: Schema.Attribute.Boolean & + Schema.Attribute.Required & + Schema.Attribute.DefaultTo; publishedAt: Schema.Attribute.DateTime; reference: Schema.Attribute.String & Schema.Attribute.Required &