Skip to content

Commit b3bfd0c

Browse files
authored
Add Response.json() static method
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.
1 parent 03c041e commit b3bfd0c

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

fetch.bs

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7445,6 +7445,7 @@ interface Response {
74457445

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

74497450
readonly attribute ResponseType type;
74507451

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

7500+
<dt><code><var>response</var> = <a idl>Response</a> . <a method for=Response lt=json()>json</a>(<var>data</var> [, <var>init</var>])</code>
7501+
<dd><p>Creates a {{Response}} whose body is the JSON-encoded <var>data</var>, and status, status
7502+
message, and headers are provided by <var>init</var>.
7503+
74997504
<dt><code><var>response</var> . <a attribute for=Response>type</a></code>
75007505
<dd><p>Returns <var>response</var>'s type, e.g., "<code>cors</code>".
75017506

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

7544-
<hr>
7545-
7546-
<p>The
7547-
<dfn constructor for=Response id=dom-response lt="Response(body, init)"><code>new Response(<var>body</var>, <var>init</var>)</code></dfn>
7548-
constructor steps are:
7549+
<p>To <dfn>initialize a response</dfn>, given a {{Response}} object <var>response</var>,
7550+
{{ResponseInit}} <var>init</var>, and an optional <a for=/>body with type</a> <var>body</var>, run
7551+
these steps:
75497552

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

7557-
<li><p>Set <a>this</a>'s <a for=Response>response</a> to a new <a for=/>response</a>.
7558-
7559-
<li><p>Set <a>this</a>'s <a for=Response>headers</a> to a <a for=/>new</a> {{Headers}} object with
7560-
<a>this</a>'s <a>relevant Realm</a>, whose <a for=Headers>header list</a> is <a>this</a>'s
7561-
<a for=Response>response</a>'s <a for=response>header list</a> and <a for=Headers>guard</a> is
7562-
"<code>response</code>".
7563-
7564-
<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>status</a> to
7560+
<li><p>Set <var>response</var>'s <a for=Response>response</a>'s <a for=response>status</a> to
75657561
<var>init</var>["{{ResponseInit/status}}"].
75667562

7567-
<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>status message</a> to
7568-
<var>init</var>["{{ResponseInit/statusText}}"].
7563+
<li><p>Set <var>response</var>'s <a for=Response>response</a>'s <a for=response>status message</a>
7564+
to <var>init</var>["{{ResponseInit/statusText}}"].
75697565

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

75747570
<li>
7575-
<p>If <var>body</var> is non-null, then:
7571+
<p>If <var>body</var> was given, then:
75767572

75777573
<ol>
75787574
<li>
7579-
<p>If <var>init</var>["{{ResponseInit/status}}"] is a <a>null body status</a>, then <a>throw</a>
7580-
a {{TypeError}}.
7575+
<p>If <var>response</var>'s <a for=response>status</a> is a <a>null body status</a>, then
7576+
<a>throw</a> a {{TypeError}}.
75817577

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

7585-
<li><p>Let <var>bodyWithType</var> be the result of <a for=BodyInit>extracting</a>
7586-
<var>body</var>.
7587-
7588-
<li><p>Set <a>this</a>'s <a for=Response>response</a>'s <a for=response>body</a> to
7589-
<var>bodyWithType</var>'s <a for="body with type">body</a>.
7581+
<li><p>Set <var>response</var>'s <a for=response>body</a> to <var>body</var>'s
7582+
<a for="body with type">body</a>.
75907583

7591-
<li><p>Let <var>type</var> be <var>bodyWithType</var>'s <a for="body with type">type</a>.
7592-
7593-
<li><p>If <var>type</var> is non-null and <a>this</a>'s <a for=Response>response</a>'s
7594-
<a for=response>header list</a> <a for="header list">does not contain</a>
7584+
<li><p>If <var>body</var>'s <a for="body with type">type</a> is non-null and
7585+
<var>response</var>'s <a for=response>header list</a> <a for="header list">does not contain</a>
75957586
`<code>Content-Type</code>`, then <a for="header list">append</a> (`<code>Content-Type</code>`,
7596-
<var>type</var>) to <a>this</a>'s <a for=Response>response</a>'s
7587+
<var>body</var>'s <a for="body with type">type</a>) to <var>response</var>'s
75977588
<a for=response>header list</a>.
75987589
</ol>
75997590
</ol>
76007591

7592+
<hr>
7593+
7594+
<p>The
7595+
<dfn constructor for=Response id=dom-response lt="Response(body, init)"><code>new Response(<var>body</var>, <var>init</var>)</code></dfn>
7596+
constructor steps are:
7597+
7598+
<ol>
7599+
<li><p>Set <a>this</a>'s <a for=Response>response</a> to a new <a for=/>response</a>.
7600+
7601+
<li><p>Set <a>this</a>'s <a for=Response>headers</a> to a <a for=/>new</a> {{Headers}} object with
7602+
<a>this</a>'s <a>relevant Realm</a>, whose <a for=Headers>header list</a> is <a>this</a>'s
7603+
<a for=Response>response</a>'s <a for=response>header list</a> and <a for=Headers>guard</a> is
7604+
"<code>response</code>".
7605+
7606+
<li><p>Let <var>bodyWithType</var> be null.
7607+
7608+
<li><p>If <var>body</var> is non-null, then set <var>bodyWithType</var> to the result of
7609+
<a for=BodyInit>extracting</a> <var>body</var>.
7610+
7611+
<li><p>Perform <a>initialize a response</a> given <a>this</a>, <var>init</var>, and
7612+
<var>bodyWithType</var>.
7613+
</ol>
7614+
76017615
<p>The static <dfn method for=Response><code>error()</code></dfn> method steps are to return the
76027616
result of <a for=Response>creating</a> a {{Response}} object, given a new <a>network error</a>,
76037617
"<code>immutable</code>", and <a>this</a>'s <a>relevant Realm</a>.
@@ -7630,6 +7644,27 @@ are:
76307644
<li><p>Return <var>responseObject</var>.
76317645
</ol>
76327646

7647+
7648+
<p>The static
7649+
<dfn method for=Response><code>json(<var>data</var>, <var>init</var>)</code></dfn> method steps
7650+
are:
7651+
7652+
<ol>
7653+
<li><p>Let <var>bytes</var> the result of running <a>serialize a JavaScript value to JSON bytes</a>
7654+
on <var>data</var>.
7655+
7656+
<li><p>Let <var>body</var> be the result of <a for=BodyInit>extracting</a> <var>bytes</var>.
7657+
7658+
<li><p>Let <var>responseObject</var> be the result of <a for=Response>creating</a> a {{Response}}
7659+
object, given a new <a for=/>response</a>, "<code>response</code>", and <a>this</a>'s
7660+
<a>relevant Realm</a>.
7661+
7662+
<li><p>Perform <a>initialize a response</a> given <var>responseObject</var>, <var>init</var>, and
7663+
(<var>body</var>, "<code>application/json</code>").
7664+
7665+
<li><p>Return <var>responseObject</var>.
7666+
</ol>
7667+
76337668
<p>The <dfn attribute for=Response><code>type</code></dfn> getter steps are to return <a>this</a>'s
76347669
<a for=Response>response</a>'s <a for=response>type</a>.
76357670

0 commit comments

Comments
 (0)