refactor(api): DRY limit-query clamping onto parseBoundedLimit (BEN-150) - #133
Merged
Conversation
/api/events/recent and /api/search both parsed a "limit" query with the same "clamp to [1, max] with fallback" recipe, spelled out slightly differently at each site. Extract a single `parseBoundedLimit` helper and pass fallback + max explicitly per route so the ceilings stay route-specific but the clamping recipe lives in one place. No behavior change; the existing recent-events cap test still passes.
# Conflicts: # src/api/server.ts
benSepanski
marked this pull request as ready for review
August 2, 2026 06:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Two API routes (
/api/events/recent,/api/search) parsed alimitquery with the same "clamp to
[1, max]with fallback" recipe spelledout slightly differently at each site — a DRY nit that also made the
per-route bounds harder to read at a glance.
TL;DR
Extract a single
parseBoundedLimithelper and pass fallback + maxper route so the clamping recipe lives in one place.
Summary
parseBoundedLimit(raw, { fallback, max, min = 1 })insrc/api/server.ts./api/events/recentlimit read (fallback 50, max 200) via the helper./api/searchlimit read (fallback 100, max 500) via the helper.{ fallback, max }arguments.Demo
n/a — internal refactor, no user-visible surface change.
Alternatives
SymphonyLogger.listRecentEvents/search— those methods already re-clamp their own inputs, but they are also called from tests directly, so the string-query parsing has to live at the HTTP boundary. Helper is the smallest fix.clamp(n, min, max)— too generic for a two-caller refactor; can extract if a third site appears.Test Plan
pnpm all— typecheck + fmt:check + lint + test + eval (329 unit + 5 eval scenarios pass locally)/api/events/recent?limit=1cap test insrc/api/server.test.tsstill passespnpm build:web— web bundle builds (unaffected; server-only diff)Generated by Claude Code