UNPKG

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