Star count
This commit is contained in:
24
package-lock.json
generated
24
package-lock.json
generated
@@ -9,6 +9,7 @@
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"gsap": "^3.12.5",
|
||||
"ofetch": "^1.4.0",
|
||||
"sveltekit-i18n": "^2.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -2231,6 +2232,11 @@
|
||||
"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",
|
||||
@@ -3286,6 +3292,11 @@
|
||||
"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",
|
||||
@@ -3328,6 +3339,16 @@
|
||||
"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",
|
||||
@@ -4526,8 +4547,7 @@
|
||||
"node_modules/ufo": {
|
||||
"version": "1.5.4",
|
||||
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
|
||||
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ=="
|
||||
},
|
||||
"node_modules/unplugin": {
|
||||
"version": "1.14.1",
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"gsap": "^3.12.5",
|
||||
"ofetch": "^1.4.0",
|
||||
"sveltekit-i18n": "^2.4.2"
|
||||
}
|
||||
}
|
||||
|
||||
37
src/lib/getStars.ts
Normal file
37
src/lib/getStars.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { ofetch } from "ofetch";
|
||||
|
||||
export default async function getStarsForUser(username: string, auth?: string) {
|
||||
const userResponse = await request(`/users/${username}`, auth);
|
||||
if (!userResponse || !userResponse.public_repos) {
|
||||
throw new Error(userResponse ? userResponse.message : 'User not found.');
|
||||
}
|
||||
|
||||
const pages = Math.ceil(userResponse.public_repos / 100);
|
||||
let repos = [];
|
||||
const fetchReposPromises = [];
|
||||
|
||||
for (let i = 1; i <= pages; i++) {
|
||||
fetchReposPromises.push(request(`/users/${username}/repos?per_page=100&page=${i}`, auth));
|
||||
}
|
||||
|
||||
const reposResponses = await Promise.all(fetchReposPromises);
|
||||
repos = reposResponses.flat();
|
||||
|
||||
return starSum(repos);
|
||||
}
|
||||
|
||||
async function request(url, auth) {
|
||||
const headers = {
|
||||
'User-Agent': 'GitHub StarCounter',
|
||||
};
|
||||
|
||||
if (auth) {
|
||||
headers.Authorization = `Basic ${Buffer.from(auth).toString('base64')}`;
|
||||
}
|
||||
|
||||
return ofetch(`https://api.github.com${url}`, { headers });
|
||||
}
|
||||
|
||||
async function starSum(repos) {
|
||||
return repos.reduce((acc, curr) => acc + curr.stargazers_count, 0);
|
||||
}
|
||||
8
src/routes/+page.server.ts
Normal file
8
src/routes/+page.server.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type {PageServerLoad} from "./$types";
|
||||
import getStars from "$lib/getStars";
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
return {
|
||||
starCount: await getStars('anatolykopyl')
|
||||
};
|
||||
}
|
||||
@@ -4,11 +4,13 @@
|
||||
import About from "./About.svelte";
|
||||
import Projects from "./Projects/Projects.svelte";
|
||||
import Footer from "./Footer.svelte";
|
||||
|
||||
export let data;
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<Navbar></Navbar>
|
||||
<Hero></Hero>
|
||||
<Hero starCount={data.starCount}></Hero>
|
||||
<About></About>
|
||||
<Projects></Projects>
|
||||
<Footer></Footer>
|
||||
|
||||
@@ -6,6 +6,7 @@ import Telegram from "$lib/icons/Telegram.svelte";
|
||||
import Linkedin from "$lib/icons/Linkedin.svelte";
|
||||
import MaterialSymbolsStarRounded from '~icons/material-symbols/star-rounded';
|
||||
|
||||
export let starCount: number;
|
||||
let logo: HTMLElement;
|
||||
|
||||
onMount(async () => {
|
||||
@@ -73,7 +74,7 @@ const goTop = () => {
|
||||
>
|
||||
<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">
|
||||
123
|
||||
{starCount}
|
||||
<MaterialSymbolsStarRounded class="max-h-4 max-w-4" />
|
||||
</div>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user