md-y/mangadex-full-api

How to use order field in manga.getFeed?

johnlim5847 opened this issue · 6 comments

Hi, may I know how to use the order field in manga.getFeed?

I've tried

  let chapters = await manga.getFeed({
    translatedLanguage: ["en"],
    order: { chapter: "asc" },
  });

and

  let chapters = await manga.getFeed({
    translatedLanguage: ["en"],
    order: JSON.stringify( { chapter: "asc" }),
  });

But I keep getting APIRequestError: Error validating /order: String value found, but an object is required (400: validation_exception) error. Can anyone give me an example of how to use it? I really appreciate any help you can provide.

You didn't give it a manga id, no?

image

const dex = require("mangadex-full-api");
    await dex.Manga.getFeed("ed996855-70de-449f-bba2-e8e24224c14d", {
      translatedLanguage: ["en"],
    }).then((stuff) => {
      console.log(stuff);
    });

Well if you want to implement query to feed, just pass the manga id.

    await dex.Manga.getByQuery("Onii-chan is done for").then(async (rest) => {
      await dex.Manga.getFeed(rest.id, { translatedLanguage: ["en"] }).then(
        (result) => {
          console.log(result);
        }
      );
    });

hey @Curstantine I was referring to the readme example

// Get a manga:
    let manga = await MFA.Manga.getByQuery('Ancient Magus Bride');

// Get the manga's chapters:
    let chapters = await manga.getFeed({ translatedLanguage: ['en'] });

I'm having trouble understanding how to use the order field as stated.

let chapters = await manga.getFeed({
    translatedLanguage: ["en"],
    order: { chapter: "asc" },
  });

So the code above returning APIRequestError: Error validating /order: String value found, but an object is required (400: validation_exception)

I'm facing another issue with the getCovers() method.

  const list = await MFA.Manga.search({
        limit: 18,
      });
      const mangaList = await Promise.all(
        list.map(async (manga) => {
          return {
            title: manga.localizedTitle.en,
            genre: manga.tags.map((tag) => tag.localizedName.en),
            lastChapter: manga.lastChapter,
            image: await manga.getCovers(),
          };
        })
      );
  res.status(200).json({ result: mangaList });

I'm getting APIRequestError: The API did not respond with an array when it was expected to. I'm able to get the cover image by calling await MFA.Cover.get(manga.mainCover.id) but I'm hitting rate limit :/ may I know what is the best way to get all the cover image of my search result?

md-y commented
  1. @johnlim5847 You can use order like below:
  let chapters = await manga.getFeed({
    translatedLanguage: ['en'],
    'order[chapter]': 'asc' } // Changed
  });

However, this is confusing and I realize the documentation is lacking on this, so I'll update it so that you can use the original method you tired (order: {chapter: 'asc'}) and the related documentation.


  1. I will be adding more shortcut functions like possibly getFeedByQuery() in the next version, but it won't be faster than what @Curstantine wrote since that is already the most optimal.

  1. @johnlim5847 I would recommend getting covers like below:
const list = await MFA.Manga.search({
    limit: 18,
});
const coverList = await MFA.Cover.getMangaCovers(...list); // Changed
const mangaList = await Promise.all(
    list.map(async (manga) => {
        return {
            title: manga.title, // Changed
            genre: manga.tags.map((tag) => tag.name), // Changed
            lastChapter: manga.lastChapter,
            image: coverList.filter((cover) => cover.manga.id === manga.id) // Changed
        };
    })
);

getMangaCovers() uses Cover.search() to retrieve covers for multiple manga in the same request. It is the same as the below code:

const coverList = await MFA.Cover.search({
    manga: list, // Retrieves ALL covers for EVERY manga
    limit: Infinity
});

This is opposed to your original method which called getCovers() multiple times which then called Cover.search() with only one manga in each request. I tested this new implementation and it only required three requests to get all of the covers as opposed to 18 requests.

If you wish to only retrieve the main covers for each manga, you can use the following code:

const coverList = await MFA.Cover.search({
    ids: list.map((manga) => manga.mainCover.id), // Retrieves only the main covers for each manga
    limit: Infinity
});

This block of code can be replaced by a method similar to getMangaCovers(), so I will probably implement that later.

The reason you got APIRequestError: The API did not respond with an array when it was expected to is becuase the rate-limit error page is HTML, not JSON, so it was returned like a valid response which then caused a different error when parsing the Cover search results. I will fix this.

I would also recommend using manga.title and tag.name instead of manga.localizedTitle.en and tag.localizedName.en because it automatically gets the title or name based on the global locale (default english). Although, I don't know the context of your implementation, so you can ignore this last bit.

@md-y Thank you for your thorough explanation!