2020-10-02 10:53:18 -04:00
|
|
|
'use strict';
|
2020-10-01 14:09:44 -04:00
|
|
|
|
2020-10-02 10:53:18 -04:00
|
|
|
define('api', () => {
|
|
|
|
|
const api = {};
|
2020-10-07 11:14:37 -04:00
|
|
|
const baseUrl = config.relative_path + '/api/v3';
|
2020-10-01 14:09:44 -04:00
|
|
|
|
2020-10-14 10:02:02 -04:00
|
|
|
function call(options, callback) {
|
2020-10-14 11:05:50 -04:00
|
|
|
options.url = options.url.startsWith('/api') ?
|
|
|
|
|
config.relative_path + options.url :
|
|
|
|
|
baseUrl + options.url;
|
|
|
|
|
|
|
|
|
|
function doAjax(cb) {
|
2020-10-14 14:02:03 -04:00
|
|
|
$.ajax(options)
|
2020-10-14 10:02:02 -04:00
|
|
|
.done((res) => {
|
2020-10-14 11:05:50 -04:00
|
|
|
cb(null,
|
2020-11-06 11:55:04 -05:00
|
|
|
res && res.hasOwnProperty('status') && res.hasOwnProperty('response') ?
|
|
|
|
|
res.response : (res || {})
|
2020-10-14 11:05:50 -04:00
|
|
|
);
|
2020-10-14 10:02:02 -04:00
|
|
|
})
|
|
|
|
|
.fail((ev) => {
|
2020-11-06 11:55:04 -05:00
|
|
|
let errMessage;
|
|
|
|
|
if (ev.responseJSON) {
|
|
|
|
|
errMessage = ev.responseJSON.status && ev.responseJSON.status.message ?
|
|
|
|
|
ev.responseJSON.status.message :
|
|
|
|
|
ev.responseJSON.error;
|
|
|
|
|
}
|
2020-10-14 10:02:02 -04:00
|
|
|
|
2020-10-17 22:59:12 -04:00
|
|
|
cb(new Error(errMessage || ev.statusText));
|
2020-10-14 10:02:02 -04:00
|
|
|
});
|
2020-10-14 11:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
doAjax(callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
doAjax(function (err, data) {
|
|
|
|
|
if (err) reject(err);
|
|
|
|
|
else resolve(data);
|
|
|
|
|
});
|
2020-10-14 10:02:02 -04:00
|
|
|
});
|
2020-10-02 10:53:18 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-06 12:18:42 -05:00
|
|
|
api.get = (route, payload, onSuccess) => call({
|
2020-10-14 11:05:50 -04:00
|
|
|
url: route + (Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
|
|
|
|
}, onSuccess);
|
2020-10-01 14:09:44 -04:00
|
|
|
|
2020-11-06 11:55:04 -05:00
|
|
|
api.head = (route, payload, onSuccess) => call({
|
|
|
|
|
url: route + (Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
|
|
|
|
method: 'head',
|
|
|
|
|
}, onSuccess);
|
|
|
|
|
|
2020-10-14 11:05:50 -04:00
|
|
|
api.post = (route, payload, onSuccess) => call({
|
|
|
|
|
url: route,
|
2020-10-01 14:09:44 -04:00
|
|
|
method: 'post',
|
|
|
|
|
data: payload,
|
2020-10-14 14:02:03 -04:00
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': config.csrf_token,
|
|
|
|
|
},
|
2020-10-14 11:05:50 -04:00
|
|
|
}, onSuccess);
|
2020-10-01 14:09:44 -04:00
|
|
|
|
2020-10-14 11:05:50 -04:00
|
|
|
api.put = (route, payload, onSuccess) => call({
|
|
|
|
|
url: route,
|
2020-10-01 14:09:44 -04:00
|
|
|
method: 'put',
|
|
|
|
|
data: payload,
|
2020-10-14 14:02:03 -04:00
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': config.csrf_token,
|
|
|
|
|
},
|
2020-10-14 11:05:50 -04:00
|
|
|
}, onSuccess);
|
2020-10-01 14:09:44 -04:00
|
|
|
|
2020-10-14 11:05:50 -04:00
|
|
|
api.del = (route, payload, onSuccess) => call({
|
|
|
|
|
url: route,
|
2020-10-01 14:09:44 -04:00
|
|
|
method: 'delete',
|
|
|
|
|
data: payload,
|
2020-10-14 14:02:03 -04:00
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': config.csrf_token,
|
|
|
|
|
},
|
2020-10-14 11:05:50 -04:00
|
|
|
}, onSuccess);
|
2020-10-02 10:53:18 -04:00
|
|
|
|
|
|
|
|
return api;
|
|
|
|
|
});
|