Skip to content

Instantly share code, notes, and snippets.

@Jatapiaro
Created March 28, 2019 06:02
Show Gist options
  • Save Jatapiaro/d9765887a41fb2711c5428ca319029e4 to your computer and use it in GitHub Desktop.
Save Jatapiaro/d9765887a41fb2711c5428ca319029e4 to your computer and use it in GitHub Desktop.
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