docs/Structs/CodableHelpers.html
public struct CodableHelpers
Building blocks for Codable routing, intended for convenience when implementing extensions to the Router that interoperate with Codable types.
`
isContentTypeJSON(_:)
`
Check if the given request has content type JSON
Swift
public static func isContentTypeJSON(_ request: RouterRequest) -> Bool
| request |
The RouterRequest to check
|
True if the content type of the request is application/json, false otherwise
`
isContentTypeURLEncoded(_:)
`
Check if the given request has content type x-www-form-urlencoded
Swift
public static func isContentTypeURLEncoded(_ request: RouterRequest) -> Bool
| request |
The RouterRequest to check
|
True if the content type of the request is application/x-www-form-urlencoded, false otherwise
`
httpStatusCode(from:)
`
Get the HTTPStatusCode corresponding to the provided RequestError
Swift
public static func httpStatusCode(from error: RequestError) -> HTTPStatusCode
| from |
The RequestError to map to a HTTPStatusCode
|
A HTTPStatusCode corresponding to the RequestError http code if valid, or HTTPStatusCode.unknown otherwise
`
constructResultHandler(response:completion:)
`
Create a closure that can be called by a codable route handler that provides only an optional RequestError
Note
This function is intended for use by the codable router or extensions thereof. It will create a closure that can be passed to the registered route handler.
Swift
public static func constructResultHandler(response: RouterResponse, completion: @escaping () -> Void) -> ResultClosure
| response |
The RouterResponse to which the codable response error and status code will be written
|
| completion |
The completion to be called after the returned closure completes execution.
|
The closure to pass to the codable route handler. The closure takes one argument (RequestError?). If the argument is nil then the response will be considered successful, otherwise it will be considered failed.
If successful, the HTTP status code will be set to `HTTPStatusCode.noContent` and no
body will be sent.
If failed, the HTTP status code used for the response will be set to either the
`httpCode` of the `RequestError`, if that is a valid HTTP status code, or
`HTTPStatusCode.unknown` otherwise. If the `RequestError` has a codable `body` then
it will be encoded and sent as the body of the response.
`
constructOutResultHandler(successStatus:response:completion:)
`
Create a closure that can be called by a codable route handler that provides an optional Codable body and an optional RequestError
Note
This function is intended for use by the codable router or extensions thereof. It will create a closure that can be passed to the registered route handler.
Swift
public static func constructOutResultHandler<OutputType>(successStatus: HTTPStatusCode = .OK, response: RouterResponse, completion: @escaping () -> Void) -> CodableResultClosure<OutputType> where OutputType : Decodable, OutputType : Encodable
| successStatus |
The HTTPStatusCode to use for a successful response (see below)
|
| response |
The RouterResponse to which the codable response body (or codable error) and status code will be written
|
| completion |
The completion to be called after the returned closure completes execution.
|
The closure to pass to the codable route handler. The closure takes two arguments (OutputType?, RequestError?). If the second (error) argument is nil then the first (body) argument should be non-nil and the response will be considered successful. If the second (error) argument is non-nil then the first argument is ignored and the response is considered failed.
If successful, the HTTP status code will be set to `successStatus` and the `CodableResultClosure` output
will be JSON encoded and sent as the body of the response.
If failed, the HTTP status code used for the response will be set to either the
`httpCode` of the `RequestError`, if that is a valid HTTP status code, or
`HTTPStatusCode.unknown` otherwise. If the `RequestError` has a codable `body` then
it will be encoded and sent as the body of the response.
`
constructTupleArrayOutResultHandler(successStatus:response:completion:)
`
Create a closure that can be called by a codable route handler that provides an array of tuples of (Identifier, Codable) and an optional RequestError
Note
This function is intended for use by the codable router or extensions thereof. It will create a closure that can be passed to the registered route handler.
Swift
public static func constructTupleArrayOutResultHandler<Id, OutputType>(successStatus: HTTPStatusCode = .OK, response: RouterResponse, completion: @escaping () -> Void) -> IdentifierCodableArrayResultClosure<Id, OutputType> where Id : Identifier, OutputType : Decodable, OutputType : Encodable
| successStatus |
The HTTPStatusCode to use for a successful response (see below)
|
| response |
The RouterResponse to which the codable response body (or codable error) and status code will be written
|
| completion |
The completion to be called after the returned closure completes execution.
|
The closure to pass to the codable route handler. The closure takes two arguments ([(Id, OutputType)]?, RequestError?). If the second (error) argument is nil then the first argument (body) should be non-nil and the response will be considered successful. If the second (error) argument is non-nil then the first argument is ignored and the response is considered failed.
If successful, the HTTP status code will be set to `successStatus` and the `IdentifierCodableArrayResultClosure` output
will be JSON encoded as an array of dictionaries, which is then sent as the body of the response.
If failed, the HTTP status code used for the response will be set to either the
`httpCode` of the `RequestError`, if that is a valid HTTP status code, or
`HTTPStatusCode.unknown` otherwise. If the `RequestError` has a codable `body` then
it will be encoded and sent as the body of the response.
`
constructIdentOutResultHandler(successStatus:response:completion:)
`
Create a closure that can be called by a codable route handler that provides an optional Identifier id, optional Codable body and an optional RequestError
Note
This function is intended for use by the codable router or extensions thereof. It will create a closure that can be passed to the registered route handler.
Swift
public static func constructIdentOutResultHandler<IdType, OutputType>(successStatus: HTTPStatusCode = .OK, response: RouterResponse, completion: @escaping () -> Void) -> IdentifierCodableResultClosure<IdType, OutputType> where IdType : Identifier, OutputType : Decodable, OutputType : Encodable
| successStatus |
The HTTPStatusCode to use for a successful response (see below)
|
| response |
The RouterResponse to which the id, codable response body (or codable error) and status code will be written
|
| completion |
The completion to be called after the returned closure completes execution.
|
The closure to pass to the codable route handler. The closure takes three arguments (IdType?, OutputType?, RequestError?). If the third (error) argument is nil then the first (id) and second (body) arguments should both be non-nil and the response will be considered successful. If the third (error) argument is non-nil then the first and second arguments are ignored and the response is considered failed.
If successful, the HTTP status code will be set to `successStatus`, the `IdentifierCodableResultClosure` output
will be JSON encoded and sent as the body of the response, and the `Location` header of the
response will be set to the id (by converting it to a `String` using its `value` property).
If failed, the HTTP status code used for the response will be set to either the
`httpCode` of the `RequestError`, if that is a valid HTTP status code, or
`HTTPStatusCode.unknown` otherwise. If the `RequestError` has a codable `body` then
it will be encoded and sent as the body of the response.
`
readCodableOrSetResponseStatus(_:from:response:)
`
Read data from the request body and decode as the given InputType, setting an error status on the given response in the case of failure.
Note
This function is intended for use by the codable router or extensions thereof. It will read the codable input object from the request that can be passed to a codable route handler.
Swift
public static func readCodableOrSetResponseStatus<InputType>(_ inputCodableType: InputType.Type, from request: RouterRequest, response: RouterResponse) -> InputType? where InputType : Decodable, InputType : Encodable
| inputCodableType |
The InputType.Type (a concrete type complying to Codable) to use to represent the decoded body data.
|
| request |
The RouterRequest from which to read the body data.
|
| response |
The RouterResponse on which to set any error HTTP status codes in cases where reading or decoding the data fails.
|
An instance of InputType representing the decoded body data.
`
parseIdOrSetResponseStatus(_:from:response:)
`
Read an id from the request URL, setting an error status on the given response in the case of failure.
Note
This function is intended for use by the codable router or extensions thereof. It will read and id from the request that can be passed to a codable route handler.
Swift
public static func parseIdOrSetResponseStatus<IdType>(_ idType: IdType.Type, from request: RouterRequest, response: RouterResponse) -> IdType? where IdType : Identifier
| idType |
The IdType.Type (a concrete type complying to Identifier) to use to represent the id.
|
| request |
The RouterRequest from which to read the URL.
|
| response |
The RouterResponse on which to set any error HTTP status codes in cases where reading or decoding the data fails.
|
An instance of IdType representing the id.