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) -> BoolParameters
requestThe 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) -> BoolParameters
requestThe 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) -> HTTPStatusCodeParameters
fromThe 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
RequestErrorNote
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) -> ResultClosureParameters
responseThe
RouterResponseto which the codable response error and status code will be writtencompletionThe 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 isnilthen 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
Codablebody and an optionalRequestErrorNote
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 : EncodableParameters
successStatusThe
HTTPStatusCodeto use for a successful response (see below)responseThe
RouterResponseto which the codable response body (or codable error) and status code will be writtencompletionThe 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 isnilthen the first (body) argument should be non-niland the response will be considered successful. If the second (error) argument is non-nilthen 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
RequestErrorNote
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 : EncodableParameters
successStatusThe
HTTPStatusCodeto use for a successful response (see below)responseThe
RouterResponseto which the codable response body (or codable error) and status code will be writtencompletionThe 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 isnilthen the first argument (body) should be non-niland the response will be considered successful. If the second (error) argument is non-nilthen 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
Identifierid, optionalCodablebody and an optionalRequestErrorNote
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 : EncodableParameters
successStatusThe
HTTPStatusCodeto use for a successful response (see below)responseThe
RouterResponseto which the id, codable response body (or codable error) and status code will be writtencompletionThe 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 isnilthen the first (id) and second (body) arguments should both be non-niland the response will be considered successful. If the third (error) argument is non-nilthen 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 : EncodableParameters
inputCodableTypeThe
InputType.Type(a concrete type complying toCodable) to use to represent the decoded body data.requestThe
RouterRequestfrom which to read the body data.responseThe
RouterResponseon which to set any error HTTP status codes in cases where reading or decoding the data fails.Return Value
An instance of
InputTyperepresenting 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 : IdentifierParameters
idTypeThe
IdType.Type(a concrete type complying toIdentifier) to use to represent the id.requestThe
RouterRequestfrom which to read the URL.responseThe
RouterResponseon which to set any error HTTP status codes in cases where reading or decoding the data fails.Return Value
An instance of
IdTyperepresenting the id.
View on GitHub
CodableHelpers Structure Reference