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 => { 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"]; };