import type { AniListAuthorisation, UserIdentity } from './identity'; export interface Wrapped { statistics: { anime: { startYears: { startYear: number; minutesWatched: number; count: number; }[]; }; manga: { startYears: { startYear: number; chaptersRead: number; count: number; }[]; }; }; activities: { statusCount: number; messageCount: number; }; avatar: { large: string; }; } const profileActivities = async (user: AniListAuthorisation, identity: UserIdentity) => { const get = async (page: number) => await ( await fetch('https://graphql.anilist.co', { method: 'POST', headers: { Authorization: `${user.tokenType} ${user.accessToken}`, 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ query: `{ Page(page: ${page}) { activities(userId: ${identity.id}, type_in: [ TEXT, MESSAGE ]) { ... on TextActivity { type } ... on MessageActivity { type } } pageInfo { hasNextPage } } }` }) }) ).json(); const pages = []; let page = 1; let response = await get(page); pages.push(response['data']['Page']['activities']); while (response['data']['Page']['pageInfo']['hasNextPage']) { page += 1; response = await get(page); pages.push(response['data']['Page']['activities']); } return { statusCount: pages.flat().filter((activity) => activity.type == 'TEXT').length, messageCount: pages.flat().filter((activity) => activity.type == 'MESSAGE').length }; }; export const wrapped = async ( anilistAuthorisation: AniListAuthorisation, identity: UserIdentity ): Promise => { const wrappedResponse = 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: "${identity.name}") { avatar { large } statistics { anime { startYears { startYear minutesWatched count } } manga { startYears { startYear chaptersRead count } } } } }` }) }) ).json(); const { statusCount, messageCount } = await profileActivities(anilistAuthorisation, identity); return { statistics: wrappedResponse['data']['User']['statistics'], activities: { statusCount, messageCount }, avatar: wrappedResponse['data']['User']['avatar'] }; };