/
session.ts
49 lines (45 loc) 路 1.38 KB
/
session.ts
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
import {
ResDelMsg,
ResFetchMsgs,
ResFetchMsgTotal,
} from "../types/ResponseType";
const fetchMsgTotal = async (): Promise<ResFetchMsgTotal> => {
const headers = new Headers({
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
});
const response = await fetch(`/api/messages/total`, { headers });
if (response.status !== 200) {
throw new Error("Get message total failed.");
}
const data: ResFetchMsgTotal = await response.json();
return data;
};
const fetchMsgs = async (): Promise<ResFetchMsgs> => {
const headers = new Headers({
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
});
const response = await fetch(`/api/messages`, { headers });
if (response.status !== 200) {
throw new Error("Get messages failed.");
}
const data: ResFetchMsgs = await response.json();
return data;
};
const clearMsgs = async (): Promise<ResDelMsg> => {
const headers = new Headers({
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
});
const response = await fetch(`/api/messages`, {
method: "DELETE",
headers,
});
if (response.status !== 200) {
throw new Error("Delete messages failed.");
}
const data: ResDelMsg = await response.json();
return data;
};
export { fetchMsgTotal, fetchMsgs, clearMsgs };