Skip to content

Commit c2d29b5

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

File tree

1 file changed

+19
-69
lines changed

1 file changed

+19
-69
lines changed

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

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,30 @@
1-
import { redirect } from '@sveltejs/kit';
2-
import type { LayoutServerLoad } from '../$types';
1+
import { redirect } from '@sveltejs/kit'; import type { LayoutServerLoad } from '../$types';
32

43
export const prerender = false;
54

6-
export const load: LayoutServerLoad = async (event) => {
7-
const session = await event.locals.auth();
5+
export const load:LayoutServerLoad = async (event) => { const session = await event.locals.auth();
6+
7+
const RandomUsers = Math.floor(Math.random() * 1000000);
88

99
if (!session?.user) {
10-
throw redirect(303, '/login');
10+
throw redirect(303, '/login');
1111
}
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
12+
const getRandomUsers = async () => {
13+
const res = await fetch(`https://api.github.com/users?since=${RandomUsers}&per_page=50`, {
14+
headers: {
15+
Accept: 'application/vnd.github+json',
16+
//@ts-ignore
17+
Authorization: `Bearer ${session?.access_token}`,
18+
'X-Github-Api-Version': '2022-11-28'
19+
}
20+
});
21+
22+
return await res.json();
3223
};
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
24+
return {
25+
RandomUsers: await getRandomUsers()
4926
};
5027

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);
28+
};
6129

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-
}
8030
};

0 commit comments

Comments
 (0)