Created
June 21, 2016 09:15
-
-
Save awinogradov/0a87b860dec209e72db0206b39f1253e to your computer and use it in GitHub Desktop.
friendly-fetch
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
'use strict'; | |
const ifetch = require('isomorphic-fetch'); | |
ifetch.Promise = require('bluebird'); | |
module.exports = function fetch(req) { | |
const url = `http://${window.location.hostname}:${window.location.port}${req.path}`; | |
return ifetch(url, { | |
method: req.method || 'POST', | |
body: JSON.stringify(req.body) | |
}).then(res => { | |
const data = res.json(); | |
if (data && data.error) { | |
throw new Error({ | |
point: `error in ${req.desc}` | |
}); | |
} | |
return data; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment