Files
memegoat/frontend/src/lib/api.ts
Mathis HERRIOT c6b23de481 feat(api): add TagService and enhance API error handling
Introduce `TagService` to manage tag-related API interactions. Add SSR cookie interceptor for API requests and implement token refresh logic on 401 errors. Update `FavoriteService` to use `favoritesOnly` filter for exploring content.
2026-01-14 22:19:11 +01:00

77 lines
2.1 KiB
TypeScript

import axios from "axios";
const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL || "https://api.memegoat.fr",
withCredentials: true,
headers: {
"Content-Type": "application/json",
},
});
// Interceptor for Server-Side Rendering to pass cookies
api.interceptors.request.use(async (config) => {
if (typeof window === "undefined") {
try {
const { cookies } = await import("next/headers");
const cookieStore = await cookies();
const cookieHeader = cookieStore.toString();
if (cookieHeader) {
config.headers.Cookie = cookieHeader;
}
} catch (_error) {
// Fail silently if cookies() is not available (e.g. during build)
}
}
return config;
});
// Système anti-spam rudimentaire pour les erreurs répétitives
const errorCache = new Map<string, number>();
const SPAM_THRESHOLD_MS = 2000; // 2 secondes de silence après une erreur sur le même endpoint
api.interceptors.response.use(
(response) => {
// Nettoyer le cache d'erreur en cas de succès sur cet endpoint
const url = response.config.url || "";
errorCache.delete(url);
return response;
},
async (error) => {
const originalRequest = error.config;
// Handle Token Refresh (401 Unauthorized)
if (
error.response?.status === 401 &&
!originalRequest._retry &&
!originalRequest.url?.includes("/auth/refresh") &&
!originalRequest.url?.includes("/auth/login")
) {
originalRequest._retry = true;
try {
await api.post("/auth/refresh");
return api(originalRequest);
} catch (refreshError) {
// If refresh fails, we might want to redirect to login on the client
if (typeof window !== "undefined") {
window.location.href = "/login";
}
return Promise.reject(refreshError);
}
}
const url = error.config?.url || "unknown";
const now = Date.now();
const lastErrorTime = errorCache.get(url);
if (lastErrorTime && now - lastErrorTime < SPAM_THRESHOLD_MS) {
// Ignorer l'erreur si elle se produit trop rapidement (déjà signalée)
return new Promise(() => {});
}
errorCache.set(url, now);
return Promise.reject(error);
},
);
export default api;