Skip to content

Instantly share code, notes, and snippets.

@awinogradov
Created June 21, 2016 09:15
Show Gist options
  • Save awinogradov/0a87b860dec209e72db0206b39f1253e to your computer and use it in GitHub Desktop.
Save awinogradov/0a87b860dec209e72db0206b39f1253e to your computer and use it in GitHub Desktop.
friendly-fetch
'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