Cache stars
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { ofetch } from "ofetch";
|
||||
|
||||
export default async function getStarsForUser(username: string, auth?: string) {
|
||||
const userResponse = await request(`/users/${username}`, auth);
|
||||
export default async function getStarsForUser(username: string) {
|
||||
const userResponse = await request(`/users/${username}`);
|
||||
if (!userResponse || !userResponse.public_repos) {
|
||||
throw new Error(userResponse ? userResponse.message : 'User not found.');
|
||||
}
|
||||
@@ -11,7 +9,7 @@ export default async function getStarsForUser(username: string, auth?: string) {
|
||||
const fetchReposPromises = [];
|
||||
|
||||
for (let i = 1; i <= pages; i++) {
|
||||
fetchReposPromises.push(request(`/users/${username}/repos?per_page=100&page=${i}`, auth));
|
||||
fetchReposPromises.push(request(`/users/${username}/repos?per_page=100&page=${i}`));
|
||||
}
|
||||
|
||||
const reposResponses = await Promise.all(fetchReposPromises);
|
||||
@@ -20,16 +18,14 @@ export default async function getStarsForUser(username: string, auth?: string) {
|
||||
return starSum(repos);
|
||||
}
|
||||
|
||||
async function request(url, auth) {
|
||||
const headers = {
|
||||
async function request(url) {
|
||||
const headers= {
|
||||
'User-Agent': 'GitHub StarCounter',
|
||||
// Authorization: `Basic ${Buffer.from(auth).toString('base64')}`
|
||||
};
|
||||
|
||||
if (auth) {
|
||||
headers.Authorization = `Basic ${Buffer.from(auth).toString('base64')}`;
|
||||
}
|
||||
|
||||
return ofetch(`https://api.github.com${url}`, { headers });
|
||||
const response = await fetch(`https://api.github.com${url}`, { headers });
|
||||
return response.json()
|
||||
}
|
||||
|
||||
async function starSum(repos) {
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
import type {PageServerLoad} from "./$types";
|
||||
import getStars from "$lib/getStars";
|
||||
|
||||
let lastRequest = Date.now()
|
||||
let starCount: number
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const now = Date.now()
|
||||
const CACHE_LIFE = 1000 * 60 * 5
|
||||
|
||||
if (now - CACHE_LIFE > lastRequest || starCount === undefined) {
|
||||
lastRequest = Date.now()
|
||||
try {
|
||||
starCount = await getStars('anatolykopyl')
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
starCount: await getStars('anatolykopyl')
|
||||
starCount,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -73,10 +73,12 @@ const goTop = () => {
|
||||
href="https://github.com/anatolykopyl"
|
||||
>
|
||||
<Github></Github>
|
||||
<div class="rounded-lg text-sm border border-slate-200 border-opacity-50 px-2 bg-slate-500 bg-opacity-25 absolute bottom-0 w-max translate-y-full mb-1 flex gap-1 items-center">
|
||||
{starCount}
|
||||
<MaterialSymbolsStarRounded class="max-h-4 max-w-4" />
|
||||
</div>
|
||||
{#if starCount}
|
||||
<div class="rounded-lg text-sm border border-slate-200 border-opacity-50 px-2 bg-slate-500 bg-opacity-25 absolute bottom-0 w-max translate-y-full mb-1 flex gap-1 items-center">
|
||||
{starCount}
|
||||
<MaterialSymbolsStarRounded class="max-h-4 max-w-4" />
|
||||
</div>
|
||||
{/if}
|
||||
</a>
|
||||
<a
|
||||
class="hero__social-icon"
|
||||
|
||||
Reference in New Issue
Block a user