Route
public struct Route
extension Route: Equatable
extension Route: CustomStringConvertible
A combination of an HTTP request method, path, name, an array of Middleware
, and a handler that points to a location where a resource exists.
-
A default path
/
.Declaration
Swift
public static let defaultPath: String
-
Handler
AsynchronousA typealias for the handler.
Declaration
Swift
public typealias Handler = (Request) async throws -> Encodable
-
An HTTP request method.
Declaration
Swift
public var method: Request.Method
-
A path to a resource.
Declaration
Swift
public let path: String
-
A regular expression pattern generated for the path.
Declaration
Swift
public private(set) var pattern: String { get }
-
A unique name for
Route
.Declaration
Swift
public var name: String
-
A read-only set of parameters extracted from the path.
Declaration
Swift
public var parameters: Set<Parameter> { get }
-
An array of registered
Middleware
.Declaration
Swift
public var middleware: [Middleware]
-
A handler to call.
Declaration
Swift
public var handler: Handler
-
Initializes a new instance with the
defaultPath
.Declaration
Swift
public init( method: Request.Method, name: String = "", middleware: [Middleware] = .init(), handler: @escaping Handler )
Parameters
method
An HTTP request method.
name
A unique name for
Route
. Defaults to an empty string.middleware
An array of registered
Middleware
. Defaults to an empty array.handler
A handler to call.
-
Initializes a new instance or
nil
.Warning
It may returnnil
if the path is invalid.Declaration
Swift
public init?( method: Request.Method, path: String, name: String = "", middleware: [Middleware] = .init(), handler: @escaping Handler )
Parameters
method
An HTTP request method.
path
A path to a resource.
name
A unique name for
Route
. Defaults to an empty string.middleware
An array of registered
Middleware
. Defaults to an empty array.handler
A handler to call.
-
See
Equatable
.Declaration
Swift
public static func == (lhs: Route, rhs: Route) -> Bool
-
See
CustomStringConvertible
.Declaration
Swift
public var description: String { get }
-
Gets a parameter value for a parameter name.
Declaration
Swift
public subscript<T>(parameter name: String) -> T? { get }
Parameters
parameter
A parameter name.
Return Value
A parameter value.
-
Checks if a path is valid or not.
Declaration
Swift
public static func isValid(path: String) -> (Bool, Set<Parameter>)
Parameters
path
A path to a resource.
Return Value
If the path is valid, it returns
true
and a set of extracted parameters. Otherwise, it returnsfalse
and an emptySet<Parameter>
. -
Generates a regular expression pattern for the path with parameters.
Declaration
Swift
public static func generatePattern(for path: String, with parameters: Set<Parameter> = .init()) -> String
Parameters
path
A path to a resource.
parameters
A set of parameters.
Return Value
A regular expression pattern.