diff --git a/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift b/Sources/Hummingbird/Middleware/MiddlewareModule/MiddlewareStack.swift index 26ed2d5f..2d07f3cd 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 ba461e4c..3560508a 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 6286dcdc..caf1ce0a 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 deccf679..fd4c7d3f 100644 --- a/Sources/HummingbirdRouter/RouteBuilder.swift +++ b/Sources/HummingbirdRouter/RouteBuilder.swift @@ -44,7 +44,7 @@ public struct Handle