-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic Typescript route param parsing #150
base: v4.x
Are you sure you want to change the base?
Conversation
bf74361
to
933e703
Compare
allows custom method names without having to extend Router
933e703
to
ce040d7
Compare
Awesome, thanks David! Re. your proposed breaking change, mind including (here) a brief before & after snippet of what implementation would look like compared to what it is? |
Briefly looking at the example.ts though, I think I def prefer your direction! |
I think the change to the example shows the difference pretty well. The previous method is brittle in a couple of ways.
An alternative (possibly nicer?) option is to replace this with a simple Re my current implementation, say you want to support the Beforeinterface CustomRouter extends RouterType {
puppy: Route;
kitty: Route;
}
const router = Router({ base: '/' })
router
.all<CustomRouter>('*', () => {...})
.puppy('*', () => {...})
.kitty('*', () => {...}); Afterconst router = Router<'/', 'puppy' | 'kitty'>({ base: '/' })
router.puppy('*', () => {...})
router.kitty('*', () => {...}); |
If you agree, I think it would also be nice to allow providing your own extensions to the When provided, the extended types would replace the Edit: working on workers project now and the current v3.x paradigm of providing an augmented The particular issue I have is is the properties you wish to add on to In my current use case, I'm assigning an API client to |
Have a suggestion for solving this one in an elegant way? I totally agree this is a problem, and one I too face all the time... usually forcing me to create a custom downstream request type and case the request as that in the downstream handlers (cumbersome/boilerplatey) Basically, how do we allow an upstream middleware modify the downstream expected request type effectively? |
Know of an elegant way to allow this to work with |
Ok, revisiting this one after some time off (and time spent tackling the greater TS issues): First of all, guaranteed this no longer works with v4.x as it's made some heavy under-the-hood TS changes in the last several days (pending release). I'd love to see a PR (based on the current types in v4.x) that just tackles the route params, while playing nicely with the typing methods described here: If we can get this in without adding too much complexity, or modifying the existing type signatures (described in the docs), that would be amazing! |
This PR enables automatic type inference from provided route paths, ensuring that users can only access params defined in the route.
I've added type tests to cover these changes. I think they cover all possible eventualities, but would appreciate a second opinion.
Given that this is proposed for a major version upgrade, I did include one semi-breaking change. Instead of having to extend the Router in order to add custom methods, I've added a
TMethods
type parameter to Router. This allows the user to provide a union of any number of method types they would like (eg.'puppy' | 'kitty'
); all of which are mapped to theRoute
function type internally. This has the added benefit of ensuring theRoute
type remains consistent across all routes.P.S. thanks to this gist and @supabase/postgrest-js for some of the inspiration on how to achieve this :)