1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { fetch } from "https://esm.town/v/std/fetch";
export let // View this in a web browser!
// https://api3.val.town/express/async%20(req,%20res)%20=%3Eres.send(@healeycodes.hnTopStories)
const hnTopStories = (async () => {
let valTownFetchLimit = 100;
const topStories: Number[] = await fetch(
"https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
).then((res) => res.json());
valTownFetchLimit--;
const topStoriesData: Promise<{
title: string;
url: string;
}>[] = topStories.slice(0, valTownFetchLimit).map(async (id) => {
return await fetch(
`https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`
).then((res) => res.json());
});
return `
<ul>
${(await Promise.all(topStoriesData))
.map((story) => {
return `<li><a href="${story.url}">${story.title}</a></li>`;
})
.join("\n")}
</ul>
`;
})();