• Get multiple manga by their IDs.

    Parameters

    • ids: number[]

      Array of AniList manga IDs.

    • Optionaltoken: string

      Optional access token.

    • OptionalabortSignal: AbortSignal

      Optional abort signal to cancel the request.

    Returns Promise<AniListManga[]>

    Promise resolving to an array of AniListManga objects.

    export async function getMangaByIds(
    ids: number[],
    token?: string,
    abortSignal?: AbortSignal,
    ): Promise<AniListManga[]> {
    if (!ids.length) {
    return [];
    }

    try {
    // Updated type parameter to handle potential nested data structure
    const response = await request<{
    data?: { Page: { media: AniListManga[] } };
    Page?: { media: AniListManga[] };
    }>(GET_MANGA_BY_IDS, { ids }, token, abortSignal);

    // Validate response structure
    if (!response || !response.data) {
    console.error(
    `Invalid API response when fetching manga by IDs:`,
    response,
    );
    return [];
    }

    // Check for nested data structure
    const responseData = response.data.data
    ? response.data.data
    : response.data;

    // Safely access media array or return empty array if not found
    return responseData.Page?.media || [];
    } catch (error) {
    console.error(`Error fetching manga by IDs [${ids.join(", ")}]:`, error);
    throw error;
    }
    }