Skip to content

refactor(api): DRY limit-query clamping onto parseBoundedLimit (BEN-150) - #133

Merged
benSepanski merged 2 commits into
mainfrom
agent/ben-150
Aug 2, 2026
Merged

refactor(api): DRY limit-query clamping onto parseBoundedLimit (BEN-150)#133
benSepanski merged 2 commits into
mainfrom
agent/ben-150

Conversation

@benSepanski

Copy link
Copy Markdown
Owner

Context

Two API routes (/api/events/recent, /api/search) parsed a limit
query with the same "clamp to [1, max] with fallback" recipe spelled
out 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 parseBoundedLimit helper and pass fallback + max
per route so the clamping recipe lives in one place.

Summary

  • Add parseBoundedLimit(raw, { fallback, max, min = 1 }) in src/api/server.ts.
  • Rewrite the /api/events/recent limit read (fallback 50, max 200) via the helper.
  • Rewrite the /api/search limit read (fallback 100, max 500) via the helper.
  • No behavior change; per-route ceilings preserved by explicit { fallback, max } arguments.

Demo

n/a — internal refactor, no user-visible surface change.

Alternatives

  • Leave duplicated inline — cheapest today but keeps the "clamp recipe" drifting between routes; rejected.
  • Push the clamp into 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.
  • Add a shared 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=1 cap test in src/api/server.test.ts still passes
  • pnpm build:web — web bundle builds (unaffected; server-only diff)

Generated by Claude Code

claude added 2 commits August 2, 2026 06:18
/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.
@benSepanski
benSepanski marked this pull request as ready for review August 2, 2026 06:26
@benSepanski
benSepanski merged commit 9ecef91 into main Aug 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants