UNPKG

649 BPlain TextView Raw
1import fetch from "isomorphic-unfetch";
2import { hnApiBaseUrl } from "./hn-api-base-url";
3
4/**
5 * `getMaxItemId` returns the ID of the most recent item available from the API.
6 *
7 * @example
8 * ```typescript
9 * import { getMaxItemId } from 'hn-ts';
10 *
11 * (async () => {
12 * const id = await getMaxItemId();
13 *
14 * // Output: a number like `27107832`
15 * console.log(id);
16 * })();
17 * ```
18 *
19 * @see {@link https://github.com/HackerNews/API#max-item-id}
20 */
21export async function getMaxItemId(): Promise<number> {
22 const endpoint = `${hnApiBaseUrl}/maxitem.json`;
23 const res = await fetch(endpoint);
24 const id = await res.json();
25 return id;
26}