UNPKG

931 BPlain TextView Raw
1import { getRawItemById } from "./get-raw-item-by-id";
2import { Item } from "./item";
3import { normalizeRawItem } from "./normalize-raw-item";
4
5/**
6 * `getItemById` returns the `Item` with the given ID.
7 * If the item is not available, `getItemById` returns `undefined`.
8 *
9 * @example
10 * ```typescript
11 * import { getItemById } from 'hn-ts';
12 *
13 * (async () => {
14 * const item = await getItemById({
15 * id: 27107832,
16 * });
17 *
18 * // Output: `27107832`
19 * console.log(item.id);
20 *
21 * // Output: `story`
22 * console.log(item.type);
23 *
24 * // Output: `velut`
25 * console.log(item.author);
26 * })();
27 * ```
28 *
29 * @see {@link Item}
30 * @see {@link https://github.com/HackerNews/API#items}
31 */
32export async function getItemById({
33 id,
34}: {
35 id: number;
36}): Promise<Item | undefined> {
37 const rawItem = await getRawItemById({ id });
38 if (!rawItem) {
39 return undefined;
40 }
41
42 return normalizeRawItem({ rawItem });
43}