forked from LycheeOrg/Lychee-front
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
132 lines (105 loc) · 2.7 KB
/
api.js
File metadata and controls
132 lines (105 loc) · 2.7 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @description This module communicates with Lychee's API
*/
let api = {
onError: null,
};
api.isTimeout = function (errorThrown, jqXHR) {
if (
errorThrown &&
((errorThrown === "Bad Request" &&
jqXHR &&
jqXHR.responseJSON &&
jqXHR.responseJSON.error &&
jqXHR.responseJSON.error === "Session timed out") ||
(errorThrown === "unknown status" &&
jqXHR &&
jqXHR.status &&
jqXHR.status === 419 &&
jqXHR.responseJSON &&
jqXHR.responseJSON.message &&
jqXHR.responseJSON.message === "CSRF token mismatch."))
) {
return true;
}
return false;
};
api.post = function (fn, params, callback, responseProgressCB = null) {
loadingBar.show();
params = $.extend({ function: fn }, params);
let api_url = "api/" + fn;
const success = (data) => {
setTimeout(loadingBar.hide, 100);
// Catch errors
if (typeof data === "string" && data.substring(0, 7) === "Error: ") {
api.onError(data.substring(7, data.length), params, data);
return false;
}
callback(data);
};
const error = (jqXHR, textStatus, errorThrown) => {
api.onError(api.isTimeout(errorThrown, jqXHR) ? "Session timed out." : "Server error or API not found.", params, errorThrown);
};
let ajaxParams = {
type: "POST",
url: api_url,
data: params,
dataType: "json",
success,
error,
};
if (responseProgressCB !== null) {
ajaxParams.xhrFields = {
onprogress: responseProgressCB,
};
}
$.ajax(ajaxParams);
};
api.get = function (url, callback) {
loadingBar.show();
const success = (data) => {
setTimeout(loadingBar.hide, 100);
// Catch errors
if (typeof data === "string" && data.substring(0, 7) === "Error: ") {
api.onError(data.substring(7, data.length), params, data);
return false;
}
callback(data);
};
const error = (jqXHR, textStatus, errorThrown) => {
api.onError(api.isTimeout(errorThrown, jqXHR) ? "Session timed out." : "Server error or API not found.", {}, errorThrown);
};
$.ajax({
type: "GET",
url: url,
data: {},
dataType: "text",
success,
error,
});
};
api.post_raw = function (fn, params, callback) {
loadingBar.show();
params = $.extend({ function: fn }, params);
let api_url = "api/" + fn;
const success = (data) => {
setTimeout(loadingBar.hide, 100);
// Catch errors
if (typeof data === "string" && data.substring(0, 7) === "Error: ") {
api.onError(data.substring(7, data.length), params, data);
return false;
}
callback(data);
};
const error = (jqXHR, textStatus, errorThrown) => {
api.onError(api.isTimeout(errorThrown, jqXHR) ? "Session timed out." : "Server error or API not found.", params, errorThrown);
};
$.ajax({
type: "POST",
url: api_url,
data: params,
dataType: "text",
success,
error,
});
};