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