refactor(backend): BaseController gebruikt nu Axios & ondersteunt HTML responses

This commit is contained in:
Gerald Schmittinger 2025-03-31 16:20:19 +02:00
parent 8522cde18d
commit 2803a8be54

View file

@ -1,73 +1,42 @@
import { apiConfig } from "@/config.ts"; import { apiConfig } from "@/config.ts";
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 class BaseController { export abstract class BaseController {
protected baseUrl: string; protected baseUrl: string;
constructor(basePath: string) { protected constructor(basePath: string) {
this.baseUrl = `${apiConfig.baseUrl}/${basePath}`; this.baseUrl = `${apiConfig.baseUrl}/${basePath}`;
} }
protected async get<T>(path: string, queryParams?: Record<string, any>): Promise<T> { private assertSuccessResponse(response: AxiosResponse<unknown, unknown>) {
let url = `${this.baseUrl}${path}`; if (response.status / 200 !== 2) {
if (queryParams) { throw new HttpErrorResponseException(response);
const query = new URLSearchParams();
Object.entries(queryParams).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
query.append(key, value.toString());
} }
});
url += `?${query.toString()}`;
} }
const res = await fetch(url); protected async get<T>(path: string, queryParams?: Record<string, any>, responseType?: ResponseType): Promise<T> {
if (!res.ok) { let response = await apiClient.get<T>(path, {params: queryParams, responseType});
const errorData = await res.json().catch(() => ({})); this.assertSuccessResponse(response);
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`); return response.data;
}
return res.json();
} }
protected async post<T>(path: string, body: unknown): Promise<T> { protected async post<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, { let response = await apiClient.post<T>(path, body);
method: "POST", this.assertSuccessResponse(response);
headers: { "Content-Type": "application/json" }, return response.data;
body: JSON.stringify(body),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
} }
protected async delete<T>(path: string): Promise<T> { protected async delete<T>(path: string): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, { let response = await apiClient.delete<T>(path)
method: "DELETE", this.assertSuccessResponse(response);
}); return response.data;
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
} }
protected async put<T>(path: string, body: unknown): Promise<T> { protected async put<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, { let response = await apiClient.put<T>(path, body);
method: "PUT", this.assertSuccessResponse(response);
headers: { "Content-Type": "application/json" }, return response.data;
body: JSON.stringify(body),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
} }
} }