Type Aliases

The following type aliases are available globally.

  • The definition of the closure type that is used by the Router class when routing HTTP requests to closure.

    Declaration

    Swift

    public typealias RouterHandler = (RouterRequest, RouterResponse, @escaping () -> Void) throws -> Void

    Parameters

    request

    The RouterRequest object used to work with the incoming HTTP request.

    response

    The RouterResponse object used to respond to the HTTP request.

    next

    The closure called to invoke the next handler or middleware associated with the request.

  • A type alias declaration to describe a handler for named parameters when using Router.parameter(...). The example below shows two ways to use it, both as a function named handler to handle the id parameter and as a closure to handle the name parameter.

    Usage Example:

    let router = Router()
    func handler(request: RouterRequest, response: RouterResponse, param: String, next: @escaping () -> Void) throws -> Void {
        //Code to handle id parameter here
        next()
    }
    router.parameter("id", handler: handler)
    
    router.parameter("name") { request, response, param, next in
        //Code to handle name parameter here
        next()
    }
    router.get("/item/:id") { request, response, next in
        //This will be reached after the id parameter is handled by `handler`
    }
    router.get("/user/:name") { request, response, next in
        //This will be reached after the name parameter is handled by the closure above
    }
    

    Declaration

    Swift

    public typealias RouterParameterHandler = (RouterRequest, RouterResponse, String, @escaping () -> Void) throws -> Void

    Parameters

    request

    The RouterRequest object used to work with the incoming HTTP request.

    response

    The RouterResponse object used to respond to the HTTP request.

    param

    The named parameter to be handled.

    next

    The closure called to invoke the next handler or middleware associated with the request.

  • Type alias for Before flush (i.e. before headers and body are written) lifecycle handler.

    Declaration

    Swift

    public typealias LifecycleHandler = () -> Void
  • Type alias for written data filter, i.e. pre-write lifecycle handler.

    Declaration

    Swift

    public typealias WrittenDataFilter = (Data) -> Data