Accept resultsPerPage param on the search query

Co-authored-by: Camila Loiola <camilaibs@gmail.com>
Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2022-09-30 16:07:54 +02:00
committed by Camila Belo
parent abfa5a313c
commit 876e3b3d0c
3 changed files with 31 additions and 0 deletions
@@ -112,6 +112,24 @@ describe('createRouter', () => {
});
});
it('should accept per page value under or equal to 100', async () => {
const response = await request(app).get(`/query?resultsPerPage=30`);
expect(response.status).toEqual(200);
expect(response.body).toMatchObject({
results: [],
});
});
it('should reject per page value over 100', async () => {
const response = await request(app).get(`/query?resultsPerPage=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 },
});
});
it('removes backend-only properties from search documents', async () => {
mockSearchEngine.query.mockResolvedValue({
results: [
@@ -71,6 +71,7 @@ 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(''),
@@ -79,6 +80,17 @@ export async function createRouter(
.array(z.string().refine(type => Object.keys(types).includes(type)))
.optional(),
pageCursor: z.string().optional(),
resultsPerPage: z
.string()
.transform((resultsPerPage) => parseInt(resultsPerPage))
.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`,
}),
)
.optional(),
});
let permissionEvaluator: PermissionEvaluator;
+1
View File
@@ -26,6 +26,7 @@ export interface SearchQuery {
filters?: JsonObject;
types?: string[];
pageCursor?: string;
resultsPerPage?: number;
}
/**