aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/AniList/follow.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-02-08 00:01:24 -0800
committerFuwn <[email protected]>2024-02-08 00:01:24 -0800
commitf78f5f4857f24ee5338fb1643c666a6b18d75769 (patch)
tree57b1b09f20b6b261a3b1ae15bfa441965f71ecd9 /src/lib/Data/AniList/follow.ts
parentrefactor(data): move static data to module (diff)
downloaddue.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.tar.xz
due.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.zip
refactor(anilist): move to data module
Diffstat (limited to 'src/lib/Data/AniList/follow.ts')
-rw-r--r--src/lib/Data/AniList/follow.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/Data/AniList/follow.ts b/src/lib/Data/AniList/follow.ts
new file mode 100644
index 00000000..3601d510
--- /dev/null
+++ b/src/lib/Data/AniList/follow.ts
@@ -0,0 +1,48 @@
+import type { AniListAuthorisation } from './identity';
+
+export interface User {
+ id: number;
+ name: string;
+ isFollowing: boolean;
+ isFollower: boolean;
+ avatar: {
+ large: string;
+ };
+}
+
+export const toggleFollow = async (
+ anilistAuthorisation: AniListAuthorisation,
+ username: string
+): Promise<User> => {
+ const {
+ data: { User: user }
+ } = await (
+ await fetch('https://graphql.anilist.co', {
+ method: 'POST',
+ headers: {
+ Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`,
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ },
+ body: JSON.stringify({
+ query: `{ User(name: "${username}") { id } }`
+ })
+ })
+ ).json();
+
+ return (
+ await (
+ await fetch('https://graphql.anilist.co', {
+ method: 'POST',
+ headers: {
+ Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`,
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ },
+ body: JSON.stringify({
+ mutation: `{ ToggleFollow(userId: ${user.id}) { id name isFollowing isFollower } }`
+ })
+ })
+ ).json()
+ )['data']['ToggleFollow'];
+};