-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatApi.js
More file actions
126 lines (118 loc) · 3.39 KB
/
Copy pathchatApi.js
File metadata and controls
126 lines (118 loc) · 3.39 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
class ChatApiService {
constructor() {
this.mainUrl = 'https://jslabdb.datamola.com';
this.urlGetMesseges = 'https://jslabdb.datamola.com/messages';
this.urlGetUsers = 'https://jslabdb.datamola.com/users';
this.urlRegister = 'https://jslabdb.datamola.com/auth/register';
this.urlLogin = 'https://jslabdb.datamola.com/auth/login';
this.urlLogOut = 'https://jslabdb.datamola.com/auth/logout';
}
async getUsers() {
try {
let response = await fetch(this.urlGetUsers);
let users = await response.json();
this.users = users;
return users;
} catch (error) {
return alert(error);
}
}
async getMesseges(skip, params) {
try {
let response = await fetch(`${this.urlGetMesseges}?skip=0&top=${skip}&${new URLSearchParams(params)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
authorization: 'Bearer ' + localStorage.getItem('curUserToken')
}
});
const messages = await response.json();
return messages;
} catch (error) {
return alert(error);
}
}
async postMsg(body) {
try {
let response = await fetch(this.urlGetMesseges, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
authorization: 'Bearer ' + localStorage.getItem('curUserToken')
},
body: JSON.stringify(body)
});
} catch (error) {
console.log(error);
}
}
async editMsg(id, body) {
try {
let response = await fetch(`${this.urlGetMesseges}/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json;charset=utf-8',
authorization: 'Bearer ' + localStorage.getItem('curUserToken')
},
body: JSON.stringify(body)
});
} catch (error) {
console.log(error);
}
}
async deleteMsg(id) {
try {
let response = await fetch(`${this.urlGetMesseges}/${id}`, {
method: 'DELETE',
headers: {
authorization: 'Bearer ' + localStorage.getItem('curUserToken')
}
});
} catch (error) {
console.log(error);
}
}
async logOut() {
try {
let response = await fetch(this.urlLogOut, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
authorization: 'Bearer ' + localStorage.getItem('curUserToken')
}
});
} catch (error) {
console.log(error);
}
}
// async filter(skip, obj) {
// try {
// let response = await fetch(`${this.urlGetMesseges}?skip=0&top=${skip}?${new URLSearchParams(obj)}`, {
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json;charset=utf-8',
// authorization: 'Bearer ' + localStorage.getItem('curUserToken')
// }
// }).then(data=> data);
// } catch (error) {
// console.log(error);
// }
// }
}
// fetch(`${url}?${new URLSearchParams(params)}`, {
// method: "GET",
// headers: {
// // "Content-Type": "application/json;charset=utf-8",
// //"authorization": "Bearer " + this.token
// },
// })
// .then((data) => {
// return data.json();
// })
// .then((data) => {
// document.querySelector("#joke").innerHTML = data.value.joke;
// this.jokeTimeout = setTimeout(() => {
// this.getJoke();
// }, 10000);
// });
// }