Skip to content

Commit 31f99db

Browse files
committed
Alternative version with a ContextTransformingMiddlewareProtocol
1 parent fce191d commit 31f99db

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

Sources/Hummingbird/Middleware/Middleware.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414

1515
import NIOCore
1616

17-
/// Middleware protocol with generic input, context and output types
18-
public protocol MiddlewareProtocol<Input, Output, Context>: Sendable {
17+
public protocol ContextTransformingMiddlewareProtocol<Input, Output, InputContext, OutputContext>: Sendable {
1918
associatedtype Input
2019
associatedtype Output
21-
associatedtype Context
20+
associatedtype InputContext
21+
associatedtype OutputContext
22+
23+
func handle(_ input: Input, context: InputContext, next: (Input, OutputContext) async throws -> Output) async throws -> Output
24+
}
2225

23-
func handle(_ input: Input, context: Context, next: (Input, Context) async throws -> Output) async throws -> Output
26+
/// Middleware protocol with generic input, context and output types
27+
public protocol MiddlewareProtocol<Input, Output, Context>: ContextTransformingMiddlewareProtocol where InputContext == OutputContext, Context == InputContext {
28+
associatedtype Context
2429
}
2530

2631
/// Applied to `Request` before it is dealt with by the router. Middleware passes the processed request onto the next responder

Sources/Hummingbird/Router/RouterMethods.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ extension RouterMethods {
9494
/// ```
9595
/// - Parameters
9696
/// - path: path prefix to add to routes inside this group
97-
/// - mapContext: Function converting context
97+
/// - middleware: A middleware that can transform the context
9898
@discardableResult public func group<TargetContext>(
9999
_ path: RouterPath = "",
100-
mapContext: @escaping @Sendable (Request, Context) async throws -> TargetContext
100+
middleware: some ContextTransformingMiddlewareProtocol<Request, Response, Context, TargetContext>
101101
) -> RouterGroup<TargetContext> {
102102
return RouterGroup(
103103
path: path,
104-
parent: TransformingRouterGroup(parent: self, transform: mapContext)
104+
parent: TransformingRouterGroup(parent: self, middleware: middleware)
105105
)
106106
}
107107

Sources/Hummingbird/Router/TransformingRouterGroup.swift

+14-8
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,38 @@ import NIOCore
2020
struct TransformingRouterGroup<InputContext: RequestContext, Context: RequestContext>: RouterMethods {
2121
typealias TransformContext = Context
2222
let parent: any RouterMethods<InputContext>
23-
let transform: @Sendable (Request, InputContext) async throws -> TransformContext
23+
let transform: @Sendable (Request, InputContext, (Request, TransformContext) async throws -> Response) async throws -> Response
2424

2525
struct ContextTransformingResponder: HTTPResponder {
2626
typealias Context = InputContext
2727
let responder: any HTTPResponder<TransformContext>
28-
let transform: @Sendable (Request, InputContext) async throws -> TransformContext
28+
let transform: @Sendable (
29+
Request,
30+
InputContext,
31+
(Request, TransformContext) async throws -> Response
32+
) async throws -> Response
2933

3034
func respond(to request: Request, context: InputContext) async throws -> Response {
31-
let newContext = try await transform(request, context)
32-
return try await self.responder.respond(to: request, context: newContext)
35+
try await transform(request, context) { req, context in
36+
try await self.responder.respond(to: request, context: context)
37+
}
3338
}
3439
}
3540

3641
init(parent: any RouterMethods<InputContext>) where Context.Source == InputContext {
3742
self.parent = parent
38-
self.transform = { _, context in
39-
TransformContext(source: context)
43+
self.transform = { req, context, next in
44+
let context = TransformContext(source: context)
45+
return try await next(req, context)
4046
}
4147
}
4248

4349
init(
4450
parent: any RouterMethods<InputContext>,
45-
transform: @escaping @Sendable (Request, InputContext) async throws -> TransformContext
51+
middleware: some ContextTransformingMiddlewareProtocol<Request, Response, InputContext, Context>
4652
) {
4753
self.parent = parent
48-
self.transform = transform
54+
self.transform = middleware.handle
4955
}
5056

5157
/// Add middleware (Stub function as it isn't used)

0 commit comments

Comments
 (0)