Protocols
The following protocols are available globally.
-
A class that conforms to
See moreBodyDecoder
must be able to decode fromData
into aCodable
type. This class can then be used to produce input objects for a Codable route.Declaration
Swift
public protocol BodyDecoder : AnyObject
-
A class that conforms to
See moreBodyEncoder
must be able to encode aCodable
type intoData
. This class can then be used to produce output objects for a Codable route.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 byQueryDecoder
is as follows:- Any Optional type (including
String?
) defaults tonil
- Non-optional
String
successfully decodes to""
- Non-optional
Bool
decodes tofalse
- All other non-optional types throw a decoding error
Declaration
Swift
public protocol QueryParams : Decodable, Encodable
- Any Optional type (including
-
An identifier for an entity with a string representation.
Usage Example:
See more// Used in the Id field. public typealias IdentifierCodableClosure<Id: Identifier, I: Codable, O: Codable> = (Id, I, @escaping CodableResultClosure<O>) -> Void
Declaration
Swift
public protocol Identifier : Decodable, Encodable
-
An identifier for an operation object.
See moreDeclaration
Swift
public protocol Operation : Decodable, Encodable