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