Route

public struct Route
extension Route: Equatable
extension Route: CustomStringConvertible

Route is a combination of an HTTP request method, path, name, an array of Middleware, and handler.

  • A default / path.

    Declaration

    Swift

    public static let defaultPath: String
  • A typealias for the handler.

    Declaration

    Swift

    public typealias Handler = (Request) -> Any
  • 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 a default / path.

    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 ornil.

    Warning

    It may return nil 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.

  • An extracted parameter from the path of Route.

    See more

    Declaration

    Swift

    public struct Parameter
    extension Route.Parameter: Hashable
    extension Route.Parameter: CustomStringConvertible
  • See Equatable.

    Declaration

    Swift

    public static func == (lhs: Route, rhs: Route) -> Bool
  • See CustomStringConvertible.

    Declaration

    Swift

    public var description: String { get }
  • 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 returns false and nil.

  • Normalizes a path.

    Declaration

    Swift

    public static func normalize(path: String) -> String

    Parameters

    path

    A path to a resource.

    Return Value

    A normalized path.

  • Generates a regular expression pattern for the path with parameters.

    Declaration

    Swift

    public static func generatePattern(for path: String, with parameters: Set<Parameter>? = nil) -> String

    Parameters

    path

    A path to a resource.

    parameters

    A set of parameters.

    Return Value

    A regular expression pattern.

  • Creates a new instance of Parameter from a parameter pattern {name<requirement>?defaultValue}.

    Declaration

    Swift

    public static func createParameter(from part: String) -> Parameter?

    Parameters

    part

    A part of a path component.

    Return Value

    A new instance of Parameter or nil.