web/src/utils/fetch-reddit-info.ts
46 lines · 1041 bytes
1import { error } from './logger';
2
3export const fetchRedditInfo = async (
4 subreddit: string,
5): Promise<RedditData | null> => {
6 const endpoint = `https://subreddit-info.as93.net/${subreddit}`;
7 try {
8 const res = await fetch(endpoint);
9 if (!res.ok) {
10 error('Reddit', `HTTP ${res.status} for r/${subreddit} (${endpoint})`);
11 return null;
12 }
13 return await res.json();
14 } catch (err) {
15 error('Reddit', `Network error for r/${subreddit}: ${err}`);
16 return null;
17 }
18};
19
20interface SubredditInfo {
21 name: string | null;
22 title: string | null;
23 description: string | null;
24 longDescription: string | null;
25 icon: string | null;
26 banner: string | null;
27 color: string | null;
28 subscribers: number | null;
29 activeSubscribers: number | null;
30 dateCreated: number | null;
31 descriptionHtml: string | null;
32}
33
34interface Post {
35 title: string;
36 body: string;
37 upVotes: number;
38 downVotes: number;
39 date: number;
40 url: string;
41}
42
43export interface RedditData {
44 info: SubredditInfo;
45 posts: Post[];
46}