gm4ebnr3j: Add three-column layout with featured experts and trending minnects rails
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// Browser-side reader for the same-origin GET /api/discover route.
|
||||
import type { DiscoverData } from "./discover";
|
||||
|
||||
export class DiscoverLoadError extends Error {
|
||||
constructor() {
|
||||
super("discover_unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
const arr = <T>(v: unknown): T[] => (Array.isArray(v) ? (v as T[]) : []);
|
||||
|
||||
export async function fetchDiscover(
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
): Promise<DiscoverData> {
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetchImpl("/api/discover", { credentials: "same-origin" });
|
||||
} catch {
|
||||
throw new DiscoverLoadError();
|
||||
}
|
||||
if (!res.ok) throw new DiscoverLoadError();
|
||||
try {
|
||||
const body = (await res.json()) as Record<string, unknown>;
|
||||
return {
|
||||
featuredExperts: arr(body.featuredExperts),
|
||||
trendingMinnects: arr(body.trendingMinnects),
|
||||
categories: arr(body.categories),
|
||||
};
|
||||
} catch {
|
||||
throw new DiscoverLoadError();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user