hn-ts

  • Version 0.1.0
  • Published
  • 107 kB
  • 2 dependencies
  • MIT license

Install

npm i hn-ts
yarn add hn-ts
pnpm add hn-ts

Overview

This package provides a TypeScript client for the Hacker News API.

Remarks

Example 1

Get the ID of the latest item created on HN:

import { getMaxItemId } from 'hn-ts';
(async () => {
const id = await getMaxItemId();
// Output: a number like `27107832`
console.log(id);
})();

Example 2

Get the item (story, comment, job, poll or poll option) with the given ID:

import { getItemById } from 'hn-ts';
(async () => {
const item = await getItemById({
id: 27107832,
});
// Output: `27107832`
console.log(item.id);
// Output: `story`
console.log(item.type);
// Output: `velut`
console.log(item.author);
})();

Example 3

Get the user with the given name:

import { getUserById } from 'hn-ts';
(async () => {
const user = await getUserById({
id: "velut",
});
// Output: `velut`
console.log(user.id);
})();

See Also

Index

Variables

variable hnApiBaseUrl

const hnApiBaseUrl: string;
  • Base URL for all calls to the Hacker News API

Functions

function getAskStoriesIds

getAskStoriesIds: () => Promise<number[]>;

function getBestStoriesIds

getBestStoriesIds: () => Promise<number[]>;

function getItemById

getItemById: ({ id }: { id: number }) => Promise<Item | undefined>;
  • getItemById returns the Item with the given ID. If the item is not available, getItemById returns undefined.

    Example 1

    import { getItemById } from 'hn-ts';
    (async () => {
    const item = await getItemById({
    id: 27107832,
    });
    // Output: `27107832`
    console.log(item.id);
    // Output: `story`
    console.log(item.type);
    // Output: `velut`
    console.log(item.author);
    })();

    See Also

function getJobStoriesIds

getJobStoriesIds: () => Promise<number[]>;

function getLatestItemAndUserUpdates

getLatestItemAndUserUpdates: () => Promise<Updated>;

function getMaxItemId

getMaxItemId: () => Promise<number>;
  • getMaxItemId returns the ID of the most recent item available from the API.

    Example 1

    import { getMaxItemId } from 'hn-ts';
    (async () => {
    const id = await getMaxItemId();
    // Output: a number like `27107832`
    console.log(id);
    })();

    See Also

function getNewStoriesIds

getNewStoriesIds: () => Promise<number[]>;

function getRawItemById

getRawItemById: ({ id }: { id: number }) => Promise<RawItem | undefined>;

function getRawUserById

getRawUserById: ({ id }: { id: string }) => Promise<RawUser | undefined>;

function getShowStoriesIds

getShowStoriesIds: () => Promise<number[]>;

function getTopStoriesIds

getTopStoriesIds: () => Promise<number[]>;

function getUserById

getUserById: ({ id }: { id: string }) => Promise<User | undefined>;
  • getUserById returns the User with the given ID. If the user is not available, getUserById returns undefined.

    Example 1

    import { getUserById } from 'hn-ts';
    (async () => {
    const user = await getUserById({
    id: "velut",
    });
    // Output: `velut`
    console.log(user.id);
    })();

    See Also

Interfaces

interface Item

interface Item {}

property author

readonly author?: string;
  • User who submitted the item

property childrenIds

readonly childrenIds?: number[];
  • List of IDs of the item's comments, in ranked display order

property dead

readonly dead: boolean;
  • Set to true if the item is dead

property deleted

readonly deleted: boolean;
  • Set to true if the item is deleted

property id

readonly id: number;
  • Unique integer item ID

property numChildren

readonly numChildren?: number;
  • Number of total comments for a story or poll

property parentId

readonly parentId?: number;
  • ID of the parent item of a comment (a story or another comment)

property pollId

readonly pollId?: number;
  • ID of the poll associated to a poll option

property pollOptionsIds

readonly pollOptionsIds?: number[];
  • List of IDs of related poll options, in display order

property score

readonly score?: number;
  • Score for a story, job or poll; votes for a poll option

property text

readonly text?: string;
  • Text for a story, comment, poll, poll option or job

property timestamp

readonly timestamp?: string;
  • UTC timestamp in ISO 8601 format for when the item was created (for example, 2021-10-02T18:12:10.149Z)

property title

readonly title?: string;
  • Title text for a story, poll or job

property type

readonly type: ItemType;
  • Item type (job, story, comment, poll, pollopt or unknown)

property url

readonly url?: string;
  • URL for a story or job

interface RawItem

interface RawItem {}

property by

readonly by?: string;
  • User who submitted the item

property dead

readonly dead?: boolean;
  • Set to true if the item is dead

property deleted

readonly deleted?: boolean;
  • Set to true if the item is deleted

property descendants

readonly descendants?: number;
  • Number of total comments for a story or poll

property id

readonly id: number;
  • Unique integer item ID; this is the only required property

property kids

readonly kids?: number[];
  • List of IDs of the item's comments, in ranked display order

property parent

readonly parent?: number;
  • ID of the parent item of a comment (a story or another comment)

property parts

readonly parts?: number[];
  • List of IDs of related poll options, in display order

property poll

readonly poll?: number;
  • ID of the poll associated to a poll option

property score

readonly score?: number;
  • Score for a story, job or poll; votes for a poll option

property text

readonly text?: string;
  • Text for a story, comment, poll, poll option or job; may include HTML

property time

readonly time?: number;
  • Unix timestamp in seconds for when the item was created

property title

readonly title?: string;
  • Title text for a story, poll or job; may include HTML

property type

readonly type?: RawItemType;
  • Item type (job, story, comment, poll, or pollopt)

property url

readonly url?: string;
  • URL for a story or job

interface RawUser

interface RawUser {}

property about

readonly about?: string;
  • User's profile description; may contain HTML

property created

readonly created: number;
  • Unix timestamp in seconds for when the user was created

property id

readonly id: string;
  • Unique, case-sensitive username

property karma

readonly karma: number;
  • User's total karma

property submitted

readonly submitted?: number[];
  • List of IDs for the user's submissions

interface Updated

interface Updated {}

property items

readonly items: number[];
  • List of IDs for the updated items

property profiles

readonly profiles: string[];
  • List of IDs for the updated user profiles

interface User

interface User {}

property about

readonly about?: string;
  • User's profile description

property createdAt

readonly createdAt: string;
  • UTC timestamp in ISO 8601 format for when the user was created (for example, 2021-10-02T18:12:10.149Z)

property id

readonly id: string;
  • Unique, case-sensitive username

property karma

readonly karma: number;
  • User's total karma

property submittedIds

readonly submittedIds?: number[];
  • List of IDs for the user's submissions

Type Aliases

type ItemType

type ItemType = 'job' | 'story' | 'comment' | 'poll' | 'pollopt' | 'unknown';
  • ItemType lists the possible item types (job, story, comment, poll, or pollopt) with unknown indicating that a type was not given by the API.

type RawItemType

type RawItemType = 'job' | 'story' | 'comment' | 'poll' | 'pollopt';
  • RawItemType lists the possible item types (job, story, comment, poll, or pollopt)

Package Files (21)

Dependencies (2)

Dev Dependencies (21)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/hn-ts.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/hn-ts)
  • HTML
    <a href="https://www.jsdocs.io/package/hn-ts"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>