Created
January 8, 2017 10:14
-
-
Save bermanboris/c37980147b56901117387bd984b6c2d8 to your computer and use it in GitHub Desktop.
Api Static Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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