diff options
Diffstat (limited to 'fe/src/http.ts')
-rw-r--r-- | fe/src/http.ts | 96 |
1 files changed, 64 insertions, 32 deletions
diff --git a/fe/src/http.ts b/fe/src/http.ts index cc5a906..3b2a4f0 100644 --- a/fe/src/http.ts +++ b/fe/src/http.ts | |||
@@ -1,60 +1,92 @@ | |||
1 | export default class HttpClient { | 1 | let instance; |
2 | private static instance: HttpClient; | 2 | const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? "http://localhost:8080/api/v1"; |
3 | |||
4 | class HttpClient { | ||
3 | baseURL: string; | 5 | baseURL: string; |
4 | 6 | commonHeaders: Headers; | |
5 | private constructor(baseURL: string) { | 7 | |
8 | constructor(baseURL: string) { | ||
6 | this.baseURL = baseURL; | 9 | this.baseURL = baseURL; |
10 | this.commonHeaders = new Headers({ | ||
11 | "Content-Type": "application/json" | ||
12 | }) | ||
13 | if (instance) { | ||
14 | throw new Error("New instance cannot be created!"); | ||
15 | } | ||
16 | |||
17 | instance = this; | ||
7 | } | 18 | } |
8 | 19 | ||
9 | private getURL(endpoint: string): URL { | 20 | private getURL(endpoint: string): URL { |
10 | return new URL(endpoint, this.baseURL) | 21 | return new URL(endpoint, this.baseURL); |
11 | } | 22 | } |
12 | 23 | ||
13 | public static getInstance(): HttpClient { | 24 | private token(): string | null { |
14 | if (!HttpClient.instance) { | 25 | return localStorage.getItem('token'); |
15 | const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? 'http://localhost:8080/api/v1'; | 26 | } |
16 | HttpClient.instance = new HttpClient(baseUrl); | ||
17 | } | ||
18 | 27 | ||
19 | return HttpClient.instance; | 28 | private async makeRequest(request: Request): Promise<Response> { |
29 | return fetch(request) | ||
20 | } | 30 | } |
21 | 31 | ||
22 | async get({ endpoint }: IHttpParameters): Promise<Response> { | 32 | async get({ endpoint, headers }: IHttpParameters): Promise<Response> { |
23 | const url = this.getURL(endpoint); | 33 | const url: URL = this.getURL(endpoint); |
24 | const response = await fetch(url, { | 34 | headers = Object.assign<Headers, Headers>(headers, this.commonHeaders); |
25 | method: 'GET', | 35 | const request: Request = new Request(url, { |
26 | headers: headers, | 36 | method: "GET", |
37 | headers | ||
27 | }); | 38 | }); |
28 | return response.json(); | 39 | |
40 | return this.makeRequest(request); | ||
29 | } | 41 | } |
30 | 42 | ||
31 | async post({ endpoint }: IHttpParameters): Promise<Response> { | 43 | async post({ endpoint, authenticated, body, headers }: IHttpParameters): Promise<Response> { |
32 | const url = this.getURL(endpoint); | 44 | const url = this.getURL(endpoint); |
33 | const response = await fetch(url, { | 45 | |
34 | method: 'POST', | 46 | if (authenticated) { |
47 | const token: string | null = this.token(); | ||
48 | headers.append('Authorization', `Bearer ${token}`); | ||
49 | } | ||
50 | |||
51 | const request: Request = new Request(url, { | ||
52 | method: "POST", | ||
35 | body: JSON.stringify(body), | 53 | body: JSON.stringify(body), |
36 | headers: headers, | 54 | headers |
37 | }); | 55 | }) |
38 | return response.json(); | 56 | |
57 | return this.makeRequest(request); | ||
39 | } | 58 | } |
40 | 59 | ||
41 | async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise<Response> { | 60 | async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise<Response> { |
42 | const url = this.getURL(endpoint); | 61 | const url = this.getURL(endpoint); |
43 | if (authenticated) { | 62 | if (authenticated) { |
44 | 63 | ||
45 | } | 64 | } |
46 | const response: Response = await fetch(url) | 65 | const response: Response = await fetch(url, { |
66 | method: "PATCH", | ||
67 | headers | ||
68 | }); | ||
47 | } | 69 | } |
48 | 70 | ||
49 | async delete({ endpoint, authenticated }: IHttpParameters): Promise<Response> { | 71 | async delete({ endpoint, authenticated, headers }: IHttpParameters): Promise<Response> { |
50 | const url = this.getURL(endpoint); | 72 | const url = this.getURL(endpoint); |
51 | if (authenticated) { } | 73 | if (authenticated) { |
52 | const response = await fetch() | 74 | |
75 | } | ||
76 | const response: Response = await fetch(url, { | ||
77 | method: "DELETE", | ||
78 | headers | ||
79 | }) | ||
53 | } | 80 | } |
54 | } | 81 | } |
55 | 82 | ||
56 | interface IHttpParameters { | 83 | interface IHttpParameters { |
57 | endpoint: string; | 84 | endpoint: string; |
85 | body: Record<string, any>; | ||
58 | authenticated: boolean; | 86 | authenticated: boolean; |
59 | headers: Headers | 87 | headers: Headers; |
60 | } | 88 | } |
89 | |||
90 | let http: Readonly<HttpClient> = Object.freeze(new HttpClient(baseUrl)); | ||
91 | |||
92 | export default http; | ||