Protocols

The following protocols are available globally.

RouterMiddleware protocol

  • Defines the protocol which all Kitura compliant middleware must implement.

    Middleware are class or struct based request handlers. They are often generic in nature and not tied to a specific request.

    See more

    Declaration

    Swift

    public protocol RouterMiddleware
  • The protocol that type-safe middleware must implement to be used in Kitura Codable routes.

    Classes or structs conforming to TypeSafeMiddleware must contain a static handle function that processes an incoming request. On success, the handle function creates an instance of Self and passes this instance to the users route handler.

    Usage Example:

    In this example, a UserMiddleware struct is defined that checks the request for the header “TestHeader”. If the header is found UserMiddleware initialises itself with the header and passes itself to the route. If the header is not found it returns a RequestError.

    struct UserMiddleware: TypeSafeMiddleware {
        let header: String
    
        static func handle(
                    request: RouterRequest,
                    response: RouterResponse,
                    completion: @escaping (UserMiddleware?, RequestError?) -> Void
        ) {
            guard let expectedHeader = request.headers["TestHeader"] else {
                return completion(nil, .badRequest)
            }
            let selfInstance: UserMiddleware = UserMiddleware(header: expectedHeader)
            completion(selfInstance, nil)
        }
    }
    
    See more

    Declaration

    Swift

    public protocol TypeSafeMiddleware
  • A protocol for providing a custom method for setting the headers of the response of static file serving middleware.

    See more

    Declaration

    Swift

    public protocol ResponseHeadersSetter