Protocol
URLQueryContainer
public protocol URLQueryContainer
Helper for encoding and decoding data from an HTTP request query string.
See Request.query
for more information.
Default Implementations
encode(_:)
public mutating func encode<C>(_ content: C) throws
where C: Content
Serializes a Content
type to this HTTP request query string.
let flags: Flags ...
try req.query.encode(flags)
A MediaType.urlEncodedForm
encoder will be used.
Parameters
Name | Type | Description |
---|---|---|
content | C |
|
Throws
Any errors making the decoder for this media type or serializing the query string.
decode(_:)
public func decode<C>(_ content: C.Type) throws -> C
where C: Content
Parses a Content
type from this HTTP request query string.
let flags = try req.query.decode(Flags.self)
print(flags) // Flags
A MediaType.urlEncodedForm
decoder will be used.
Parameters
Name | Type | Description |
---|---|---|
content | C.Type |
|
Throws
Any errors making the decoder for this media type or parsing the query string.
Returns
Instance of the Decodable
type.
encode(_:)
public mutating func encode<E>(_ encodable: E) throws where E: Encodable
Serializes an Encodable
type to this HTTP request query string.
let flags: Flags ...
try req.query.encode(flags)
A MediaType.urlEncodedForm
encoder will be used.
Parameters
Name | Type | Description |
---|---|---|
encodable | E |
|
Throws
Any errors making the decoder for this media type or serializing the query string.
decode(_:)
public func decode<D>(_ decodable: D.Type) throws -> D where D: Decodable
Parses a Decodable
type from this HTTP request query string.
let flags = try req.query.decode(Flags.self)
print(flags) // Flags
A MediaType.urlEncodedForm
decoder will be used.
Parameters
Name | Type | Description |
---|---|---|
decodable | D.Type |
|
Throws
Any errors making the decoder for this media type or parsing the query string.
Returns
Instance of the Decodable
type.
subscript(_:)
public subscript<D>(_ keyPath: CodingKeyRepresentable...) -> D?
where D: Decodable
Fetches a single Decodable
value at the supplied key-path from this HTTP request's query string.
Note: This is a non-throwing subscript convenience method for get(_:at:)
.
let name: String? = req.query["user", "name"]
print(name) /// String?
Parameters
Name | Type | Description |
---|---|---|
keyPath | CodingKeyRepresentable |
One or more key path components to the desired value. |
Returns
Decoded Decodable
value.
subscript(_:at:)
public subscript<D>(_ type: D.Type, at keyPath: CodingKeyRepresentable...) -> D?
where D: Decodable
Fetches a single Decodable
value at the supplied key-path from this HTTP request's query string.
Note: This is a non-throwing subscript convenience method for get(_:at:)
.
let name = req.query[String.self, at: "user", "name"]
print(name) /// String?
Parameters
Name | Type | Description |
---|---|---|
type | D.Type |
The |
keyPath | CodingKeyRepresentable |
One or more key path components to the desired value. |
Returns
Decoded Decodable
value.
subscript(_:at:)
public subscript<D>(_ type: D.Type, at keyPath: [CodingKeyRepresentable]) -> D?
where D: Decodable
Fetches a single Decodable
value at the supplied key-path from this HTTP request's query string.
Note: This is a non-throwing subscript convenience method for get(_:at:)
. This is the non-variadic version.
let name = req.query[String.self, at: "user", "name"]
print(name) /// String?
Parameters
Name | Type | Description |
---|---|---|
type | D.Type |
The |
keyPath | [CodingKeyRepresentable] |
One or more key path components to the desired value. |
Returns
Decoded Decodable
value.
get(_:at:)
public func get<D>(_ type: D.Type = D.self, at keyPath: CodingKeyRepresentable...) throws -> D
where D: Decodable
Fetches a single Decodable
value at the supplied key-path from this HTTP request's query string.
let name = try req.query.get(String.self, at: "user", "name")
print(name) /// String
Parameters
Name | Type | Description |
---|---|---|
type | D.Type |
The |
keyPath | CodingKeyRepresentable |
One or more key path components to the desired value. |
Returns
Decoded Decodable
value.
get(_:at:)
public func get<D>(_ type: D.Type = D.self, at keyPath: [CodingKeyRepresentable]) throws -> D
where D: Decodable
Fetches a single Decodable
value at the supplied key-path from this HTTP request's query string.
Note: This is the non-variadic version.
let name = try req.query.get(String.self, at: "user", "name")
print(name) /// String
Parameters
Name | Type | Description |
---|---|---|
type | D.Type |
The |
keyPath | [CodingKeyRepresentable] |
One or more key path components to the desired value. |
Returns
Decoded Decodable
value.
Requirements
decode(_:using:)
func decode<D>(_ decodable: D.Type, using decoder: URLQueryDecoder) throws -> D
where D: Decodable
encode(_:using:)
mutating func encode<E>(_ encodable: E, using encoder: URLQueryEncoder) throws
where E: Encodable