Files
frontend/src/api/organizacion.js

38 lines
1.1 KiB
JavaScript

import { refreshToken } from './auth';
import { extractApiError } from './apiError';
const API_URL = import.meta.env.VITE_EFC_API_URL;
export async function fetchOrganizationUsage(token) {
let res = await fetch(`${API_URL}/organization/uso-almacenamiento/mi_organizacion/`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
if (res.status === 401) {
const refresh = localStorage.getItem('refresh');
if (refresh) {
try {
const data = await refreshToken(refresh);
localStorage.setItem('access', data.access);
res = await fetch(`${API_URL}/organization/uso-almacenamiento/mi_organizacion/`, {
headers: {
'Authorization': `Bearer ${data.access}`,
'Content-Type': 'application/json',
},
});
if (res.status === 401) throw new Error('SESSION_EXPIRED');
} catch {
throw new Error('SESSION_EXPIRED');
}
} else {
throw new Error('SESSION_EXPIRED');
}
}
if (!res.ok) throw new Error(await extractApiError(res));
return res.json();
}