UNPKG

813 BPlain TextView Raw
1import { getRawUserById } from "./get-raw-user-by-id";
2import { normalizeRawUser } from "./normalize-raw-user";
3import { User } from "./user";
4
5/**
6 * `getUserById` returns the `User` with the given ID.
7 * If the user is not available, `getUserById` returns `undefined`.
8 *
9 * @example
10 * ```typescript
11 * import { getUserById } from 'hn-ts';
12 *
13 * (async () => {
14 * const user = await getUserById({
15 * id: "velut",
16 * });
17 *
18 * // Output: `velut`
19 * console.log(user.id);
20 * })();
21 * ```
22 *
23 * @see {@link User}
24 * @see {@link https://github.com/HackerNews/API#users}
25 */
26export async function getUserById({
27 id,
28}: {
29 id: string;
30}): Promise<User | undefined> {
31 const rawUser = await getRawUserById({ id });
32 if (!rawUser) {
33 return undefined;
34 }
35
36 return normalizeRawUser({ rawUser });
37}