57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
/**
|
|
* API Configuration - URLs e funções de requisição
|
|
*/
|
|
|
|
// URL base da API - pode ser alterada por variável de ambiente
|
|
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://api.localhost';
|
|
|
|
/**
|
|
* Endpoints da API
|
|
*/
|
|
export const API_ENDPOINTS = {
|
|
// Auth
|
|
register: `${API_BASE_URL}/api/auth/register`,
|
|
login: `${API_BASE_URL}/api/auth/login`,
|
|
logout: `${API_BASE_URL}/api/auth/logout`,
|
|
refresh: `${API_BASE_URL}/api/auth/refresh`,
|
|
me: `${API_BASE_URL}/api/me`,
|
|
|
|
// Admin / Agencies
|
|
adminAgencyRegister: `${API_BASE_URL}/api/admin/agencies/register`,
|
|
|
|
// Health
|
|
health: `${API_BASE_URL}/health`,
|
|
apiHealth: `${API_BASE_URL}/api/health`,
|
|
} as const;
|
|
|
|
/**
|
|
* Wrapper para fetch com tratamento de erros
|
|
*/
|
|
export async function apiRequest<T = any>(
|
|
url: string,
|
|
options?: RequestInit
|
|
): Promise<T> {
|
|
try {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options?.headers,
|
|
},
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.message || `Erro ${response.status}`);
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
throw error;
|
|
}
|
|
throw new Error('Erro desconhecido na requisição');
|
|
}
|
|
}
|