feat: return user info when authenticating

This commit is contained in:
2024-09-21 14:02:07 -04:00
parent 755ae4b94f
commit 3dba56cb45
2 changed files with 28 additions and 10 deletions

View File

@@ -0,0 +1,39 @@
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)
.catch((err) => {
if (err.response?.status === 401 || err.response?.status === 429) {
return null;
}
throw err;
});
}