Cesar Mendivil a501d398ff chore: update tailwindcss to version 4.1.0 across admin, broadcast, landing, and studio panels
refactor: remove unnecessary React import in AuthButton component due to new JSX transform

refactor: simplify imports in LanguageSelector component

fix: remove unused AuthResponse type import in useAuth hook

fix: access Vite environment variables in a type-safe manner in api utility
2025-11-09 19:02:50 -07:00

113 lines
2.9 KiB
TypeScript

import type { LoginCredentials, RegisterData, AuthResponse, ApiResponse } from '@avanzacast/shared-types';
import { getAuthHeader } from './auth';
// Access import.meta.env in a type-safe way even if the project doesn't declare the Vite env typing
const _env = (import.meta as any).env;
const API_BASE_URL = _env.VITE_API_URL || 'https://avanzacast-servertokens.bfzqqk.easypanel.host/api/v1';
/**
* Cliente HTTP para hacer peticiones a la API
*/
class ApiClient {
private baseUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
const url = `${this.baseUrl}${endpoint}`;
const headers = {
'Content-Type': 'application/json',
...getAuthHeader(),
...options.headers,
};
try {
const response = await fetch(url, {
...options,
headers,
});
const data = await response.json();
if (!response.ok) {
return {
success: false,
error: {
code: data.code || 'UNKNOWN_ERROR',
message: data.message || 'An error occurred',
details: data.details,
},
};
}
return {
success: true,
data,
};
} catch (error) {
return {
success: false,
error: {
code: 'NETWORK_ERROR',
message: error instanceof Error ? error.message : 'Network error',
},
};
}
}
async get<T>(endpoint: string): Promise<ApiResponse<T>> {
return this.request<T>(endpoint, { method: 'GET' });
}
async post<T>(endpoint: string, data?: any): Promise<ApiResponse<T>> {
return this.request<T>(endpoint, {
method: 'POST',
body: data ? JSON.stringify(data) : undefined,
});
}
async put<T>(endpoint: string, data?: any): Promise<ApiResponse<T>> {
return this.request<T>(endpoint, {
method: 'PUT',
body: data ? JSON.stringify(data) : undefined,
});
}
async delete<T>(endpoint: string): Promise<ApiResponse<T>> {
return this.request<T>(endpoint, { method: 'DELETE' });
}
// Auth endpoints
async login(credentials: LoginCredentials): Promise<ApiResponse<AuthResponse>> {
return this.post<AuthResponse>('/auth/login', credentials);
}
async register(data: RegisterData): Promise<ApiResponse<AuthResponse>> {
return this.post<AuthResponse>('/auth/register', data);
}
async logout(): Promise<ApiResponse<void>> {
return this.post<void>('/auth/logout');
}
async refreshToken(refreshToken: string): Promise<ApiResponse<AuthResponse>> {
return this.post<AuthResponse>('/auth/refresh', { refreshToken });
}
async getProfile(): Promise<ApiResponse<any>> {
return this.get('/auth/profile');
}
}
// Instancia única del cliente API
export const apiClient = new ApiClient(API_BASE_URL);
// Exportar cliente por defecto
export default apiClient;