koel/resources/assets/js/services/http.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import { extend } from 'lodash';
/**
* Responsible for all HTTP requests.
*
* IMPORTANT:
* If the user has a good enough connection to stream music, he or she shouldn't
* encounter any HTTP errors. That's why Koel doesn't handle HTTP errors.
* After all, even if there were errors, how bad can it be?
*/
export default {
2015-12-29 01:35:22 +00:00
request(method, url, data, successCb = null, errorCb = null, options = {}) {
2015-12-13 04:42:28 +00:00
switch (method) {
case 'get':
2015-12-29 01:35:22 +00:00
return Vue.http.get(url, data, options).then(successCb, errorCb);
2015-12-13 04:42:28 +00:00
case 'post':
2015-12-29 01:35:22 +00:00
return Vue.http.post(url, data, options).then(successCb, errorCb);
2015-12-13 04:42:28 +00:00
case 'put':
2015-12-30 04:14:47 +00:00
return Vue.http.put(url, data, options).then(successCb, errorCb);
2015-12-13 04:42:28 +00:00
case 'delete':
2015-12-30 04:14:47 +00:00
return Vue.http.delete(url, data, options).then(successCb, errorCb);
2015-12-13 04:42:28 +00:00
default:
break;
}
},
2015-12-29 01:35:22 +00:00
get(url, data = {}, successCb = null, errorCb = null, options = {}) {
return this.request('get', url, data, successCb, errorCb, options);
},
post(url, data, successCb = null, errorCb = null, options = {}) {
return this.request('post', url, data, successCb, errorCb, options);
2015-12-13 04:42:28 +00:00
},
2015-12-29 01:35:22 +00:00
put(url, data, successCb = null, errorCb = null, options = {}) {
return this.request('put', url, data, successCb, errorCb, options);
2015-12-13 04:42:28 +00:00
},
2015-12-29 01:35:22 +00:00
delete(url, data = {}, successCb = null, errorCb = null, options = {}) {
return this.request('delete', url, data, successCb, errorCb, options);
2015-12-13 04:42:28 +00:00
},
/**
* A shortcut method to ping and check if the user session is still valid.
*/
ping() {
return this.get('/');
},
};