-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneral.js
54 lines (48 loc) · 1.31 KB
/
general.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
// global namespace
var general = general || {};
// module for API calls
general.API = (function(){
'use strict';
var _api_url = null;
/**
* Generic function to get JSON data from API
* @param {string} path
*/
function _getJson(path) {
document.getElementsByTagName('body')[0].classList.add('loading')
return fetch(_api_url + '/' + path)
.then(function(response) {
if (response.ok) {
return response.json();
} else {
// if not found, just return null
return Promise.resolve(null);
}
})
.catch(function(err) {
console.log(err);
return Promise.reject();
})
.finally(function() {
document.getElementsByTagName("body")[0].classList.remove('loading')
})
}
/**
* Get JSON customer data
* @param {string} email
*/
function _getCustomerData(email) {
return _getJson('customers/' + encodeURIComponent(email));
};
/**
* Initialize
*/
function _init(api_url) {
_api_url = api_url;
};
return {
getJson: _getJson,
getCustomerData: _getCustomerData,
init: _init
};
})();