Created
March 28, 2019 06:02
-
-
Save Jatapiaro/d9765887a41fb2711c5428ca319029e4 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 HttpService { | |
makeGet(route) { | |
const endpoint = this.getEnpoint(route); | |
return window.axios.get(endpoint).then(res => { | |
return res.data.data | |
}) | |
.catch(err => { | |
return Promise.reject(err.response.data); | |
}); | |
} | |
makePost(route, body, isFormData = false) { | |
const endpoint = this.getEnpoint(route); | |
const headers = this.getHeaders(isFormData); | |
return window.axios.post(endpoint, body, {headers: headers}).then(res => { | |
return res.data.data; | |
}) | |
.catch(err => { | |
return Promise.reject(err.response.data); | |
}); | |
} | |
makePut(route, body) { | |
const endpoint = this.getEnpoint(route); | |
const headers = this.getHeaders(false); | |
return window.axios.put(endpoint, body, { headers: headers }).then(res => { | |
return res.data.data; | |
}) | |
.catch(err => { | |
return Promise.reject(err.response.data); | |
}); | |
} | |
makeDelete(route, body = {}) { | |
const endpoint = this.getEnpoint(route); | |
const headers = this.getHeaders(false); | |
return window.axios.delete(endpoint, body, { headers: headers }).then(res => { | |
return Promise.resolve(res.data.data); | |
}) | |
.catch(err => { | |
return Promise.reject(err.response.data); | |
}); | |
} | |
getHeaders(isFormData) { | |
const headers = { | |
'Content-Type': (isFormData) ? 'multipart/form-data' : 'application/json', | |
'Accept': 'application/json' | |
} | |
return headers; | |
} | |
getEnpoint(route) { | |
return `${window.baseUrl}/api/v1${route}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment