From 9cae9c1d2a0b4f7fa72f3075541b9ffafe1a7275 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Fri, 15 Mar 2024 18:49:43 -0400 Subject: Add routes for preference, clean up and add types --- fe/src/http.ts | 96 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 32 deletions(-) (limited to 'fe/src/http.ts') 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 @@ -export default class HttpClient { - private static instance: HttpClient; +let instance; +const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? "http://localhost:8080/api/v1"; + +class HttpClient { baseURL: string; - - private constructor(baseURL: string) { + commonHeaders: Headers; + + constructor(baseURL: string) { this.baseURL = baseURL; + this.commonHeaders = new Headers({ + "Content-Type": "application/json" + }) + if (instance) { + throw new Error("New instance cannot be created!"); + } + + instance = this; } - + private getURL(endpoint: string): URL { - return new URL(endpoint, this.baseURL) + 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); - } + private token(): string | null { + return localStorage.getItem('token'); + } - return HttpClient.instance; + private async makeRequest(request: Request): Promise { + return fetch(request) } - async get({ endpoint }: IHttpParameters): Promise { - const url = this.getURL(endpoint); - const response = await fetch(url, { - method: 'GET', - headers: headers, + async get({ endpoint, headers }: IHttpParameters): Promise { + const url: URL = this.getURL(endpoint); + headers = Object.assign(headers, this.commonHeaders); + const request: Request = new Request(url, { + method: "GET", + headers }); - return response.json(); + + return this.makeRequest(request); } - async post({ endpoint }: IHttpParameters): Promise { + async post({ endpoint, authenticated, body, headers }: IHttpParameters): Promise { const url = this.getURL(endpoint); - const response = await fetch(url, { - method: 'POST', + + if (authenticated) { + const token: string | null = this.token(); + headers.append('Authorization', `Bearer ${token}`); + } + + const request: Request = new Request(url, { + method: "POST", body: JSON.stringify(body), - headers: headers, - }); - return response.json(); + headers + }) + + return this.makeRequest(request); } - + async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise { const url = this.getURL(endpoint); if (authenticated) { - + } - const response: Response = await fetch(url) + const response: Response = await fetch(url, { + method: "PATCH", + headers + }); } - - async delete({ endpoint, authenticated }: IHttpParameters): Promise { + + async delete({ endpoint, authenticated, headers }: IHttpParameters): Promise { const url = this.getURL(endpoint); - if (authenticated) { } - const response = await fetch() + if (authenticated) { + + } + const response: Response = await fetch(url, { + method: "DELETE", + headers + }) } } interface IHttpParameters { endpoint: string; + body: Record; authenticated: boolean; - headers: Headers + headers: Headers; } + +let http: Readonly = Object.freeze(new HttpClient(baseUrl)); + +export default http; -- cgit v1.1