From a1028d2b3c74fbf6f537abca6022d7c0d54a5d24 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Mon, 22 Jul 2024 09:14:16 +0100 Subject: [PATCH] ContextTransform(context:) renamed to ContextTransform(to:) (#514) --- .../MiddlewareModule/MiddlewareStack.swift | 2 +- .../Hummingbird/Router/RouterMethods.swift | 27 ++++++++++++++----- .../HummingbirdRouter/ContextTransform.swift | 4 +-- Sources/HummingbirdRouter/RouteBuilder.swift | 2 +- .../HummingbirdRouterTests/RouterTests.swift | 4 +-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift b/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift index 26ed2d5fb..2d07f3cdf 100644 --- a/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift +++ b/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift @@ -34,7 +34,7 @@ public struct _Middleware2: Midd } } -/// Result builder used by ``RouterBuilder`` +/// Middleware stack result builder /// /// Generates a middleware stack from the elements inside the result builder. The input, /// context and output types passed through the middleware stack are fixed and cannot be changed. diff --git a/Sources/Hummingbird/Router/RouterMethods.swift b/Sources/Hummingbird/Router/RouterMethods.swift index ba461e4c7..3560508ab 100644 --- a/Sources/Hummingbird/Router/RouterMethods.swift +++ b/Sources/Hummingbird/Router/RouterMethods.swift @@ -83,6 +83,27 @@ extension RouterMethods { ) } + /// Add middleware stack to router + /// + /// Add multiple middleware to the router using the middleware stack result builder + /// ``MiddlewareFixedTypeBuilder``. + /// + /// ```swift + /// router.add { + /// LogRequestsMiddleware() + /// MetricsMiddleware() + /// } + /// ``` + /// This gives a slight performance boost over adding them individually. + /// + /// - Parameter middlewareStack: Middleware stack result builder + /// - Returns: router + @discardableResult public func add( + @MiddlewareFixedTypeBuilder middlewareStack: () -> some MiddlewareProtocol + ) -> Self { + return self.add(middleware: middlewareStack()) + } + /// GET path for async closure returning type conforming to ResponseGenerator @discardableResult public func get( _ path: RouterPath = "", @@ -131,12 +152,6 @@ extension RouterMethods { return self.on(path, method: .patch, use: handler) } - @discardableResult public func add( - @MiddlewareFixedTypeBuilder middleware: () -> some MiddlewareProtocol - ) -> Self { - return self.add(middleware: middleware()) - } - internal func constructResponder( use closure: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator ) -> CallbackResponder { diff --git a/Sources/HummingbirdRouter/ContextTransform.swift b/Sources/HummingbirdRouter/ContextTransform.swift index 6286dcdcc..caf1ce0a7 100644 --- a/Sources/HummingbirdRouter/ContextTransform.swift +++ b/Sources/HummingbirdRouter/ContextTransform.swift @@ -14,7 +14,7 @@ import Hummingbird -/// Router middleware that transforms the ``RequestContext`` and uses it with the contained +/// Router middleware that transforms the ``Hummingbird/RequestContext`` and uses it with the contained /// Middleware chain /// /// For the transform to work the `Source` of the transformed `RequestContext`` needs to be @@ -41,7 +41,7 @@ public struct ContextTransform builder: () -> Handler ) { self.handler = builder() diff --git a/Sources/HummingbirdRouter/RouteBuilder.swift b/Sources/HummingbirdRouter/RouteBuilder.swift index deccf6795..fd4c7d3f6 100644 --- a/Sources/HummingbirdRouter/RouteBuilder.swift +++ b/Sources/HummingbirdRouter/RouteBuilder.swift @@ -44,7 +44,7 @@ public struct Handle