You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and in handle: (request: TRequest, ...extra: any) => Promise<any> the Promise<any> is the same any as any-the-return-type of RouteHandler
As I can see, having any there allows me to use itty-router to route requests into something that's not a Response.
I mean: that's good for me that Response is not enforced:
And it keeps the handle protected. But type assertion is a poor man's choice.
In order to type-check the return type of each RouteHandler - at least with my current TS skills that's best I came up with - I have to
declare return type on each of them (not quite scalable, not even speaking of rigidity), or
redefine the Route type, then define IMethods and alter all existing keys, then pass IMethods to Router<..., IMethods> (which, yeah, is doable, but it seems that ability to pass IMethods there was added to make custom methods strongly-typed, not to rewrite Route)
Anyway: I build a solution that uses itty-router to select some kind of configuration for tools that will alter the intercepted response in my worker. Configuring it with the router-alike syntax seems like a good choice for my case
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I propose so that there might be
to allow strong typing what router
handleproduces without a need to rewrite most of types delivered with package.Details & motivation
Right now there's
itty-router/src/itty-router.d.ts
Lines 5 to 7 in 2a51d57
It doesn't allow to declare what do methods of router (which conform
RouteHandler) return.Then there's
itty-router/src/itty-router.d.ts
Lines 44 to 50 in 2a51d57
and in
handle: (request: TRequest, ...extra: any) => Promise<any>thePromise<any>is the sameanyasany-the-return-type ofRouteHandlerAs I can see, having
anythere allows me to useitty-routerto route requests into something that's not aResponse.I mean: that's good for me that
Responseis not enforced:because then I'd have some problems (more at the end of post).
But: the way it is now doesn't allow me to explicitly declare what my router produces, but by doing something like this (which I actually do):
And it keeps the
handleprotected. But type assertion is a poor man's choice.In order to type-check the return type of each
RouteHandler- at least with my current TS skills that's best I came up with - I have toRoutetype, then defineIMethodsand alter all existing keys, then passIMethodstoRouter<..., IMethods>(which, yeah, is doable, but it seems that ability to passIMethodsthere was added to make custom methods strongly-typed, not to rewriteRoute)Anyway: I build a solution that uses
itty-routerto select some kind of configuration for tools that will alter the intercepted response in my worker. Configuring it with the router-alike syntax seems like a good choice for my caseBeta Was this translation helpful? Give feedback.
All reactions