From f983eac367e3be139d5ce25deba8860ba07867b5 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Fri, 9 Dec 2022 21:55:12 +0100 Subject: [PATCH] anilist typescript + improvements --- src/lib/util/{anilist.js => anilist.ts} | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) rename src/lib/util/{anilist.js => anilist.ts} (77%) diff --git a/src/lib/util/anilist.js b/src/lib/util/anilist.ts similarity index 77% rename from src/lib/util/anilist.js rename to src/lib/util/anilist.ts index 051f2c7..2711f33 100644 --- a/src/lib/util/anilist.js +++ b/src/lib/util/anilist.ts @@ -1,26 +1,30 @@ +var isLogedInCache: boolean | null = null; +var isLogedInCacheTime: number | null = null; export function isLogedIn() { if(typeof window === "undefined") return; + if(isLogedInCache !== null && Date.now() - isLogedInCacheTime! < 10) return isLogedInCache; const token = localStorage.getItem("token"); - const expiration = new Date(localStorage.getItem("expiration")); + const expiration = new Date(localStorage.getItem("expiration")!); - if(!token) return false; + isLogedInCacheTime = Date.now(); + if(!token) return isLogedInCache = false; if(expiration.getTime() < Date.now()) { localStorage.removeItem("token"); localStorage.removeItem("expiration"); - return false; + return isLogedInCache = false; } - return true; + return isLogedInCache = true; } export function getUserID() { - const token = localStorage.getItem("token"); + const token = localStorage.getItem("token")!; let data = JSON.parse(atob(token.substring(token.indexOf(".") + 1, token.lastIndexOf(".")))); return data.sub; } export function makeRequest(query, variables) { - let auth = {}; + let auth: any = {}; if(isLogedIn()) { auth.Authorization = "Bearer " + localStorage.getItem("token"); } @@ -38,9 +42,11 @@ export function makeRequest(query, variables) { }).then(data => data.json()); } +let detailsCache: null | { id: string, data: Promise } = null; export function getUserDetails() { const id = getUserID(); - return makeRequest(` + if(detailsCache && detailsCache.id === id) return detailsCache.data; + let data = makeRequest(` query ($id: Int) { User(id: $id) { name @@ -60,6 +66,8 @@ export function getUserDetails() { } } }`, { id }); + detailsCache = { id, data }; + return data; } export function getUserManga() {