Skip to content

Instantly share code, notes, and snippets.

@designviacode
Forked from Ajith-Pandian/Api.js
Created August 9, 2017 15:21
Show Gist options
  • Save designviacode/19e272b138784ce6b7f1e31881861aaa to your computer and use it in GitHub Desktop.
Save designviacode/19e272b138784ce6b7f1e31881861aaa to your computer and use it in GitHub Desktop.
export default class Api {
static headers() {
return {
Accept: "application/json",
"Content-Type": "application/json"
};
}
static get(route) {
return this.request(route, null, "GET");
}
static put(route, params) {
return this.request(route, params, "PUT");
}
static post(route, params) {
return this.request(route, params, "POST");
}
static delete(route, params) {
return this.request(route, params, "DELETE");
}
static request(route, params, verb) {
const host = "http://10.0.2.2:3000/";//SERVER BASE URL
const url = `${host}${route}`;
console.log(url);
let options = Object.assign(
{ method: verb },
params ? { body: JSON.stringify(params) } : null
);
options.headers = Api.headers();
return fetch(url, options)
.then(response => response.json())
.then(responseJson => responseJson)
.catch(error => {
console.error(error);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment