import { FrontendVariables } from "@Front/Config/VariablesFront"; import CookieService from "@Front/Services/CookieService/CookieService"; export enum ContentType { JSON = "application/json", FORM_DATA = "multipart/form-data;", } export default abstract class BaseApiService { private static baseUrl: string; protected readonly variables = FrontendVariables.getInstance(); protected constructor() { BaseApiService.baseUrl ??= FrontendVariables.getInstance().BACK_API_PROTOCOL + FrontendVariables.getInstance().BACK_API_HOST + FrontendVariables.getInstance().BACK_API_ROOT_URL + FrontendVariables.getInstance().BACK_API_VERSION; } protected getBaseUrl() { return BaseApiService.baseUrl; } protected buildHeaders(contentType: ContentType) { const token = CookieService.getInstance().getCookie("leCoffreAccessToken"); const headers = new Headers(); if (contentType === ContentType.JSON) { headers.set("Content-Type", contentType); } headers.set("Authorization", `Bearer ${token}`); return headers; } protected buildBody(body: { [key: string]: unknown }): string { return JSON.stringify(body); } protected async getRequest(url: URL, token?: string) { const request = async () => await fetch(url, { method: "GET", headers: this.buildHeaders(ContentType.JSON), }); return this.sendRequest(request); } protected async postRequest(url: URL, body: { [key: string]: unknown } = {}, token?: string) { return this.sendRequest( async () => await fetch(url, { method: "POST", headers: this.buildHeaders(ContentType.JSON), body: this.buildBody(body), }), ); } protected async postRequestFormData(url: URL, body: FormData) { return this.sendRequest( async () => await fetch(url, { method: "POST", headers: this.buildHeaders(ContentType.FORM_DATA), body, }), ); } protected async putRequest(url: URL, body: { [key: string]: unknown } = {}, token?: string) { const request = async () => await fetch(url, { method: "PUT", headers: this.buildHeaders(ContentType.JSON), body: this.buildBody(body), }); return this.sendRequest(request); } protected async patchRequest(url: URL, body: { [key: string]: unknown } = {}) { const request = async () => await fetch(url, { method: "PATCH", headers: this.buildHeaders(ContentType.JSON), body: this.buildBody(body), }); return this.sendRequest(request); } protected async deleteRequest(url: URL, body: { [key: string]: unknown } = {}, token?: string) { const request = async () => await fetch(url, { method: "DELETE", headers: this.buildHeaders(ContentType.JSON), body: this.buildBody(body), }); return this.sendRequest(request); } protected async putFormDataRequest(url: URL, body: FormData, token?: string) { const request = async () => await fetch(url, { method: "PUT", headers: this.buildHeaders(ContentType.FORM_DATA), body, }); return this.sendRequest(request); } private async sendRequest(request: () => Promise): Promise { const response = await request(); return this.processResponse(response, request); } protected async processResponse(response: Response, request: () => Promise): Promise { let responseJson: any | null; try { responseJson = await response.json(); } catch (err: unknown) { responseJson = null; return Promise.reject(err); } if (!response.ok) { return Promise.reject(responseJson); } return responseJson as T; } protected onError(error: unknown) { //console.error(error); } } export interface IResponse { http_status: number; }