aboutsummaryrefslogtreecommitdiff
path: root/fe/src/http.ts
blob: cc5a90646318eebbfc17c4508e143a78fdb03238 (plain)
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
export default class HttpClient {
    private static instance: HttpClient;
    baseURL: string;
    
    private constructor(baseURL: string) {
        this.baseURL = baseURL;
    }
    
    private getURL(endpoint: string): URL {
        return new URL(endpoint, this.baseURL)
    }

    public static getInstance(): HttpClient {
        if (!HttpClient.instance) {
            const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? 'http://localhost:8080/api/v1';
            HttpClient.instance = new HttpClient(baseUrl);
        }

        return HttpClient.instance;
    }

    async get({ endpoint }: IHttpParameters): Promise<Response> {
        const url = this.getURL(endpoint);
        const response = await fetch(url, {
            method: 'GET',
            headers: headers,
        });
        return response.json();
    }

    async post({ endpoint }: IHttpParameters): Promise<Response> {
        const url = this.getURL(endpoint);
        const response = await fetch(url, {
            method: 'POST',
            body: JSON.stringify(body),
            headers: headers,
        });
        return response.json();
    }
    
    async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise<Response> {
        const url = this.getURL(endpoint);
        if (authenticated) {
            
        }
        const response: Response = await fetch(url)
    }
    
    async delete({ endpoint, authenticated }: IHttpParameters): Promise<Response> {
        const url = this.getURL(endpoint);
        if (authenticated) { }
        const response = await fetch()
    }
}

interface IHttpParameters {
    endpoint: string;
    authenticated: boolean;
    headers: Headers
}