From 6ec5a3ec43388554b2b423c8f04081605c2f8deb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 2 Aug 2021 15:54:29 +0200 Subject: [PATCH] Add max pagination count --- server/initializers/constants.ts | 7 +++++-- server/middlewares/pagination.ts | 5 ++--- server/middlewares/validators/pagination.ts | 10 ++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index cb8c51a..2c2877d 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -121,7 +121,10 @@ const SORTABLE_COLUMNS = { PLAYLISTS_SEARCH: [ 'match', 'displayName', 'createdAt' ] } -const PAGINATION_COUNT_DEFAULT = 20 +const PAGINATION_COUNT = { + DEFAULT: 20, + MAX: 500 +} const SCHEDULER_INTERVALS_MS = { indexation: 60000 * 60 * 24 // 24 hours @@ -174,7 +177,7 @@ export { CONFIG, API_VERSION, - PAGINATION_COUNT_DEFAULT, + PAGINATION_COUNT, SORTABLE_COLUMNS, INDEXER_QUEUE_CONCURRENCY, SCHEDULER_INTERVALS_MS, diff --git a/server/middlewares/pagination.ts b/server/middlewares/pagination.ts index 063be3e..b702b87 100644 --- a/server/middlewares/pagination.ts +++ b/server/middlewares/pagination.ts @@ -1,13 +1,12 @@ import 'express-validator' import * as express from 'express' - -import { PAGINATION_COUNT_DEFAULT } from '../initializers/constants' +import { PAGINATION_COUNT } from '../initializers/constants' function setDefaultPagination (req: express.Request, res: express.Response, next: express.NextFunction) { if (!req.query.start) req.query.start = 0 else req.query.start = parseInt(req.query.start, 10) - if (!req.query.count) req.query.count = PAGINATION_COUNT_DEFAULT + if (!req.query.count) req.query.count = PAGINATION_COUNT.DEFAULT else req.query.count = parseInt(req.query.count, 10) return next() diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index cc69ea9..b00954e 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts @@ -1,10 +1,16 @@ import * as express from 'express' import { check } from 'express-validator' +import { PAGINATION_COUNT } from 'server/initializers/constants' import { areValidationErrors } from './utils' const paginationValidator = [ - check('start').optional().isInt({ min: 0 }).withMessage('Should have a number start'), - check('count').optional().isInt({ min: 0 }).withMessage('Should have a number count'), + check('start') + .optional() + .isInt({ min: 0 }).withMessage('Should have a number start'), + + check('count') + .optional() + .isInt({ min: 0, max: PAGINATION_COUNT.MAX }).withMessage(`Should have a number count (> 0 and < ${PAGINATION_COUNT.MAX}`), (req: express.Request, res: express.Response, next: express.NextFunction) => { if (areValidationErrors(req, res)) return