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