@@ -7445,6 +7445,7 @@ interface Response {
7445
7445
7446
7446
[NewObject] static Response error();
7447
7447
[NewObject] static Response redirect(USVString url, optional unsigned short status = 302);
7448
+ [NewObject] static Response json(any data, optional ResponseInit init = {});
7448
7449
7449
7450
readonly attribute ResponseType type;
7450
7451
@@ -7496,6 +7497,10 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
7496
7497
<dd><p> Creates a redirect {{Response}} that redirects to <var> url</var> with status
7497
7498
<var> status</var> .
7498
7499
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
+
7499
7504
<dt><code><var> response</var> . <a attribute for=Response>type</a> </code>
7500
7505
<dd><p> Returns <var> response</var> 's type, e.g., "<code> cors</code> ".
7501
7506
@@ -7541,11 +7546,9 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
7541
7546
<li><p> Return <var> responseObject</var> .
7542
7547
</ol>
7543
7548
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:
7549
7552
7550
7553
<ol>
7551
7554
<li><p> If <var> init</var> ["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive,
@@ -7554,50 +7557,61 @@ constructor steps are:
7554
7557
<li><p> If <var> init</var> ["{{ResponseInit/statusText}}"] does not match the
7555
7558
<a spec=http>reason-phrase</a> token production, then <a>throw</a> a {{TypeError}} .
7556
7559
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
7565
7561
<var> init</var> ["{{ResponseInit/status}}"] .
7566
7562
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}}"] .
7569
7565
7570
7566
<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
7572
7568
<var> init</var> ["{{ResponseInit/headers}}"] .
7573
7569
7574
7570
<li>
7575
- <p> If <var> body</var> is non-null , then:
7571
+ <p> If <var> body</var> was given , then:
7576
7572
7577
7573
<ol>
7578
7574
<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}} .
7581
7577
7582
7578
<p class="note no-backref"> 101 is included in <a>null body status</a> due to its use elsewhere.
7583
7579
It does not affect this step.
7584
7580
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> .
7590
7583
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>
7595
7586
`<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
7597
7588
<a for=response>header list</a> .
7598
7589
</ol>
7599
7590
</ol>
7600
7591
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
+
7601
7615
<p> The static <dfn method for=Response><code>error()</code></dfn> method steps are to return the
7602
7616
result of <a for=Response>creating</a> a {{Response}} object, given a new <a>network error</a> ,
7603
7617
"<code> immutable</code> ", and <a>this</a> 's <a>relevant Realm</a> .
@@ -7630,6 +7644,27 @@ are:
7630
7644
<li><p> Return <var> responseObject</var> .
7631
7645
</ol>
7632
7646
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
+
7633
7668
<p> The <dfn attribute for=Response><code>type</code></dfn> getter steps are to return <a>this</a> 's
7634
7669
<a for=Response>response</a> 's <a for=response>type</a> .
7635
7670
0 commit comments