import apiClient from "@/services/api-client/api-client.ts"; import type {AxiosResponse, ResponseType} from "axios"; import {HttpErrorResponseException} from "@/exception/http-error-response-exception.ts"; export abstract class BaseController { protected basePath: string; protected constructor(basePath: string) { this.basePath = basePath; } private assertSuccessResponse(response: AxiosResponse) { if (response.status / 100 !== 2) { throw new HttpErrorResponseException(response); } } private absolutePathFor(path: string) { return "/" + this.basePath + path; } protected async get(path: string, queryParams?: Record, responseType?: ResponseType): Promise { let response = await apiClient.get( this.absolutePathFor(path), {params: queryParams, responseType} ); this.assertSuccessResponse(response); return response.data; } protected async post(path: string, body: unknown): Promise { let response = await apiClient.post(this.absolutePathFor(path), body); this.assertSuccessResponse(response); return response.data; } protected async delete(path: string): Promise { let response = await apiClient.delete(this.absolutePathFor(path)) this.assertSuccessResponse(response); return response.data; } protected async put(path: string, body: unknown): Promise { let response = await apiClient.put(this.absolutePathFor(path), body); this.assertSuccessResponse(response); return response.data; } }