import { FrontendVariables } from "@Front/Config/VariablesFront"; 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() { console.log("BaseApiService.baseUrl >>> ", BaseApiService.baseUrl); return BaseApiService.baseUrl; } protected buildHeaders(contentType: ContentType) { const headers = new Headers(); if (contentType === ContentType.JSON) { headers.set("Content-Type", contentType); } return headers; } protected buildBody(body: { [key: string]: unknown }): string { return JSON.stringify(body); } protected async getRequest(url: URL) { 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 } = {}) { 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 } = {}) { 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 } = {}) { 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) { 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; } if (!response.ok) { return Promise.reject(response); } return responseJson as T; } protected onError(error: unknown) { console.error(error); } } export interface IResponse { http_status: number; }