Private
Public Access
hecho crud de permisos
This commit is contained in:
1 parent
0370a958b4
commit
4062a0232f
8 files changed
+369
-1
No files matched your search
@@ -5,5 +5,9 @@ export { obtenerUsuarioPorId } from './hooks/obtenerUsuarioPorId';
|
||||
export { modificarUsuario } from './hooks/modificarUsuario';
|
||||
export { borrarUsuario } from './hooks/borrarUsuario';
|
||||
export { ObtenerGruposDeUsuario } from './hooks/ObtenerGruposDeUsuario';
|
||||
export { obtenerPermisos } from './hooks/obtenerPermisos';
|
||||
export { crearPermiso } from './hooks/crearPermiso';
|
||||
export { actualizarPermiso } from './hooks/actualizarPermiso';
|
||||
export { eliminarPermiso } from './hooks/eliminarPermiso';
|
||||
export { setToken, removeToken, getToken } from './hooks/token';
|
||||
export { getCurrentUser, setCurrentUser, removeCurrentUser } from './hooks/user';
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { Permiso } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
export async function actualizarPermiso(id: number, data: Partial<Permiso>): Promise<Permiso> {
|
||||
const response = await fetch(`${API_URL}/permisos/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Permiso>(response);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { Permiso } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
export async function crearPermiso(data: Partial<Permiso>): Promise<Permiso> {
|
||||
const response = await fetch(`${API_URL}/permisos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Permiso>(response);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function eliminarPermiso(id: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/permisos/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
if (!response.ok) throw new Error('Error al eliminar permiso');
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { Permiso } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
export async function obtenerPermisos(): Promise<Permiso[]> {
|
||||
const response = await fetch(`${API_URL}/permisos`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
return handleResponse<Permiso[]>(response);
|
||||
}
|
||||
@@ -42,4 +42,4 @@ export function removeAuthTokens(): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem('auth');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user