-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
60 lines (56 loc) · 1.79 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var API_ROOT = 'https://api.xtack.space';
function getApi(endpoint, params, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', `${API_ROOT}${endpoint}${params}`, true);
xhr.withCredentials = true;
var token = Cookies.get('session_token');
if (typeof token !== 'undefined' && token.length > 0) {
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
callback(xhr.responseText, xhr.status);
}
}
xhr.send(null);
}
function postApi(endpoint, params, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', `${API_ROOT}${endpoint}`, true);
xhr.withCredentials = true;
var token = Cookies.get('session_token');
if (typeof token !== 'undefined' && token.length > 0) {
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
}
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
callback(xhr.responseText, xhr.status);
}
}
xhr.send(params);
}
function isEmailValid(mail) {
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail);
}
var accountsCache = {};
function queryAccountsCache(uuid, callback) {
if (typeof accountsCache[uuid] === 'undefined') {
accountsCache[uuid] = {};
accountsCache[uuid].callbackQueue = [ callback ];
getApi(`/account/${uuid}`, '', function(data, status) {
if (status === 200) {
accountsCache[uuid].account = JSON.parse(data);
for (var i in accountsCache[uuid].callbackQueue) {
accountsCache[uuid].callbackQueue[i](accountsCache[uuid].account);
}
}
});
}
else if (typeof accountsCache[uuid].account === 'undefined') {
accountsCache[uuid].callbackQueue.push(callback);
}
else {
callback(accountsCache[uuid].account);
}
}