This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implementation ResponseModifier * Adds documentation * Adds ResponseModifier test * Fixes Swiftlint errors
- Loading branch information
1 parent
3955621
commit f87f948
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Vapor | ||
import Fluent | ||
|
||
/// A class that wraps a component which utilizes a `.respond(with:)` modifier. That | ||
/// allows Corvus to chain modifiers, as it gets treated as any other struct | ||
/// conforming to `RestEndpoint`. | ||
public final class ResponseModifier< | ||
Q: RestEndpoint, | ||
R: CorvusResponse>: | ||
RestEndpoint where Q.Element == R.Item { | ||
|
||
/// The `Response` of this modifier. | ||
public typealias Response = R | ||
|
||
/// The `RestEndpoint` the `.respond(with:)` modifier is attached to. | ||
public let restEndpoint: Q | ||
|
||
/// The HTTP operation type of the component. | ||
public let operationType: OperationType | ||
|
||
/// Initializes the modifier with its underlying `RestEndpoint`. | ||
/// | ||
/// - Parameters: | ||
/// - queryEndpoint: The `QueryEndpoint` which the modifer is attached | ||
/// to. | ||
public init(_ restEndpoint: Q) { | ||
self.restEndpoint = restEndpoint | ||
self.operationType = restEndpoint.operationType | ||
} | ||
|
||
/// A method which transform the restEndpoints's handler return value to a | ||
/// `Response`. | ||
/// | ||
/// - Parameter req: An incoming `Request`. | ||
/// - Returns: An `EventLoopFuture` containing the | ||
/// `ResponseModifier`'s `Response`. | ||
public func handler(_ req: Request) | ||
throws -> EventLoopFuture<Response> { | ||
try restEndpoint.handler(req).map(Response.init) | ||
} | ||
|
||
} | ||
|
||
/// An extension that adds a `.respond(with:)` modifier to `RestEndpoint`. | ||
extension RestEndpoint { | ||
|
||
/// A modifier used to transform the values returned by a component using a | ||
/// `CorvusResponse`. | ||
/// | ||
/// - Parameter as: A type conforming to `CorvusResponse`. | ||
/// - Returns: An instance of a `ResponseModifier` with the supplied `CorvusResponse`. | ||
public func respond<R: CorvusResponse>( | ||
with: R.Type | ||
) -> ResponseModifier<Self, R> { | ||
ResponseModifier(self) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Vapor | ||
|
||
/// `CorvusResponse` is a wrapper type for the result of`QueryEndpoint`s. Can | ||
/// be used to add metadata to a response. | ||
/// | ||
public protocol CorvusResponse: Content { | ||
|
||
/// The item is equivalent to the `QueryEndpoint`'s `QuerySubject`. | ||
associatedtype Item | ||
|
||
/// Initialises a `CorvusResponse` with a given item. | ||
/// Normally this is the result of the `QueryEndpoints`'s handler function. | ||
init(item: Item) | ||
} |
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