developit /unfetch

🐕 Bare minimum 500b fetch polyfill.

★ 5,696⑂ 204👁 38⑃ 86● 85▤ 17
JavaScriptMITCreated 2017-02-22npm.im/unfetch
TA的项目
下载:5.0.05.0.0 ↓5.0.02022-12-31
+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, HEA

Top issues

Latest release

5.0.0 5.0.0 2022-12-31
What'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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON