feat: create script to initialize "next episode" queue
This commit is contained in:
38
src/libs/anilist/getTitle.ts
Normal file
38
src/libs/anilist/getTitle.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
import type { Title } from "~/types/title";
|
||||
import { MediaFragment } from "~/types/title/mediaFragment";
|
||||
|
||||
const GetTitleQuery = graphql(
|
||||
`
|
||||
query GetTitle($id: Int!) {
|
||||
Media(id: $id) {
|
||||
...Media
|
||||
}
|
||||
}
|
||||
`,
|
||||
[MediaFragment],
|
||||
);
|
||||
|
||||
export async function fetchTitleFromAnilist(
|
||||
id: number,
|
||||
token?: string | undefined,
|
||||
): Promise<Title | undefined> {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
const headers = new Headers();
|
||||
if (token) {
|
||||
headers.append("Authorization", `Bearer ${token}`);
|
||||
}
|
||||
|
||||
return client
|
||||
.request(GetTitleQuery, { id }, headers)
|
||||
.then((data) => data?.Media ?? undefined)
|
||||
.catch((error) => {
|
||||
if (error.message.includes("Not Found")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user