Skip to content

Commit 1c793a5

Browse files
Update +layout.server.ts
1 parent 0daf7d7 commit 1c793a5

File tree

1 file changed

+74
-23
lines changed

1 file changed

+74
-23
lines changed

src/routes/users/+layout.server.ts

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,78 @@ import type { LayoutServerLoad } from '../$types';
33

44
export const prerender = false;
55

6-
export const load:LayoutServerLoad = async (event) => {
7-
const session = await event.locals.auth();
8-
9-
const RandomUsers = Math.floor(Math.random() * 1000000);
10-
11-
if (!session?.user) {
12-
throw redirect(303, '/login');
13-
}
14-
const getRandomUsers = async () => {
15-
const res = await fetch(`https://api.github.com/users?since=${RandomUsers}&per_page=50`, {
16-
headers: {
17-
Accept: 'application/vnd.github+json',
18-
//@ts-ignore
19-
Authorization: `Bearer ${session?.access_token}`,
20-
'X-Github-Api-Version': '2022-11-28'
21-
}
22-
});
23-
24-
return await res.json();
25-
};
26-
return {
27-
RandomUsers: await getRandomUsers()
28-
};
6+
export const load: LayoutServerLoad = async (event) => {
7+
const session = await event.locals.auth();
8+
9+
if (!session?.user) {
10+
throw redirect(303, '/login');
11+
}
12+
13+
// Helper function to fetch popular repositories
14+
const fetchPopularRepositories = async () => {
15+
const res = await fetch(
16+
'https://api.github.com/search/repositories?q=stars:>1000&sort=stars&order=desc',
17+
{
18+
headers: {
19+
Accept: 'application/vnd.github+json',
20+
Authorization: `Bearer ${session.access_token}`,
21+
'X-Github-Api-Version': '2022-11-28',
22+
},
23+
}
24+
);
25+
26+
if (!res.ok) {
27+
throw new Error(`Failed to fetch repositories: ${res.statusText}`);
28+
}
29+
30+
const data = await res.json();
31+
return data.items; // Array of repositories
32+
};
33+
34+
// Helper function to fetch contributors for a repository
35+
const fetchContributors = async (contributorsUrl: string) => {
36+
const res = await fetch(contributorsUrl, {
37+
headers: {
38+
Accept: 'application/vnd.github+json',
39+
Authorization: `Bearer ${session.access_token}`,
40+
'X-Github-Api-Version': '2022-11-28',
41+
},
42+
});
43+
44+
if (!res.ok) {
45+
throw new Error(`Failed to fetch contributors: ${res.statusText}`);
46+
}
47+
48+
return res.json(); // Array of contributors
49+
};
50+
51+
try {
52+
// Fetch popular repositories
53+
const repositories = await fetchPopularRepositories();
54+
55+
if (repositories.length > 0) {
56+
// Select a random repository
57+
const randomRepo = repositories[Math.floor(Math.random() * repositories.length)];
58+
59+
// Fetch contributors for the random repository
60+
const contributors = await fetchContributors(randomRepo.contributors_url);
61+
62+
// Return contributors and repository info
63+
return {
64+
repository: randomRepo,
65+
contributors,
66+
};
67+
}
68+
69+
return {
70+
repository: null,
71+
contributors: [],
72+
};
73+
} catch (error) {
74+
console.error('Error fetching data from GitHub:', error);
75+
return {
76+
repository: null,
77+
contributors: [],
78+
};
79+
}
2980
};

0 commit comments

Comments
 (0)