-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
106 lines (84 loc) · 2.87 KB
/
api.js
File metadata and controls
106 lines (84 loc) · 2.87 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
const API = "http://192.168.111.30:5090/";
const API_GET = async (path) => {
return fetch(API + path, {
method: 'GET',
headers: {
'auth-token': window.localStorage.getItem("auth-token")
}
});
}
const API_POST = async (path, pbody) => {
return fetch(API + path, {
method: 'POST',
body: JSON.stringify(pbody),
headers: {
'auth-token': window.localStorage.getItem("auth-token")
}
});
}
const API_DELETE = async (path) => {
return fetch(API + path, {
method: 'DELETE',
headers: {
'auth-token': window.localStorage.getItem("auth-token")
}
});
}
const auth_login = async (user, passwd) => {
const response = await API_POST('login', { name: user, password: passwd });
if (response.ok) {
const response_parsed = await response.json();
window.localStorage.setItem('name', response_parsed['name']);
window.localStorage.setItem('display-name', response_parsed['display-name']);
window.localStorage.setItem('auth-token', JSON.stringify(response_parsed['auth-token']));
return true;
} else {
return false;
}
}
const auth_logout = async () => {
const response = await API_POST('logout', "");
window.localStorage.removeItem('name');
window.localStorage.removeItem('display-name');
window.localStorage.removeItem('auth-token');
return response.ok
}
const get_projects = async () => {
const response = await API_GET('projects');
if (!response.ok) return null;
const response_parsed = await response.json();
return response_parsed;
}
const get_project = async (id) => {
const response = await API_GET('projects/' + id);
if (!response.ok) return null;
const response_parsed = await response.json();
return response_parsed;
}
const get_project_files = async (project) => {
const response = await API_GET('projects/' + project + '/files');
if (!response.ok) return null;
const response_parsed = await response.json();
return response_parsed;
}
const get_project_file = async (project, id) => {
const response = await API_GET('projects/' + project + '/files/' + encodeURIComponent(encodeURIComponent(id)));
if (!response.ok) return null;
return response.text();
}
const save_project_file = async (project, id, file) => {
const response = await API_POST('projects/' + project + '/files/' + encodeURIComponent(encodeURIComponent(id)), file);
return response.ok;
}
const lock_project_file = async (project, id) => {
const response = await API_POST('projects/' + project + '/files/' + encodeURIComponent(encodeURIComponent(id)) + '/lock', "");
return response.ok;
}
const unlock_project_file = async (project, id) => {
const response = await API_POST('projects/' + project + '/files/' + encodeURIComponent(encodeURIComponent(id)) + '/unlock', "");
return response.ok;
}
const delete_project_file = async (project, id) => {
const response = await API_DELETE('projects/' + project + '/files/' + encodeURIComponent(encodeURIComponent(id)));
return response.ok;
}