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 Asynchronous

    A 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 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 }
  • 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 returns false and an empty Set<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.

  • Creates a new instance of Parameter based on the 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.

  • Updates a parameter’s value extracted from the path.

    Declaration

    Swift

    @discardableResult
    public mutating func updateParameter(_ parameter: Parameter) -> Parameter?

    Parameters

    parameter

    An instance of Parameter.

    Return Value

    An updated instance of Parameter or nil.