-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
96 lines (93 loc) · 3.19 KB
/
index.mjs
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
import fetch from 'node-fetch';
class HeroAPI {
constructor(api_token) {
this.api_token = api_token;
}
async fetchJSON(url, method, body) {
const headers = {
'Authorization': `Bearer ${this.api_token}`,
'Content-Type': 'application/json',
};
const response = await fetch(url, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
// Get response text
const text = await response.text();
// Try to parse the text as JSON
try {
return JSON.parse(text);
}
catch {
// If parsing fails, return the text as a message object
return { message: text };
}
}
createSpace(name, isPublic, img) {
return this.fetchJSON('https://hero.page/api/v1/createSpace', 'POST', {
name,
isPublic,
img,
});
}
getSpace(spaceId) {
return this.fetchJSON(`https://hero.page/api/v1/getSpace/${spaceId}`, 'GET');
}
updateSpace(spaceId, body) {
return this.fetchJSON(`https://hero.page/api/v1/updateSpace/${spaceId}`, 'POST', body);
}
listSpaces() {
return this.fetchJSON('https://hero.page/api/v1/listSpaces', 'GET');
}
deleteSpace(spaceId) {
return this.fetchJSON(`https://hero.page/api/v1/deleteSpace/${spaceId}`, 'POST');
}
createList(spaceId, name, color) {
return this.fetchJSON('https://hero.page/api/v1/createList', 'POST', {
spaceId,
name,
color,
});
}
getList(spaceId, listId) {
return this.fetchJSON(`https://hero.page/api/v1/getList/${spaceId}/${listId}`, 'GET');
}
updateList(spaceId, listId, name, color) {
return this.fetchJSON(`https://hero.page/api/v1/updateList/${spaceId}/${listId}`, 'POST', {
name,
color,
});
}
deleteList(spaceId, listId) {
return this.fetchJSON(`https://hero.page/api/v1/deleteList/${spaceId}/${listId}`, 'POST');
}
listLists(spaceId) {
return this.fetchJSON(`https://hero.page/api/v1/listLists/${spaceId}`, 'GET');
}
createItem(spaceId, requestBody) {
return this.fetchJSON(`https://hero.page/api/v1/createItem/${spaceId}`, 'POST', requestBody);
}
updateItem(spaceId, itemId, requestBody) {
return this.fetchJSON(`https://hero.page/api/v1/updateItem/${spaceId}/${itemId}`, 'POST', requestBody);
}
getItem(spaceId, itemId) {
return this.fetchJSON(`https://hero.page/api/v1/getItem/${spaceId}/${itemId}`, 'GET');
}
listItems(spaceId, listId) {
return this.fetchJSON(`https://hero.page/api/v1/listItems/${spaceId}/${listId}`, 'GET');
}
deleteItem(spaceId, itemId) {
return this.fetchJSON(`https://hero.page/api/v1/deleteItem/${spaceId}/${itemId}`, 'POST');
}
uploadImage(base64, name) {
return this.fetchJSON('https://hero.page/api/v1/uploadImage', 'POST', {
base64: base64,
name: name,
});
}
}
export { HeroAPI };