feat: add route contracts for state and search#32
Conversation
Relax Routes index signature to allow heterogeneous state types per handler while keeping the S generic for route guards. Add wildcard "*" fallback in _NavigateRoutes so paths like /x/z correctly resolve to the wildcard handler's state type.
WalkthroughThis PR extends the esroute type system to support typed search parameters alongside existing typed state. It introduces a ChangesSearch Parameter and Contract System
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/esroute/src/router.ts (1)
13-23: 💤 Low valueConsider aligning the
Searchdefault with the rest of the surface.
OnResolveListener,Router,RouterConf, andcreateRouterdefaultSearchtoRecord<string, string>, whileNavMeta/NavOpts(innav-opts.ts),Routes/Resolve(inroutes.ts), andResolved/RouteResolver(inroute-resolver.ts) default it toRecord<never, never>. Withsatisfies Routes<…>the explicit type usually wins, but bare aliases likeRouter<string>vsResolved<string>will silently differ insearchkey openness, which can surprise consumers readingrouter.current.searchvs a directly constructedNavOpts.If the open default in
router.tsis intentional (because router-level types are aggregated across all routes), it's worth a brief comment near these declarations; otherwise consider standardizing onRecord<never, never>.♻️ Possible alignment
export type OnResolveListener< T, S = never, - Search extends Record<string, string> = Record<string, string> + Search extends Record<string, string> = Record<never, never> > = (resolved: Resolved<T, S, Search>) => void; export interface Router< T = any, S = never, R extends RawRoutes = RawRoutes, - Search extends Record<string, string> = Record<string, string> + Search extends Record<string, string> = Record<never, never> > {…and analogously in
RouterConfandcreateRouter.Also applies to: 87-92, 115-125
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/esroute/src/router.ts` around lines 13 - 23, The Search generic default in OnResolveListener, Router (and related RouterConf/createRouter) is currently Record<string, string> while other surface types (NavMeta/NavOpts, Routes/Resolve, Resolved/RouteResolver) default to Record<never, never>, causing inconsistent openness of the search key; either change the Search default in OnResolveListener, Router, RouterConf and createRouter to Record<never, never> to match the rest of the API, or add a short comment above these declarations documenting that the router-level types are intentionally open because they aggregate across routes—update the generic defaults (or add the comment) in the symbols OnResolveListener, Router, RouterConf, and createRouter accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/esroute/src/router.ts`:
- Around line 13-23: The Search generic default in OnResolveListener, Router
(and related RouterConf/createRouter) is currently Record<string, string> while
other surface types (NavMeta/NavOpts, Routes/Resolve, Resolved/RouteResolver)
default to Record<never, never>, causing inconsistent openness of the search
key; either change the Search default in OnResolveListener, Router, RouterConf
and createRouter to Record<never, never> to match the rest of the API, or add a
short comment above these declarations documenting that the router-level types
are intentionally open because they aggregate across routes—update the generic
defaults (or add the comment) in the symbols OnResolveListener, Router,
RouterConf, and createRouter accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d97f5480-cb3e-4a22-93a4-886c76b0235c
📒 Files selected for processing (6)
packages/esroute/README.mdpackages/esroute/src/nav-opts.tspackages/esroute/src/route-resolver.tspackages/esroute/src/router.spec.tspackages/esroute/src/router.tspackages/esroute/src/routes.ts
Summary
Validation
Summary by CodeRabbit
New Features
Documentation
Refactor