Migrating from services to dedicated API files

This commit is contained in:
2025-05-03 21:28:41 +02:00
parent e789025cd1
commit 043eab423f
22 changed files with 265 additions and 228 deletions

47
app/src/api/api.ts Normal file
View File

@@ -0,0 +1,47 @@
import { refreshToken } from './auth';
/**
* Makes an API call with proper cookie handling and error handling
*/
export const apiCall = async (
url: string,
options: RequestInit = {}
): Promise<Response> => {
try {
const response = await fetch(url, {
...options,
// Include credentials to send/receive cookies
credentials: 'include',
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
// Handle 401 responses
if (response.status === 401) {
const isRefreshEndpoint = url.endsWith('/auth/refresh');
if (!isRefreshEndpoint) {
// Attempt token refresh and retry the request
const refreshSuccess = await refreshToken();
if (refreshSuccess) {
// Retry the original request
return apiCall(url, options);
}
}
throw new Error('Authentication failed');
}
if (!response.ok && response.status !== 204) {
const errorData = (await response.json()) as { message: string };
throw new Error(
errorData?.message || `HTTP error! status: ${response.status}`
);
}
return response;
} catch (error) {
console.error(`API call failed: ${(error as Error).message}`);
throw error;
}
};