feat: create route to search for titles
Summary: Test Plan:
This commit is contained in:
43
src/controllers/search/anilist.ts
Normal file
43
src/controllers/search/anilist.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
const SearchQuery = graphql(`
|
||||
query Search($query: String!, $page: Int!) {
|
||||
Page(page: $page) {
|
||||
media(search: $query, type: ANIME, sort: [POPULARITY_DESC, SCORE_DESC]) {
|
||||
id
|
||||
title {
|
||||
userPreferred
|
||||
english
|
||||
}
|
||||
coverImage {
|
||||
extraLarge
|
||||
large
|
||||
medium
|
||||
}
|
||||
}
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
export async function fetchSearchResultsFromAnilist(
|
||||
query: string,
|
||||
page: number,
|
||||
) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
|
||||
return client
|
||||
.request(SearchQuery, { page, query })
|
||||
.then((data) => data?.Page)
|
||||
.then((page) => {
|
||||
if (!page || page.media?.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { media: results, pageInfo } = page;
|
||||
return { results, hasNextPage: pageInfo?.hasNextPage };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user