Files
aniplay-api/src/controllers/title/anilist.ts
2024-05-15 23:03:17 -04:00

33 lines
736 B
TypeScript

import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import type { Title } from "~/types/title";
import { MediaFragment } from "./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);
}