Structurevapor 4.106.1Vapor
AnyAsyncResponse
A type erased response useful for routes that can return more than one type.
AnyResponse+Concurrency.swift:35struct AnyAsyncResponse
router.get("foo") { req -> AnyAsyncResponse in
if /* something */ {
return AnyAsyncResponse(42)
} else {
return AnyAsyncResponse("string")
}
}
This can also be done using a AsyncResponseEncodable
enum.
enum IntOrString: AsyncResponseEncodable {
case int(Int)
case string(String)
func encode(for req: Request) throws -> EventLoopFuture<Response> {
switch self {
case .int(let i): return try i.encode(for: req)
case .string(let s): return try s.encode(for: req)
}
}
}
router.get("foo") { req -> IntOrString in
if /* something */ {
return .int(42)
} else {
return .string("string")
}
}
Citizens in Vapor
Conformances
protocol AsyncResponseEncodable
Can convert
self
to aResponse
.
Type members
init(AsyncResponseEncodable
) Creates a new
AnyAsyncResponse
.
Instance members
Instance features
func encodeResponse(status: HTTPStatus, headers: HTTPHeaders, for: Request
) async throws -> Response Asynchronously encodes
Self
into aResponse
, setting the supplied status and headers.