diff --git a/src/lib/services/base-api.service.ts b/src/lib/services/base-api.service.ts index c87f90e..a84cec2 100644 --- a/src/lib/services/base-api.service.ts +++ b/src/lib/services/base-api.service.ts @@ -9,6 +9,11 @@ interface KomgaRequestInit extends RequestInit { isImage?: boolean; } +interface KomgaUrlBuilder { + path: string; + params?: Record; +} + export abstract class BaseApiService { protected static async getKomgaConfig(): Promise { try { @@ -78,10 +83,24 @@ export abstract class BaseApiService { } protected static async fetchFromApi( - url: string, - headers: Headers, + urlBuilder: KomgaUrlBuilder, + headersOptions = {}, options: KomgaRequestInit = {} ): Promise { + const config = await this.getKomgaConfig(); + const { path, params } = urlBuilder; + const url = this.buildUrl(config, path, params); + + const headers = this.getAuthHeaders(config); + if (headersOptions) { + for (const [key, value] of Object.entries(headersOptions)) { + headers.set(key as string, value as string); + } + } + // console.log("Fetching from", url); + // console.log("Headers", headers); + // console.log("headersOptions", headersOptions); + // console.log("options", options); const response = await fetch(url, { headers, ...options }); if (!response.ok) { diff --git a/src/lib/services/book.service.ts b/src/lib/services/book.service.ts index 0cc15b2..74219f5 100644 --- a/src/lib/services/book.service.ts +++ b/src/lib/services/book.service.ts @@ -6,23 +6,16 @@ import { PreferencesService } from "./preferences.service"; export class BookService extends BaseApiService { static async getBook(bookId: string): Promise<{ book: KomgaBook; pages: number[] }> { try { - const config = await this.getKomgaConfig(); - const headers = this.getAuthHeaders(config); - return this.fetchWithCache<{ book: KomgaBook; pages: number[] }>( `book-${bookId}`, async () => { // Récupération des détails du tome - const book = await this.fetchFromApi( - this.buildUrl(config, `books/${bookId}`), - headers - ); + const book = await this.fetchFromApi({ path: `books/${bookId}` }); // Récupération des pages du tome - const pages = await this.fetchFromApi<{ number: number }[]>( - this.buildUrl(config, `books/${bookId}/pages`), - headers - ); + const pages = await this.fetchFromApi<{ number: number }[]>({ + path: `books/${bookId}/pages`, + }); return { book, diff --git a/src/lib/services/home.service.ts b/src/lib/services/home.service.ts index cfcd89a..2da6fc2 100644 --- a/src/lib/services/home.service.ts +++ b/src/lib/services/home.service.ts @@ -12,45 +12,47 @@ interface HomeData { export class HomeService extends BaseApiService { static async getHomeData(): Promise { try { - const config = await this.getKomgaConfig(); - const headers = this.getAuthHeaders(config); - - // Construction des URLs avec des paramètres optimisés - const ongoingUrl = this.buildUrl(config, "series", { - read_status: "IN_PROGRESS", - sort: "readDate,desc", - page: "0", - size: "10", - media_status: "READY", - }); - - const recentlyReadUrl = this.buildUrl(config, "books/latest", { - page: "0", - size: "10", - media_status: "READY", - }); - - const onDeckUrl = this.buildUrl(config, "books/ondeck", { - page: "0", - size: "10", - media_status: "READY", - }); - // Appels API parallèles avec cache individuel const [ongoing, recentlyRead, onDeck] = await Promise.all([ this.fetchWithCache>( "home-ongoing", - async () => this.fetchFromApi>(ongoingUrl, headers), + async () => + this.fetchFromApi>({ + path: "series", + params: { + read_status: "IN_PROGRESS", + sort: "readDate,desc", + page: "0", + size: "10", + media_status: "READY", + }, + }), "HOME" ), this.fetchWithCache>( "home-recently-read", - async () => this.fetchFromApi>(recentlyReadUrl, headers), + async () => + this.fetchFromApi>({ + path: "books/latest", + params: { + page: "0", + size: "10", + media_status: "READY", + }, + }), "HOME" ), this.fetchWithCache>( "home-on-deck", - async () => this.fetchFromApi>(onDeckUrl, headers), + async () => + this.fetchFromApi>({ + path: "books/ondeck", + params: { + page: "0", + size: "10", + media_status: "READY", + }, + }), "HOME" ), ]); diff --git a/src/lib/services/image.service.ts b/src/lib/services/image.service.ts index 9847eff..97f2ba6 100644 --- a/src/lib/services/image.service.ts +++ b/src/lib/services/image.service.ts @@ -8,15 +8,12 @@ interface ImageResponse { export class ImageService extends BaseApiService { static async getImage(path: string): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, path); - const headers = this.getAuthHeaders(config); - headers.set("Accept", "image/jpeg, image/png, image/gif, image/webp, */*"); + const headers = { Accept: "image/jpeg, image/png, image/gif, image/webp, */*" }; return this.fetchWithCache( `image-${path}`, async () => { - const response = await this.fetchFromApi(url, headers, { isImage: true }); + const response = await this.fetchFromApi({ path }, headers, { isImage: true }); const contentType = response.headers.get("content-type"); const arrayBuffer = await response.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); diff --git a/src/lib/services/library.service.ts b/src/lib/services/library.service.ts index 579e393..a59286d 100644 --- a/src/lib/services/library.service.ts +++ b/src/lib/services/library.service.ts @@ -6,13 +6,9 @@ import { serverCacheService } from "./server-cache.service"; export class LibraryService extends BaseApiService { static async getLibraries(): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, "libraries"); - const headers = this.getAuthHeaders(config); - return this.fetchWithCache( "libraries", - async () => this.fetchFromApi(url, headers), + async () => this.fetchFromApi({ path: "libraries" }), "LIBRARIES" ); } catch (error) { @@ -31,12 +27,7 @@ export class LibraryService extends BaseApiService { static async getAllLibrarySeries(libraryId: string): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, "series/list", { - size: "1000", // On récupère un maximum de séries - }); - const headers = this.getAuthHeaders(config); - headers.set("Content-Type", "application/json"); + const headers = { "Content-Type": "application/json" }; const searchBody = { condition: { @@ -51,10 +42,19 @@ export class LibraryService extends BaseApiService { const response = await this.fetchWithCache>( cacheKey, async () => - this.fetchFromApi>(url, headers, { - method: "POST", - body: JSON.stringify(searchBody), - }), + this.fetchFromApi>( + { + path: "series/list", + params: { + size: "1000", // On récupère un maximum de livres + }, + }, + headers, + { + method: "POST", + body: JSON.stringify(searchBody), + } + ), "SERIES" ); @@ -74,7 +74,6 @@ export class LibraryService extends BaseApiService { try { // Récupérer toutes les séries depuis le cache const allSeries = await this.getAllLibrarySeries(libraryId); - // Filtrer les séries let filteredSeries = allSeries; diff --git a/src/lib/services/series.service.ts b/src/lib/services/series.service.ts index f18d403..74c8d65 100644 --- a/src/lib/services/series.service.ts +++ b/src/lib/services/series.service.ts @@ -9,13 +9,9 @@ import { serverCacheService } from "./server-cache.service"; export class SeriesService extends BaseApiService { static async getSeries(seriesId: string): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, `series/${seriesId}`); - const headers = this.getAuthHeaders(config); - return this.fetchWithCache( `series-${seriesId}`, - async () => this.fetchFromApi(url, headers), + async () => this.fetchFromApi({ path: `series/${seriesId}` }), "SERIES" ); } catch (error) { @@ -29,12 +25,7 @@ export class SeriesService extends BaseApiService { static async getAllSeriesBooks(seriesId: string): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, "books/list", { - size: "1000", // On récupère un maximum de livres - }); - const headers = this.getAuthHeaders(config); - headers.set("Content-Type", "application/json"); + const headers = { "Content-Type": "application/json" }; const searchBody = { condition: { @@ -49,10 +40,19 @@ export class SeriesService extends BaseApiService { const response = await this.fetchWithCache>( cacheKey, async () => - this.fetchFromApi>(url, headers, { - method: "POST", - body: JSON.stringify(searchBody), - }), + this.fetchFromApi>( + { + path: "books/list", + params: { + size: "1000", // On récupère un maximum de livres + }, + }, + headers, + { + method: "POST", + body: JSON.stringify(searchBody), + } + ), "BOOKS" ); @@ -131,17 +131,13 @@ export class SeriesService extends BaseApiService { static async getFirstBook(seriesId: string): Promise { try { - const config = await this.getKomgaConfig(); - const url = this.buildUrl(config, `series/${seriesId}/books`); - const headers = this.getAuthHeaders(config); - return this.fetchWithCache( `series-first-book-${seriesId}`, async () => { - const data = await this.fetchFromApi>( - `series/${seriesId}/books?page=0&size=1`, - headers - ); + const data = await this.fetchFromApi>({ + path: `series/${seriesId}/books`, + params: { page: "0", size: "1" }, + }); if (!data.content || data.content.length === 0) { throw new Error("Aucun livre trouvé dans la série"); }