Files
aniplay-api/src/controllers/auth/anilist/getUser.ts

43 lines
857 B
TypeScript

import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
const GetUserQuery = graphql(`
query GetUser {
Viewer {
name
avatar {
medium
large
}
statistics {
anime {
minutesWatched
episodesWatched
count
meanScore
}
}
}
}
`);
export function getUser(aniListToken: string) {
const client = new GraphQLClient("https://graphql.anilist.co/");
return client
.request(GetUserQuery, undefined, {
Authorization: `Bearer ${aniListToken}`,
})
.then((data) => ({
...data?.Viewer,
statistics: { ...data?.Viewer?.statistics?.anime },
}))
.catch((err) => {
if (err.response?.status === 401 || err.response?.status === 429) {
return null;
}
throw err;
});
}