-
thanks for the great library, i'm a pointfree subscriber and a big fan of your work 👍 i'm trying to figure out if i can model the concept of whole subsets of routes that are authenticated by various methods, where the nested route enums would get access to previously parsed off data. for instance, imagine routes like this: enum AppRoute {
case home
case headerAuthed(UUID, AuthedRoute)
// ^--- uuid from Bearer token header
}
enum AuthedRoute {
case foo
case bar
} i'm trying model the idea that for certain routes, i should always be able to pull a let authedRouter = OneOf {
Route(.case(AuthedRoute.foo)) {
Path { "foo" }
}
Route(.case(AuthedRoute.bar)) {
Path { "bar" }
}
}
let appRouter = OneOf {
Route(.case(AppRoute.home))
Route(.case(AppRoute.headerAuthed)) {
Headers {
Field("Authorization") {
Skip { "Bearer " }
UUID.parser()
}
}
// ^^^--- this is where i'm totally lost...
authedRouter
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @jaredh159, I don't really understand your issue here. When I try your code it works as expected. Example Code// This Playground was generated by the SPI-Playgrounds app, which is part of the
// Swift Package Index (https://swiftpackageindex.com), using 🏟 Arena
// (https://github.com/finestructure/arena) to import the dependency.
//
// For more information about the SPI-Playgrounds app and to download it, visit
// https://swiftpackageindex.com/try-in-a-playground
import URLRouting
import Foundation
enum AppRoute {
case home
case headerAuthed(UUID, AuthedRoute)
}
enum AuthedRoute {
case foo
case bar
}
let authedRouter = OneOf {
Route(.case(AuthedRoute.foo)) {
Path { "foo" }
}
Route(.case(AuthedRoute.bar)) {
Path { "bar" }
}
}
let appRouter = OneOf {
Route(.case(AppRoute.home))
Route(.case(AppRoute.headerAuthed)) {
Headers {
Field("Authorization") {
Skip { "Bearer " }
UUID.parser()
}
}
authedRouter
}
}
var request = URLRequest(url: URL(string: "/bar")!)
request.allHTTPHeaderFields = ["Authorization" : "Bearer 9cd91192-0217-415f-bb1a-230399e0b7a4" ]
var requestData = URLRequestData(request: request)!
print(try appRouter.parse(requestData))
// headerAuthed(9CD91192-0217-415F-BB1A-230399E0B7A4, __lldb_expr_35.AuthedRoute.bar)
request = URLRequest(url: URL(string: "/")!)
requestData = URLRequestData(request: request)!
print(try appRouter.parse(requestData))
// home |
Beta Was this translation helpful? Give feedback.
Hi @jaredh159,
I don't really understand your issue here. When I try your code it works as expected.
Example Code