From cca2a960dbee1df4f6c22a784e91db1b0e40feef Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Jan 2024 09:03:22 -0800 Subject: refactor(wrapped): optional authorisation --- src/lib/AniList/wrapped.ts | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'src/lib') diff --git a/src/lib/AniList/wrapped.ts b/src/lib/AniList/wrapped.ts index 3f3e0bf8..abcbe5c7 100644 --- a/src/lib/AniList/wrapped.ts +++ b/src/lib/AniList/wrapped.ts @@ -115,18 +115,26 @@ const profileActivities = async ( }; export const wrapped = async ( - anilistAuthorisation: AniListAuthorisation, + anilistAuthorisation: AniListAuthorisation | undefined, identity: UserIdentity, - year = new Date().getFullYear() + year = new Date().getFullYear(), + skipActivities = false ): Promise => { + const headers: { [key: string]: string } = { + 'Content-Type': 'application/json', + Accept: 'application/json' + }; + + if (anilistAuthorisation) { + headers[ + 'Authorization' + ] = `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`; + } + const wrappedResponse = await ( await fetch('https://graphql.anilist.co', { method: 'POST', - headers: { - Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`, - 'Content-Type': 'application/json', - Accept: 'application/json' - }, + headers, body: JSON.stringify({ query: `{ User(name: "${identity.name}") { @@ -148,17 +156,25 @@ export const wrapped = async ( }) }) ).json(); - const { statusCount, messageCount } = await profileActivities( - anilistAuthorisation, - identity, - new Date(year, 11, 31).getTime() - ); + let statusCountActivities = 0; + let messageCountActivities = 0; + + if (!skipActivities && anilistAuthorisation) { + const { statusCount, messageCount } = await profileActivities( + anilistAuthorisation, + identity, + new Date(year, 11, 31).getTime() + ); + + statusCountActivities = statusCount; + messageCountActivities = messageCount; + } return { statistics: wrappedResponse['data']['User']['statistics'], activities: { - statusCount, - messageCount + statusCount: statusCountActivities, + messageCount: messageCountActivities }, avatar: wrappedResponse['data']['User']['avatar'] }; -- cgit v1.2.3