CodableHelpers
public struct CodableHelpers
Building blocks for Codable routing, intended for convenience when implementing
extensions to the Router
that interoperate with Codable
types.
-
Check if the given request has content type JSON
Declaration
Swift
public static func isContentTypeJSON(_ request: RouterRequest) -> Bool
Parameters
request
The RouterRequest to check
Return Value
True if the content type of the request is application/json, false otherwise
-
Check if the given request has content type x-www-form-urlencoded
Declaration
Swift
public static func isContentTypeURLEncoded(_ request: RouterRequest) -> Bool
Parameters
request
The RouterRequest to check
Return Value
True if the content type of the request is application/x-www-form-urlencoded, false otherwise
-
Get the HTTPStatusCode corresponding to the provided RequestError
Declaration
Swift
public static func httpStatusCode(from error: RequestError) -> HTTPStatusCode
Parameters
from
The RequestError to map to a HTTPStatusCode
Return Value
A HTTPStatusCode corresponding to the RequestError http code if valid, or HTTPStatusCode.unknown otherwise
-
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.
Declaration
Swift
public static func constructResultHandler(response: RouterResponse, completion: @escaping () -> Void) -> ResultClosure
Parameters
response
The
RouterResponse
to which the codable response error and status code will be writtencompletion
The completion to be called after the returned closure completes execution.
Return Value
The closure to pass to the codable route handler. The closure takes one argument
(RequestError?)
. If the argument isnil
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.
-
Create a closure that can be called by a codable route handler that provides an optional
Codable
body and an optionalRequestError
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.
Declaration
Swift
public static func constructOutResultHandler<OutputType>(successStatus: HTTPStatusCode = .OK, response: RouterResponse, completion: @escaping () -> Void) -> CodableResultClosure<OutputType> where OutputType : Decodable, OutputType : Encodable
Parameters
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 writtencompletion
The completion to be called after the returned closure completes execution.
Return Value
The closure to pass to the codable route handler. The closure takes two arguments
(OutputType?, RequestError?)
. If the second (error) argument isnil
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.
-
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.
Declaration
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
Parameters
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 writtencompletion
The completion to be called after the returned closure completes execution.
Return Value
The closure to pass to the codable route handler. The closure takes two arguments
([(Id, OutputType)]?, RequestError?)
. If the second (error) argument isnil
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.
-
Create a closure that can be called by a codable route handler that provides an optional
Identifier
id, optionalCodable
body and an optionalRequestError
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.
Declaration
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
Parameters
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 writtencompletion
The completion to be called after the returned closure completes execution.
Return Value
The closure to pass to the codable route handler. The closure takes three arguments
(IdType?, OutputType?, RequestError?)
. If the third (error) argument isnil
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.
-
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.
Declaration
Swift
public static func readCodableOrSetResponseStatus<InputType>(_ inputCodableType: InputType.Type, from request: RouterRequest, response: RouterResponse) -> InputType? where InputType : Decodable, InputType : Encodable
Parameters
inputCodableType
The
InputType.Type
(a concrete type complying toCodable
) 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.Return Value
An instance of
InputType
representing the decoded body data. -
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.
Declaration
Swift
public static func parseIdOrSetResponseStatus<IdType>(_ idType: IdType.Type, from request: RouterRequest, response: RouterResponse) -> IdType? where IdType : Identifier
Parameters
idType
The
IdType.Type
(a concrete type complying toIdentifier
) 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.Return Value
An instance of
IdType
representing the id.