Last active
October 29, 2018 18:14
-
-
Save heleneshaikh/68835dd7d4d27485080ae10661493ec0 to your computer and use it in GitHub Desktop.
Fetch with polyfill and error wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'whatwg-fetch'; //polyfill | |
window.fetch(Routing.generate('booking_data'), { //window.fetch -> polyfill | |
method: 'post', | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
}, | |
body: JSON.stringify({ | |
checkin: event.start.toDateString(), | |
checkout: addDays(event.start, 7).toDateString() | |
}) | |
}) | |
.then(checkStatus) | |
.then(response => response.json()) | |
.catch(error => console.log(error)); | |
function checkStatus(response) { | |
if (response.status >= 200 && response.status < 300) { | |
return response | |
} else { | |
let error = new Error(response.statusText); | |
error.response = response; | |
throw error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment