-
Notifications
You must be signed in to change notification settings - Fork 348
Proposal: Response.json
helper
#1389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yeah, Workers would be interested in this! |
If |
Vercel would be interested in this! |
Bun would be interested in this! |
Following, to replace the code here The current minimum: respondWith(
new Response(JSON.stringify({
data: "value!"
}), {
headers: {
"Content-Type": "application/json"
}
})
) This would reduce the code to: respondWith(
Response.json({
data: "value!"
})
) Looks great 👍🏻 |
Remix already implements this and it’s super handy. 👍 |
Is it handy? Obviously yes. Is it good? Probably not. |
It would make sense to include all these others. For For For And for |
@SukkaW No need to add those, as those already exist. They are overloads of the const resp = new Response(new ArrayBuffer(5));
const resp = new Response(new Blob(["hello world"]));
const resp = new Response("foo bar");
const resp = new Response(new FormData()); The only reason JSON can't be included in the overload is that the plain object can not be differentiated from other object inputs in a non confusing manner. |
A simple/tentative implementation of whatwg/fetch#1389
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
A method that can be used to create well-formed JSON responses with very little effort. The JSON response is not pretty printed. Tests: web-platform-tests/wpt#32825. Closes #1389.
JFYI, I requested a W3C TAG review of this feature in preparation for shipping in Chrome: w3ctag/design-reviews#741 |
I'll note that at least the Geometry API has static methods with https://drafts.fxtf.org/geometry/#dommatrix It seems easier to find the right documentation between
|
I find the naming of this highly confusing. MDN for example lists |
That is being tracked by mdn/content#21550. I think this has shipped so I don't think we're in a position to rename. I think the name is reasonable and consistent with other names on the object. That it pushes the ecosystem to no longer conflate statics and non-statics is a bonus. |
I proposed that types for the |
Uh oh!
There was an error while loading. Please reload this page.
The
Response
object currently has aResponse.redirect
helper method to createResponse
objects with presetLocation
headers.Request
andResponse
already have ajson
prototype method that allows parsing bodies as JSON, but there is currently no trivial one-liner to create JSONResponse
objects directly from a POJO.I propose the addition of a
Response.json(obj, opts)
static method that creates aResponse
object with a body string containing the JSON serialized representation ofobj
.opts
would allow further modification of the JSON response, namely setting headers and status code. By default the status code is 200, and the headers are the default response headers +content-type: application/json
.The motivation for this addition is that in server side runtimes using WHATWG
Request
andResponse
objects, folks often create helper methods for this behaviour that need to be duplicated across each project. This being built in would be very useful.Thoughts?
Implementer interest:
The text was updated successfully, but these errors were encountered: