RouteCollection

public final class RouteCollection
extension RouteCollection: Collection

RouteCollection helps to create Routes with path and name prefixes and can assign an array of Middleware to apply before Routes’ handler is called.

  • A typealias for the underlying storage type.

    Declaration

    Swift

    public typealias DictionaryType = [Request.Method : [Route]]
  • A path prefix for Routes.

    Declaration

    Swift

    public private(set) var path: String { get }
  • A name prefix for Routes.

    Declaration

    Swift

    public private(set) var name: String { get }
  • A read-only array of registered Middleware.

    Declaration

    Swift

    public let middleware: [Middleware]
  • A Builder.

    Declaration

    Swift

    public private(set) lazy var builder: Builder { get set }
  • Initializes a new instance with defaults.

    Declaration

    Swift

    public init()
  • Initializes a new instance.

    Warning

    It may return nil if the path prefix is invalid.

    Declaration

    Swift

    public init?(
        _ routes: RouteCollection = .init(),
        path: String = Route.defaultPath,
        name: String = "",
        middleware: [Middleware] = .init()
    )

    Parameters

    routes

    An instance of RouteCollection.

    path

    A path prefix to a resource. Defaults to /.

    name

    A name prefix for Routes. Defaults to an empty string.

    middleware

    An array of registered Middleware. Defaults to an empty array.

  • Initializes a new instance with another RouteCollection.

    Declaration

    Swift

    public convenience init(_ routes: RouteCollection)

    Parameters

    routes

    An instance of RouteCollection.

  • Initializes a new instance with An array of Routes.

    Declaration

    Swift

    public convenience init(_ routes: [Route])

    Parameters

    routes

    An array of Routes.

  • Initializes a new instance with a name prefix.

    Declaration

    Swift

    public convenience init(name: String)

    Parameters

    name

    A name prefix for Routes.

  • Initializes a new instance.

    Warning

    It may return nil if the path prefix is invalid.

    Declaration

    Swift

    public convenience init?(
        _ routes: [Route],
        path: String = Route.defaultPath,
        name: String = ""
    )

    Parameters

    routes

    An array of Routes.

    path

    A path prefix for Routes.

    name

    A name prefix for Routes.

  • Builder helps to build a tree of RouteCollections with Routes.

    See more

    Declaration

    Swift

    open class Builder
  • Gets or sets an array of Routes for a particular HTTP request method.

    Declaration

    Swift

    public subscript(method: Request.Method) -> [Route] { get set }

    Parameters

    method

    An HTTP request method.

    Return Value

    An array of Routes for a particular HTTP request method.

  • Inserts Routes from another RouteCollection.

    Declaration

    Swift

    public func insert(_ routes: RouteCollection)

    Parameters

    routes

    An instance of RouteCollection.

  • Inserts a Route.

    Declaration

    Swift

    @discardableResult
    public func insert(_ route: Route) -> Route?

    Parameters

    route

    An instance of Route.

    Return Value

    An instance of an inserted Route or nil if the Route already exists.

  • Checks if a Route exists or not.

    Declaration

    Swift

    public func has(_ route: Route) -> Bool

    Parameters

    route

    An instance of Route.

    Return Value

    true if a Route exists. false if it doesn’t.

  • Removes an array of Routes.

    Declaration

    Swift

    public func remove(_ routes: [Route])

    Parameters

    routes

    An array of Routes.

  • Removes a Route.

    Declaration

    Swift

    @discardableResult
    public func remove(_ route: Route) -> Route?

    Parameters

    route

    An instance of Route.

    Return Value

    An instance of a deleted Route or nil if the Route doesn’t exist.

  • See Collection.

    Declaration

    Swift

    public typealias Index = DictionaryType.Index
  • See Collection.

    Declaration

    Swift

    public typealias Element = DictionaryType.Element
  • See Collection.

    Declaration

    Swift

    public var startIndex: Index { get }
  • See Collection.

    Declaration

    Swift

    public var endIndex: Index { get }
  • See Collection.

    Declaration

    Swift

    public subscript(index: Index) -> Element { get }
  • See Collection.

    Declaration

    Swift

    public func index(after index: Index) -> Index