diff options
Diffstat (limited to 'src/lib/Data/AniList/identity.ts')
| -rw-r--r-- | src/lib/Data/AniList/identity.ts | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/src/lib/Data/AniList/identity.ts b/src/lib/Data/AniList/identity.ts index 23f47a2e..973e1184 100644 --- a/src/lib/Data/AniList/identity.ts +++ b/src/lib/Data/AniList/identity.ts @@ -1,34 +1,50 @@ export interface UserIdentity { - id: number; - name: string; - avatar: string; + id: number; + name: string; + avatar: string; } export interface AniListAuthorisation { - tokenType: string; - accessToken: string; - expiresIn: number; - refreshToken: string; + tokenType: string; + accessToken: string; + expiresIn: number; + refreshToken: string; } export const userIdentity = async ( - anilistAuthorisation: AniListAuthorisation + anilistAuthorisation: AniListAuthorisation, ): Promise<UserIdentity> => { - const userIdResponse = 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: `{ Viewer { id name avatar { large } } }` }) - }) - ).json(); + const userIdResponse = 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: `{ Viewer { id name avatar { large } } }`, + }), + }) + ).json(); - return { - id: userIdResponse['data']['Viewer']['id'], - name: userIdResponse['data']['Viewer']['name'], - avatar: userIdResponse['data']['Viewer']['avatar']['large'] - }; + return { + id: userIdResponse["data"]["Viewer"]["id"], + name: userIdResponse["data"]["Viewer"]["name"], + avatar: userIdResponse["data"]["Viewer"]["avatar"]["large"], + }; +}; + +export const safeUserIdentity = async ( + anilistAuthorisation: AniListAuthorisation, +): Promise<UserIdentity | null> => { + try { + const identity = await userIdentity(anilistAuthorisation); + + if (!identity.id || !identity.name || !identity.avatar) return null; + + return identity; + } catch { + return null; + } }; |