Cache stars
This commit is contained in:
24
package-lock.json
generated
24
package-lock.json
generated
@@ -9,7 +9,6 @@
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"gsap": "^3.12.5",
|
||||
"ofetch": "^1.4.0",
|
||||
"sveltekit-i18n": "^2.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -2232,11 +2231,6 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/destr": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/destr/-/destr-2.0.3.tgz",
|
||||
"integrity": "sha512-2N3BOUU4gYMpTP24s5rF5iP7BDr7uNTCs4ozw3kf/eKfvWSIu93GEBi5m427YoyJoeOzQ5smuu4nNAPGb8idSQ=="
|
||||
},
|
||||
"node_modules/detect-libc": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz",
|
||||
@@ -3292,11 +3286,6 @@
|
||||
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/node-fetch-native": {
|
||||
"version": "1.6.4",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch-native/-/node-fetch-native-1.6.4.tgz",
|
||||
"integrity": "sha512-IhOigYzAKHd244OC0JIMIUrjzctirCmPkaIfhDeGcEETWof5zKYUW7e7MYvChGWh/4CJeXEgsRyGzuF334rOOQ=="
|
||||
},
|
||||
"node_modules/node-releases": {
|
||||
"version": "2.0.18",
|
||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz",
|
||||
@@ -3339,16 +3328,6 @@
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/ofetch": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/ofetch/-/ofetch-1.4.0.tgz",
|
||||
"integrity": "sha512-MuHgsEhU6zGeX+EMh+8mSMrYTnsqJQQrpM00Q6QHMKNqQ0bKy0B43tk8tL1wg+CnsSTy1kg4Ir2T5Ig6rD+dfQ==",
|
||||
"dependencies": {
|
||||
"destr": "^2.0.3",
|
||||
"node-fetch-native": "^1.6.4",
|
||||
"ufo": "^1.5.4"
|
||||
}
|
||||
},
|
||||
"node_modules/optionator": {
|
||||
"version": "0.9.4",
|
||||
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz",
|
||||
@@ -4547,7 +4526,8 @@
|
||||
"node_modules/ufo": {
|
||||
"version": "1.5.4",
|
||||
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
|
||||
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ=="
|
||||
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/unplugin": {
|
||||
"version": "1.14.1",
|
||||
|
||||
@@ -36,7 +36,6 @@
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"gsap": "^3.12.5",
|
||||
"ofetch": "^1.4.0",
|
||||
"sveltekit-i18n": "^2.4.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
{#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