Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Body mixin; document Request and Response APIs instead #6455

Merged
merged 5 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7005,6 +7005,13 @@
/en-US/docs/Web/API/BluetoothGattService/getIncludedServices /en-US/docs/Web/API/BluetoothRemoteGATTService/getIncludedServices
/en-US/docs/Web/API/BluetoothGattService/isPrimary /en-US/docs/Web/API/BluetoothRemoteGATTService/isPrimary
/en-US/docs/Web/API/BluetoothGattService/uuid /en-US/docs/Web/API/BluetoothRemoteGATTService/uuid
/en-US/docs/Web/API/Body/arrayBuffer /en-US/docs/Web/API/Response/arrayBuffer
/en-US/docs/Web/API/Body/blob /en-US/docs/Web/API/Response/blob
/en-US/docs/Web/API/Body/body /en-US/docs/Web/API/Response/body
/en-US/docs/Web/API/Body/bodyUsed /en-US/docs/Web/API/Response/bodyUsed
/en-US/docs/Web/API/Body/formData /en-US/docs/Web/API/Response/formData
/en-US/docs/Web/API/Body/json /en-US/docs/Web/API/Response/json
/en-US/docs/Web/API/Body/text /en-US/docs/Web/API/Response/text
/en-US/docs/Web/API/Boolean /en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
/en-US/docs/Web/API/BroadcastChannel.BroadcastChannel /en-US/docs/Web/API/BroadcastChannel/BroadcastChannel
/en-US/docs/Web/API/BroadcastChannel.close /en-US/docs/Web/API/BroadcastChannel/close
Expand Down Expand Up @@ -8396,20 +8403,10 @@
/en-US/docs/Web/API/ReadableByteStream /en-US/docs/Web/API/ReadableStream
/en-US/docs/Web/API/Reference /en-US/docs/Web/API
/en-US/docs/Web/API/RegisterProperty /en-US/docs/Web/API/CSS/RegisterProperty
/en-US/docs/Web/API/Request/arrayBuffer /en-US/docs/Web/API/Body/arrayBuffer
/en-US/docs/Web/API/Request/blob /en-US/docs/Web/API/Body/blob
/en-US/docs/Web/API/Request/bodyUsed /en-US/docs/Web/API/Body/bodyUsed
/en-US/docs/Web/API/Request/context /en-US/docs/Web/API/Request/destination
/en-US/docs/Web/API/Request/json /en-US/docs/Web/API/Body/json
/en-US/docs/Web/API/Request/text /en-US/docs/Web/API/Body/text
/en-US/docs/Web/API/Resource_Timing /en-US/docs/Web/API/Resource_Timing_API
/en-US/docs/Web/API/Response/Response.headers /en-US/docs/Web/API/Response/headers
/en-US/docs/Web/API/Response/Response.ok /en-US/docs/Web/API/Response/ok
/en-US/docs/Web/API/Response/arrayBuffer /en-US/docs/Web/API/Body/arrayBuffer
/en-US/docs/Web/API/Response/blob /en-US/docs/Web/API/Body/blob
/en-US/docs/Web/API/Response/bodyUsed /en-US/docs/Web/API/Body/bodyUsed
/en-US/docs/Web/API/Response/json /en-US/docs/Web/API/Body/json
/en-US/docs/Web/API/Response/text /en-US/docs/Web/API/Body/text
/en-US/docs/Web/API/RotationRate /en-US/docs/Web/API/DeviceMotionEvent/rotationRate
/en-US/docs/Web/API/RotationRate.alpha /en-US/docs/Web/API/DeviceMotionEvent/rotationRate
/en-US/docs/Web/API/RotationRate.beta /en-US/docs/Web/API/DeviceMotionEvent/rotationRate
Expand Down
214 changes: 97 additions & 117 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -30593,123 +30593,6 @@
"rolfedh"
]
},
"Web/API/Body": {
"modified": "2020-10-15T21:33:19.070Z",
"contributors": [
"Wind1808",
"fmeyertoens",
"sideshowbarker",
"Akh-rman",
"RickBrown",
"fscholz",
"vqrs",
"chrisdavidmills",
"jpmedley",
"yqjiang",
"ZaneHannanAU",
"Hugues-Antoine",
"erikadoyle",
"teoli",
"kscarfone"
]
},
"Web/API/Body/arrayBuffer": {
"modified": "2020-10-15T21:33:27.479Z",
"contributors": [
"Wind1808",
"NotWoods",
"mfluehr",
"fscholz",
"vitaly-zdanevich",
"chrisdavidmills",
"erikadoyle",
"simonlc",
"jakubkoci",
"Sebastianz",
"kscarfone"
]
},
"Web/API/Body/blob": {
"modified": "2020-10-15T21:33:28.572Z",
"contributors": [
"Wind1808",
"ChayimFriedman2",
"mfluehr",
"tomayac",
"fscholz",
"chrisdavidmills",
"ishasaran",
"erikadoyle",
"omriBernstein",
"kscarfone"
]
},
"Web/API/Body/body": {
"modified": "2020-10-15T21:58:08.136Z",
"contributors": [
"Wind1808",
"tomasdev",
"jcsahnwaldt",
"fscholz",
"chrisdavidmills"
]
},
"Web/API/Body/bodyUsed": {
"modified": "2020-10-15T21:33:27.925Z",
"contributors": [
"Wind1808",
"fscholz",
"chrisdavidmills",
"Hugues-Antoine",
"erikadoyle",
"kscarfone"
]
},
"Web/API/Body/formData": {
"modified": "2020-10-15T21:33:28.099Z",
"contributors": [
"Wind1808",
"fscholz",
"jpmedley",
"chrisdavidmills",
"riking",
"kscarfone"
]
},
"Web/API/Body/json": {
"modified": "2020-10-15T21:33:28.245Z",
"contributors": [
"Flimm",
"Wind1808",
"kevinwei00",
"mfuji09",
"ExE-Boss",
"fscholz",
"Sheppy",
"chrisdavidmills",
"alexpelan",
"TJ_Crowder",
"MoritzKn",
"erikadoyle",
"kscarfone"
]
},
"Web/API/Body/text": {
"modified": "2020-10-15T21:33:28.789Z",
"contributors": [
"timtim17",
"Zearin_Galaurum",
"Wind1808",
"paulkarcher",
"fscholz",
"vqrs",
"chrisdavidmills",
"erikadoyle",
"Hendry",
"jpmedley",
"kscarfone"
]
},
"Web/API/BroadcastChannel": {
"modified": "2020-10-15T21:32:14.287Z",
"contributors": [
Expand Down Expand Up @@ -75533,6 +75416,58 @@
"kscarfone"
]
},
"Web/API/Response/arrayBuffer": {
"modified": "2020-10-15T21:33:27.479Z",
"contributors": [
"Wind1808",
"NotWoods",
"mfluehr",
"fscholz",
"vitaly-zdanevich",
"chrisdavidmills",
"erikadoyle",
"simonlc",
"jakubkoci",
"Sebastianz",
"kscarfone"
]
},
"Web/API/Response/blob": {
"modified": "2020-10-15T21:33:28.572Z",
"contributors": [
"Wind1808",
"ChayimFriedman2",
"mfluehr",
"tomayac",
"fscholz",
"chrisdavidmills",
"ishasaran",
"erikadoyle",
"omriBernstein",
"kscarfone"
]
},
"Web/API/Response/body": {
"modified": "2020-10-15T21:58:08.136Z",
"contributors": [
"Wind1808",
"tomasdev",
"jcsahnwaldt",
"fscholz",
"chrisdavidmills"
]
},
"Web/API/Response/bodyUsed": {
"modified": "2020-10-15T21:33:27.925Z",
"contributors": [
"Wind1808",
"fscholz",
"chrisdavidmills",
"Hugues-Antoine",
"erikadoyle",
"kscarfone"
]
},
"Web/API/Response/clone": {
"modified": "2020-10-15T21:33:30.369Z",
"contributors": [
Expand All @@ -75555,6 +75490,17 @@
"kscarfone"
]
},
"Web/API/Response/formData": {
"modified": "2020-10-15T21:33:28.099Z",
"contributors": [
"Wind1808",
"fscholz",
"jpmedley",
"chrisdavidmills",
"riking",
"kscarfone"
]
},
"Web/API/Response/headers": {
"modified": "2020-10-15T21:33:29.716Z",
"contributors": [
Expand All @@ -75566,6 +75512,24 @@
"kscarfone"
]
},
"Web/API/Response/json": {
"modified": "2020-10-15T21:33:28.245Z",
"contributors": [
"Flimm",
"Wind1808",
"kevinwei00",
"mfuji09",
"ExE-Boss",
"fscholz",
"Sheppy",
"chrisdavidmills",
"alexpelan",
"TJ_Crowder",
"MoritzKn",
"erikadoyle",
"kscarfone"
]
},
"Web/API/Response/ok": {
"modified": "2020-10-15T21:33:28.876Z",
"contributors": [
Expand Down Expand Up @@ -75623,6 +75587,22 @@
"kscarfone"
]
},
"Web/API/Response/text": {
"modified": "2020-10-15T21:33:28.789Z",
"contributors": [
"timtim17",
"Zearin_Galaurum",
"Wind1808",
"paulkarcher",
"fscholz",
"vqrs",
"chrisdavidmills",
"erikadoyle",
"Hendry",
"jpmedley",
"kscarfone"
]
},
"Web/API/Response/type": {
"modified": "2020-10-15T21:33:28.734Z",
"contributors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h2 id="Explaining_basic_promise_syntax_A_real_example">Explaining basic promise

<p>Promises are important to understand because most modern Web APIs use them for functions that perform potentially lengthy tasks. To use modern web technologies you'll need to use promises. Later on in the chapter, we'll look at how to write your own promise, but for now, we'll look at some simple examples that you'll encounter in Web APIs.</p>

<p>In the first example, we'll use the <code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch()</a></code> method to fetch an image from the web, the {{domxref("Body.blob", "blob()")}} method to transform the fetch response's raw body contents into a {{domxref("Blob")}} object, and then display that blob inside an {{htmlelement("img")}} element. This is very similar to the example we looked at in the <a href="/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#asynchronous_javascript">first article of the series</a>, but we'll do it a bit differently as we get you building your own promise-based code.</p>
<p>In the first example, we'll use the <code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch()</a></code> method to fetch an image from the web, the {{domxref("Response.blob()")}} method to transform the fetch response's raw body contents into a {{domxref("Blob")}} object, and then display that blob inside an {{htmlelement("img")}} element. This is very similar to the example we looked at in the <a href="/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#asynchronous_javascript">first article of the series</a>, but we'll do it a bit differently as we get you building your own promise-based code.</p>

<div class="notecard note">
<p><strong>Note</strong>: The following example will not work if you just run it directly from the file (i.e. via a <code>file://</code> URL). You need to run it through a <a href="/en-US/docs/Learn/Common_questions/set_up_a_local_testing_server">local testing server</a>, or use an online solution such as <a href="https://glitch.com/">Glitch</a> or <a href="/en-US/docs/Learn/Common_questions/Using_Github_pages">GitHub pages</a>.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ <h3 id="Storing_complex_data_via_IndexedDB">Storing complex data via IndexedDB</
}</pre>
</li>
<li>
<p>The following snippet is taken from inside <code>fetchVideoFromNetwork()</code> — here we fetch MP4 and WebM versions of the video using two separate {{domxref("WindowOrWorkerGlobalScope/fetch", "WindowOrWorkerGlobalScope.fetch()")}} requests. We then use the {{domxref("blob()", "Body.blob()")}} method to extract each response's body as a blob, giving us an object representation of the videos that can be stored and displayed later on.</p>
<p>The following snippet is taken from inside <code>fetchVideoFromNetwork()</code> — here we fetch MP4 and WebM versions of the video using two separate {{domxref("WindowOrWorkerGlobalScope/fetch", "WindowOrWorkerGlobalScope.fetch()")}} requests. We then use the {{domxref("Response.blob()")}} method to extract each response's body as a blob, giving us an object representation of the videos that can be stored and displayed later on.</p>

<p>We have a problem here though — these two requests are both asynchronous, but we only want to try to display or store the video when both promises have fulfilled. Fortunately there is a built-in method that handles such a problem — {{jsxref("Promise.all()")}}. This takes one argument — references to all the individual promises you want to check for fulfillment placed in an array — and is itself promise-based.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ <h4 id="So_what_is_going_on_in_the_Fetch_code">So what is going on in the Fetch

<p>After that, you can see the {{jsxref("Promise.then",".then()")}} method chained onto the end of <code>fetch()</code> — this method is a part of {{jsxref("Promise","Promises")}}, a modern JavaScript feature for performing asynchronous operations. <code>fetch()</code> returns a <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</a>, which resolves to the response sent back from the server — we use <code>.then()</code> to run some follow-up code after the promise resolves, which is the function we've defined inside it. This is the equivalent of the <code>onload</code> event handler in the XHR version.</p>

<p>This function is automatically given the response from the server as a parameter when the <code>fetch()</code> promise resolves. Inside the function we grab the response and run its {{domxref("Body.text","text()")}} method, which basically returns the response as raw text. This is the equivalent of <code>request.responseType = 'text'</code> in the XHR version.</p>
<p>This function is automatically given the response from the server as a parameter when the <code>fetch()</code> promise resolves. Inside the function we grab the response and run its {{domxref("Response.text","text()")}} method, which basically returns the response as raw text. This is the equivalent of <code>request.responseType = 'text'</code> in the XHR version.</p>

<p>You'll see that <code>text()</code> also returns a promise, so we chain another <code>.then()</code> onto it, inside of which we define a function to receive the raw text that the <code>text()</code> promise resolves to.</p>

Expand Down Expand Up @@ -250,7 +250,7 @@ <h3 id="Aside_on_promises">Aside on promises</h3>
});
}</pre>

<p>The response object has a method {{domxref("Body.text","text()")}} that takes the raw data contained in the response body and turns it into plain text — the format we want it in. It also returns a promise (which resolves to the resulting text string), so here we use another {{jsxref("Promise.then",".then()")}}, inside of which we define another function that dictates what we want to do with that text string. We are just setting the <code><a href="/en-US/docs/Web/API/Node/textContent">textContent</a></code> property of our poem's {{htmlelement("pre")}} element to equal the text string, so this works out pretty simple.</p>
<p>The response object has a method {{domxref("Response.text","text()")}} that takes the raw data contained in the response body and turns it into plain text — the format we want it in. It also returns a promise (which resolves to the resulting text string), so here we use another {{jsxref("Promise.then",".then()")}}, inside of which we define another function that dictates what we want to do with that text string. We are just setting the <code><a href="/en-US/docs/Web/API/Node/textContent">textContent</a></code> property of our poem's {{htmlelement("pre")}} element to equal the text string, so this works out pretty simple.</p>

<p>It is also worth noting that you can directly chain multiple promise blocks (<code>.then()</code> blocks, but there are other types too) onto the end of one another, passing the result of each block to the next block as you travel down the chain. This makes promises very powerful.</p>

Expand Down Expand Up @@ -297,7 +297,7 @@ <h2 id="A_more_complex_example">A more complex example</h2>

<p>The <code>fetch()</code> function returns a promise. If this completes successfully, the function inside the first <code>.then()</code> block contains the <code>response</code> returned from the network.</p>

<p>Inside this function we run {{domxref("Body.json","json()")}} on the response, not {{domxref("Body.text","text()")}}, as we want to return our response as structured JSON data, not plain text.</p>
<p>Inside this function we run {{domxref("Response.json","json()")}} on the response, not {{domxref("Response.text","text()")}}, as we want to return our response as structured JSON data, not plain text.</p>

<p>Next, we chain another <code>.then()</code> onto the end of our first one, the success function that contains the <code>json</code> returned from the <code>response.json()</code> promise. We set this to be the value of the <code>products</code> variable, then run <code>initialize(products)</code>, which starts the process of displaying all the products in the user interface.</p>

Expand Down Expand Up @@ -326,7 +326,7 @@ <h2 id="A_more_complex_example">A more complex example</h2>
  showProduct(objectURL, product);
});</pre>

<p>This works in much the same way as the previous one, except that instead of using {{domxref("Body.json","json()")}}, we use {{domxref("Body.blob","blob()")}}. In this case we want to return our response as an image file, and the data format we use for that is <a href="/en-US/docs/Web/API/Blob">Blob</a> (the term is an abbreviation of "Binary Large Object" and can basically be used to represent large file-like objects, such as images or video files).</p>
<p>This works in much the same way as the previous one, except that instead of using {{domxref("Response.json","json()")}}, we use {{domxref("Response.blob","blob()")}}. In this case we want to return our response as an image file, and the data format we use for that is <a href="/en-US/docs/Web/API/Blob">Blob</a> (the term is an abbreviation of "Binary Large Object" and can basically be used to represent large file-like objects, such as images or video files).</p>

<p>Once we've successfully received our blob, we create an object URL out of it using {{domxref("URL.createObjectURL()", "createObjectURL()")}}. This returns a temporary internal URL that points to an object referenced inside the browser. These are not very readable, but you can see what one looks like by opening up the Can Store app, Ctrl-/Right-clicking on an image, and selecting the "View image" option (which might vary slightly depending on what browser you are using). The object URL will be visible inside the address bar, and should be something like this:</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ <h3 id="Requesting_data_from_the_API">Requesting data from the API</h3>
displayResults(json);
});</pre>

<p>Here we run the request by passing our <code>url</code> variable to <code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch()</a></code>, convert the response body to JSON using the <code><a href="/en-US/docs/Web/API/Body/json">json()</a></code> function, then pass the resulting JSON to the <code>displayResults()</code> function so the data can be displayed in our UI.</p>
<p>Here we run the request by passing our <code>url</code> variable to <code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch">fetch()</a></code>, convert the response body to JSON using the <code><a href="/en-US/docs/Web/API/Response/json">json()</a></code> function, then pass the resulting JSON to the <code>displayResults()</code> function so the data can be displayed in our UI.</p>

<h3 id="Displaying_the_data">Displaying the data</h3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ <h4>Note</h4>
<li><code><a href="/en-US/docs/Web/API/KeyboardEvent">KeyboardEvent</a></code> (20)</li>
</ul>

<h3 id="fetch_api">Fetch API (47)</h3>
<h3 id="fetch_api">Fetch API (39)</h3>

<ul>
<li><code><a href="/en-US/docs/Web/API/Body">Body</a></code> (8)</li>
<li><code><a href="/en-US/docs/Web/API/Headers">Headers</a></code> (11)</li>
<li><code><a href="/en-US/docs/Web/API/Request">Request</a></code> (15)</li>
<li><code><a href="/en-US/docs/Web/API/Response">Response</a></code> (13)</li>
Expand Down
Loading