Created
May 20, 2023 04:57
-
-
Save omochi/fb0ea297ee16de7f61c92d47a1a44b08 to your computer and use it in GitHub Desktop.
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
protocol Middleware<Input, Output, NextInput, NextOutput> { | |
associatedtype Input | |
associatedtype Output | |
associatedtype NextInput | |
associatedtype NextOutput | |
} | |
protocol Routes<Input, Output> { | |
associatedtype Input | |
associatedtype Output | |
} | |
extension Routes { | |
func compose<M>(_ middleware: M) -> any Routes<M.NextInput, M.NextOutput> where | |
M: Middleware, | |
M.Input == Input, | |
M.Output == Output | |
{ | |
fatalError() | |
} | |
func mapInput<NewInput>( | |
_ transform: @escaping (Input) -> NewInput | |
) -> any Routes<NewInput, Output> { | |
fatalError() | |
} | |
} | |
struct Request {} | |
struct Response {} | |
struct User {} | |
struct AuthMiddleware<Input, Output>: Middleware { | |
typealias NextInput = (Input, login: User) | |
typealias NextOutput = Output | |
} | |
func main(routes: any Routes<Request, Response>) { | |
let routes = routes.compose(AuthMiddleware()) | |
.mapInput { | |
(request: $0.0, login: $0.login) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment