Skip to content

Commit

Permalink
Remove router options (#358)
Browse files Browse the repository at this point in the history
There are currently no options and I don't see there being any in the future. So no point passing around an empty OptionSet
  • Loading branch information
adam-fowler authored Jan 26, 2024
1 parent 834ecd9 commit ad0cd0b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
21 changes: 7 additions & 14 deletions Sources/Hummingbird/Router/RouteHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ extension HBRouterMethods {
@discardableResult public func on<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String,
method: HTTPRequest.Method,
options: HBRouterMethodOptions = [],
use handlerType: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: method, options: options) { request, context -> Output in
return self.on(path, method: method) { request, context -> Output in
let handler = try await Handler(from: request, context: context)
return try await handler.handle(context: context)
}
Expand All @@ -62,54 +61,48 @@ extension HBRouterMethods {
/// GET path for closure returning type conforming to HBResponseGenerator
@discardableResult public func get<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .get, options: options, use: handler)
return self.on(path, method: .get, use: handler)
}

/// PUT path for closure returning type conforming to HBResponseGenerator
@discardableResult public func put<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .put, options: options, use: handler)
return self.on(path, method: .put, use: handler)
}

/// POST path for closure returning type conforming to HBResponseGenerator
@discardableResult public func post<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .post, options: options, use: handler)
return self.on(path, method: .post, use: handler)
}

/// HEAD path for closure returning type conforming to HBResponseGenerator
@discardableResult public func head<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .head, options: options, use: handler)
return self.on(path, method: .head, use: handler)
}

/// DELETE path for closure returning type conforming to HBResponseGenerator
@discardableResult public func delete<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .delete, options: options, use: handler)
return self.on(path, method: .delete, use: handler)
}

/// PATCH path for closure returning type conforming to HBResponseGenerator
@discardableResult public func patch<Handler: HBRouteHandler, Output: HBResponseGenerator>(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: Handler.Type
) -> Self where Handler.Output == Output {
return self.on(path, method: .patch, options: options, use: handler)
return self.on(path, method: .patch, use: handler)
}
}
3 changes: 1 addition & 2 deletions Sources/Hummingbird/Router/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ public final class HBRouter<Context: HBBaseRequestContext>: HBRouterMethods, HBR
@discardableResult public func on(
_ path: String,
method: HTTPRequest.Method,
options: HBRouterMethodOptions = [],
use closure: @escaping @Sendable (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
let responder = constructResponder(options: options, use: closure)
let responder = constructResponder(use: closure)
self.add(path, method: method, responder: responder)
return self
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Hummingbird/Router/RouterGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ public struct HBRouterGroup<Context: HBBaseRequestContext>: HBRouterMethods {
@discardableResult public func on(
_ path: String = "",
method: HTTPRequest.Method,
options: HBRouterMethodOptions = [],
use closure: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
let responder = constructResponder(options: options, use: closure)
let responder = constructResponder(use: closure)
let path = self.combinePaths(self.path, path)
self.router.add(path, method: method, responder: self.middlewares.constructResponder(finalResponder: responder))
return self
Expand Down
28 changes: 6 additions & 22 deletions Sources/Hummingbird/Router/RouterMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
import HTTPTypes
import NIOCore

/// Options available to routes
public struct HBRouterMethodOptions: OptionSet, Sendable {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
}

/// Conform to `HBRouterMethods` to add standard router verb (get, post ...) methods
public protocol HBRouterMethods {
associatedtype Context: HBBaseRequestContext
Expand All @@ -32,7 +24,6 @@ public protocol HBRouterMethods {
@discardableResult func on<Output: HBResponseGenerator>(
_ path: String,
method: HTTPRequest.Method,
options: HBRouterMethodOptions,
use: @Sendable @escaping (HBRequest, Context) async throws -> Output
) -> Self

Expand All @@ -44,59 +35,52 @@ extension HBRouterMethods {
/// GET path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func get(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .get, options: options, use: handler)
return on(path, method: .get, use: handler)
}

/// PUT path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func put(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .put, options: options, use: handler)
return on(path, method: .put, use: handler)
}

/// DELETE path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func delete(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .delete, options: options, use: handler)
return on(path, method: .delete, use: handler)
}

/// HEAD path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func head(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .head, options: options, use: handler)
return on(path, method: .head, use: handler)
}

/// POST path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func post(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .post, options: options, use: handler)
return on(path, method: .post, use: handler)
}

/// PATCH path for async closure returning type conforming to HBResponseGenerator
@discardableResult public func patch(
_ path: String = "",
options: HBRouterMethodOptions = [],
use handler: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> Self {
return on(path, method: .patch, options: options, use: handler)
return on(path, method: .patch, use: handler)
}

func constructResponder(
options: HBRouterMethodOptions,
use closure: @Sendable @escaping (HBRequest, Context) async throws -> some HBResponseGenerator
) -> HBCallbackResponder<Context> {
return HBCallbackResponder { request, context in
Expand Down

0 comments on commit ad0cd0b

Please sign in to comment.