Skip to content

Instantly share code, notes, and snippets.

@JetForMe
Last active January 18, 2023 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JetForMe/796ac8d2e48f67d078782b9884257716 to your computer and use it in GitHub Desktop.
Save JetForMe/796ac8d2e48f67d078782b9884257716 to your computer and use it in GitHub Desktop.
RoutesBuilder extension to make it easy to return response status along with Content DTOs
import Vapor
/**
Convenience extensions to allow writing route handlers that return a tuple
of `(HTTPStatus, Content)`, so it’s easier to control the response status.
*/
extension
RoutesBuilder
{
@discardableResult
public func get<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.GET, path, use: closure)
}
@discardableResult
public func get<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.GET, path, use: closure)
}
@discardableResult
public func post<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.POST, path, use: closure)
}
@discardableResult
public func post<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.POST, path, use: closure)
}
@discardableResult
public func patch<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.PATCH, path, use: closure)
}
@discardableResult
public func patch<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.PATCH, path, use: closure)
}
@discardableResult
public func put<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.PUT, path, use: closure)
}
@discardableResult
public func put<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.PUT, path, use: closure)
}
@discardableResult
public func delete<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.DELETE, path, use: closure)
}
@discardableResult
public func delete<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(.DELETE, path, use: closure)
}
@discardableResult
public func on<Response>(
_ method: HTTPMethod,
_ path: [PathComponent],
body: HTTPBodyStreamStrategy = .collect,
use closure: @escaping (Request) async throws -> (HTTPStatus, Response)
) -> Route
where Response: AsyncResponseEncodable
{
return self.on(method, path, body: body)
{ inReq in
let (s, r) = try await closure(inReq)
return try await r.encodeResponse(status: s, for: inReq)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment