91 lines
1.9 KiB
JavaScript
91 lines
1.9 KiB
JavaScript
app.api = (function(app){
|
|
var baseURL = '/__api/'
|
|
|
|
|
|
$( document ).on( "ajaxError", function(event, res, req) {
|
|
console.log('bad!', `app:api:error:`, res.status);
|
|
app.publish(`app:api:error:${res.status}`, {res, res});
|
|
} );
|
|
|
|
function post(url, data, callback){
|
|
callback = callback || app.util.emptyFuction;
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: baseURL+url,
|
|
headers:{
|
|
'auth-token': app.auth.getToken()
|
|
},
|
|
data: JSON.stringify(data),
|
|
contentType: "application/json; charset=utf-8",
|
|
dataType: "json",
|
|
complete: function(res, text){
|
|
callback(
|
|
text !== 'success' ? res.statusText : null,
|
|
JSON.parse(res.responseText),
|
|
res.status
|
|
)
|
|
}
|
|
});
|
|
}
|
|
|
|
function put(url, data, callback){
|
|
$.ajax({
|
|
type: 'PUT',
|
|
url: baseURL+url,
|
|
headers:{
|
|
'auth-token': app.auth.getToken()
|
|
},
|
|
data: JSON.stringify(data),
|
|
contentType: "application/json; charset=utf-8",
|
|
dataType: "json",
|
|
complete: function(res, text){
|
|
callback(
|
|
text !== 'success' ? res.statusText : null,
|
|
JSON.parse(res.responseText),
|
|
res.status
|
|
)
|
|
}
|
|
});
|
|
}
|
|
|
|
function remove(url, callback, callback2){
|
|
if(!$.isFunction(callback)) callback = callback2;
|
|
$.ajax({
|
|
type: 'delete',
|
|
url: baseURL+url,
|
|
headers:{
|
|
'auth-token': app.auth.getToken()
|
|
},
|
|
contentType: "application/json; charset=utf-8",
|
|
dataType: "json",
|
|
complete: function(res, text){
|
|
callback(
|
|
text !== 'success' ? res.statusText : null,
|
|
JSON.parse(res.responseText),
|
|
res.status
|
|
)
|
|
}
|
|
});
|
|
}
|
|
|
|
function get(url, callback){
|
|
$.ajax({
|
|
type: 'GET',
|
|
url: baseURL+url,
|
|
headers:{
|
|
'auth-token': app.auth.getToken()
|
|
},
|
|
contentType: "application/json; charset=utf-8",
|
|
dataType: "json",
|
|
complete: function(res, text){
|
|
callback(
|
|
text !== 'success' ? res.statusText : null,
|
|
JSON.parse(res.responseText),
|
|
res.status
|
|
)
|
|
}
|
|
});
|
|
}
|
|
|
|
return {post: post, get: get, put: put, delete: remove}
|
|
})(app); |