-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
V4.x - itty-router-extras are coming in house! #148
Closed
Conversation
This file contains 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
Labels
in progress
I'm working on it... allegedly...
PENDING RELEASE
Final stages before release! :D
PRIORITY
Do this first!
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.
Proposed/TODO
error
- pass this to the.catch()
functionjson
/etc - pass this to a.then()
afterrouter.handle
to transform unformed data into a Responseroute
intorequest.route
for determining route catch from within handlersTypes
Let me get this out there... types in itty are HARD. First, it's a Proxy, which are not very type-friendly to begin with. Next, we want ALL the flexibility (to serve all the various audiences), and that's where the problems really begin. We could, for instance, pass generics at a router level, to define Request type, additional args, etc. This works great, and a single set of types can serve an entire router.
But what if you want to override just a particular route? For example, use a UserRequest on a route that has a
withUser
middleware upstream. This no longer works.Conversely, you can do the opposite, and allow full, per-route customization. It's more verbose to be sure, but it allows you to define what you want, where you want it - the tradeoff being that you have to define them more often.
Ultimately, this is the path I'm leaning towards. There's nothing more frustrating than fighting types - including boilerplate (as much as I obviously hate that). With that in mind, here are the proposed type changes:
Changes
IRequest
will now be a union onRequest
(Web Standard), to allow type hinting on the Request object. This is overdue.<RequestType, Args>
though, as that is the signature I'd ultimately like to pass down, if we can ever achieve that.IRequestStrict
, which is just likeIRequest
, but without the generic traps to allow any undeclared variable. Want to lock down a route? Define a custom Request type based onIRequestStrict
instead ofIRequest
.Usage Example
In the example below, note the lack of manual
new Response()
or evenjson(data)
creation. By including a single downstream handler at the outermost/root router, we can let routes return Responses, raw data, or even Promises to raw data/Responses (e.g. a request to a database). Downstream, we can wrap everything in a Response if not already formed :)Update
Rather than include a separate handler (
respondWithJSON
/respondWithError
), I've overloaded the existing function signatures to ignore Requests/Responses. This allows them to be used as global post-route handlers (as well).