hn-ts
- Version 0.1.0
- Published
- 107 kB
- 2 dependencies
- MIT license
Install
npm i hn-tsyarn add hn-tspnpm add hn-tsOverview
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
Functions
Interfaces
Type Aliases
Variables
variable hnApiBaseUrl
const hnApiBaseUrl: string;Base URL for all calls to the Hacker News API
Functions
function getAskStoriesIds
getAskStoriesIds: () => Promise<number[]>;getAskStoriesIdsreturns the list of up to 200 IDs of the "Ask HN" stories.See Also
function getBestStoriesIds
getBestStoriesIds: () => Promise<number[]>;getBestStoriesIdsreturns the list of up to 500 IDs of the best HN stories.See Also
function getItemById
getItemById: ({ id }: { id: number }) => Promise<Item | undefined>;getItemByIdreturns theItemwith the given ID. If the item is not available,getItemByIdreturnsundefined.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[]>;getJobStoriesIdsreturns the list of up to 200 IDs of the job HN stories.See Also
function getLatestItemAndUserUpdates
getLatestItemAndUserUpdates: () => Promise<Updated>;getLatestItemAndUserUpdatesreturns the latest updates for items and users.See Also
function getMaxItemId
getMaxItemId: () => Promise<number>;getMaxItemIdreturns 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[]>;getNewStoriesIdsreturns the list of up to 500 IDs of the newer HN stories.See Also
function getRawItemById
getRawItemById: ({ id }: { id: number }) => Promise<RawItem | undefined>;getRawItemByIdreturns the item with the given ID as is from the API. If the item is not available,getRawItemByIdreturnsundefined.See Also
function getRawUserById
getRawUserById: ({ id }: { id: string }) => Promise<RawUser | undefined>;getRawUserByIdreturns the user with the given ID as is from the API. If the user is not available,getRawUserByIdreturnsundefined.See Also
function getShowStoriesIds
getShowStoriesIds: () => Promise<number[]>;getShowStoriesIdsreturns the list of up to 200 IDs of the "Show HN" stories.See Also
function getTopStoriesIds
getTopStoriesIds: () => Promise<number[]>;getTopStoriesIdsreturns the list of up to 500 IDs of the top HN stories.See Also
function getUserById
getUserById: ({ id }: { id: string }) => Promise<User | undefined>;getUserByIdreturns theUserwith the given ID. If the user is not available,getUserByIdreturnsundefined.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 {}Itemrepresents an HN item.Remarks
An
Itemdiffers from aRawItemas some of its properties are renamed and their values cleaned up.See Also
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
trueif the item is dead
property deleted
readonly deleted: boolean;Set to
trueif 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,polloptorunknown)
property url
readonly url?: string;URL for a story or job
interface RawItem
interface RawItem {}RawItemrepresents an item fetched from the HN API as is.See Also
property by
readonly by?: string;User who submitted the item
property dead
readonly dead?: boolean;Set to
trueif the item is dead
property deleted
readonly deleted?: boolean;Set to
trueif 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, orpollopt)
property url
readonly url?: string;URL for a story or job
interface RawUser
interface RawUser {}RawUserrepresents a user fetched from the HN API as is.See Also
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 {}Updatedcontains the latest changes for items and user profiles.See Also
interface User
interface User {}Userrepresents a user on HN.Remarks
A
Userdiffers from aRawUseras some of its properties are renamed and their values cleaned up.See Also
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';ItemTypelists the possible item types (job,story,comment,poll, orpollopt) withunknownindicating that a type was not given by the API.
type RawItemType
type RawItemType = 'job' | 'story' | 'comment' | 'poll' | 'pollopt';RawItemTypelists the possible item types (job,story,comment,poll, orpollopt)
Package Files (21)
- src/get-ask-stories-ids.ts
- src/get-best-stories-ids.ts
- src/get-item-by-id.ts
- src/get-job-stories-ids.ts
- src/get-latest-item-and-user-updates.ts
- src/get-max-item-id.ts
- src/get-new-stories-ids.ts
- src/get-raw-item-by-id.ts
- src/get-raw-user-by-id.ts
- src/get-show-stories-ids.ts
- src/get-top-stories-ids.ts
- src/get-user-by-id.ts
- src/hn-api-base-url.ts
- src/index.ts
- src/item-type.ts
- src/item.ts
- src/raw-item-type.ts
- src/raw-item.ts
- src/raw-user.ts
- src/updated.ts
- src/user.ts
Dependencies (2)
Dev Dependencies (21)
- @commitlint/cli
- @commitlint/config-conventional
- @pollyjs/adapter-node-http
- @pollyjs/core
- @pollyjs/persister-fs
- @size-limit/preset-small-lib
- @tsconfig/recommended
- @types/html-to-text
- @types/pollyjs__adapter-node-http
- @types/pollyjs__persister-fs
- @types/setup-polly-jest
- dts-cli
- eslint-plugin-prettier
- husky
- nock
- np
- prettier
- setup-polly-jest
- size-limit
- tslib
- typescript
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto 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[](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>
- Updated .
Package analyzed in 1474 ms. - Missing or incorrect documentation? Open an issue for this package.
