RouterResponse
public class RouterResponse
Router response that the server sends as a reply to RouterRequest
.
-
Set of cookies to return with the response.
Declaration
Swift
public var cookies = [String: HTTPCookie]()
-
Optional error value.
Declaration
Swift
public var error: Swift.Error?
-
HTTP headers of the response.
Declaration
Swift
public var headers: Headers
-
HTTP status code of the response.
Declaration
Swift
public var statusCode: HTTPStatusCode
-
User info. Can be used by middlewares and handlers to store and pass information on to subsequent handlers.
Declaration
Swift
public var userInfo: [String: Any] = [:]
-
End the response.
Throws
Socket.Error if an error occurred while writing to a socket.Declaration
Swift
public func end() throws
-
Send a string.
Declaration
Swift
public func send(_ str: String) -> RouterResponse
Parameters
str
the string to send.
Return Value
this RouterResponse.
-
Send an optional string.
Declaration
Swift
public func send(_ str: String?) -> RouterResponse
Parameters
str
the string to send.
Return Value
this RouterResponse.
-
Send data.
Declaration
Swift
public func send(data: Data) -> RouterResponse
Parameters
data
the data to send.
Return Value
this RouterResponse.
-
Send a file.
Throws
An error in the Cocoa domain, if the file cannot be read.Note
Sets the Content-Type header based on the
extension
of the file. If the fileName is relative, it is relative to the current directory.Declaration
Swift
public func send(fileName: String) throws -> RouterResponse
Parameters
fileName
the name of the file to send.
Return Value
this RouterResponse.
-
Send JSON.
Declaration
Swift
public func send(json: [Any]) -> RouterResponse
Parameters
json
The array to send in JSON format.
Return Value
this RouterResponse.
-
Send JSON.
Declaration
Swift
public func send(json: [String: Any]) -> RouterResponse
Parameters
json
The Dictionary to send in JSON format as a hash.
Return Value
this RouterResponse.
-
Set the status code.
Declaration
Swift
public func status(_ status: HTTPStatusCode) -> RouterResponse
Parameters
status
the HTTP status code object.
Return Value
this RouterResponse.
-
Send the HTTP status code.
Declaration
Swift
public func send(status: HTTPStatusCode) -> RouterResponse
Parameters
status
the HTTP status code.
Return Value
this RouterResponse.
-
Redirect to path with status code.
Throws
Socket.Error if an error occurred while writing to a socket.Declaration
Swift
public func redirect(_ path: String, status: HTTPStatusCode = .movedTemporarily) throws -> RouterResponse
Return Value
this RouterResponse.
-
Render a resource using Router’s template engine.
Throws
TemplatingError if no file extension was specified or there is no template engine defined for the extension.Declaration
Swift
public func render(_ resource: String, context: [String:Any], options: RenderingOptions = NullRenderingOptions()) throws -> RouterResponse
Parameters
resource
the resource name without extension.
context
a dictionary of local variables of the resource.
options
rendering options, specific per template engine
Return Value
this RouterResponse.
-
Render a resource using Router’s template engine.
Throws
TemplatingError if no file extension was specified or there is no template engine defined for the extension.Declaration
Swift
public func render<T: Encodable>(_ resource: String, with value: T, forKey key: String? = nil, options: RenderingOptions = NullRenderingOptions()) throws -> RouterResponse
Parameters
resource
the resource name without extension.
with
a value that conforms to Encodable that is used to generate the content.
forKey
A value used to match the Encodable value to the correct variable in a template file. The
forKey
value should match the desired variable in the template file.options
rendering options, specific per template engine
Return Value
this RouterResponse.
-
Render a resource using Router’s template engine.
Throws
TemplatingError if no file extension was specified or there is no template engine defined for the extension.Declaration
Swift
public func render<I: Identifier, T: Encodable>(_ resource: String, with values: [(I, T)], forKey key: String, options: RenderingOptions = NullRenderingOptions()) throws -> RouterResponse
Parameters
resource
the resource name without extension.
with
an array of tuples of type (Identifier, Encodable). The Encodable values are used to generate the content.
forKey
A value used to match the Encodable values to the correct variable in a template file. The
forKey
value should match the desired variable in the template file.options
rendering options, specific per template engine
Return Value
this RouterResponse.
-
Set headers and attach file for downloading.
Throws
An error in the Cocoa domain, if the file cannot be read.Declaration
Swift
public func send(download: String) throws
Parameters
download
the file to download.
-
Set the pre-flush lifecycle handler and return the previous one.
Declaration
Swift
public func setOnEndInvoked(_ newOnEndInvoked: @escaping LifecycleHandler) -> LifecycleHandler
Parameters
newOnEndInvoked
The new pre-flush lifecycle handler.
Return Value
The old pre-flush lifecycle handler.
-
Set the written data filter and return the previous one.
Declaration
Swift
public func setWrittenDataFilter(_ newWrittenDataFilter: @escaping WrittenDataFilter) -> WrittenDataFilter
Parameters
newWrittenDataFilter
The new written data filter.
Return Value
The old written data filter.
-
Perform content-negotiation on the Accept HTTP header on the request, when present.
Uses request.accepts() to select a handler for the request, based on the acceptable types ordered by their quality values. If the header is not specified, the default callback is invoked. When no match is found, the server invokes the default callback if exists, or responds with 406 “Not Acceptable”. The Content-Type response header is set when a callback is selected.
Throws
Socket.Error if an error occurred while writing to a socket.Declaration
Swift
public func format(callbacks: [String : ((RouterRequest, RouterResponse) -> Void)]) throws
Parameters
callbacks
a dictionary that maps content types to handlers.
-
Send Encodable Object.
Declaration
Swift
public func send<T : Encodable>(_ obj: T) -> RouterResponse
Parameters
obj
the Codable object to send.
Return Value
this RouterResponse.
-
Send JSON Encodable Object
Declaration
Swift
public func send<T : Encodable>(json: T) -> RouterResponse
Parameters
json
the JSON Encodable object to send.
Return Value
this RouterResponse.
-
Send JSON with JSONP callback.
Throws
JSONPError.invalidCallbackName
if the the callback query parameter of the request URL is missing or its value is empty or contains invalid characters (the set of valid characters is the alphanumeric characters and[]$._
).Declaration
Swift
public func send<T : Encodable>(jsonp: T, callbackParameter: String = "callback") throws -> RouterResponse
Parameters
json
the JSON object to send.
callbackParameter
the name of the URL query parameter whose value contains the JSONP callback function.
Return Value
this RouterResponse.