140 lines
3.4 KiB
TypeScript
140 lines
3.4 KiB
TypeScript
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<T>(url: URL) {
|
|
const request = async () =>
|
|
await fetch(url, {
|
|
method: "GET",
|
|
headers: this.buildHeaders(ContentType.JSON),
|
|
});
|
|
return this.sendRequest<T>(request);
|
|
}
|
|
|
|
protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}) {
|
|
return this.sendRequest<T>(
|
|
async () =>
|
|
await fetch(url, {
|
|
method: "POST",
|
|
headers: this.buildHeaders(ContentType.JSON),
|
|
body: this.buildBody(body),
|
|
}),
|
|
);
|
|
}
|
|
|
|
protected async postRequestFormData<T>(url: URL, body: FormData) {
|
|
return this.sendRequest<T>(
|
|
async () =>
|
|
await fetch(url, {
|
|
method: "POST",
|
|
headers: this.buildHeaders(ContentType.FORM_DATA),
|
|
body,
|
|
}),
|
|
);
|
|
}
|
|
|
|
protected async putRequest<T>(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<T>(request);
|
|
}
|
|
|
|
protected async patchRequest<T>(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<T>(request);
|
|
}
|
|
|
|
protected async deleteRequest<T>(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<T>(request);
|
|
}
|
|
|
|
protected async putFormDataRequest<T>(url: URL, body: FormData) {
|
|
const request = async () =>
|
|
await fetch(url, {
|
|
method: "PUT",
|
|
headers: this.buildHeaders(ContentType.FORM_DATA),
|
|
body,
|
|
});
|
|
|
|
return this.sendRequest<T>(request);
|
|
}
|
|
|
|
private async sendRequest<T>(request: () => Promise<Response>): Promise<T> {
|
|
const response = await request();
|
|
return this.processResponse<T>(response, request);
|
|
}
|
|
|
|
protected async processResponse<T>(response: Response, request: () => Promise<Response>): Promise<T> {
|
|
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;
|
|
}
|