diff options
Diffstat (limited to 'index.ts')
| -rw-r--r-- | index.ts | 21 |
1 files changed, 20 insertions, 1 deletions
@@ -27,6 +27,15 @@ window.onload = function() { } } +const cache = { + added: 0, + value: {} as QueryResponse, + set(data: QueryResponse) { + cache.value = data; + cache.added = +new Date(); + } +} + async function getRepositories(): Promise<Repository[] | undefined> { const response = await fetch(API_URL, { method: "POST", @@ -52,7 +61,17 @@ async function getRepositories(): Promise<Repository[] | undefined> { }) if (response.ok) { - const json = await response.json() as QueryResponse; + let json + + if (cache.added < (+new Date() - 3.6E6)) { // cached for 1 hour + json = await response.json() as QueryResponse; + cache.set(json); + console.log("from api"); + } else { + json = cache.value; + console.log("from cache"); + } + return json?.data.repositories.results?.filter(repo => repo.visibility === "PUBLIC") ?? []; } else { throw response; |
