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",
|
"version": "0.0.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"gsap": "^3.12.5",
|
"gsap": "^3.12.5",
|
||||||
"ofetch": "^1.4.0",
|
|
||||||
"sveltekit-i18n": "^2.4.2"
|
"sveltekit-i18n": "^2.4.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -2232,11 +2231,6 @@
|
|||||||
"node": ">=0.10.0"
|
"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": {
|
"node_modules/detect-libc": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz",
|
||||||
@@ -3292,11 +3286,6 @@
|
|||||||
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
|
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
|
||||||
"dev": true
|
"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": {
|
"node_modules/node-releases": {
|
||||||
"version": "2.0.18",
|
"version": "2.0.18",
|
||||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz",
|
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz",
|
||||||
@@ -3339,16 +3328,6 @@
|
|||||||
"node": ">= 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": {
|
"node_modules/optionator": {
|
||||||
"version": "0.9.4",
|
"version": "0.9.4",
|
||||||
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz",
|
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz",
|
||||||
@@ -4547,7 +4526,8 @@
|
|||||||
"node_modules/ufo": {
|
"node_modules/ufo": {
|
||||||
"version": "1.5.4",
|
"version": "1.5.4",
|
||||||
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
|
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
|
||||||
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ=="
|
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ==",
|
||||||
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/unplugin": {
|
"node_modules/unplugin": {
|
||||||
"version": "1.14.1",
|
"version": "1.14.1",
|
||||||
|
|||||||
@@ -36,7 +36,6 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"gsap": "^3.12.5",
|
"gsap": "^3.12.5",
|
||||||
"ofetch": "^1.4.0",
|
|
||||||
"sveltekit-i18n": "^2.4.2"
|
"sveltekit-i18n": "^2.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { ofetch } from "ofetch";
|
export default async function getStarsForUser(username: string) {
|
||||||
|
const userResponse = await request(`/users/${username}`);
|
||||||
export default async function getStarsForUser(username: string, auth?: string) {
|
|
||||||
const userResponse = await request(`/users/${username}`, auth);
|
|
||||||
if (!userResponse || !userResponse.public_repos) {
|
if (!userResponse || !userResponse.public_repos) {
|
||||||
throw new Error(userResponse ? userResponse.message : 'User not found.');
|
throw new Error(userResponse ? userResponse.message : 'User not found.');
|
||||||
}
|
}
|
||||||
@@ -11,7 +9,7 @@ export default async function getStarsForUser(username: string, auth?: string) {
|
|||||||
const fetchReposPromises = [];
|
const fetchReposPromises = [];
|
||||||
|
|
||||||
for (let i = 1; i <= pages; i++) {
|
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);
|
const reposResponses = await Promise.all(fetchReposPromises);
|
||||||
@@ -20,16 +18,14 @@ export default async function getStarsForUser(username: string, auth?: string) {
|
|||||||
return starSum(repos);
|
return starSum(repos);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function request(url, auth) {
|
async function request(url) {
|
||||||
const headers = {
|
const headers= {
|
||||||
'User-Agent': 'GitHub StarCounter',
|
'User-Agent': 'GitHub StarCounter',
|
||||||
|
// Authorization: `Basic ${Buffer.from(auth).toString('base64')}`
|
||||||
};
|
};
|
||||||
|
|
||||||
if (auth) {
|
const response = await fetch(`https://api.github.com${url}`, { headers });
|
||||||
headers.Authorization = `Basic ${Buffer.from(auth).toString('base64')}`;
|
return response.json()
|
||||||
}
|
|
||||||
|
|
||||||
return ofetch(`https://api.github.com${url}`, { headers });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function starSum(repos) {
|
async function starSum(repos) {
|
||||||
|
|||||||
@@ -1,8 +1,23 @@
|
|||||||
import type {PageServerLoad} from "./$types";
|
import type {PageServerLoad} from "./$types";
|
||||||
import getStars from "$lib/getStars";
|
import getStars from "$lib/getStars";
|
||||||
|
|
||||||
|
let lastRequest = Date.now()
|
||||||
|
let starCount: number
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
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 {
|
return {
|
||||||
starCount: await getStars('anatolykopyl')
|
starCount,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,10 +73,12 @@ const goTop = () => {
|
|||||||
href="https://github.com/anatolykopyl"
|
href="https://github.com/anatolykopyl"
|
||||||
>
|
>
|
||||||
<Github></Github>
|
<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">
|
{#if starCount}
|
||||||
{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">
|
||||||
<MaterialSymbolsStarRounded class="max-h-4 max-w-4" />
|
{starCount}
|
||||||
</div>
|
<MaterialSymbolsStarRounded class="max-h-4 max-w-4" />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</a>
|
</a>
|
||||||
<a
|
<a
|
||||||
class="hero__social-icon"
|
class="hero__social-icon"
|
||||||
|
|||||||
Reference in New Issue
Block a user