refactor: utilisation des types de cache dans tous les services
This commit is contained in:
@@ -2,6 +2,16 @@ import { cookies } from "next/headers";
|
||||
import { AuthConfig } from "@/types/auth";
|
||||
import { serverCacheService } from "./server-cache.service";
|
||||
|
||||
// Types de cache disponibles
|
||||
export type CacheType =
|
||||
| "DEFAULT"
|
||||
| "HOME"
|
||||
| "LIBRARIES"
|
||||
| "SERIES"
|
||||
| "BOOKS"
|
||||
| "IMAGES"
|
||||
| "READ_PROGRESS";
|
||||
|
||||
export abstract class BaseApiService {
|
||||
protected static async getKomgaConfig(): Promise<AuthConfig> {
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
@@ -34,9 +44,9 @@ export abstract class BaseApiService {
|
||||
protected static async fetchWithCache<T>(
|
||||
key: string,
|
||||
fetcher: () => Promise<T>,
|
||||
ttl: number = 5 * 60 // 5 minutes par défaut
|
||||
type: CacheType = "DEFAULT"
|
||||
): Promise<T> {
|
||||
return serverCacheService.getOrSet(key, fetcher, ttl);
|
||||
return serverCacheService.getOrSet(key, fetcher, type);
|
||||
}
|
||||
|
||||
protected static handleError(error: unknown, defaultMessage: string): never {
|
||||
@@ -64,6 +74,22 @@ export abstract class BaseApiService {
|
||||
});
|
||||
}
|
||||
|
||||
// Log de l'URL finale
|
||||
console.log(`🔄 [Komga API] ${url.toString()}`);
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
protected static async fetchFromApi<T>(url: string, headers: Headers): Promise<T> {
|
||||
const response = await fetch(url, { headers });
|
||||
|
||||
// Log du résultat de la requête
|
||||
console.log(`📡 [Komga API] ${response.status} ${response.statusText} - ${url}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export class BookService extends BaseApiService {
|
||||
pages: pages.map((page: any) => page.number),
|
||||
};
|
||||
},
|
||||
5 * 60 // Cache de 5 minutes
|
||||
"BOOKS"
|
||||
);
|
||||
} catch (error) {
|
||||
return this.handleError(error, "Impossible de récupérer le tome");
|
||||
|
||||
@@ -17,64 +17,44 @@ export class HomeService extends BaseApiService {
|
||||
return this.fetchWithCache<HomeData>(
|
||||
"home",
|
||||
async () => {
|
||||
// Appels API parallèles
|
||||
const [ongoingResponse, recentlyReadResponse, popularResponse] = await Promise.all([
|
||||
// Séries en cours
|
||||
fetch(
|
||||
this.buildUrl(config, "series", {
|
||||
read_status: "IN_PROGRESS",
|
||||
sort: "readDate,desc",
|
||||
page: "0",
|
||||
size: "20",
|
||||
media_status: "READY",
|
||||
}),
|
||||
{ headers }
|
||||
),
|
||||
// Derniers livres lus
|
||||
fetch(
|
||||
this.buildUrl(config, "books", {
|
||||
read_status: "READ",
|
||||
sort: "readDate,desc",
|
||||
page: "0",
|
||||
size: "20",
|
||||
}),
|
||||
{ headers }
|
||||
),
|
||||
// Séries populaires
|
||||
fetch(
|
||||
this.buildUrl(config, "series", {
|
||||
page: "0",
|
||||
size: "20",
|
||||
sort: "metadata.titleSort,asc",
|
||||
media_status: "READY",
|
||||
}),
|
||||
{ headers }
|
||||
),
|
||||
// Construction des URLs
|
||||
const ongoingUrl = this.buildUrl(config, "series", {
|
||||
read_status: "IN_PROGRESS",
|
||||
sort: "readDate,desc",
|
||||
page: "0",
|
||||
size: "20",
|
||||
media_status: "READY",
|
||||
});
|
||||
|
||||
const recentlyReadUrl = this.buildUrl(config, "books", {
|
||||
read_status: "READ",
|
||||
sort: "readDate,desc",
|
||||
page: "0",
|
||||
size: "20",
|
||||
media_status: "READY",
|
||||
});
|
||||
|
||||
const popularUrl = this.buildUrl(config, "series", {
|
||||
page: "0",
|
||||
size: "20",
|
||||
sort: "metadata.titleSort,asc",
|
||||
media_status: "READY",
|
||||
});
|
||||
|
||||
// Appels API parallèles avec fetchFromApi
|
||||
const [ongoing, recentlyRead, popular] = await Promise.all([
|
||||
this.fetchFromApi<LibraryResponse<KomgaSeries>>(ongoingUrl, headers),
|
||||
this.fetchFromApi<LibraryResponse<KomgaBook>>(recentlyReadUrl, headers),
|
||||
this.fetchFromApi<LibraryResponse<KomgaSeries>>(popularUrl, headers),
|
||||
]);
|
||||
|
||||
// Vérifier les réponses
|
||||
if (!ongoingResponse.ok || !recentlyReadResponse.ok || !popularResponse.ok) {
|
||||
throw new Error("Erreur lors de la récupération des données");
|
||||
}
|
||||
|
||||
// Récupérer les données
|
||||
const [ongoing, recentlyRead, popular] = (await Promise.all([
|
||||
ongoingResponse.json(),
|
||||
recentlyReadResponse.json(),
|
||||
popularResponse.json(),
|
||||
])) as [
|
||||
LibraryResponse<KomgaSeries>,
|
||||
LibraryResponse<KomgaBook>,
|
||||
LibraryResponse<KomgaSeries>
|
||||
];
|
||||
|
||||
return {
|
||||
ongoing: ongoing.content || [],
|
||||
recentlyRead: recentlyRead.content || [],
|
||||
popular: popular.content || [],
|
||||
};
|
||||
},
|
||||
5 * 60 // Cache de 5 minutes
|
||||
"HOME" // Type de cache
|
||||
);
|
||||
} catch (error) {
|
||||
return this.handleError(error, "Impossible de récupérer les données de la page d'accueil");
|
||||
|
||||
@@ -11,14 +11,8 @@ export class LibraryService extends BaseApiService {
|
||||
|
||||
return this.fetchWithCache<Library[]>(
|
||||
"libraries",
|
||||
async () => {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la récupération des bibliothèques");
|
||||
}
|
||||
return response.json();
|
||||
},
|
||||
5 * 60 // Cache de 5 minutes
|
||||
async () => this.fetchFromApi<Library[]>(url, headers),
|
||||
"LIBRARIES"
|
||||
);
|
||||
} catch (error) {
|
||||
return this.handleError(error, "Impossible de récupérer les bibliothèques");
|
||||
@@ -42,14 +36,8 @@ export class LibraryService extends BaseApiService {
|
||||
|
||||
return this.fetchWithCache<LibraryResponse<Series>>(
|
||||
`library-${libraryId}-series-${page}-${size}-${unreadOnly}`,
|
||||
async () => {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la récupération des séries");
|
||||
}
|
||||
return response.json();
|
||||
},
|
||||
5 * 60 // Cache de 5 minutes
|
||||
async () => this.fetchFromApi<LibraryResponse<Series>>(url, headers),
|
||||
"SERIES"
|
||||
);
|
||||
} catch (error) {
|
||||
return this.handleError(error, "Impossible de récupérer les séries");
|
||||
|
||||
@@ -12,13 +12,7 @@ export class SeriesService extends BaseApiService {
|
||||
|
||||
return this.fetchWithCache<Series>(
|
||||
`series-${seriesId}`,
|
||||
async () => {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la récupération de la série");
|
||||
}
|
||||
return response.json();
|
||||
},
|
||||
async () => this.fetchFromApi<Series>(url, headers),
|
||||
5 * 60 // Cache de 5 minutes
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -37,20 +31,14 @@ export class SeriesService extends BaseApiService {
|
||||
const url = this.buildUrl(config, `series/${seriesId}/books`, {
|
||||
page: page.toString(),
|
||||
size: size.toString(),
|
||||
sort: "metadata.numberSort,asc",
|
||||
sort: "metadata.number,asc",
|
||||
...(unreadOnly && { read_status: "UNREAD,IN_PROGRESS" }),
|
||||
});
|
||||
const headers = this.getAuthHeaders(config);
|
||||
|
||||
return this.fetchWithCache<LibraryResponse<KomgaBook>>(
|
||||
`series-${seriesId}-books-${page}-${size}-${unreadOnly}`,
|
||||
async () => {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la récupération des tomes");
|
||||
}
|
||||
return response.json();
|
||||
},
|
||||
async () => this.fetchFromApi<LibraryResponse<KomgaBook>>(url, headers),
|
||||
5 * 60 // Cache de 5 minutes
|
||||
);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user