Protocols

The following protocols are available globally.

  • A class that conforms to BodyDecoder must be able to decode from Data into a Codable type. This class can then be used to produce input objects for a Codable route.

    See more

    Declaration

    Swift

    public protocol BodyDecoder : AnyObject
  • A class that conforms to BodyEncoder must be able to encode a Codable type into Data. This class can then be used to produce output objects for a Codable route.

    See more

    Declaration

    Swift

    public protocol BodyEncoder : AnyObject
  • An object that conforms to QueryParams is identified as being decodable from URLEncoded data. This can be applied to a Codable route to define the names and types of the expected query parameters, and provide type-safe access to their values. The QueryDecoder is used to decode the URL encoded parameters into an instance of the conforming type.

    Usage Example:

    struct Query: QueryParams {
       let id: Int
    }
    router.get("/user") { (query: Query, respondWith: (User?, RequestError?) -> Void) in
        guard let user: User = userArray[query.id] else {
           return respondWith(nil, .notFound)
        }
        respondWith(user, nil)
    }
    

    Decoding Empty Values:

    When an HTML form is sent with an empty or unchecked field, the corresponding key/value pair is sent with an empty value (i.e. &key1=&key2=). The corresponding mapping to Swift types performed by QueryDecoder is as follows:

    • Any Optional type (including String?) defaults to nil
    • Non-optional String successfully decodes to ""
    • Non-optional Bool decodes to false
    • All other non-optional types throw a decoding error
    See more

    Declaration

    Swift

    public protocol QueryParams : Decodable, Encodable
  • An identifier for an entity with a string representation.

    Usage Example:

     // Used in the Id field.
     public typealias IdentifierCodableClosure<Id: Identifier, I: Codable, O: Codable> = (Id, I, @escaping CodableResultClosure<O>) -> Void
    
    See more

    Declaration

    Swift

    public protocol Identifier : Decodable, Encodable
  • An identifier for an operation object.

    See more

    Declaration

    Swift

    public protocol Operation : Decodable, Encodable