Skip to content

Instantly share code, notes, and snippets.

@oluosiname
Last active April 26, 2020 08:18
Show Gist options
  • Save oluosiname/04f824927fdfeb1e5e73b1b1b8bd02e3 to your computer and use it in GitHub Desktop.
Save oluosiname/04f824927fdfeb1e5e73b1b1b8bd02e3 to your computer and use it in GitHub Desktop.
Fetch Http wrapper
class Http {
constructor() {
this.postInit = { method: 'POST' }
this.getInit = { method: 'GET' }
}
async get({ url, ...rest }) {
const init = {...this.getInit, ...rest};
try {
const response = await fetch.get(url, init);
return response.json();
} catch (e) {
throw e;
}
}
async post({ url, body, ...rest }) {
const init = {...this.postInit, ...rest};
try {
const response = await fetch(url, init);
return response.json();
} catch (e) {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment