refactor: modularize API client by splitting resources into individual files and standardizing HTTP request handling.
This commit is contained in:
34
apps/frontend/src/lib/api/base.ts
Normal file
34
apps/frontend/src/lib/api/base.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user