Files
Repuestera/front/src/lib/hooks/updateUser.ts
T
2026-05-08 21:20:18 -03:00

29 lines
833 B
TypeScript

import type { User } from '../../types';
import { API_URL } from '../env';
async function handleResponse<T>(response: Response): Promise<T> {
if (!response.ok) {
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
throw new Error(error.message || 'Error en la petición');
}
return response.json();
}
function getToken(): string {
if (typeof window !== 'undefined') {
return localStorage.getItem('token') || '';
}
return '';
}
export async function updateUser(id: number, data: Partial<{ email: string; name: string }>): Promise<User> {
const response = await fetch(`${API_URL}/users/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`
},
body: JSON.stringify(data)
});
return handleResponse<User>(response);
}