Skip to content

Commit 0bc2cf9

Browse files
committed
Update readme for 4.0.0
1 parent 4180f39 commit 0bc2cf9

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

README.md

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,78 @@
2626
2727
* * *
2828

29-
## Table of Contents
30-
31-
- [Install](#install)
32-
- [Usage](#usage)
33-
- [Examples & Demos](#examples--demos)
34-
- [API](#api)
35-
- [Caveats](#caveats)
36-
- [Contribute](#contribute)
37-
- [License](#license)
29+
- [Unfetch](#unfetch)
30+
- [Installation](#installation)
31+
- [Usage: As a Polyfill](#usage-as-a-polyfill)
32+
- [Usage: As a Ponyfill](#usage-as-a-ponyfill)
33+
- [Examples & Demos](#examples--demos)
34+
- [API](#api)
35+
- [Caveats](#caveats)
36+
- [Contribute](#contribute)
37+
- [License](#license)
3838

3939
* * *
4040

41-
## Install
41+
## Installation
4242

43-
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
43+
For use with [node](http://nodejs.org) and [npm](https://npmjs.com):
4444

4545
```sh
46-
$ npm install --save unfetch
46+
npm install --save unfetch
4747
```
4848

49-
Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
49+
Otherwise, grab it from [unpkg.com/unfetch](https://unpkg.com/unfetch/).
5050

51-
```javascript
52-
// using ES6 modules
53-
import fetch from 'unfetch'
51+
* * *
5452

55-
// using CommonJS modules
56-
var fetch = require('unfetch')
53+
## Usage: As a [Polyfill](https://ponyfill.com/#polyfill)
54+
55+
This automatically "installs" unfetch as `window.fetch()` if it detects Fetch isn't supported:
56+
57+
```js
58+
import 'unfetch/polyfill'
59+
60+
// fetch is now available globally!
61+
fetch('/foo.json')
62+
.then( r => r.json() )
63+
.then( data => console.log(data) )
5764
```
5865

59-
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
66+
This polyfill version is particularly useful for hotlinking from [unpkg](https://unpkg.com):
6067

6168
```html
62-
<script src="//unpkg.com/unfetch/dist/unfetch.umd.js"></script>
69+
<script src="https://unpkg.com/unfetch/polyfill"></script>
70+
<script>
71+
// now our page can use fetch!
72+
fetch('/foo')
73+
</script>
6374
```
6475

65-
This exposes the `unfetch()` function as a global.
66-
6776
* * *
6877

69-
## Usage
78+
## Usage: As a [Ponyfill](https://github.com/sindresorhus/ponyfill)
7079

71-
As a [**ponyfill**](https://ponyfill.com):
80+
With a module bundler like [rollup](http://rollupjs.org) or [webpack](https://webpack.js.org),
81+
you can import unfetch to use in your code without modifying any globals:
7282

7383
```js
74-
import fetch from 'unfetch';
84+
// using JS Modules:
85+
import fetch from 'unfetch'
86+
87+
// or using CommonJS:
88+
var fetch = require('unfetch')
7589

90+
// usage:
7691
fetch('/foo.json')
7792
.then( r => r.json() )
78-
.then( data => {
79-
console.log(data);
80-
});
93+
.then( data => console.log(data) )
8194
```
8295

83-
Globally, as a [**polyfill**](https://ponyfill.com/#polyfill):
96+
The above will always return `unfetch()`. _(even if `window.fetch` exists!)_
8497

85-
```js
86-
import 'unfetch/polyfill';
98+
There's also a UMD bundle available as [unfetch/dist/unfetch.umd.js](https://unpkg.com/unfetch/dist/unfetch.umd.js), which doesn't automatically install itself as `window.fetch`.
8799

88-
// "fetch" is now installed globally if it wasn't already available
89-
90-
fetch('/foo.json')
91-
.then( r => r.json() )
92-
.then( data => {
93-
console.log(data);
94-
});
95-
```
100+
* * *
96101

97102
## Examples & Demos
98103

@@ -118,6 +123,8 @@ fetch('/bear', {
118123
})
119124
```
120125

126+
* * *
127+
121128
## API
122129
While one of Unfetch's goals is to provide a familiar interface, its API may differ from other `fetch` polyfills/ponyfills.
123130
One of the key differences is that Unfetch focuses on implementing the [`fetch()` API](https://fetch.spec.whatwg.org/#fetch-api), while offering minimal (yet functional) support to the other sections of the [Fetch spec](https://fetch.spec.whatwg.org/), like the [Headers class](https://fetch.spec.whatwg.org/#headers-class) or the [Response class](https://fetch.spec.whatwg.org/#response-class).

0 commit comments

Comments
 (0)