refactor: modularize API client by splitting resources into individual files and standardizing HTTP request handling.

This commit is contained in:
Jose Selesan
2026-04-20 16:05:37 -03:00
parent 28fd51f926
commit 34cb63364c
11 changed files with 327 additions and 267 deletions

View File

@@ -0,0 +1,34 @@
export class ApiClientError extends Error {
status?: number;
details?: unknown;
causeError?: unknown;
constructor(
message: string,
options?: { status?: number; details?: unknown; causeError?: unknown }
) {
super(message);
this.name = 'ApiClientError';
this.status = options?.status;
this.details = options?.details;
this.causeError = options?.causeError;
}
}
export type ErrorHandlers = {
onForbidden?: (error: ApiClientError) => void | Promise<void>;
onError?: (error: ApiClientError) => void | Promise<void>;
};
const handlers: ErrorHandlers = {};
export const apiBaseUrl =
import.meta.env.VITE_API_BASE_URL?.trim() ||
(import.meta.env.DEV ? 'http://localhost:3000' : window.location.origin);
export function configureApiClient(nextHandlers: ErrorHandlers) {
handlers.onForbidden = nextHandlers.onForbidden;
handlers.onError = nextHandlers.onError;
}
export { handlers };