You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This exposes the `unfetch()` function as a global.
66
-
67
76
* * *
68
77
69
-
## Usage
78
+
## Usage: As a [Ponyfill](https://github.com/sindresorhus/ponyfill)
70
79
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:
72
82
73
83
```js
74
-
importfetchfrom'unfetch';
84
+
// using JS Modules:
85
+
importfetchfrom'unfetch'
86
+
87
+
// or using CommonJS:
88
+
var fetch =require('unfetch')
75
89
90
+
// usage:
76
91
fetch('/foo.json')
77
92
.then( r=>r.json() )
78
-
.then( data=> {
79
-
console.log(data);
80
-
});
93
+
.then( data=>console.log(data) )
81
94
```
82
95
83
-
Globally, as a [**polyfill**](https://ponyfill.com/#polyfill):
96
+
The above will always return `unfetch()`. _(even if `window.fetch` exists!)_
84
97
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`.
87
99
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
+
* * *
96
101
97
102
## Examples & Demos
98
103
@@ -118,6 +123,8 @@ fetch('/bear', {
118
123
})
119
124
```
120
125
126
+
* * *
127
+
121
128
## API
122
129
While one of Unfetch's goals is to provide a familiar interface, its API may differ from other `fetch` polyfills/ponyfills.
123
130
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