-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstaticData.ts
More file actions
54 lines (48 loc) · 1.56 KB
/
staticData.ts
File metadata and controls
54 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { TPayloadCoursesDataStatic, TPayloadCoursesDataDynamic } from '@/lib/types';
const DATA_REPO_BASE =
'https://raw.githubusercontent.com/omshub/data/main/static';
async function fetchStaticData<T>(filename: string): Promise<T> {
const url = `${DATA_REPO_BASE}/${filename}`;
const res = await fetch(url, { cache: 'no-store' });
if (!res.ok) {
throw new Error(`Failed to fetch ${filename}: ${res.status}`);
}
return res.json();
}
/**
* Fetch static course data (names, aliases, etc.) from data repo
*/
export async function getCoursesDataStatic(): Promise<TPayloadCoursesDataStatic> {
return fetchStaticData<TPayloadCoursesDataStatic>('courses.json');
}
/**
* Fetch dynamic course stats (avgWorkload, avgDifficulty, etc.) from data repo
*/
export async function getCourseStats(): Promise<TPayloadCoursesDataDynamic> {
try {
return await fetchStaticData<TPayloadCoursesDataDynamic>('course-stats.json');
} catch (error) {
// Return empty object if stats file doesn't exist yet
console.warn('Could not fetch course stats:', error);
return {} as TPayloadCoursesDataDynamic;
}
}
export interface GlobalStats {
hoursSuffered: number;
semesterWeeks: {
spring: number;
fall: number;
summer: number;
};
}
/**
* Fetch global stats (hoursSuffered, semester weeks) from data repo
*/
export async function getGlobalStats(): Promise<GlobalStats | null> {
try {
return await fetchStaticData<GlobalStats>('global-stats.json');
} catch (error) {
console.warn('Could not fetch global stats:', error);
return null;
}
}