Skip to content

Instantly share code, notes, and snippets.

@RayLuxembourg
Forked from bermanboris/Api.js
Created January 8, 2017 10:17
Show Gist options
  • Save RayLuxembourg/0550b11265efa4c7f6ae087dd94d0fd8 to your computer and use it in GitHub Desktop.
Save RayLuxembourg/0550b11265efa4c7f6ae087dd94d0fd8 to your computer and use it in GitHub Desktop.
Api Static Class
export default class Api {
static headers() {
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
'dataType': 'json',
}
}
static get(route) {
return this.xhr(route, null, 'GET');
}
static put(route, params) {
return this.xhr(route, params, 'PUT')
}
static post(route, params) {
return this.xhr(route, params, 'POST')
}
static delete(route, params) {
return this.xhr(route, params, 'DELETE')
}
static async xhr(route, params, method) {
const host = 'https://jsonplaceholder.typicode.com';
const url = `${host}${route}`;
const options = { method, params: (params ? { body: JSON.stringify(params) } : null), header: Api.headers() };
try {
const response = await fetch(url, options);
return response.json();
} catch (err) {
throw err;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment