diff options
Diffstat (limited to 'fe/src/http.ts')
-rw-r--r-- | fe/src/http.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/fe/src/http.ts b/fe/src/http.ts new file mode 100644 index 0000000..cc5a906 --- /dev/null +++ b/fe/src/http.ts | |||
@@ -0,0 +1,60 @@ | |||
1 | export default class HttpClient { | ||
2 | private static instance: HttpClient; | ||
3 | baseURL: string; | ||
4 | |||
5 | private constructor(baseURL: string) { | ||
6 | this.baseURL = baseURL; | ||
7 | } | ||
8 | |||
9 | private getURL(endpoint: string): URL { | ||
10 | return new URL(endpoint, this.baseURL) | ||
11 | } | ||
12 | |||
13 | public static getInstance(): HttpClient { | ||
14 | if (!HttpClient.instance) { | ||
15 | const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? 'http://localhost:8080/api/v1'; | ||
16 | HttpClient.instance = new HttpClient(baseUrl); | ||
17 | } | ||
18 | |||
19 | return HttpClient.instance; | ||
20 | } | ||
21 | |||
22 | async get({ endpoint }: IHttpParameters): Promise<Response> { | ||
23 | const url = this.getURL(endpoint); | ||
24 | const response = await fetch(url, { | ||
25 | method: 'GET', | ||
26 | headers: headers, | ||
27 | }); | ||
28 | return response.json(); | ||
29 | } | ||
30 | |||
31 | async post({ endpoint }: IHttpParameters): Promise<Response> { | ||
32 | const url = this.getURL(endpoint); | ||
33 | const response = await fetch(url, { | ||
34 | method: 'POST', | ||
35 | body: JSON.stringify(body), | ||
36 | headers: headers, | ||
37 | }); | ||
38 | return response.json(); | ||
39 | } | ||
40 | |||
41 | async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise<Response> { | ||
42 | const url = this.getURL(endpoint); | ||
43 | if (authenticated) { | ||
44 | |||
45 | } | ||
46 | const response: Response = await fetch(url) | ||
47 | } | ||
48 | |||
49 | async delete({ endpoint, authenticated }: IHttpParameters): Promise<Response> { | ||
50 | const url = this.getURL(endpoint); | ||
51 | if (authenticated) { } | ||
52 | const response = await fetch() | ||
53 | } | ||
54 | } | ||
55 | |||
56 | interface IHttpParameters { | ||
57 | endpoint: string; | ||
58 | authenticated: boolean; | ||
59 | headers: Headers | ||
60 | } | ||