aboutsummaryrefslogtreecommitdiff
path: root/lib/anilist/aniAdvanceSearch.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/anilist/aniAdvanceSearch.js')
-rw-r--r--lib/anilist/aniAdvanceSearch.js92
1 files changed, 41 insertions, 51 deletions
diff --git a/lib/anilist/aniAdvanceSearch.js b/lib/anilist/aniAdvanceSearch.js
index 263ca9d..02a5c53 100644
--- a/lib/anilist/aniAdvanceSearch.js
+++ b/lib/anilist/aniAdvanceSearch.js
@@ -1,63 +1,53 @@
-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
- }
+import { advanceSearchQuery } from "../graphql/query";
+
+export async function aniAdvanceSearch({
+ search,
+ type,
+ genres,
+ page,
+ sort,
+ format,
+ season,
+ seasonYear,
+ perPage,
+}) {
+ const categorizedGenres = genres?.reduce((result, item) => {
+ const existingEntry = result[item.type];
+
+ if (existingEntry) {
+ existingEntry.push(item.value);
+ } else {
+ result[item.type] = [item.value];
}
- }
-`;
-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);
+ return result;
+ }, {});
+
const response = await fetch("https://graphql.anilist.co/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
- query: advance,
+ query: advanceSearchQuery,
variables: {
- search: search,
- type: type,
- seasonYear: seasonYear,
- season: season,
- genres: genres,
- perPage: perPage,
- sort: sort,
- page: page,
+ ...(search && {
+ search: search,
+ ...(!sort && { sort: "SEARCH_MATCH" }),
+ }),
+ ...(type && { type: type }),
+ ...(seasonYear && { seasonYear: seasonYear }),
+ ...(season && {
+ season: season,
+ ...(!seasonYear && { seasonYear: new Date().getFullYear() }),
+ }),
+ ...(categorizedGenres && { ...categorizedGenres }),
+ ...(format && { format: format }),
+ // ...(genres && { genres: genres }),
+ // ...(tags && { tags: tags }),
+ ...(perPage && { perPage: perPage }),
+ ...(sort && { sort: sort }),
+ ...(page && { page: page }),
},
}),
});