web/src/utils/fetch-reddit-info.ts

38 lines · 866 bytes

 1
 2export const fetchRedditInfo = async (subreddit: string): Promise<RedditData | null> => {
 3  const endpoint = `https://subreddit-info.as93.net/${subreddit}`;
 4  try {
 5    return await fetch(endpoint).then((res) => res.json());
 6  } catch (error) {
 7    console.error('Error fetching reddit data:', error);
 8    return null;
 9  }
10};
11
12interface SubredditInfo {
13  name: string | null;
14  title: string | null;
15  description: string | null;
16  longDescription: string | null;
17  icon: string | null;
18  banner: string | null;
19  color: string | null;
20  subscribers: number | null;
21  activeSubscribers: number | null;
22  dateCreated: number | null;
23  descriptionHtml: string | null;
24}
25
26interface Post {
27  title: string;
28  body: string;
29  upVotes: number;
30  downVotes: number;
31  date: number;
32  url: string;
33}
34
35export interface RedditData {
36  info: SubredditInfo;
37  posts: Post[];
38}