Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd Data responses and TypedDataResponse #1657
Conversation
aydenp
changed the title
Add data responses and TypedDataResponse
Add Data responses and TypedDataResponse
May 7, 2018
tanner0101
reviewed
May 8, 2018
Just some gardening comments. Looks great I love the idea. |
@@ -0,0 +1,30 @@ | |||
/// A container representing `Data` with a specific content type (used for returning as a `Response`). | |||
public struct TypedDataResponse: ResponseEncodable { |
This comment has been minimized.
This comment has been minimized.
tanner0101
May 8, 2018
Member
We should make this type private and just publicize ResponseEncodable
.
This comment has been minimized.
This comment has been minimized.
aydenp
May 8, 2018
•
Author
Contributor
ResponseEncodable
is already public, but I agree that this type should be private. An earlier iteration of it didn't have the Data
extension for types, so I'd forgotten to make it private.
Get a `ResponseEncodable` container holding this data with your provided contentType. | ||
- parameter contentType: The type of data to return the container with. | ||
*/ | ||
public func response(type: MediaType) -> TypedDataResponse { |
This comment has been minimized.
This comment has been minimized.
public struct TypedDataResponse: ResponseEncodable { | ||
/// The data held by this container. | ||
let data: Data | ||
/// The type of the data held by this container. |
This comment has been minimized.
This comment has been minimized.
} | ||
|
||
/** | ||
Get a `ResponseEncodable` container holding this data with your provided contentType. |
This comment has been minimized.
This comment has been minimized.
tanner0101
May 8, 2018
Member
couple gardening notes here:
- vapor uses
///
format comments - parameter
s should be listed as a separate bulleted list, see any of the other doc blocks for an example- methods expected to be used directly by the end user should have a small example code section if possible! that really helps people figure out how to use it.
tanner0101
added
the
enhancement
label
May 8, 2018
tanner0101
self-assigned this
May 8, 2018
aydenp
and others
added some commits
May 8, 2018
tanner0101
added this to the 3.0.2 milestone
May 8, 2018
tanner0101
approved these changes
May 8, 2018
I will fix the test errors on a different branch. thanks! |
tanner0101
merged commit 199593a
into
vapor:master
May 8, 2018
This comment has been minimized.
This comment has been minimized.
penny-coin
commented
May 8, 2018
Hey @aydenp, you just merged a pull request, have a coin! You now have 1 coins. |
added a commit
that referenced
this pull request
May 8, 2018
This comment has been minimized.
This comment has been minimized.
Ended on something slightly different, but I think it better matches the other available APIs which will make things more intuitive. // default to plaintext
router.get("hello") { req in
return req.makeResponse("Hello!")
}
// specify content type
router.get("hello-html") { req in
return req.makeResponse("Hey!", as: .html)
}
// and of course, what we had previously
router.get("hello-json") { req in
let res = req.makeResponse()
try res.content.encode(["hello": "world"], as: .json)
return res
} |
This comment has been minimized.
This comment has been minimized.
Worth noting that both |
This comment has been minimized.
This comment has been minimized.
nullpixel
commented
May 9, 2018
•
Do your changes let you just return If they don't, then I'd suggest this is less initiative. |
This comment has been minimized.
This comment has been minimized.
@nullpixel what Content-Type would |
This comment has been minimized.
This comment has been minimized.
nullpixel
commented
May 9, 2018
•
Well, |
aydenp commentedMay 5, 2018
•
edited
In some cases, you may want to just return raw
Data
as a Response. Before this commit, you might have had to make your ownResponseEncodable
type that just provides the data, which seems a bit much for such a simple function.Say we have this super important data that we need to respond with:
At it's simplest, this new functionality allows you to respond with a
Data
object by simply returning it:However, sometimes, you might want to return this data with a custom content type. In this case, you can simply get a
TypedDataResponse
from a new Data extension, and return it, instead of theData
:As an aside,
TypedDataResponse
is also used under the hood if you just returnData
on it's own, with a content type ofMediaType.any
.Checklist
///
.