resolvePerPage function

int resolvePerPage(
  1. Request request, {
  2. int fallback = 15,
  3. int max = 100,
})

Page size from ?per_page=, clamped to max.

The cap is not optional: ?per_page=1000000 is a trivial denial-of-service against any paginated endpoint. Laravel leaves this to the developer and it is routinely forgotten.

Implementation

int resolvePerPage(Request request, {int fallback = 15, int max = 100}) {
  final raw = int.tryParse(request.query('per_page') ?? '');
  if (raw == null || raw < 1) return fallback;
  return raw > max ? max : raw;
}