Skip to content

Instantly share code, notes, and snippets.

@dmrz
Created September 12, 2016 18:55
Show Gist options
  • Select an option

  • Save dmrz/454b9cd6c0d30d38fbb8687504dcdd04 to your computer and use it in GitHub Desktop.

Select an option

Save dmrz/454b9cd6c0d30d38fbb8687504dcdd04 to your computer and use it in GitHub Desktop.
import { stringify } from 'query-string';
const _fetch = (url, config) => {
// Put query string params to url if query is given
if (config !== undefined && 'query' in config) {
url = `${url}?${stringify(config.query)}`;
}
return fetch(url, config).then(
res => {
if (res.ok) {
return res.json()
} else {
console.log('Not ok response', res);
}
}
).catch(err => console.error('There was an error', err))
}
export const fetcher = new Proxy(_fetch, {
get (receiver, name) {
return (url, config) => {
if (config === undefined) {
config = {};
}
config['method'] = name.toUpperCase();
return receiver(url, config);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment