aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/AniList/follow.ts
blob: 6aec8b9a3f90a9150ce987964edaf6aadd1ad107 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { AniListAuthorisation } from "./identity";

export interface User {
	id: number;
	name: string;
	isFollowing: boolean;
	isFollower: boolean;
	avatar: {
		large: string;
		medium: 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"];
};