From 62569afbca9d2abbd3b649eee115f6111215efbd Mon Sep 17 00:00:00 2001 From: Javad Mnjd Date: Fri, 24 May 2024 04:58:44 +0330 Subject: [PATCH 1/2] fix: handle unpaginated find response --- src/hooks/6-normalize-find.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/hooks/6-normalize-find.ts b/src/hooks/6-normalize-find.ts index 7c0ad75c..0bb68568 100644 --- a/src/hooks/6-normalize-find.ts +++ b/src/hooks/6-normalize-find.ts @@ -21,11 +21,20 @@ export function normalizeFind() { next && await next() if (context.method === 'find' && !context.result?.data) { - context.result = { - data: [], - limit: context.params.$limit, - skip: context.params.$skip, - total: 0, + if (Array.isArray(context.result)) { + context.result = { + data: context.result, + limit: context.result.length, + skip: 0, + total: context.result.length, + } + } else { + context.result = { + data: [], + limit: context.params.$limit, + skip: context.params.$skip, + total: 0, + } } } } From 3577275bf334acb5f4b893d87ad8bd922fcd6224 Mon Sep 17 00:00:00 2001 From: Javad Mnjd Date: Fri, 24 May 2024 05:05:14 +0330 Subject: [PATCH 2/2] fix: update the unpaginated response handling --- src/hooks/6-normalize-find.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/hooks/6-normalize-find.ts b/src/hooks/6-normalize-find.ts index 0bb68568..e053b652 100644 --- a/src/hooks/6-normalize-find.ts +++ b/src/hooks/6-normalize-find.ts @@ -20,21 +20,14 @@ export function normalizeFind() { next && await next() - if (context.method === 'find' && !context.result?.data) { - if (Array.isArray(context.result)) { - context.result = { - data: context.result, - limit: context.result.length, - skip: 0, - total: context.result.length, - } - } else { - context.result = { - data: [], - limit: context.params.$limit, - skip: context.params.$skip, - total: 0, - } + // this makes sure it only affects finds that are not paginated and are not custom. + // so the custom find responses fall through. + if (context.method === 'find' && !context.result?.data && Array.isArray(context.result)) { + context.result = { + data: context.result, + limit: context.params.$limit || context.result.length, + skip: context.params.$skip || 0, + total: context.result.length, } } }