Vapor Documentation

Protocol Routes​Builder

public protocol RoutesBuilder  
%433 RoutesBuilder RoutesBuilder Routes Routes Routes->RoutesBuilder Application Application Application->RoutesBuilder HTTPRoutesGroup HTTPRoutesGroup HTTPRoutesGroup->RoutesBuilder MiddlewareGroup MiddlewareGroup MiddlewareGroup->RoutesBuilder

Types Conforming to Routes​Builder

Application

Core type representing a Vapor application.

Routes

Default Implementations

get(_:​use:​)

@discardableResult
    public func get<Response>(
        _ path: PathComponent...,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

get(_:​use:​)

@discardableResult
    public func get<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

post(_:​use:​)

@discardableResult
    public func post<Response>(
        _ path: PathComponent...,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

post(_:​use:​)

@discardableResult
    public func post<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

patch(_:​use:​)

@discardableResult
    public func patch<Response>(
        _ path: PathComponent...,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

patch(_:​use:​)

@discardableResult
    public func patch<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

put(_:​use:​)

@discardableResult
    public func put<Response>(
        _ path: PathComponent...,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

put(_:​use:​)

@discardableResult
    public func put<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

delete(_:​use:​)

@discardableResult
    public func delete<Response>(
        _ path: PathComponent...,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

delete(_:​use:​)

@discardableResult
    public func delete<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

on(_:​_:​body:​use:​)

@discardableResult
    public func on<Response>(
        _ method: HTTPMethod,
        _ path: PathComponent...,
        body: HTTPBodyStreamStrategy = .collect,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

on(_:​_:​body:​use:​)

@discardableResult
    public func on<Response>(
        _ method: HTTPMethod,
        _ path: [PathComponent],
        body: HTTPBodyStreamStrategy = .collect,
        use closure: @escaping (Request) async throws -> Response
    ) -> Route
        where Response: AsyncResponseEncodable

web​Socket(_:​max​Frame​Size:​should​Upgrade:​on​Upgrade:​)

@discardableResult
    public func webSocket(
        _ path: PathComponent...,
        maxFrameSize: WebSocketMaxFrameSize = .`default`,
        shouldUpgrade: @escaping ((Request) async throws -> HTTPHeaders?) = { _ in [:] },
        onUpgrade: @escaping (Request, WebSocket) async -> ()
    ) -> Route  

Adds a route for opening a web socket connection

Parameters

path Path​Component

Path components separated by commas.

max​Frame​Size Web​Socket​Max​Frame​Size

The maximum allowed frame size. See NIOWebSocketServerUpgrader.

should​Upgrade @escaping ((Request) async throws -> HTTPHeaders?)

Closure to apply before upgrade to web socket happens. Returns additional HTTPHeaders for response, nil to deny upgrading. See NIOWebSocketServerUpgrader.

on​Upgrade @escaping (Request, Web​Socket) async -> ()

Closure to apply after web socket is upgraded successfully.

Returns

Route instance for newly created web socket endpoint

web​Socket(_:​max​Frame​Size:​should​Upgrade:​on​Upgrade:​)

@discardableResult
    public func webSocket(
        _ path: [PathComponent],
        maxFrameSize: WebSocketMaxFrameSize = .`default`,
        shouldUpgrade: @escaping ((Request) async throws -> HTTPHeaders?) = { _ in [:] },
        onUpgrade: @escaping (Request, WebSocket) async -> ()
    ) -> Route  

Adds a route for opening a web socket connection

Parameters

path [Path​Component]

Array of path components.

max​Frame​Size Web​Socket​Max​Frame​Size

The maximum allowed frame size. See NIOWebSocketServerUpgrader.

should​Upgrade @escaping ((Request) async throws -> HTTPHeaders?)

Closure to apply before upgrade to web socket happens. Returns additional HTTPHeaders for response, nil to deny upgrading. See NIOWebSocketServerUpgrader.

on​Upgrade @escaping (Request, Web​Socket) async -> ()

Closure to apply after web socket is upgraded successfully.

Returns

Route instance for newly created web socket endpoint

register(collection:​)

public func register(collection: RouteCollection) throws  

Registers all of the routes in the group to this router.

Parameters

collection Route​Collection

RouteCollection to register.

grouped(_:​)

public func grouped(_ path: PathComponent...) -> RoutesBuilder  

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") { ... }

Parameters

path Path​Component

Group path components separated by commas.

Returns

Newly created Router wrapped in the path.

grouped(_:​)

public func grouped(_ path: [PathComponent]) -> RoutesBuilder  

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") { ... }

Parameters

path [Path​Component]

Array of group path components.

Returns

Newly created Router wrapped in the path.

group(_:​configure:​)

public func group(_ path: PathComponent..., configure: (RoutesBuilder) throws -> ()) rethrows  

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") { ... }
}

Parameters

path Path​Component

Group path components separated by commas.

configure (Routes​Builder) throws -> ()

Closure to configure the newly created Router.

group(_:​configure:​)

public func group(_ path: [PathComponent], configure: (RoutesBuilder) throws -> ()) rethrows  

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") { ... }
}

Parameters

path [Path​Component]

Array of group path components.

configure (Routes​Builder) throws -> ()

Closure to configure the newly created Router.

get(_:​use:​)

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

get(_:​use:​)

@discardableResult
    public func get<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

post(_:​use:​)

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

post(_:​use:​)

@discardableResult
    public func post<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

patch(_:​use:​)

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

patch(_:​use:​)

@discardableResult
    public func patch<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

put(_:​use:​)

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

put(_:​use:​)

@discardableResult
    public func put<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

delete(_:​use:​)

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

delete(_:​use:​)

@discardableResult
    public func delete<Response>(
        _ path: [PathComponent],
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

on(_:​_:​body:​use:​)

@discardableResult
    public func on<Response>(
        _ method: HTTPMethod,
        _ path: PathComponent...,
        body: HTTPBodyStreamStrategy = .collect,
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

on(_:​_:​body:​use:​)

@discardableResult
    public func on<Response>(
        _ method: HTTPMethod,
        _ path: [PathComponent],
        body: HTTPBodyStreamStrategy = .collect,
        use closure: @escaping (Request) throws -> Response
    ) -> Route
        where Response: ResponseEncodable

grouped(_:​)

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

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(...) { ... }

Parameters

middleware Middleware

Variadic Middleware to wrap Router in.

Returns

New Router wrapped in Middleware.

group(_:​configure:​)

public func group(_ middleware: Middleware..., configure: (RoutesBuilder) throws -> ()) rethrows  

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(...) { ... }
}

Parameters

middleware Middleware

Variadic Middleware to wrap Router in.

configure (Routes​Builder) throws -> ()

Closure to configure the newly created Router.

grouped(_:​)

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

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(...) { ... }

Parameters

middleware [Middleware]

Array of [Middleware] to wrap Router in.

Returns

New Router wrapped in Middleware.

group(_:​configure:​)

public func group(_ middleware: [Middleware], configure: (RoutesBuilder) throws -> ()) rethrows  

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(...) { ... }
}

Parameters

middleware [Middleware]

Array of [Middleware] to wrap Router in.

configure (Routes​Builder) throws -> ()

Closure to configure the newly created Router.

web​Socket(_:​max​Frame​Size:​should​Upgrade:​on​Upgrade:​)

@discardableResult
    public func webSocket(
        _ path: PathComponent...,
        maxFrameSize: WebSocketMaxFrameSize = .`default`,
        shouldUpgrade: @escaping ((Request) -> EventLoopFuture<HTTPHeaders?>) = {
            $0.eventLoop.makeSucceededFuture([:])
        },
        onUpgrade: @escaping (Request, WebSocket) -> ()
    ) -> Route  

Adds a route for opening a web socket connection

Parameters

path Path​Component

Path components separated by commas.

max​Frame​Size Web​Socket​Max​Frame​Size

The maximum allowed frame size. See NIOWebSocketServerUpgrader.

should​Upgrade @escaping ((Request) -> Event​Loop​Future<HTTPHeaders?>)

Closure to apply before upgrade to web socket happens. Returns additional HTTPHeaders for response, nil to deny upgrading. See NIOWebSocketServerUpgrader.

on​Upgrade @escaping (Request, Web​Socket) -> ()

Closure to apply after web socket is upgraded successfully.

Returns

Route instance for newly created web socket endpoint

web​Socket(_:​max​Frame​Size:​should​Upgrade:​on​Upgrade:​)

@discardableResult
    public func webSocket(
        _ path: [PathComponent],
        maxFrameSize: WebSocketMaxFrameSize = .`default`,
        shouldUpgrade: @escaping ((Request) -> EventLoopFuture<HTTPHeaders?>) = {
            $0.eventLoop.makeSucceededFuture([:])
        },
        onUpgrade: @escaping (Request, WebSocket) -> ()
    ) -> Route  

Adds a route for opening a web socket connection

Parameters

path [Path​Component]

Array of path components.

max​Frame​Size Web​Socket​Max​Frame​Size

The maximum allowed frame size. See NIOWebSocketServerUpgrader.

should​Upgrade @escaping ((Request) -> Event​Loop​Future<HTTPHeaders?>)

Closure to apply before upgrade to web socket happens. Returns additional HTTPHeaders for response, nil to deny upgrading. See NIOWebSocketServerUpgrader.

on​Upgrade @escaping (Request, Web​Socket) -> ()

Closure to apply after web socket is upgraded successfully.

Returns

Route instance for newly created web socket endpoint

Requirements

add(_:​)

func add(_ route: Route)