Skip to content

Commit

Permalink
ContextTransform(context:) renamed to ContextTransform(to:) (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Jul 22, 2024
1 parent a563c7b commit a1028d2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct _Middleware2<M0: MiddlewareProtocol, M1: MiddlewareProtocol>: 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.
Expand Down
27 changes: 21 additions & 6 deletions Sources/Hummingbird/Router/RouterMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request, Response, Context> middlewareStack: () -> some MiddlewareProtocol<Request, Response, Context>
) -> Self {
return self.add(middleware: middlewareStack())
}

/// GET path for async closure returning type conforming to ResponseGenerator
@discardableResult public func get(
_ path: RouterPath = "",
Expand Down Expand Up @@ -131,12 +152,6 @@ extension RouterMethods {
return self.on(path, method: .patch, use: handler)
}

@discardableResult public func add(
@MiddlewareFixedTypeBuilder<Request, Response, Context> middleware: () -> some MiddlewareProtocol<Request, Response, Context>
) -> Self {
return self.add(middleware: middleware())
}

internal func constructResponder(
use closure: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
) -> CallbackResponder<Context> {
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdRouter/ContextTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +41,7 @@ public struct ContextTransform<Context: RouterRequestContext, HandlerContext: Ro
/// - routerPath: Path local to group route this group is defined in
/// - builder: RouteGroup builder
public init(
context: HandlerContext.Type,
to context: HandlerContext.Type,
@MiddlewareFixedTypeBuilder<Request, Response, HandlerContext> builder: () -> Handler
) {
self.handler = builder()
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdRouter/RouteBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct Handle<HandlerOutput: ResponseGenerator, Context: RouterRequestCon

/// Result builder for a Route.
///
/// This is very similar to the ``MiddlewareFixedTypeBuilder`` result builder except it requires
/// This is very similar to the ``Hummingbird/MiddlewareFixedTypeBuilder`` result builder except it requires
/// the last entry of the builder to be a ``Handle`` so we are guaranteed a Response. It also
/// adds the ability to pass in a closure instead of ``Handle`` type.
@resultBuilder
Expand Down
4 changes: 2 additions & 2 deletions Tests/HummingbirdRouterTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ final class RouterTests: XCTestCase {
var string: String

typealias Source = BasicRouterRequestContext
init(source: Source) {
init(source: BasicRouterRequestContext) {
self.coreContext = .init(source: source)
self.string = ""
self.routerContext = source.routerContext
Expand All @@ -299,7 +299,7 @@ final class RouterTests: XCTestCase {
RouteGroup("/test") {
TestMiddleware()
RouteGroup("/group") {
ContextTransform(context: TestRouterContext2.self) {
ContextTransform(to: TestRouterContext2.self) {
TestTransformMiddleware()
Get { _, context in
return Response(status: .ok, headers: [.middleware2: context.string])
Expand Down

0 comments on commit a1028d2

Please sign in to comment.