Last active
December 22, 2017 10:10
-
-
Save Ajith-Pandian/935e43b795941113cdcebc14a5cdd720 to your computer and use it in GitHub Desktop.
This file contains 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" | |
}; | |
} | |
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}`; | |
let options = Object.assign( | |
{ method: verb }, | |
params ? { body: JSON.stringify(params) } : null | |
); | |
options.headers = Api.headers(); | |
return fetch(url, options) | |
.then(response => response.json()) | |
.catch(error => { | |
console.error(error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment