Skip to content
Source
Sign inSign up
samwillis/hackernews-realtime
Open History to see the last commit

types.ts

36 lines1.0 KB

1export type Feed = "top" | "new";
2export const PAGE_SIZE = 30;
3export const FEED_SIZE = 150;
4export interface Story {
5 item_id: number;
6 rank: number;
7 title: string;
8 url: string | null;
9 author: string | null;
10 created: number;
11 score: number;
12 comments: number;
13 type: string;
14 committed_at: string;
15 body?: string | null;
17export interface SourceHealth {
18 state: "starting" | "healthy" | "reconnecting" | "degraded" | "stopped";
19 checkedAt: string | null;
20 committedAt: string | null;
22export function sortStories(rows: readonly Story[]): Story[] {
23 return [...rows].sort((a, b) => a.rank - b.rank || a.item_id - b.item_id);
25export function parseFeedPage(
26 feed?: string | null,
27 page?: string | null,
28): { feed: Feed; page: number } {
29 if (feed && feed !== "top" && feed !== "new") throw new Error("Invalid feed");
30 if (page && !/^[1-5]$/.test(page))
31 throw new Error("Page must be between 1 and 5");
32 return {
33 feed: feed === "new" ? "new" : "top",
34 page: page ? Number(page) : 1,
35 };