Rename param to page limit and validate its type
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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<express.Router> {
|
||||
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(),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user