-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.ts
55 lines (45 loc) · 1.34 KB
/
request.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
50
51
52
53
54
55
import axios from "axios";
import { io, Socket } from "socket.io-client";
import backendURL, { host, backendPath } from "../data/backendURL";
class RequestController {
private config = { headers: {} };
token: string | null = "";
socket: Socket | null = null;
setToken(tokenToSet: string) {
this.config = {
headers: { Authorization: `Bearer ${tokenToSet}` },
};
this.token = tokenToSet;
const socket = io(host, {
path: `/${backendPath}/ws`,
auth: { token: `Bearer ${tokenToSet}` },
autoConnect: false,
});
socket.connect();
this.socket = socket;
}
clearToken() {
this.config = { headers: {} };
this.token = "";
this.socket?.off();
this.socket = null;
}
async get(url: string) {
const res = await axios.get(`${backendURL}${url}`, this.config);
return res.data;
}
async post(url: string, body?: Record<string, unknown>) {
const res = await axios.post(`${backendURL}${url}`, body, this.config);
return res.data;
}
async put(url: string, body?: Record<string, unknown>) {
const res = await axios.put(`${backendURL}${url}`, body, this.config);
return res.data;
}
async del(url: string) {
const res = await axios.delete(`${backendURL}${url}`, this.config);
return res.data;
}
}
const request = new RequestController();
export default request;