Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hackernews-js

willowusu237MIT0.1.4TypeScript support: included

Tiny, fast Hacker News API wrapper with batching, hydration, and updates.

hackernews, api, wrapper, typescript

readme

📦 hackernews-js

npm version Build Status License: MIT

A lightweight JavaScript/TypeScript wrapper for the Hacker News API. Provides typed methods to fetch stories, items, users, and more — with built-in caching.


🚀 Features

  • ✅ Simple wrapper around the official Hacker News API
  • ✅ TypeScript support (fully typed responses)
  • ✅ Built-in LRU cache for better performance
  • ✅ ESM & CJS builds
  • ✅ Tested with Vitest

📥 Installation

npm install hackernews-js

or

yarn add hackernews-js

🛠 Usage

import { HackerNewsClient } from "hackernews-js";

const hn = new HackerNewsClient({ cacheSize: 100 });

// Get top stories
const topStories = await hn.getTopStories();
console.log(topStories.slice(0, 5));

// Get a single item
const item = await hn.getItem(8863);
console.log(item.title);

// Get a user
const user = await hn.getUser("pg");
console.log(user.karma);

Note:


Fetch Environments

This library works in both frontend and backend environments, but you may need to configure fetch depending on where you run it:

Browser / React / Vue / Svelte

Explicitly bind the browser’s window.fetch:

import { HackerNewsClient } from "hackernews-js";

const client = new HackerNewsClient({
  fetch: window.fetch.bind(window),
});
`

Node.js (v18+)

Node 18+ ships with a global fetch, so no extra setup is needed:

import { HackerNewsClient } from "hackernews-js";

const client = new HackerNewsClient();

Older Node (<18)

You’ll need to bring your own fetch implementation (e.g. undici):

import { fetch } from "undici";
import { HackerNewsClient } from "hackernews-js";

const client = new HackerNewsClient({ fetch });

📚 API

new HackerNewsClient(options?)

Create a new client.

  • options.cacheSize (number, default: 100) – max entries in LRU cache

Methods

  • getItem(id: number)Promise<Item>
  • getUser(id: string)Promise<User>
  • getTopStories()Promise<number[]>
  • getNewStories()Promise<number[]>
  • getBestStories()Promise<number[]>
  • getAskStories()Promise<number[]>
  • getShowStories()Promise<number[]>
  • getJobStories()Promise<number[]>

🧪 Testing

Run the test suite with:

npm test

📦 Build

npm run build

This outputs bundled files to dist/ in both ESM and CJS formats.


📜 License

MIT © 2025 William Owusu