aboutsummaryrefslogtreecommitdiff
path: root/lib/anilist/aniAdvanceSearch.js
blob: 263ca9dbe1025bfa1205d9f7429bca82d17a4db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const advance = `
  query ($search: String, $type: MediaType, $status: MediaStatus, $season: MediaSeason, $seasonYear: Int, $genres: [String], $tags: [String], $sort: [MediaSort], $page: Int, $perPage: Int) {
    Page (page: $page, perPage: $perPage) {
      pageInfo {
        total
        currentPage
        lastPage
        hasNextPage
      }
      media (search: $search, type: $type, status: $status, season: $season, seasonYear: $seasonYear, genre_in: $genres, tag_in: $tags, sort: $sort, isAdult: false) {
        id
        title {
            userPreferred
        }
        type
        episodes
        chapters
        status
        format
        season
        seasonYear
        coverImage {
          extraLarge
          color
        }
        averageScore
        isAdult
      }
    }
  }
`;

export async function aniAdvanceSearch(options = {}) {
  const {
    search = null,
    type = "ANIME",
    seasonYear = NaN,
    season = undefined,
    genres = null,
    page = 1,
    perPage = null,
    sort = "POPULARITY_DESC",
  } = options;
  // console.log(page);
  const response = await fetch("https://graphql.anilist.co/", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      query: advance,
      variables: {
        search: search,
        type: type,
        seasonYear: seasonYear,
        season: season,
        genres: genres,
        perPage: perPage,
        sort: sort,
        page: page,
      },
    }),
  });

  const datas = await response.json();
  // console.log(datas);
  const data = datas.data.Page;
  return data;
}