Gathering detailed insights and metrics for fetch-polyfill
Gathering detailed insights and metrics for fetch-polyfill
Gathering detailed insights and metrics for fetch-polyfill
Gathering detailed insights and metrics for fetch-polyfill
npm install fetch-polyfill
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
45 Stars
303 Commits
9 Forks
4 Watching
1 Branches
1 Contributors
Updated on 14 Sept 2024
Minified
Minified + Gzipped
JavaScript (88.5%)
HTML (5.33%)
Shell (4.22%)
Makefile (1.28%)
Ruby (0.67%)
Cumulative downloads
Total Downloads
Last day
26.1%
2,172
Compared to previous day
Last week
5.9%
9,566
Compared to previous week
Last month
10.5%
39,085
Compared to previous month
Last year
32.8%
501,077
Compared to previous year
5
This fork supports IE8 with es5-shim, es5-sham and es6-promise
This module is out of maintanance, use fetch-ie8 instead. or you can try promisingagent if you want to use promise api for ajax and don't need ServiceWorker
The global fetch
function is an easier way to make web requests and handle
responses than using an XMLHttpRequest. This polyfill is written as closely as
possible to the standard Fetch specification at https://fetch.spec.whatwg.org.
Available on Bower as fetch-polyfill.
1$ bower install fetch-polyfill
You'll also need a Promise polyfill for older browsers.
1$ bower install es6-promise
This can also be installed with npm
.
1$ npm install fetch-polyfill --save
(For a node.js implementation, try node-fetch)
The fetch
function supports any HTTP method. We'll focus on GET and POST
example requests.
1fetch('/users.html') 2 .then(function(response) { 3 return response.text() 4 }).then(function(body) { 5 document.body.innerHTML = body 6 })
1fetch('/users.json') 2 .then(function(response) { 3 return response.json() 4 }).then(function(json) { 5 console.log('parsed json', json) 6 }).catch(function(ex) { 7 console.log('parsing failed', ex) 8 })
1fetch('/users.json').then(function(response) { 2 console.log(response.headers.get('Content-Type')) 3 console.log(response.headers.get('Date')) 4 console.log(response.status) 5 console.log(response.statusText) 6})
1var form = document.querySelector('form') 2 3fetch('/query', { 4 method: 'post', 5 body: new FormData(form) 6})
1fetch('/users', { 2 method: 'post', 3 headers: { 4 'Accept': 'application/json', 5 'Content-Type': 'application/json' 6 }, 7 body: JSON.stringify({ 8 name: 'Hubot', 9 login: 'hubot', 10 }) 11})
1var input = document.querySelector('input[type="file"]') 2 3var form = new FormData() 4form.append('file', input.files[0]) 5form.append('user', 'hubot') 6 7fetch('/avatars', { 8 method: 'post', 9 body: form 10})
This causes fetch
to behave like jQuery's $.ajax
by rejecting the Promise
on HTTP failure status codes like 404, 500, etc. The response Promise
is
resolved only on successful, 200 level, status codes.
1function status(response) { 2 if (response.status >= 200 && response.status < 300) { 3 return response 4 } 5 throw new Error(response.statusText) 6} 7 8function json(response) { 9 return response.json() 10} 11 12fetch('/users') 13 .then(status) 14 .then(json) 15 .then(function(json) { 16 console.log('request succeeded with json response', json) 17 }).catch(function(error) { 18 console.log('request failed', error) 19 })
The Response
object has a URL attribute for the final responded resource.
Usually this is the same as the Request
url, but in the case of a redirect,
its all transparent. Newer versions of XHR include a responseURL
attribute
that returns this value. But not every browser supports this. The compromise
requires setting a special server side header to tell the browser what URL it
just requested (yeah, I know browsers).
1response.headers['X-Request-URL'] = request.url
If you want response.url
to be reliable, you'll want to set this header. The
day that you ditch this polyfill and use native fetch only, you can remove the
header hack.
Latest ✔ | Latest ✔ | 8+ ✔ | Latest ✔ | 6.1+ ✔ |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More