+0
日增长
5,696
stars
204
forks
38
watchers
86
prs
85
issues
17
releases
README
unfetch
Tiny 500b fetch "barely-polyfill"
- Tiny: about 500 bytes of ES3 gzipped
- Minimal: just fetch() with headers and text/json responses
- Familiar: a subset of the full API
- Supported: supports IE8+ _(assuming Promise is polyfilled of course!)_
- Standalone: one function, no dependencies
- Modern: written in ES2015, transpiled to 500b of old-school JS
🤔 What's Missing?
- Uses simple Arrays instead of Iterables, since Arrays _are_ iterables
- No streaming, just Promisifies existing XMLHttpRequest response bodies
- Use in Node.JS is handled by isomorphic-unfetch
- Unfetch
- Installation
- Usage: As a Polyfill
- Usage: As a Ponyfill
- Examples & Demos
- API
- Caveats
- Contribute
- License
Installation
For use with node and npm:
sh
npm i unfetch
Otherwise, grab it from unpkg.com/unfetch.
Usage: As a Polyfill
This automatically "installs" unfetch as window.fetch() if it detects Fetch isn't supported:
js
import 'unfetch/polyfill'
// fetch is now available globally!
fetch('/foo.json')
.then( r = r.json() )
.then( data = console.log(data) )
This polyfill version is particularly useful for hotlinking from unpkg:
html
// now our page can use fetch!
fetch('/foo')
Usage: As a Ponyfill
With a module bundler like rollup or webpack,
you can import unfetch to use in your code without modifying any globals:
js
// using JS Modules:
import fetch from 'unfetch'
// or using CommonJS:
const fetch = require('unfetch')
// usage:
fetch('/foo.json')
.then( r = r.json() )
.then( data = console.log(data) )
The above will always return unfetch(). _(even if window.fetch exists!)_
There's also a UMD bundle available as unfetch/dist/unfetch.umd.js, which doesn't automatically install itself as window.fetch.
Examples & Demos
Real Example on JSFiddle ➡️
js
// simple GET request:
fetch('/foo')
.then( r = r.text() )
.then( txt = console.log(txt) )
// complex POST request with JSON, headers:
fetch('/bear', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ hungry: true })
}).then( r = {
open(r.headers.get('location'));
return r.json();
})
API
While one of Unfetch's goals is to provide a familiar interface, its API may differ from other fetch polyfills/ponyfills.
One of the key differences is that Unfetch focuses on implementing the fetch() API, while offering minimal (yet functional) support to the other sections of the Fetch spec, like the Headers class or the Response class.
Unfetch's API is organized as follows:
fetch(url: string, options: Object)
This function is the heart of Unfetch. It will fetch resources from url according to the given options, returning a Promise that will eventually resolve to the response.
Unfetch will account for the following properties in options:
method: Indicates the request method to be performed on the
target resource (The most common ones being GET, POST, PUT, PATCH, HEATop issues
#22global default options and handlers22 comments · 2017-02-23#96Typescript issue: weird dependency on node-fetch in declaration file9 comments · 2018-10-13#54add support for aborting via AbortController9 comments · 2017-09-29#143isomorphic-unfetch: Consider making node-fetch a peerDependency 8 comments · 2020-09-15#33Regex tests8 comments · 2017-03-11#106XDomainRequest. IE 8 will not work when CORS requests6 comments · 2019-01-08#2Run GitHub/fetch tests against this polyfill3 comments · 2017-02-22#172Module not found: Can't resolve 'unfetch'2 comments · 2023-07-07
Latest release
5.0.0 5.0.0 2022-12-31What's Changed Add TypeScript definitions and export them @developit in https://github.com/developit/unfetch/pull/159 Add Package Exports to both unfetch and isomorphic-unfetch New Contributors @RishikeshDarandale made their first contribution to isomorphic-unfetch in https://github.com/developit/unfetch/pull/156 Full Changelog: https://github.com/developit/unfetch/compare/4.2.0...5.0.0