-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathapi.js
174 lines (159 loc) · 3.5 KB
/
api.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const BASE_API_PATH = location.origin.toString();
const api_url_object = new URL(BASE_API_PATH);
const BASE_DOMAIN = api_url_object.host;
async function api_request(method, path, body) {
var request_options = {
method: method,
credentials: 'include',
mode: 'cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Buster': 'true'
},
redirect: 'follow'
};
if (body) {
request_options.body = JSON.stringify(body);
}
window.app.loading = true;
try {
var response = await fetch(
`${BASE_API_PATH}${path}`,
request_options
);
} catch ( e ) {
window.app.loading = false;
throw e;
}
window.app.loading = false;
const response_body = await response.json();
return response_body;
}
async function is_authenticated() {
return api_request(
'GET',
`/api/v1/auth-check`,
false
);
}
async function authenticate(password) {
return api_request(
'POST',
`/api/v1/login`,
{
'password': password
}
);
}
async function get_payload_fires(page, limit) {
return api_request(
'GET',
`/api/v1/payloadfires?page=${page}&limit=${limit}`,
false
);
}
async function delete_payload_fires(payload_id_array) {
return api_request(
'DELETE',
`/api/v1/payloadfires`,
{
'ids': payload_id_array
}
);
}
async function get_collect_pages(page, limit) {
return api_request(
'GET',
`/api/v1/collected_pages?page=${page}&limit=${limit}`,
false
);
}
async function delete_collect_pages(collected_pages_id_array) {
return api_request(
'DELETE',
`/api/v1/collected_pages`,
{
'ids': collected_pages_id_array
}
);
}
async function get_settings() {
return api_request(
'GET',
`/api/v1/settings`,
false
);
}
async function update_password(new_password) {
return api_request(
'PUT',
`/api/v1/settings`,
{
"password": new_password,
}
);
}
async function generate_new_correlation_api_key() {
return api_request(
'PUT',
`/api/v1/settings`,
{
"correlation_api_key": true,
}
);
}
async function set_chainload_uri(chainload_uri) {
return api_request(
'PUT',
`/api/v1/settings`,
{
"chainload_uri": chainload_uri,
}
);
}
async function set_email_alerts(send_alerts_bool) {
return api_request(
'PUT',
`/api/v1/settings`,
{
"send_alert_emails": send_alerts_bool,
}
);
}
async function revoke_all_sessions() {
return api_request(
'PUT',
`/api/v1/settings`,
{
"revoke_all_sessions": true,
}
);
}
async function update_pages_to_collect(pages_to_collect) {
return api_request(
'PUT',
`/api/v1/settings`,
{
"pages_to_collect": pages_to_collect,
}
);
}
module.exports = {
BASE_API_PATH,
BASE_DOMAIN,
api_request,
is_authenticated,
authenticate,
get_payload_fires,
delete_payload_fires,
get_collect_pages,
delete_collect_pages,
get_settings,
update_password,
generate_new_correlation_api_key,
set_chainload_uri,
set_email_alerts,
revoke_all_sessions,
update_pages_to_collect
}