diff options
| author | Fuwn <[email protected]> | 2024-01-07 09:03:22 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-01-07 09:03:22 -0800 |
| commit | cca2a960dbee1df4f6c22a784e91db1b0e40feef (patch) | |
| tree | 6fcf58daae94c5a107b0be66b4a7ed18c79bf375 /src/lib | |
| parent | refactor(hovercover): simple state (diff) | |
| download | due.moe-cca2a960dbee1df4f6c22a784e91db1b0e40feef.tar.xz due.moe-cca2a960dbee1df4f6c22a784e91db1b0e40feef.zip | |
refactor(wrapped): optional authorisation
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/AniList/wrapped.ts | 44 |
1 files changed, 30 insertions, 14 deletions
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<Wrapped> => { + 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'] }; |