Last active
January 18, 2023 11:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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