feat: add defineRoute and defineRoutes declarative helpers - #17
Merged
Conversation
Removes the handler-closure boilerplate consumers repeat per route: page, loading, and error components receive the route context (params, query) as props, and loader/meta/extra receive it as their first argument. defineRoutes groups routes and supports per-key page component overrides while preserving loaders, meta, extra, options, and route metadata. Adds react as a peer dependency (the helpers use createElement to bind context into components). Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
defineRoute(path, def, options?, routeMeta?): a declarative variant ofcreateRoutethat removes the per-route handler-closure boilerplate.page/loading/errorcomponents receive the route context (params,query) as props;loader/meta/extrareceive it as their first argument, with extra caller args (e.g.AbortSignal) forwarded after it.defineRoutes(routes, { pages? }): groups routes forcreateRouterwith optional per-key page component overrides. Overrides ondefineRouteroutes are rebuilt so the replacement component still receives the route context; plaincreateRouteroutes get theirPageComponentswapped as-is.reactas a peer dependency (the helpers usecreateElementto bind context into components); bumps version to 1.3.0.Motivation: consumers like
@btst/stackcurrently repeat a handler closure per route wiringPageComponentoverride lookup,loader, andmeta(~6x per plugin). These helpers collapse that to one declaration per route while staying fully compatible withcreateRoute/createRouter.Test plan
src/__tests__/define.test.ts: context injection into page/loading/error props, render-time prop merging, loader/meta/extra binding (sync + async + extra args), query validation passthrough (pass and fail), route-level metadata,createRouterinterop (routeKey,params), page overrides for bothdefineRouteand plaincreateRouteroutespnpm build,pnpm lint,pnpm typecheck,pnpm test -- --runall pass locally (79/79 tests)Made with Cursor