2.3 KiB
2.3 KiB
API Refactor Lightweight Plan
Goal
Reduce api/src/server.ts size and duplication with low-risk, incremental moves.
Current state (2026-03-15):
server.tshas ~4.8k lines and 50 endpoint registrations.- Duplicate endpoint signatures also exist in
api/src/routes/*but are not currently registered.
Refactor Guardrails
- Keep route behavior identical while moving code.
- Move one domain at a time; do not mix domains in one PR.
- Do not redesign architecture (no repositories/DI/container work).
- Extract only duplicated logic into shared helpers/services.
- Preserve global hooks and plugin registration in
server.tsuntil final cleanup. - Keep response shapes stable (
ok,code,message, etc.) during moves. - Require tests to pass after each move phase.
Canonical Source Rule
server.ts is the canonical route logic right now.
When moving a domain:
- Copy current logic from
server.tsinto the domain route module. - Register the module.
- Remove the original block from
server.ts. - Confirm no duplicate route registrations remain.
Shared Helpers (Phase 0)
Create these helpers first to keep endpoint moves lightweight:
api/src/services/user-context.ts
getUserTimezone(...)- Removes repeated user timezone lookup logic.
api/src/services/category-shares.ts
computePercentShares(...)computeWithdrawShares(...)computeOverdraftShares(...)computeDepositShares(...)- Centralizes repeated category-share math.
api/src/services/budget-session.ts
getLatestBudgetSession(...)ensureBudgetSession(...)ensureBudgetSessionAvailableSynced(...)- Prevents session/value drift bugs.
api/src/services/api-errors.ts
- Standard typed error object and response body builder.
- Keeps endpoint error handling consistent.
Move Order (Incremental)
- Low-risk endpoints:
health,session,me,user/config. auth+accountendpoints.variable-categoriesendpoints.transactionsendpoints.fixed-plansendpoints.income,budget,paydayendpoints.dashboard+crisis-status.adminendpoints.
Definition of Done per Phase
- Endpoints compile and register once.
- Existing tests pass.
- No route path/method changes.
- No response contract changes.
server.tsnet line count decreases.