Router

public protocol Router : AnyObject, Service

Capable of registering Responder routes and returning appropriate responders for a given request.

  • post(_:at:use:) Extension method

    Creates a Route that automatically decodes Content at the provided path using the POST method.

    router.post(User.self, at: "users") { req, user in
        print(user) // User
        // create user and return response...
    }
    

    The above route closure would automatically decode a User to requests to POST /users.

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func post<C, T>(_ content: C.Type, at path: PathComponentsRepresentable..., use closure: @escaping (Request, C) throws -> T) -> Route<Responder>
        where C: RequestDecodable, T: ResponseEncodable

    Parameters

    content

    Content type to automatically decode.

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • patch(_:at:use:) Extension method

    Creates a Route that automatically decodes Content at the provided path using the PATCH method.

    router.patch(User.self, at: "users", Int.parameter) { req, user in
        let id = try req.parameters.next(Int.self)
        print(id) // Int
        print(user) // User
        // update user and return response...
    }
    

    The above route closure would automatically decode a User to requests to PATCH /users/:id.

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func patch<C, T>(_ content: C.Type, at path: PathComponentsRepresentable..., use closure: @escaping (Request, C) throws -> T) -> Route<Responder>
        where C: RequestDecodable, T: ResponseEncodable

    Parameters

    content

    Content type to automatically decode.

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • put(_:at:use:) Extension method

    Creates a Route that automatically decodes Content at the provided path using the PUT method.

    router.put(User.self, at: "users", Int.parameter) { req, user in
        let id = try req.parameters.next(Int.self)
        print(id) // Int
        print(user) // User
        // update user and return response...
    }
    

    The above route closure would automatically decode a User to requests to PUT /users/:id.

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func put<C, T>(_ content: C.Type, at path: PathComponentsRepresentable..., use closure: @escaping (Request, C) throws -> T) -> Route<Responder>
        where C: RequestDecodable, T: ResponseEncodable

    Parameters

    content

    Content type to automatically decode.

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • on(_:_:at:use:) Extension method

    Creates a Route that automatically decodes Content at the provided path using an HTTP method.

    router.on(.PUT, User.self, at: "users", Int.parameter) { req, user in
        let id = try req.parameters.next(Int.self)
        print(id) // Int
        print(user) // User
        // update user and return response...
    }
    

    The above route closure would automatically decode a User to requests to PUT /users/:id.

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func on<C, T>(_ method: HTTPMethod, _ content: C.Type, at path: PathComponentsRepresentable..., use closure: @escaping (Request, C) throws -> T) -> Route<Responder>
        where C: RequestDecodable, T: ResponseEncodable

    Parameters

    method

    HTTPMethod to accept.

    content

    Content type to automatically decode.

    path

    Array of PathComponents to define path.

    closure

    Converts Request to a Response.

    Return Value

    Discardable Route that was just created.

  • grouped(_:) Extension method

    Creates a sub Router wrapped in the supplied middleware function.

    let group = router.grouped { req, next in
        // this closure will be called with each request
        print(req)
        // use next responder in chain to respond
        return try next.respond(to: req)
    }
    group.get("/") { ... }
    

    The above example logs all incoming requests.

    Declaration

    Swift

    public func grouped(_ respond: @escaping (Request, Responder) throws -> Future<Response>) -> Router

    Parameters

    respond

    Closure accepting a Request and Responder and returning a Future<Response>.

    Return Value

    Router with closure attached

  • group(_:configure:) Extension method

    Creates a sub Router wrapped in the supplied middleware function.

    router.group({ req, next in
        // this closure will be called with each request
        print(req)
        // use next responder in chain to respond
        return try next.respond(to: req)
    }) { group in
        group.get("/") { ... }
    }
    

    The above example logs all incoming requests.

    Declaration

    Swift

    public func group(_ respond: @escaping (Request, Responder) throws -> Future<Response>, configure: (Router) -> ())

    Parameters

    respond

    Closure accepting a Request and Responder and returning a Future<Response>.

    configure

    Closure to configure the newly created sub Router.

  • grouped(_:) Extension method

    Creates a group with the provided Middleware type.

    let group = router.grouped(DateMiddleware.self)
    group.get("date") { ... }
    

    This middleware will be lazily initialized using the container upon the first request that invokes it.

    Declaration

    Swift

    public func grouped<M>(_ middleware: M.Type) -> Router where M : Middleware
  • group(_:configure:) Extension method

    Creates a group with the provided Middleware type.

    router.group(DateMiddleware.self) { group in
        group.get("date") { ... }
    }
    

    This middleware will be lazily initialized using the container upon the first request that invokes it.

    Declaration

    Swift

    public func group<M>(_ middleware: M.Type, configure: (Router) throws -> ()) rethrows where M : Middleware
  • get(_:use:) Extension method

    Creates a Route at the provided path using the GET method.

    router.get("hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to GET /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.get("users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func get<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • post(_:use:) Extension method

    Creates a Route at the provided path using the POST method.

    router.post("hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to POST /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.post("users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func post<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • patch(_:use:) Extension method

    Creates a Route at the provided path using the PATCH method.

    router.patch("hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to PATCH /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.patch("users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func patch<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • put(_:use:) Extension method

    Creates a Route at the provided path using the PUT method.

    router.put("hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to PUT /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.put("users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func put<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • delete(_:use:) Extension method

    Creates a Route at the provided path using the DELETE method.

    router.delete("hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to DELETE /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.delete("users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func delete<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • on(_:at:use:) Extension method

    Creates a Route at the provided path using an HTTP method.

    router.on(.GET, at: "hello", "world") { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to GET /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.on(.GET, at: "users", Int.parameter) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func on<T>(_ method: HTTPMethod, at path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    method

    HTTPMethod to accept.

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • on(_:at:use:) Extension method

    Creates a Route at the provided path using an HTTP method.

    router.on(.GET, at: ["hello", "world"]) { req in
        return "Hello, world!"
    }
    

    The above route closure would return "Hello, world" to requests to GET /hello/world.

    You can use anything PathComponentsRepresentable to create the path, including dynamic parameters.

    router.on(.GET, at: ["users", Int.parameter]) { req in
        let id = try req.parameters.next(Int.self)
        return "User #\(id)"
    }
    

    See ParametersContainer for more information on using dynamic parameters.

    Declaration

    Swift

    @discardableResult
    public func on<T>(_ method: HTTPMethod, at path: [PathComponentsRepresentable], use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable

    Parameters

    method

    HTTPMethod to accept.

    path

    Variadic PathComponentsRepresentable items.

    closure

    Creates a Response for the incoming Request.

    Return Value

    Discardable Route that was just created.

  • grouped(_:) Extension method

    Creates a new Router wrapped in the supplied variadic Middleware.

    let group = router.grouped(FooMiddleware(), BarMiddleware())
    // all routes added will be wrapped by Foo & Bar middleware
    group.get(...) { ... }
    

    Declaration

    Swift

    public func grouped(_ middleware: Middleware...) -> Router

    Parameters

    middleware

    Variadic Middleware to wrap Router in.

    Return Value

    New Router wrapped in Middleware.

  • group(_:configure:) Extension method

    Creates a new Router wrapped in the supplied variadic Middleware.

    router.group(FooMiddleware(), BarMiddleware()) { group in
        // all routes added will be wrapped by Foo & Bar middleware
        group.get(...) { ... }
    }
    

    Declaration

    Swift

    public func group(_ middleware: Middleware..., configure: (Router) -> ())

    Parameters

    middleware

    Variadic Middleware to wrap Router in.

    configure

    Closure to configure the newly created Router.

  • grouped(_:) Extension method

    Creates a new Router wrapped in the supplied array of Middleware.

    let group = router.grouped([FooMiddleware(), BarMiddleware()])
    // all routes added will be wrapped by Foo & Bar middleware
    group.get(...) { ... }
    

    Declaration

    Swift

    public func grouped(_ middleware: [Middleware]) -> Router

    Parameters

    middleware

    Array of [Middleware] to wrap Router in.

    Return Value

    New Router wrapped in Middleware.

  • group(_:configure:) Extension method

    Creates a new Router wrapped in the supplied array of Middleware.

    router.group([FooMiddleware(), BarMiddleware()]) { group in
        // all routes added will be wrapped by Foo & Bar middleware
        group.get(...) { ... }
    }
    

    Declaration

    Swift

    public func group(_ middleware: [Middleware], configure: (Router) -> ())

    Parameters

    middleware

    Array of [Middleware] to wrap Router in.

    configure

    Closure to configure the newly created Router.

  • grouped(_:) Extension method

    Creates a new Router that will automatically prepend the supplied path components.

    let users = router.grouped("user")
    // Adding "user/auth/" route to router.
    users.get("auth") { ... }
    // adding "user/profile/" route to router
    users.get("profile") { ... }
    

    Declaration

    Swift

    public func grouped(_ path: PathComponentsRepresentable...) -> Router

    Parameters

    path

    Group path components separated by commas.

    Return Value

    Newly created Router wrapped in the path.

  • group(_:configure:) Extension method

    Creates a new Router that will automatically prepend the supplied path components.

    router.group("user") { users in
        // Adding "user/auth/" route to router.
        users.get("auth") { ... }
        // adding "user/profile/" route to router
        users.get("profile") { ... }
    }
    

    Declaration

    Swift

    public func group(_ path: PathComponentsRepresentable..., configure: (Router) -> ())

    Parameters

    path

    Group path components separated by commas.

    configure

    Closure to configure the newly created Router.