import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
import { searchParams } from "https://esm.town/v/stevekrouse/searchParams";
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;
}
};