Skip to content

Commit

Permalink
Add Response.json() static method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucacasonato committed May 18, 2022
1 parent 03c041e commit b3bfd0c
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions fetch.bs
Original file line number Diff line number Diff line change
Expand Up @@ -7445,6 +7445,7 @@ interface Response {

[NewObject] static Response error();
[NewObject] static Response redirect(USVString url, optional unsigned short status = 302);
[NewObject] static Response json(any data, optional ResponseInit init = {});

readonly attribute ResponseType type;

Expand Down Expand Up @@ -7496,6 +7497,10 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
<dd><p>Creates a redirect {{Response}} that redirects to <var>url</var> with status
<var>status</var>.

<dt><code><var>response</var> = <a idl>Response</a> . <a method for=Response lt=json()>json</a>(<var>data</var> [, <var>init</var>])</code>
<dd><p>Creates a {{Response}} whose body is the JSON-encoded <var>data</var>, and status, status
message, and headers are provided by <var>init</var>.

<dt><code><var>response</var> . <a attribute for=Response>type</a></code>
<dd><p>Returns <var>response</var>'s type, e.g., "<code>cors</code>".

Expand Down Expand Up @@ -7541,11 +7546,9 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
<li><p>Return <var>responseObject</var>.
</ol>

<hr>

<p>The
<dfn constructor for=Response id=dom-response lt="Response(body, init)"><code>new Response(<var>body</var>, <var>init</var>)</code></dfn>
constructor steps are:
<p>To <dfn>initialize a response</dfn>, given a {{Response}} object <var>response</var>,
{{ResponseInit}} <var>init</var>, and an optional <a for=/>body with type</a> <var>body</var>, run
these steps:

<ol>
<li><p>If <var>init</var>["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive,
Expand All @@ -7554,50 +7557,61 @@ constructor steps are:
<li><p>If <var>init</var>["{{ResponseInit/statusText}}"] does not match the
<a spec=http>reason-phrase</a> token production, then <a>throw</a> a {{TypeError}}.

<li><p>Set <a>this</a>'s <a for=Response>response</a> to a new <a for=/>response</a>.

<li><p>Set <a>this</a>'s <a for=Response>headers</a> to a <a for=/>new</a> {{Headers}} object with
<a>this</a>'s <a>relevant Realm</a>, whose <a for=Headers>header list</a> is <a>this</a>'s
<a for=Response>response</a>'s <a for=response>header list</a> and <a for=Headers>guard</a> is
"<code>response</code>".

<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>status</a> to
<li><p>Set <var>response</var>'s <a for=Response>response</a>'s <a for=response>status</a> to
<var>init</var>["{{ResponseInit/status}}"].

<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>status message</a> to
<var>init</var>["{{ResponseInit/statusText}}"].
<li><p>Set <var>response</var>'s <a for=Response>response</a>'s <a for=response>status message</a>
to <var>init</var>["{{ResponseInit/statusText}}"].

<li><p>If <var>init</var>["{{ResponseInit/headers}}"] <a for=map>exists</a>, then
<a for=Headers>fill</a> <a>this</a>'s <a for=Response>headers</a> with
<a for=Headers>fill</a> <var>response</var>'s <a for=Response>headers</a> with
<var>init</var>["{{ResponseInit/headers}}"].

<li>
<p>If <var>body</var> is non-null, then:
<p>If <var>body</var> was given, then:

<ol>
<li>
<p>If <var>init</var>["{{ResponseInit/status}}"] is a <a>null body status</a>, then <a>throw</a>
a {{TypeError}}.
<p>If <var>response</var>'s <a for=response>status</a> is a <a>null body status</a>, then
<a>throw</a> a {{TypeError}}.

<p class="note no-backref">101 is included in <a>null body status</a> due to its use elsewhere.
It does not affect this step.

<li><p>Let <var>bodyWithType</var> be the result of <a for=BodyInit>extracting</a>
<var>body</var>.

<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>body</a> to
<var>bodyWithType</var>'s <a for="body with type">body</a>.
<li><p>Set <var>response</var>'s <a for=response>body</a> to <var>body</var>'s
<a for="body with type">body</a>.

<li><p>Let <var>type</var> be <var>bodyWithType</var>'s <a for="body with type">type</a>.

<li><p>If <var>type</var> is non-null and <a>this</a>'s <a for=Response>response</a>'s
<a for=response>header list</a> <a for="header list">does not contain</a>
<li><p>If <var>body</var>'s <a for="body with type">type</a> is non-null and
<var>response</var>'s <a for=response>header list</a> <a for="header list">does not contain</a>
`<code>Content-Type</code>`, then <a for="header list">append</a> (`<code>Content-Type</code>`,
<var>type</var>) to <a>this</a>'s <a for=Response>response</a>'s
<var>body</var>'s <a for="body with type">type</a>) to <var>response</var>'s
<a for=response>header list</a>.
</ol>
</ol>

<hr>

<p>The
<dfn constructor for=Response id=dom-response lt="Response(body, init)"><code>new Response(<var>body</var>, <var>init</var>)</code></dfn>
constructor steps are:

<ol>
<li><p>Set <a>this</a>'s <a for=Response>response</a> to a new <a for=/>response</a>.

<li><p>Set <a>this</a>'s <a for=Response>headers</a> to a <a for=/>new</a> {{Headers}} object with
<a>this</a>'s <a>relevant Realm</a>, whose <a for=Headers>header list</a> is <a>this</a>'s
<a for=Response>response</a>'s <a for=response>header list</a> and <a for=Headers>guard</a> is
"<code>response</code>".

<li><p>Let <var>bodyWithType</var> be null.

<li><p>If <var>body</var> is non-null, then set <var>bodyWithType</var> to the result of
<a for=BodyInit>extracting</a> <var>body</var>.

<li><p>Perform <a>initialize a response</a> given <a>this</a>, <var>init</var>, and
<var>bodyWithType</var>.
</ol>

<p>The static <dfn method for=Response><code>error()</code></dfn> method steps are to return the
result of <a for=Response>creating</a> a {{Response}} object, given a new <a>network error</a>,
"<code>immutable</code>", and <a>this</a>'s <a>relevant Realm</a>.
Expand Down Expand Up @@ -7630,6 +7644,27 @@ are:
<li><p>Return <var>responseObject</var>.
</ol>


<p>The static
<dfn method for=Response><code>json(<var>data</var>, <var>init</var>)</code></dfn> method steps
are:

<ol>
<li><p>Let <var>bytes</var> the result of running <a>serialize a JavaScript value to JSON bytes</a>
on <var>data</var>.

<li><p>Let <var>body</var> be the result of <a for=BodyInit>extracting</a> <var>bytes</var>.

<li><p>Let <var>responseObject</var> be the result of <a for=Response>creating</a> a {{Response}}
object, given a new <a for=/>response</a>, "<code>response</code>", and <a>this</a>'s
<a>relevant Realm</a>.

<li><p>Perform <a>initialize a response</a> given <var>responseObject</var>, <var>init</var>, and
(<var>body</var>, "<code>application/json</code>").

<li><p>Return <var>responseObject</var>.
</ol>

<p>The <dfn attribute for=Response><code>type</code></dfn> getter steps are to return <a>this</a>'s
<a for=Response>response</a>'s <a for=response>type</a>.

Expand Down

0 comments on commit b3bfd0c

Please sign in to comment.