Middleware

public protocol Middleware : AnyObject

Middleware can mutate aRequest before a resolved Route‘s handler is called and Response before a client receives it. It can also return non-Void to stop propagating events down or up in the chain.

  • A typealias for the handler down in the chain.

    Declaration

    Swift

    typealias RequestHandler = (Request) -> Void
  • A typealias for the handler up in the chain.

    Declaration

    Swift

    typealias ResponseHandler = (Response) -> Void
  • handle(request:nextHandler:) Default implementation

    Receives a Request and can call the next RequestHandler down in the chain.

    Default Implementation

    Calls the next RequestHandler down in the chain.

    Declaration

    Swift

    func handle(request: Request, nextHandler: @escaping RequestHandler) -> Any

    Parameters

    request

    An instance of Request.

    nextHandler

    The next RequestHandler down in the chain.

    Return Value

    Void to call the next RequestHandler down in the chain or non-Void to return a Response to a client.

  • handle(response:nextHandler:) Default implementation

    Receives a Response and can call the next ResponseHandler up in the chain.

    Default Implementation

    Calls the next ResponseHandler up in the chain.

    Declaration

    Swift

    func handle(response: Response, nextHandler: @escaping ResponseHandler) -> Any

    Parameters

    response

    An instance of Response.

    nextHandler

    The next ResponseHandler up in the chain.

    Return Value

    Void to call the next ResponseHandler up in the chain or non-Void to return a Response to a client.