summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndres Rey <[email protected]>2016-12-10 12:09:22 +0000
committerAndres Rey <[email protected]>2016-12-10 12:09:22 +0000
commit8e506e18528546060c75bdc39c11138968786efe (patch)
treea5b427d371e17edb27175f339a16c4abc6846934 /test
parent3757f8f838687374e0cb6255928dee83492678f7 (diff)
Modified the expected files to match the readability.php style of result
Diffstat (limited to 'test')
-rw-r--r--test/test-pages/001/expected.html92
-rw-r--r--test/test-pages/002/expected.html814
2 files changed, 494 insertions, 412 deletions
diff --git a/test/test-pages/001/expected.html b/test/test-pages/001/expected.html
index fa8ffd2..bf3075c 100644
--- a/test/test-pages/001/expected.html
+++ b/test/test-pages/001/expected.html
@@ -1,24 +1,41 @@
- <section>
- <p><strong>So finally you're <a href="http://fakehost/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">testing your frontend JavaScript code</a>? Great! The more you
+<section><p><strong>So finally you're <a href="/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">testing your frontend JavaScript code</a>? Great! The more you
write tests, the more confident you are with your code… but how much precisely?
That's where <a href="http://en.wikipedia.org/wiki/Code_coverage">code coverage</a> might
-help.</strong> </p>
- <p>The idea behind code coverage is to record which parts of your code (functions, statements, conditionals and so on) have been executed by your test suite, to compute metrics out of these data and usually to provide tools for navigating and inspecting them.</p>
- <p>Not a lot of frontend developers I know actually test their frontend code, and I can barely imagine how many of them have ever setup code coverage… Mostly because there are not many frontend-oriented tools in this area I guess.</p>
- <p>Actually I've only found one which provides an adapter for <a href="http://visionmedia.github.io/mocha/">Mocha</a> and actually works…</p>
- <blockquote class="twitter-tweet tw-align-center">
- <p>Drinking game for web devs:
- <br/>(1) Think of a noun
- <br/>(2) Google "&lt;noun&gt;.js"
- <br/>(3) If a library with that name exists - drink</p>— Shay Friedman (@ironshay) <a href="https://twitter.com/ironshay/statuses/370525864523743232">August 22, 2013</a> </blockquote>
- <p><strong><a href="http://blanketjs.org/">Blanket.js</a></strong> is an <em>easy to install, easy to configure,
+help.</strong>
+ </p>
+ <p>The idea behind code coverage is to record which parts of your code (functions,
+ statements, conditionals and so on) have been executed by your test suite,
+ to compute metrics out of these data and usually to provide tools for navigating
+ and inspecting them.</p>
+ <p>Not a lot of frontend developers I know actually test their frontend code,
+ and I can barely imagine how many of them have ever setup code coverage…
+ Mostly because there are not many frontend-oriented tools in this area
+ I guess.</p>
+ <p>Actually I've only found one which provides an adapter for <a href="http://visionmedia.github.io/mocha/">Mocha</a> and
+ actually works…</p>
+ <blockquote class="twitter-tweet tw-align-center">
+ <p>Drinking game for web devs:
+ <br></br>(1) Think of a noun
+ <br></br>(2) Google "&lt;noun&gt;.js"
+ <br></br>(3) If a library with that name exists - drink</p>— Shay Friedman (@ironshay)
+ <a href="https://twitter.com/ironshay/statuses/370525864523743232">August 22, 2013</a>
+ </blockquote>
+ <p><strong><a href="http://blanketjs.org/">Blanket.js</a></strong> is an <em>easy to install, easy to configure,
and easy to use JavaScript code coverage library that works both in-browser and
-with nodejs.</em> </p>
- <p>Its use is dead easy, adding Blanket support to your Mocha test suite is just matter of adding this simple line to your HTML test file:</p> <pre><code>&lt;script src="vendor/blanket.js"
+with nodejs.</em>
+ </p>
+ <p>Its use is dead easy, adding Blanket support to your Mocha test suite
+ is just matter of adding this simple line to your HTML test file:</p>
+<pre><code>&lt;script src="vendor/blanket.js"
data-cover-adapter="vendor/mocha-blanket.js"&gt;&lt;/script&gt;
</code></pre>
- <p>Source files: <a href="https://raw.github.com/alex-seville/blanket/master/dist/qunit/blanket.min.js">blanket.js</a>, <a href="https://raw.github.com/alex-seville/blanket/master/src/adapters/mocha-blanket.js">mocha-blanket.js</a> </p>
- <p>As an example, let's reuse the silly <code>Cow</code> example we used <a href="http://fakehost/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">in a previous episode</a>:</p> <pre><code>// cow.js
+
+ <p>Source files: <a href="https://raw.github.com/alex-seville/blanket/master/dist/qunit/blanket.min.js">blanket.js</a>,
+ <a href="https://raw.github.com/alex-seville/blanket/master/src/adapters/mocha-blanket.js">mocha-blanket.js</a>
+ </p>
+ <p>As an example, let's reuse the silly <code>Cow</code> example we used
+ <a href="/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">in a previous episode</a>:</p>
+<pre><code>// cow.js
(function(exports) {
"use strict";
@@ -36,7 +53,9 @@ with nodejs.</em> </p>
};
})(this);
</code></pre>
- <p>And its test suite, powered by Mocha and <a href="http://chaijs.com/">Chai</a>:</p> <pre><code>var expect = chai.expect;
+
+ <p>And its test suite, powered by Mocha and <a href="http://chaijs.com/">Chai</a>:</p>
+<pre><code>var expect = chai.expect;
describe("Cow", function() {
describe("constructor", function() {
@@ -59,7 +78,10 @@ describe("Cow", function() {
});
});
</code></pre>
- <p>Let's create the HTML test file for it, featuring Blanket and its adapter for Mocha:</p> <pre><code>&lt;!DOCTYPE html&gt;
+
+ <p>Let's create the HTML test file for it, featuring Blanket and its adapter
+ for Mocha:</p>
+<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
@@ -81,14 +103,26 @@ describe("Cow", function() {
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
- <p><strong>Notes</strong>:</p>
- <ul>
- <li>Notice the <code>data-cover</code> attribute we added to the script tag loading the source of our library;</li>
- <li>The HTML test file <em>must</em> be served over HTTP for the adapter to be loaded.</li>
- </ul>
- <p>Running the tests now gives us something like this:</p>
- <p> <img alt="screenshot" src="http://fakehost/static/code/2013/blanket-coverage.png"/> </p>
- <p>As you can see, the report at the bottom highlights that we haven't actually tested the case where an error is raised in case a target name is missing. We've been informed of that, nothing more, nothing less. We simply know we're missing a test here. Isn't this cool? I think so!</p>
- <p>Just remember that code coverage will only <a href="http://codebetter.com/karlseguin/2008/12/09/code-coverage-use-it-wisely/">bring you numbers</a> and raw information, not actual proofs that the whole of your <em>code logic</em> has been actually covered. If you ask me, the best inputs you can get about your code logic and implementation ever are the ones issued out of <a href="http://www.extremeprogramming.org/rules/pair.html">pair programming</a> sessions and <a href="http://alexgaynor.net/2013/sep/26/effective-code-review/">code reviews</a> — but that's another story.</p>
- <p><strong>So is code coverage silver bullet? No. Is it useful? Definitely. Happy testing!</strong> </p>
- </section> \ No newline at end of file
+
+ <p><strong>Notes</strong>:</p>
+ <ul><li>Notice the <code>data-cover</code> attribute we added to the script tag
+ loading the source of our library;</li>
+ <li>The HTML test file <em>must</em> be served over HTTP for the adapter to
+ be loaded.</li>
+ </ul><p>Running the tests now gives us something like this:</p>
+ <p>
+ <img alt="screenshot" src="/static/code/2013/blanket-coverage.png"></img></p>
+ <p>As you can see, the report at the bottom highlights that we haven't actually
+ tested the case where an error is raised in case a target name is missing.
+ We've been informed of that, nothing more, nothing less. We simply know
+ we're missing a test here. Isn't this cool? I think so!</p>
+ <p>Just remember that code coverage will only <a href="http://codebetter.com/karlseguin/2008/12/09/code-coverage-use-it-wisely/">bring you numbers</a> and
+ raw information, not actual proofs that the whole of your <em>code logic</em> has
+ been actually covered. If you ask me, the best inputs you can get about
+ your code logic and implementation ever are the ones issued out of <a href="http://www.extremeprogramming.org/rules/pair.html">pair programming</a>
+sessions
+ and <a href="http://alexgaynor.net/2013/sep/26/effective-code-review/">code reviews</a> —
+ but that's another story.</p>
+ <p><strong>So is code coverage silver bullet? No. Is it useful? Definitely. Happy testing!</strong>
+ </p>
+ </section> \ No newline at end of file
diff --git a/test/test-pages/002/expected.html b/test/test-pages/002/expected.html
index 504ba86..f70b310 100644
--- a/test/test-pages/002/expected.html
+++ b/test/test-pages/002/expected.html
@@ -1,384 +1,432 @@
- <article class="post" role="article">
- <p>For more than a decade the Web has used XMLHttpRequest (XHR) to achieve asynchronous requests in JavaScript. While very useful, XHR is not a very nice API. It suffers from lack of separation of concerns. The input, output and state are all managed by interacting with one object, and state is tracked using events. Also, the event-based model doesn’t play well with JavaScript’s recent focus on Promise- and generator-based asynchronous programming.</p>
- <p>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a> intends to fix most of these problems. It does this by introducing the same primitives to JS that are used in the HTTP protocol. In addition, it introduces a utility function <code>fetch()</code> that succinctly captures the intention of retrieving a resource from the network.</p>
- <p>The <a href="https://fetch.spec.whatwg.org">Fetch specification</a>, which defines the API, nails down the semantics of a user agent fetching a resource. This, combined with ServiceWorkers, is an attempt to:</p>
- <ol>
- <li>Improve the offline experience.</li>
- <li>Expose the building blocks of the Web to the platform as part of the <a href="https://extensiblewebmanifesto.org/">extensible web movement</a>.</li>
- </ol>
- <p>As of this writing, the Fetch API is available in Firefox 39 (currently Nightly) and Chrome 42 (currently dev). Github has a <a href="https://github.com/github/fetch">Fetch polyfill</a>.</p>
- <h2>Feature detection</h2>
- <p>Fetch API support can be detected by checking for <code>Headers</code>,<code>Request</code>, <code>Response</code> or <code>fetch</code> on the <code>window</code> or <code>worker</code> scope.</p>
- <h2>Simple fetching</h2>
- <p>The most useful, high-level part of the Fetch API is the <code>fetch()</code> function. In its simplest form it takes a URL and returns a promise that resolves to the response. The response is captured as a <code>Response</code> object.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">fetch<span>(</span><span>"/data.json"</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>res<span>)</span> <span>{</span>
- <span>// res instanceof Response == true.</span>
- <span>if</span> <span>(</span>res.<span>ok</span><span>)</span> <span>{</span>
- res.<span>json</span><span>(</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>data<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span>data.<span>entries</span><span>)</span><span>;</span>
- <span>}</span><span>)</span><span>;</span>
- <span>}</span> <span>else</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Looks like the response wasn't perfect, got status"</span><span>,</span> res.<span>status</span><span>)</span><span>;</span>
- <span>}</span>
-<span>}</span><span>,</span> <span>function</span><span>(</span>e<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Fetch failed!"</span><span>,</span> e<span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>Submitting some parameters, it would look like this:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">fetch<span>(</span><span>"http://www.example.org/submit.php"</span><span>,</span> <span>{</span>
- method<span>:</span> <span>"POST"</span><span>,</span>
- headers<span>:</span> <span>{</span>
- <span>"Content-Type"</span><span>:</span> <span>"application/x-www-form-urlencoded"</span>
- <span>}</span><span>,</span>
- body<span>:</span> <span>"firstName=Nikhil&amp;favColor=blue&amp;password=easytoguess"</span>
-<span>}</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>res<span>)</span> <span>{</span>
- <span>if</span> <span>(</span>res.<span>ok</span><span>)</span> <span>{</span>
- alert<span>(</span><span>"Perfect! Your settings are saved."</span><span>)</span><span>;</span>
- <span>}</span> <span>else</span> <span>if</span> <span>(</span>res.<span>status</span> <span>==</span> <span>401</span><span>)</span> <span>{</span>
- alert<span>(</span><span>"Oops! You are not authorized."</span><span>)</span><span>;</span>
- <span>}</span>
-<span>}</span><span>,</span> <span>function</span><span>(</span>e<span>)</span> <span>{</span>
- alert<span>(</span><span>"Error submitting form!"</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The <code>fetch()</code> function’s arguments are the same as those passed to the
- <br/> <code>Request()</code> constructor, so you may directly pass arbitrarily complex requests to <code>fetch()</code> as discussed below.</p>
- <h2>Headers</h2>
- <p>Fetch introduces 3 interfaces. These are <code>Headers</code>, <code>Request</code> and
- <br/> <code>Response</code>. They map directly to the underlying HTTP concepts, but have
- <br/>certain visibility filters in place for privacy and security reasons, such as
- <br/>supporting CORS rules and ensuring cookies aren’t readable by third parties.</p>
- <p>The <a href="https://fetch.spec.whatwg.org/#headers-class">Headers interface</a> is a simple multi-map of names to values:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> content <span>=</span> <span>"Hello World"</span><span>;</span>
-<span>var</span> reqHeaders <span>=</span> <span>new</span> Headers<span>(</span><span>)</span><span>;</span>
-reqHeaders.<span>append</span><span>(</span><span>"Content-Type"</span><span>,</span> <span>"text/plain"</span>
-reqHeaders.<span>append</span><span>(</span><span>"Content-Length"</span><span>,</span> content.<span>length</span>.<span>toString</span><span>(</span><span>)</span><span>)</span><span>;</span>
-reqHeaders.<span>append</span><span>(</span><span>"X-Custom-Header"</span><span>,</span> <span>"ProcessThisImmediately"</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The same can be achieved by passing an array of arrays or a JS object literal
- <br/>to the constructor:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">reqHeaders <span>=</span> <span>new</span> Headers<span>(</span><span>{</span>
- <span>"Content-Type"</span><span>:</span> <span>"text/plain"</span><span>,</span>
- <span>"Content-Length"</span><span>:</span> content.<span>length</span>.<span>toString</span><span>(</span><span>)</span><span>,</span>
- <span>"X-Custom-Header"</span><span>:</span> <span>"ProcessThisImmediately"</span><span>,</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The contents can be queried and retrieved:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">console.<span>log</span><span>(</span>reqHeaders.<span>has</span><span>(</span><span>"Content-Type"</span><span>)</span><span>)</span><span>;</span> <span>// true</span>
-console.<span>log</span><span>(</span>reqHeaders.<span>has</span><span>(</span><span>"Set-Cookie"</span><span>)</span><span>)</span><span>;</span> <span>// false</span>
-reqHeaders.<span>set</span><span>(</span><span>"Content-Type"</span><span>,</span> <span>"text/html"</span><span>)</span><span>;</span>
-reqHeaders.<span>append</span><span>(</span><span>"X-Custom-Header"</span><span>,</span> <span>"AnotherValue"</span><span>)</span><span>;</span>
-&nbsp;
-console.<span>log</span><span>(</span>reqHeaders.<span>get</span><span>(</span><span>"Content-Length"</span><span>)</span><span>)</span><span>;</span> <span>// 11</span>
-console.<span>log</span><span>(</span>reqHeaders.<span>getAll</span><span>(</span><span>"X-Custom-Header"</span><span>)</span><span>)</span><span>;</span> <span>// ["ProcessThisImmediately", "AnotherValue"]</span>
-&nbsp;
-reqHeaders.<span>delete</span><span>(</span><span>"X-Custom-Header"</span><span>)</span><span>;</span>
-console.<span>log</span><span>(</span>reqHeaders.<span>getAll</span><span>(</span><span>"X-Custom-Header"</span><span>)</span><span>)</span><span>;</span> <span>// []</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>Some of these operations are only useful in ServiceWorkers, but they provide
- <br/>a much nicer API to Headers.</p>
- <p>Since Headers can be sent in requests, or received in responses, and have various limitations about what information can and should be mutable, <code>Headers</code> objects have a <strong>guard</strong> property. This is not exposed to the Web, but it affects which mutation operations are allowed on the Headers object.
- <br/>Possible values are:</p>
- <ul>
- <li>“none”: default.</li>
- <li>“request”: guard for a Headers object obtained from a Request (<code>Request.headers</code>).</li>
- <li>“request-no-cors”: guard for a Headers object obtained from a Request created
- <br/>with mode “no-cors”.</li>
- <li>“response”: naturally, for Headers obtained from Response (<code>Response.headers</code>).</li>
- <li>“immutable”: Mostly used for ServiceWorkers, renders a Headers object
- <br/>read-only.</li>
- </ul>
- <p>The details of how each guard affects the behaviors of the Headers object are
- <br/>in the <a href="https://fetch.spec.whatwg.org">specification</a>. For example, you may not append or set a “request” guarded Headers’ “Content-Length” header. Similarly, inserting “Set-Cookie” into a Response header is not allowed so that ServiceWorkers may not set cookies via synthesized Responses.</p>
- <p>All of the Headers methods throw TypeError if <code>name</code> is not a <a href="https://fetch.spec.whatwg.org/#concept-header-name">valid HTTP Header name</a>. The mutation operations will throw TypeError if there is an immutable guard. Otherwise they fail silently. For example:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> res <span>=</span> Response.<span>error</span><span>(</span><span>)</span><span>;</span>
-<span>try</span> <span>{</span>
- res.<span>headers</span>.<span>set</span><span>(</span><span>"Origin"</span><span>,</span> <span>"http://mybank.com"</span><span>)</span><span>;</span>
-<span>}</span> <span>catch</span><span>(</span>e<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Cannot pretend to be a bank!"</span><span>)</span><span>;</span>
-<span>}</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <h2>Request</h2>
- <p>The Request interface defines a request to fetch a resource over HTTP. URL, method and headers are expected, but the Request also allows specifying a body, a request mode, credentials and cache hints.</p>
- <p>The simplest Request is of course, just a URL, as you may do to GET a resource.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> req <span>=</span> <span>new</span> Request<span>(</span><span>"/index.html"</span><span>)</span><span>;</span>
-console.<span>log</span><span>(</span>req.<span>method</span><span>)</span><span>;</span> <span>// "GET"</span>
-console.<span>log</span><span>(</span>req.<span>url</span><span>)</span><span>;</span> <span>// "http://example.com/index.html"</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>You may also pass a Request to the <code>Request()</code> constructor to create a copy.
- <br/>(This is not the same as calling the <code>clone()</code> method, which is covered in
- <br/>the “Reading bodies” section.).</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> copy <span>=</span> <span>new</span> Request<span>(</span>req<span>)</span><span>;</span>
-console.<span>log</span><span>(</span>copy.<span>method</span><span>)</span><span>;</span> <span>// "GET"</span>
-console.<span>log</span><span>(</span>copy.<span>url</span><span>)</span><span>;</span> <span>// "http://example.com/index.html"</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
+<article class="post" role="article"><p>For more than a decade the Web has used XMLHttpRequest (XHR) to achieve
+ asynchronous requests in JavaScript. While very useful, XHR is not a very
+ nice API. It suffers from lack of separation of concerns. The input, output
+ and state are all managed by interacting with one object, and state is
+ tracked using events. Also, the event-based model doesn’t play well with
+ JavaScript’s recent focus on Promise- and generator-based asynchronous
+ programming.</p>
+ <p>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a> intends
+ to fix most of these problems. It does this by introducing the same primitives
+ to JS that are used in the HTTP protocol. In addition, it introduces a
+ utility function <code>fetch()</code> that succinctly captures the intention
+ of retrieving a resource from the network.</p>
+ <p>The <a href="https://fetch.spec.whatwg.org">Fetch specification</a>, which
+ defines the API, nails down the semantics of a user agent fetching a resource.
+ This, combined with ServiceWorkers, is an attempt to:</p>
+ <ol><li>Improve the offline experience.</li>
+ <li>Expose the building blocks of the Web to the platform as part of the
+ <a href="https://extensiblewebmanifesto.org/">extensible web movement</a>.</li>
+ </ol><p>As of this writing, the Fetch API is available in Firefox 39 (currently
+ Nightly) and Chrome 42 (currently dev). Github has a <a href="https://github.com/github/fetch">Fetch polyfill</a>.</p>
+
+<h2>Feature detection</h2>
+
+ <p>Fetch API support can be detected by checking for <code>Headers</code>,<code>Request</code>, <code>Response</code> or <code>fetch</code> on
+ the <code>window</code> or <code>worker</code> scope.</p>
+
+<h2>Simple fetching</h2>
+
+ <p>The most useful, high-level part of the Fetch API is the <code>fetch()</code> function.
+ In its simplest form it takes a URL and returns a promise that resolves
+ to the response. The response is captured as a <code>Response</code> object.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">fetch<span style="color: #009900;">(</span><span style="color: #3366CC;">"/data.json"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>res<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #006600; font-style: italic;">// res instanceof Response == true.</span>
+ <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">(</span>res.<span style="color: #660066;">ok</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ res.<span style="color: #660066;">json</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>data<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>data.<span style="color: #660066;">entries</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Looks like the response wasn't perfect, got status"</span><span style="color: #339933;">,</span> res.<span style="color: #660066;">status</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span>
+<span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Fetch failed!"</span><span style="color: #339933;">,</span> e<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>Submitting some parameters, it would look like this:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">fetch<span style="color: #009900;">(</span><span style="color: #3366CC;">"http://www.example.org/submit.php"</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span>
+ method<span style="color: #339933;">:</span> <span style="color: #3366CC;">"POST"</span><span style="color: #339933;">,</span>
+ headers<span style="color: #339933;">:</span> <span style="color: #009900;">{</span>
+ <span style="color: #3366CC;">"Content-Type"</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">"application/x-www-form-urlencoded"</span>
+ <span style="color: #009900;">}</span><span style="color: #339933;">,</span>
+ body<span style="color: #339933;">:</span> <span style="color: #3366CC;">"firstName=Nikhil&amp;favColor=blue&amp;password=easytoguess"</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>res<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">(</span>res.<span style="color: #660066;">ok</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ alert<span style="color: #009900;">(</span><span style="color: #3366CC;">"Perfect! Your settings are saved."</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">(</span>res.<span style="color: #660066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">401</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ alert<span style="color: #009900;">(</span><span style="color: #3366CC;">"Oops! You are not authorized."</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span>
+<span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ alert<span style="color: #009900;">(</span><span style="color: #3366CC;">"Error submitting form!"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The <code>fetch()</code> function’s arguments are the same as those passed
+ to the
+ <br></br><code>Request()</code> constructor, so you may directly pass arbitrarily
+ complex requests to <code>fetch()</code> as discussed below.</p>
+
+<h2>Headers</h2>
+
+ <p>Fetch introduces 3 interfaces. These are <code>Headers</code>, <code>Request</code> and
+ <br></br><code>Response</code>. They map directly to the underlying HTTP concepts,
+ but have
+ <br></br>certain visibility filters in place for privacy and security reasons,
+ such as
+ <br></br>supporting CORS rules and ensuring cookies aren’t readable by third parties.</p>
+ <p>The <a href="https://fetch.spec.whatwg.org/#headers-class">Headers interface</a> is
+ a simple multi-map of names to values:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> <span style="color: #3366CC;">"Hello World"</span><span style="color: #339933;">;</span>
+<span style="color: #000066; font-weight: bold;">var</span> reqHeaders <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Headers<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+reqHeaders.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Content-Type"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"text/plain"</span>
+reqHeaders.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Content-Length"</span><span style="color: #339933;">,</span> content.<span style="color: #660066;">length</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+reqHeaders.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"ProcessThisImmediately"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The same can be achieved by passing an array of arrays or a JS object
+ literal
+ <br></br>to the constructor:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">reqHeaders <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Headers<span style="color: #009900;">(</span><span style="color: #009900;">{</span>
+ <span style="color: #3366CC;">"Content-Type"</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">"text/plain"</span><span style="color: #339933;">,</span>
+ <span style="color: #3366CC;">"Content-Length"</span><span style="color: #339933;">:</span> content.<span style="color: #660066;">length</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
+ <span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">"ProcessThisImmediately"</span><span style="color: #339933;">,</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The contents can be queried and retrieved:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>reqHeaders.<span style="color: #660066;">has</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Content-Type"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>reqHeaders.<span style="color: #660066;">has</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Set-Cookie"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
+reqHeaders.<span style="color: #000066; font-weight: bold;">set</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Content-Type"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"text/html"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+reqHeaders.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"AnotherValue"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>reqHeaders.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Content-Length"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 11</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>reqHeaders.<span style="color: #660066;">getAll</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ["ProcessThisImmediately", "AnotherValue"]</span>
+
+reqHeaders.<span style="color: #000066; font-weight: bold;">delete</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>reqHeaders.<span style="color: #660066;">getAll</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"X-Custom-Header"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// []</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>Some of these operations are only useful in ServiceWorkers, but they provide
+ <br></br>a much nicer API to Headers.</p>
+ <p>Since Headers can be sent in requests, or received in responses, and have
+ various limitations about what information can and should be mutable, <code>Headers</code> objects
+ have a <strong>guard</strong> property. This is not exposed to the Web, but
+ it affects which mutation operations are allowed on the Headers object.
+ <br></br>Possible values are:</p>
+ <ul><li>“none”: default.</li>
+ <li>“request”: guard for a Headers object obtained from a Request (<code>Request.headers</code>).</li>
+ <li>“request-no-cors”: guard for a Headers object obtained from a Request
+ created
+ <br></br>with mode “no-cors”.</li>
+ <li>“response”: naturally, for Headers obtained from Response (<code>Response.headers</code>).</li>
+ <li>“immutable”: Mostly used for ServiceWorkers, renders a Headers object
+ <br></br>read-only.</li>
+ </ul><p>The details of how each guard affects the behaviors of the Headers object
+ are
+ <br></br>in the <a href="https://fetch.spec.whatwg.org">specification</a>. For example,
+ you may not append or set a “request” guarded Headers’ “Content-Length”
+ header. Similarly, inserting “Set-Cookie” into a Response header is not
+ allowed so that ServiceWorkers may not set cookies via synthesized Responses.</p>
+ <p>All of the Headers methods throw TypeError if <code>name</code> is not a
+ <a href="https://fetch.spec.whatwg.org/#concept-header-name">valid HTTP Header name</a>. The mutation operations will throw TypeError
+ if there is an immutable guard. Otherwise they fail silently. For example:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> res <span style="color: #339933;">=</span> Response.<span style="color: #660066;">error</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">{</span>
+ res.<span style="color: #660066;">headers</span>.<span style="color: #000066; font-weight: bold;">set</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Origin"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"http://mybank.com"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Cannot pretend to be a bank!"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span></pre>
+ </td>
+ </tr></tbody></table></p>
+
+<h2>Request</h2>
+
+ <p>The Request interface defines a request to fetch a resource over HTTP.
+ URL, method and headers are expected, but the Request also allows specifying
+ a body, a request mode, credentials and cache hints.</p>
+ <p>The simplest Request is of course, just a URL, as you may do to GET a
+ resource.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Request<span style="color: #009900;">(</span><span style="color: #3366CC;">"/index.html"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>req.<span style="color: #660066;">method</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// "GET"</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// "http://example.com/index.html"</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>You may also pass a Request to the <code>Request()</code> constructor to
+ create a copy.
+ <br></br>(This is not the same as calling the <code>clone()</code> method, which
+ is covered in
+ <br></br>the “Reading bodies” section.).</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> copy <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Request<span style="color: #009900;">(</span>req<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>copy.<span style="color: #660066;">method</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// "GET"</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>copy.<span style="color: #660066;">url</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// "http://example.com/index.html"</span></pre>
+ </td>
+ </tr></tbody></table></p>
<p>Again, this form is probably only useful in ServiceWorkers.</p>
- <p>The non-URL attributes of the <code>Request</code> can only be set by passing initial
- <br/>values as a second argument to the constructor. This argument is a dictionary.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> uploadReq <span>=</span> <span>new</span> Request<span>(</span><span>"/uploadImage"</span><span>,</span> <span>{</span>
- method<span>:</span> <span>"POST"</span><span>,</span>
- headers<span>:</span> <span>{</span>
- <span>"Content-Type"</span><span>:</span> <span>"image/png"</span><span>,</span>
- <span>}</span><span>,</span>
- body<span>:</span> <span>"image data"</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The Request’s mode is used to determine if cross-origin requests lead to valid responses, and which properties on the response are readable. Legal mode values are <code>"same-origin"</code>, <code>"no-cors"</code> (default) and <code>"cors"</code>.</p>
- <p>The <code>"same-origin"</code> mode is simple, if a request is made to another origin with this mode set, the result is simply an error. You could use this to ensure that
- <br/>a request is always being made to your origin.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> arbitraryUrl <span>=</span> document.<span>getElementById</span><span>(</span><span>"url-input"</span><span>)</span>.<span>value</span><span>;</span>
-fetch<span>(</span>arbitraryUrl<span>,</span> <span>{</span> mode<span>:</span> <span>"same-origin"</span> <span>}</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>res<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Response succeeded?"</span><span>,</span> res.<span>ok</span><span>)</span><span>;</span>
-<span>}</span><span>,</span> <span>function</span><span>(</span>e<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Please enter a same-origin URL!"</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The <code>"no-cors"</code> mode captures what the web platform does by default for scripts you import from CDNs, images hosted on other domains, and so on. First, it prevents the method from being anything other than “HEAD”, “GET” or “POST”. Second, if any ServiceWorkers intercept these requests, they may not add or override any headers except for <a href="https://fetch.spec.whatwg.org/#simple-header">these</a>. Third, JavaScript may not access any properties of the resulting Response. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues that could arise from leaking data across domains.</p>
- <p><code>"cors"</code> mode is what you’ll usually use to make known cross-origin requests to access various APIs offered by other vendors. These are expected to adhere to
- <br/>the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS protocol</a>. Only a <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">limited set</a> of headers is exposed in the Response, but the body is readable. For example, you could get a list of Flickr’s <a href="https://www.flickr.com/services/api/flickr.interestingness.getList.html">most interesting</a> photos today like this:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> u <span>=</span> <span>new</span> URLSearchParams<span>(</span><span>)</span><span>;</span>
-u.<span>append</span><span>(</span><span>'method'</span><span>,</span> <span>'flickr.interestingness.getList'</span><span>)</span><span>;</span>
-u.<span>append</span><span>(</span><span>'api_key'</span><span>,</span> <span>'&lt;insert api key here&gt;'</span><span>)</span><span>;</span>
-u.<span>append</span><span>(</span><span>'format'</span><span>,</span> <span>'json'</span><span>)</span><span>;</span>
-u.<span>append</span><span>(</span><span>'nojsoncallback'</span><span>,</span> <span>'1'</span><span>)</span><span>;</span>
-&nbsp;
-<span>var</span> apiCall <span>=</span> fetch<span>(</span><span>'https://api.flickr.com/services/rest?'</span> <span>+</span> u<span>)</span><span>;</span>
-&nbsp;
-apiCall.<span>then</span><span>(</span><span>function</span><span>(</span>response<span>)</span> <span>{</span>
- <span>return</span> response.<span>json</span><span>(</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>json<span>)</span> <span>{</span>
- <span>// photo is a list of photos.</span>
- <span>return</span> json.<span>photos</span>.<span>photo</span><span>;</span>
- <span>}</span><span>)</span><span>;</span>
-<span>}</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>photos<span>)</span> <span>{</span>
- photos.<span>forEach</span><span>(</span><span>function</span><span>(</span>photo<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span>photo.<span>title</span><span>)</span><span>;</span>
- <span>}</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>You may not read out the “Date” header since Flickr does not allow it via
- <br/> <code>Access-Control-Expose-Headers</code>.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">response.<span>headers</span>.<span>get</span><span>(</span><span>"Date"</span><span>)</span><span>;</span> <span>// null</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>The <code>credentials</code> enumeration determines if cookies for the other domain are
- <br/>sent to cross-origin requests. This is similar to XHR’s <code>withCredentials</code>
- <br/>flag, but tri-valued as <code>"omit"</code> (default), <code>"same-origin"</code> and <code>"include"</code>.</p>
- <p>The Request object will also give the ability to offer caching hints to the user-agent. This is currently undergoing some <a href="https://github.com/slightlyoff/ServiceWorker/issues/585">security review</a>. Firefox exposes the attribute, but it has no effect.</p>
- <p>Requests have two read-only attributes that are relevant to ServiceWorkers
- <br/>intercepting them. There is the string <code>referrer</code>, which is set by the UA to be
- <br/>the referrer of the Request. This may be an empty string. The other is
- <br/> <code>context</code> which is a rather <a href="https://fetch.spec.whatwg.org/#requestcredentials">large enumeration</a> defining what sort of resource is being fetched. This could be “image” if the request is from an &lt;img&gt;tag in the controlled document, “worker” if it is an attempt to load a worker script, and so on. When used with the <code>fetch()</code> function, it is “fetch”.</p>
- <h2>Response</h2>
- <p><code>Response</code> instances are returned by calls to <code>fetch()</code>. They can also be created by JS, but this is only useful in ServiceWorkers.</p>
- <p>We have already seen some attributes of Response when we looked at <code>fetch()</code>. The most obvious candidates are <code>status</code>, an integer (default value 200) and <code>statusText</code> (default value “OK”), which correspond to the HTTP status code and reason. The <code>ok</code> attribute is just a shorthand for checking that <code>status</code> is in the range 200-299 inclusive.</p>
- <p><code>headers</code> is the Response’s Headers object, with guard “response”. The <code>url</code> attribute reflects the URL of the corresponding request.</p>
- <p>Response also has a <code>type</code>, which is “basic”, “cors”, “default”, “error” or
- <br/>“opaque”.</p>
- <ul>
- <li><code>"basic"</code>: normal, same origin response, with all headers exposed except
- <br/>“Set-Cookie” and “Set-Cookie2″.</li>
- <li><code>"cors"</code>: response was received from a valid cross-origin request. <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">Certain headers and the body</a>may be accessed.</li>
- <li><code>"error"</code>: network error. No useful information describing the error is available. The Response’s status is 0, headers are empty and immutable. This is the type for a Response obtained from <code>Response.error()</code>.</li>
- <li><code>"opaque"</code>: response for “no-cors” request to cross-origin resource. <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-opaque">Severely<br/>
- restricted</a> </li>
- </ul>
- <p>The “error” type results in the <code>fetch()</code> Promise rejecting with TypeError.</p>
- <p>There are certain attributes that are useful only in a ServiceWorker scope. The
- <br/>idiomatic way to return a Response to an intercepted request in ServiceWorkers is:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">addEventListener<span>(</span><span>'fetch'</span><span>,</span> <span>function</span><span>(</span>event<span>)</span> <span>{</span>
- event.<span>respondWith</span><span>(</span><span>new</span> Response<span>(</span><span>"Response body"</span><span>,</span> <span>{</span>
- headers<span>:</span> <span>{</span> <span>"Content-Type"</span> <span>:</span> <span>"text/plain"</span> <span>}</span>
- <span>}</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>As you can see, Response has a two argument constructor, where both arguments are optional. The first argument is a body initializer, and the second is a dictionary to set the <code>status</code>, <code>statusText</code> and <code>headers</code>.</p>
- <p>The static method <code>Response.error()</code> simply returns an error response. Similarly, <code>Response.redirect(url, status)</code> returns a Response resulting in
- <br/>a redirect to <code>url</code>.</p>
- <h2>Dealing with bodies</h2>
- <p>Both Requests and Responses may contain body data. We’ve been glossing over it because of the various data types body may contain, but we will cover it in detail now.</p>
- <p>A body is an instance of any of the following types.</p>
- <ul>
- <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a> </li>
- <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView">ArrayBufferView</a> (Uint8Array and friends)</li>
- <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob">Blob</a>/ <a href="https://developer.mozilla.org/en-US/docs/Web/API/File">File</a> </li>
- <li>string</li>
- <li><a href="https://url.spec.whatwg.org/#interface-urlsearchparams">URLSearchParams</a> </li>
- <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData">FormData</a> – currently not supported by either Gecko or Blink. Firefox expects to ship this in version 39 along with the rest of Fetch.</li>
- </ul>
- <p>In addition, Request and Response both offer the following methods to extract their body. These all return a Promise that is eventually resolved with the actual content.</p>
- <ul>
- <li><code>arrayBuffer()</code> </li>
- <li><code>blob()</code> </li>
- <li><code>json()</code> </li>
- <li><code>text()</code> </li>
- <li><code>formData()</code> </li>
- </ul>
- <p>This is a significant improvement over XHR in terms of ease of use of non-text data!</p>
- <p>Request bodies can be set by passing <code>body</code> parameters:</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> form <span>=</span> <span>new</span> FormData<span>(</span>document.<span>getElementById</span><span>(</span><span>'login-form'</span><span>)</span><span>)</span><span>;</span>
-fetch<span>(</span><span>"/login"</span><span>,</span> <span>{</span>
- method<span>:</span> <span>"POST"</span><span>,</span>
- body<span>:</span> form
-<span>}</span><span>)</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>Responses take the first argument as the body.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> res <span>=</span> <span>new</span> Response<span>(</span><span>new</span> File<span>(</span><span>[</span><span>"chunk"</span><span>,</span> <span>"chunk"</span><span>]</span><span>,</span> <span>"archive.zip"</span><span>,</span>
- <span>{</span> type<span>:</span> <span>"application/zip"</span> <span>}</span><span>)</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>Both Request and Response (and by extension the <code>fetch()</code> function), will try to intelligently <a href="https://fetch.spec.whatwg.org/#concept-bodyinit-extract">determine the content type</a>. Request will also automatically set a “Content-Type” header if none is set in the dictionary.</p>
- <h3>Streams and cloning</h3>
- <p>It is important to realise that Request and Response bodies can only be read once! Both interfaces have a boolean attribute <code>bodyUsed</code> to determine if it is safe to read or not.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript"><span>var</span> res <span>=</span> <span>new</span> Response<span>(</span><span>"one time use"</span><span>)</span><span>;</span>
-console.<span>log</span><span>(</span>res.<span>bodyUsed</span><span>)</span><span>;</span> <span>// false</span>
-res.<span>text</span><span>(</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>v<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span>res.<span>bodyUsed</span><span>)</span><span>;</span> <span>// true</span>
-<span>}</span><span>)</span><span>;</span>
-console.<span>log</span><span>(</span>res.<span>bodyUsed</span><span>)</span><span>;</span> <span>// true</span>
-&nbsp;
-res.<span>text</span><span>(</span><span>)</span>.<span>catch</span><span>(</span><span>function</span><span>(</span>e<span>)</span> <span>{</span>
- console.<span>log</span><span>(</span><span>"Tried to read already consumed Response"</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>This decision allows easing the transition to an eventual <a href="https://streams.spec.whatwg.org/">stream-based</a> Fetch API. The intention is to let applications consume data as it arrives, allowing for JavaScript to deal with larger files like videos, and perform things like compression and editing on the fly.</p>
- <p>Often, you’ll want access to the body multiple times. For example, you can use the upcoming <a href="http://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-objects">Cache API</a> to store Requests and Responses for offline use, and Cache requires bodies to be available for reading.</p>
- <p>So how do you read out the body multiple times within such constraints? The API provides a <code>clone()</code> method on the two interfaces. This will return a clone of the object, with a ‘new’ body. <code>clone()</code> MUST be called before the body of the corresponding object has been used. That is, <code>clone()</code> first, read later.</p>
- <div class="wp_syntax">
- <table>
- <tbody>
- <tr>
- <td class="code"><pre class="javascript">addEventListener<span>(</span><span>'fetch'</span><span>,</span> <span>function</span><span>(</span>evt<span>)</span> <span>{</span>
- <span>var</span> sheep <span>=</span> <span>new</span> Response<span>(</span><span>"Dolly"</span><span>)</span><span>;</span>
- console.<span>log</span><span>(</span>sheep.<span>bodyUsed</span><span>)</span><span>;</span> <span>// false</span>
- <span>var</span> clone <span>=</span> sheep.<span>clone</span><span>(</span><span>)</span><span>;</span>
- console.<span>log</span><span>(</span>clone.<span>bodyUsed</span><span>)</span><span>;</span> <span>// false</span>
-&nbsp;
- clone.<span>text</span><span>(</span><span>)</span><span>;</span>
- console.<span>log</span><span>(</span>sheep.<span>bodyUsed</span><span>)</span><span>;</span> <span>// false</span>
- console.<span>log</span><span>(</span>clone.<span>bodyUsed</span><span>)</span><span>;</span> <span>// true</span>
-&nbsp;
- evt.<span>respondWith</span><span>(</span>cache.<span>add</span><span>(</span>sheep.<span>clone</span><span>(</span><span>)</span><span>)</span>.<span>then</span><span>(</span><span>function</span><span>(</span>e<span>)</span> <span>{</span>
- <span>return</span> sheep<span>;</span>
- <span>}</span><span>)</span><span>;</span>
-<span>}</span><span>)</span><span>;</span></pre> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <h2>Future improvements</h2>
- <p>Along with the transition to streams, Fetch will eventually have the ability to abort running <code>fetch()</code>es and some way to report the progress of a fetch. These are provided by XHR, but are a little tricky to fit in the Promise-based nature of the Fetch API.</p>
- <p>You can contribute to the evolution of this API by participating in discussions on the <a href="https://whatwg.org/mailing-list">WHATWG mailing list</a> and in the issues in the <a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&amp;component=Fetch&amp;resolution=---">Fetch</a> and <a href="https://github.com/slightlyoff/ServiceWorker/issues">ServiceWorker</a>specifications.</p>
- <p>For a better web!</p>
- <p><em>The author would like to thank Andrea Marchesini, Anne van Kesteren and Ben<br/>
-Kelly for helping with the specification and implementation.</em> </p>
- </article> \ No newline at end of file
+ <p>The non-URL attributes of the <code>Request</code> can only be set by passing
+ initial
+ <br></br>values as a second argument to the constructor. This argument is a dictionary.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> uploadReq <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Request<span style="color: #009900;">(</span><span style="color: #3366CC;">"/uploadImage"</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span>
+ method<span style="color: #339933;">:</span> <span style="color: #3366CC;">"POST"</span><span style="color: #339933;">,</span>
+ headers<span style="color: #339933;">:</span> <span style="color: #009900;">{</span>
+ <span style="color: #3366CC;">"Content-Type"</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">"image/png"</span><span style="color: #339933;">,</span>
+ <span style="color: #009900;">}</span><span style="color: #339933;">,</span>
+ body<span style="color: #339933;">:</span> <span style="color: #3366CC;">"image data"</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The Request’s mode is used to determine if cross-origin requests lead
+ to valid responses, and which properties on the response are readable.
+ Legal mode values are <code>"same-origin"</code>, <code>"no-cors"</code> (default)
+ and <code>"cors"</code>.</p>
+ <p>The <code>"same-origin"</code> mode is simple, if a request is made to another
+ origin with this mode set, the result is simply an error. You could use
+ this to ensure that
+ <br></br>a request is always being made to your origin.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> arbitraryUrl <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"url-input"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
+fetch<span style="color: #009900;">(</span>arbitraryUrl<span style="color: #339933;">,</span> <span style="color: #009900;">{</span> mode<span style="color: #339933;">:</span> <span style="color: #3366CC;">"same-origin"</span> <span style="color: #009900;">}</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>res<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Response succeeded?"</span><span style="color: #339933;">,</span> res.<span style="color: #660066;">ok</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Please enter a same-origin URL!"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The <code>"no-cors"</code> mode captures what the web platform does by default
+ for scripts you import from CDNs, images hosted on other domains, and so
+ on. First, it prevents the method from being anything other than “HEAD”,
+ “GET” or “POST”. Second, if any ServiceWorkers intercept these requests,
+ they may not add or override any headers except for <a href="https://fetch.spec.whatwg.org/#simple-header">these</a>.
+ Third, JavaScript may not access any properties of the resulting Response.
+ This ensures that ServiceWorkers do not affect the semantics of the Web
+ and prevents security and privacy issues that could arise from leaking
+ data across domains.</p>
+ <p><code>"cors"</code> mode is what you’ll usually use to make known cross-origin
+ requests to access various APIs offered by other vendors. These are expected
+ to adhere to
+ <br></br>the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS protocol</a>.
+ Only a <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">limited set</a> of
+ headers is exposed in the Response, but the body is readable. For example,
+ you could get a list of Flickr’s <a href="https://www.flickr.com/services/api/flickr.interestingness.getList.html">most interesting</a> photos
+ today like this:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> u <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> URLSearchParams<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+u.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">'method'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'flickr.interestingness.getList'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+u.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">'api_key'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'&lt;insert api key here&gt;'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+u.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">'format'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'json'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+u.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">'nojsoncallback'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+
+<span style="color: #000066; font-weight: bold;">var</span> apiCall <span style="color: #339933;">=</span> fetch<span style="color: #009900;">(</span><span style="color: #3366CC;">'https://api.flickr.com/services/rest?'</span> <span style="color: #339933;">+</span> u<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+
+apiCall.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>response<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #000066; font-weight: bold;">return</span> response.<span style="color: #660066;">json</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>json<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #006600; font-style: italic;">// photo is a list of photos.</span>
+ <span style="color: #000066; font-weight: bold;">return</span> json.<span style="color: #660066;">photos</span>.<span style="color: #660066;">photo</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>photos<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ photos.<span style="color: #660066;">forEach</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>photo<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>photo.<span style="color: #660066;">title</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>You may not read out the “Date” header since Flickr does not allow it
+ via
+ <br></br><code>Access-Control-Expose-Headers</code>.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">response.<span style="color: #660066;">headers</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Date"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// null</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>The <code>credentials</code> enumeration determines if cookies for the other
+ domain are
+ <br></br>sent to cross-origin requests. This is similar to XHR’s <code>withCredentials</code>
+ <br></br>flag, but tri-valued as <code>"omit"</code> (default), <code>"same-origin"</code> and <code>"include"</code>.</p>
+ <p>The Request object will also give the ability to offer caching hints to
+ the user-agent. This is currently undergoing some <a href="https://github.com/slightlyoff/ServiceWorker/issues/585">security review</a>.
+ Firefox exposes the attribute, but it has no effect.</p>
+ <p>Requests have two read-only attributes that are relevant to ServiceWorkers
+ <br></br>intercepting them. There is the string <code>referrer</code>, which is
+ set by the UA to be
+ <br></br>the referrer of the Request. This may be an empty string. The other is
+ <br></br><code>context</code> which is a rather <a href="https://fetch.spec.whatwg.org/#requestcredentials">large enumeration</a> defining
+ what sort of resource is being fetched. This could be “image” if the request
+ is from an
+ &lt;img&gt;tag in the controlled document, “worker” if it is an attempt to load a
+ worker script, and so on. When used with the <code>fetch()</code> function,
+ it is “fetch”.</p>
+
+<h2>Response</h2>
+
+ <p><code>Response</code> instances are returned by calls to <code>fetch()</code>.
+ They can also be created by JS, but this is only useful in ServiceWorkers.</p>
+ <p>We have already seen some attributes of Response when we looked at <code>fetch()</code>.
+ The most obvious candidates are <code>status</code>, an integer (default
+ value 200) and <code>statusText</code> (default value “OK”), which correspond
+ to the HTTP status code and reason. The <code>ok</code> attribute is just
+ a shorthand for checking that <code>status</code> is in the range 200-299
+ inclusive.</p>
+ <p><code>headers</code> is the Response’s Headers object, with guard “response”.
+ The <code>url</code> attribute reflects the URL of the corresponding request.</p>
+ <p>Response also has a <code>type</code>, which is “basic”, “cors”, “default”,
+ “error” or
+ <br></br>“opaque”.</p>
+ <ul><li><code>"basic"</code>: normal, same origin response, with all headers exposed
+ except
+ <br></br>“Set-Cookie” and “Set-Cookie2″.</li>
+ <li><code>"cors"</code>: response was received from a valid cross-origin request.
+ <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-cors">Certain headers and the body</a>may be accessed.</li>
+ <li><code>"error"</code>: network error. No useful information describing
+ the error is available. The Response’s status is 0, headers are empty and
+ immutable. This is the type for a Response obtained from <code>Response.error()</code>.</li>
+ <li><code>"opaque"</code>: response for “no-cors” request to cross-origin
+ resource. <a href="https://fetch.spec.whatwg.org/#concept-filtered-response-opaque">Severely<br></br>
+ restricted</a>
+ </li>
+ </ul><p>The “error” type results in the <code>fetch()</code> Promise rejecting with
+ TypeError.</p>
+ <p>There are certain attributes that are useful only in a ServiceWorker scope.
+ The
+ <br></br>idiomatic way to return a Response to an intercepted request in ServiceWorkers
+ is:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">addEventListener<span style="color: #009900;">(</span><span style="color: #3366CC;">'fetch'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>event<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ event.<span style="color: #660066;">respondWith</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">new</span> Response<span style="color: #009900;">(</span><span style="color: #3366CC;">"Response body"</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span>
+ headers<span style="color: #339933;">:</span> <span style="color: #009900;">{</span> <span style="color: #3366CC;">"Content-Type"</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">"text/plain"</span> <span style="color: #009900;">}</span>
+ <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>As you can see, Response has a two argument constructor, where both arguments
+ are optional. The first argument is a body initializer, and the second
+ is a dictionary to set the <code>status</code>, <code>statusText</code> and <code>headers</code>.</p>
+ <p>The static method <code>Response.error()</code> simply returns an error
+ response. Similarly, <code>Response.redirect(url, status)</code> returns
+ a Response resulting in
+ <br></br>a redirect to <code>url</code>.</p>
+
+<h2>Dealing with bodies</h2>
+
+ <p>Both Requests and Responses may contain body data. We’ve been glossing
+ over it because of the various data types body may contain, but we will
+ cover it in detail now.</p>
+ <p>A body is an instance of any of the following types.</p>
+ <ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a>
+ </li>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView">ArrayBufferView</a> (Uint8Array
+ and friends)</li>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob">Blob</a>/
+ <a href="https://developer.mozilla.org/en-US/docs/Web/API/File">File</a>
+ </li>
+ <li>string</li>
+ <li><a href="https://url.spec.whatwg.org/#interface-urlsearchparams">URLSearchParams</a>
+ </li>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData">FormData</a> –
+ currently not supported by either Gecko or Blink. Firefox expects to ship
+ this in version 39 along with the rest of Fetch.</li>
+ </ul><p>In addition, Request and Response both offer the following methods to
+ extract their body. These all return a Promise that is eventually resolved
+ with the actual content.</p>
+ <ul><li><code>arrayBuffer()</code>
+ </li>
+ <li><code>blob()</code>
+ </li>
+ <li><code>json()</code>
+ </li>
+ <li><code>text()</code>
+ </li>
+ <li><code>formData()</code>
+ </li>
+ </ul><p>This is a significant improvement over XHR in terms of ease of use of
+ non-text data!</p>
+ <p>Request bodies can be set by passing <code>body</code> parameters:</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> FormData<span style="color: #009900;">(</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">(</span><span style="color: #3366CC;">'login-form'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+fetch<span style="color: #009900;">(</span><span style="color: #3366CC;">"/login"</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span>
+ method<span style="color: #339933;">:</span> <span style="color: #3366CC;">"POST"</span><span style="color: #339933;">,</span>
+ body<span style="color: #339933;">:</span> form
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>Responses take the first argument as the body.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> res <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Response<span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">new</span> File<span style="color: #009900;">(</span><span style="color: #009900;">[</span><span style="color: #3366CC;">"chunk"</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"chunk"</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">"archive.zip"</span><span style="color: #339933;">,</span>
+ <span style="color: #009900;">{</span> type<span style="color: #339933;">:</span> <span style="color: #3366CC;">"application/zip"</span> <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>Both Request and Response (and by extension the <code>fetch()</code> function),
+ will try to intelligently <a href="https://fetch.spec.whatwg.org/#concept-bodyinit-extract">determine the content type</a>.
+ Request will also automatically set a “Content-Type” header if none is
+ set in the dictionary.</p>
+
+<h3>Streams and cloning</h3>
+
+ <p>It is important to realise that Request and Response bodies can only be
+ read once! Both interfaces have a boolean attribute <code>bodyUsed</code> to
+ determine if it is safe to read or not.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> res <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Response<span style="color: #009900;">(</span><span style="color: #3366CC;">"one time use"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>res.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
+res.<span style="color: #660066;">text</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>v<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>res.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>res.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
+
+res.<span style="color: #660066;">text</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"Tried to read already consumed Response"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+ <p>This decision allows easing the transition to an eventual <a href="https://streams.spec.whatwg.org/">stream-based</a> Fetch
+ API. The intention is to let applications consume data as it arrives, allowing
+ for JavaScript to deal with larger files like videos, and perform things
+ like compression and editing on the fly.</p>
+ <p>Often, you’ll want access to the body multiple times. For example, you
+ can use the upcoming <a href="http://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-objects">Cache API</a> to
+ store Requests and Responses for offline use, and Cache requires bodies
+ to be available for reading.</p>
+ <p>So how do you read out the body multiple times within such constraints?
+ The API provides a <code>clone()</code> method on the two interfaces. This
+ will return a clone of the object, with a ‘new’ body. <code>clone()</code> MUST
+ be called before the body of the corresponding object has been used. That
+ is, <code>clone()</code> first, read later.</p>
+ <p>
+ <table><tbody><tr><td class="code"><pre class="javascript" style="font-family:monospace;">addEventListener<span style="color: #009900;">(</span><span style="color: #3366CC;">'fetch'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>evt<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #000066; font-weight: bold;">var</span> sheep <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Response<span style="color: #009900;">(</span><span style="color: #3366CC;">"Dolly"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>sheep.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
+ <span style="color: #000066; font-weight: bold;">var</span> clone <span style="color: #339933;">=</span> sheep.<span style="color: #660066;">clone</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>clone.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
+
+ clone.<span style="color: #660066;">text</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>sheep.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
+ console.<span style="color: #660066;">log</span><span style="color: #009900;">(</span>clone.<span style="color: #660066;">bodyUsed</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
+
+ evt.<span style="color: #660066;">respondWith</span><span style="color: #009900;">(</span>cache.<span style="color: #660066;">add</span><span style="color: #009900;">(</span>sheep.<span style="color: #660066;">clone</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>.<span style="color: #660066;">then</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
+ <span style="color: #000066; font-weight: bold;">return</span> sheep<span style="color: #339933;">;</span>
+ <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
+<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
+ </td>
+ </tr></tbody></table></p>
+
+<h2>Future improvements</h2>
+
+ <p>Along with the transition to streams, Fetch will eventually have the ability
+ to abort running <code>fetch()</code>es and some way to report the progress
+ of a fetch. These are provided by XHR, but are a little tricky to fit in
+ the Promise-based nature of the Fetch API.</p>
+ <p>You can contribute to the evolution of this API by participating in discussions
+ on the <a href="https://whatwg.org/mailing-list">WHATWG mailing list</a> and
+ in the issues in the <a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&amp;component=Fetch&amp;resolution=---">Fetch</a> and
+ <a href="https://github.com/slightlyoff/ServiceWorker/issues">ServiceWorker</a>specifications.</p>
+ <p>For a better web!</p>
+ <p><em>The author would like to thank Andrea Marchesini, Anne van Kesteren and Ben<br></br>
+Kelly for helping with the specification and implementation.</em>
+ </p>
+ </article> \ No newline at end of file