From 60a912cd2cbdf43177d2952210333b5c7e3e7621 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Fri, 30 Sep 2022 20:23:00 +0200 Subject: [PATCH] Rename param to page limit and validate its type Signed-off-by: Camila Belo --- .../src/engines/ElasticSearchSearchEngine.ts | 2 +- .../src/engines/LunrSearchEngine.ts | 4 ++-- .../search-backend/src/service/router.test.ts | 19 ++++++++++++++++--- plugins/search-backend/src/service/router.ts | 19 ++++++++++++------- plugins/search-common/src/types.ts | 4 ++-- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index 01d319d257..a33d8939eb 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -210,7 +210,7 @@ export class ElasticSearchSearchEngine implements SearchEngine { .multiMatchQuery(['*'], term) .fuzziness('auto') .minimumShouldMatch(1); - const pageSize = query.resultsPerPage || 25; + const pageSize = query.pageLimit || 25; const { page } = decodePageCursor(pageCursor); let esbRequestBodySearch = esb diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts index e5d2d86168..d60f9951f4 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts @@ -71,9 +71,9 @@ export class LunrSearchEngine implements SearchEngine { term, filters, types, - resultsPerPage, + pageLimit, }: SearchQuery): ConcreteLunrQuery => { - const pageSize = resultsPerPage || 25; + const pageSize = pageLimit || 25; return { lunrQueryBuilder: q => { diff --git a/plugins/search-backend/src/service/router.test.ts b/plugins/search-backend/src/service/router.test.ts index 3b29e57969..c3eb6ea00f 100644 --- a/plugins/search-backend/src/service/router.test.ts +++ b/plugins/search-backend/src/service/router.test.ts @@ -113,7 +113,7 @@ describe('createRouter', () => { }); it('should accept per page value under or equal to 100', async () => { - const response = await request(app).get(`/query?resultsPerPage=30`); + const response = await request(app).get(`/query?pageLimit=30`); expect(response.status).toEqual(200); expect(response.body).toMatchObject({ @@ -122,11 +122,24 @@ describe('createRouter', () => { }); it('should reject per page value over 100', async () => { - const response = await request(app).get(`/query?resultsPerPage=200`); + const response = await request(app).get(`/query?pageLimit=200`); expect(response.status).toEqual(400); expect(response.body).toMatchObject({ - error: { message: /The maximum value of the resultsPerPage param is 100, please update and make a new request/i }, + error: { + message: /The page limit "200" is greater than "100"/i, + }, + }); + }); + + it('should reject a non number per page value', async () => { + const response = await request(app).get(`/query?pageLimit=twohundred`); + + expect(response.status).toEqual(400); + expect(response.body).toMatchObject({ + error: { + message: /The page limit "twohundred" is not a number"/i, + }, }); }); diff --git a/plugins/search-backend/src/service/router.ts b/plugins/search-backend/src/service/router.ts index c0b6661f78..5ad4d4969b 100644 --- a/plugins/search-backend/src/service/router.ts +++ b/plugins/search-backend/src/service/router.ts @@ -62,6 +62,7 @@ export type RouterOptions = { logger: Logger; }; +const maxPageLimit = 100; const allowedLocationProtocols = ['http:', 'https:']; /** @@ -71,7 +72,6 @@ export async function createRouter( options: RouterOptions, ): Promise { const { engine: inputEngine, types, permissions, config, logger } = options; - const MAX_RESULT_PER_PAGE = 100 const requestSchema = z.object({ term: z.string().default(''), @@ -80,14 +80,19 @@ export async function createRouter( .array(z.string().refine(type => Object.keys(types).includes(type))) .optional(), pageCursor: z.string().optional(), - resultsPerPage: z + pageLimit: z .string() - .transform((resultsPerPage) => parseInt(resultsPerPage)) + .transform(pageLimit => parseInt(pageLimit, 10)) .refine( - resultsPerPage => resultsPerPage <= MAX_RESULT_PER_PAGE, - resultsPerPage => ({ - message: - `The maximum value of the resultsPerPage param is ${MAX_RESULT_PER_PAGE}, you provided ${resultsPerPage}, please update and make a new request`, + pageLimit => !isNaN(pageLimit), + pageLimit => ({ + message: `The page limit "${pageLimit}" is not a number`, + }), + ) + .refine( + pageLimit => pageLimit <= maxPageLimit, + pageLimit => ({ + message: `The page limit "${pageLimit}" is greater than "${maxPageLimit}"`, }), ) .optional(), diff --git a/plugins/search-common/src/types.ts b/plugins/search-common/src/types.ts index 1daced8e5b..54abc9e1b1 100644 --- a/plugins/search-common/src/types.ts +++ b/plugins/search-common/src/types.ts @@ -23,10 +23,10 @@ import { Readable, Transform, Writable } from 'stream'; */ export interface SearchQuery { term: string; - filters?: JsonObject; types?: string[]; + filters?: JsonObject; + pageLimit?: number; pageCursor?: string; - resultsPerPage?: number; } /**