1 | import fetch from "isomorphic-unfetch";
|
2 | import { hnApiBaseUrl } from "./hn-api-base-url";
|
3 | import { RawItem } from "./raw-item";
|
4 |
|
5 | /**
|
6 | * `getRawItemById` returns the item with the given ID as is from the API.
|
7 | * If the item is not available, `getRawItemById` returns `undefined`.
|
8 | *
|
9 | * @see {@link RawItem}
|
10 | * @see {@link https://github.com/HackerNews/API#items}
|
11 | */
|
12 | export async function getRawItemById({
|
13 | id,
|
14 | }: {
|
15 | id: number;
|
16 | }): Promise<RawItem | undefined> {
|
17 | const endpoint = `${hnApiBaseUrl}/item/${id}.json`;
|
18 | const res = await fetch(endpoint);
|
19 | const rawItemOrNull = await res.json();
|
20 | return rawItemOrNull ?? undefined;
|
21 | }
|