-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
Router
generic over its route 🚏
Our `Router` and `RouteHandler` infrastructure was designed to work with plain `URL`'s on its main APIs. Despite URLs being the critical piece of information to route (e.g. in our `TrieRouter`), in certain situations the URL doesn't contain all the information, which limits the Routing infrastructure capabilities. One such example would be in a server middleware router, where the HTTP method would likely be required information to pass on to handlers to perform adequate routing. To remove this restriction, a new `Routable` protocol was created to represent a routable type, which only needs to provide a `url` and is forwarded by the router to the handler upon a successful match. To make it easier to unpack any additional information on the handlers and have more type safety, it was made an `associatedtype` of both `Router` and `RouteHandler`, effectively turning it into a new generic `R`. ## Changes - Add new `Routable` protocol, add default conformance to `URL`. - Add new `associatedtype R: Routable` to `Router` and `RouteHandler`. - Update `Router` and `RouterHandler` APIs to receive a route of type `R`. - Add new `RouteHandler.eraseToAnyRouteHandler` helper.
- Loading branch information
Showing
8 changed files
with
129 additions
and
59 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Foundation | ||
|
||
/// A type representing something that can be routed (via a URL). | ||
public protocol Routable { | ||
|
||
var route: URL { get } | ||
} | ||
|
||
extension URL: Routable { | ||
|
||
public var route: URL { self } | ||
} |
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
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
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
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
Oops, something went wrong.