Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
import { searchParams } from "https://esm.town/v/stevekrouse/searchParams";
/* Search Hacker News via Agolia
Look for the valid params: https://hn.algolia.com/api */
export let hnSearch = async ({
search_by_date,
...params
}: {
search_by_date?: boolean,
query?: string,
tags?: string,
numericFilters?: string,
page?: number,
}) => {
let type = search_by_date ? "search_by_date" : "search";
let cleaned_params = await searchParams(params);
let data = await fetchJSON(
`http://hn.algolia.com/api/v1/${type}?${cleaned_params}`
);
if (!data.hits && data.message) {
throw new Error("Error in @stevekrouse.hnSearch: " + data.message);
} else {
return data;
}
};
October 23, 2023