Socket
Socket
Sign inDemoInstall

@jamsch/hacker-news-client

Package Overview
Dependencies
9
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @jamsch/hacker-news-client

A universal API client for Hacker News with Typescript typings


Version published
Maintainers
1
Created

Readme

Source

@jamsch/hacker-news-client

A simple API wrapper for Hacker News with Typescript typings that supports browsers & Node.js.

Installation

npm install @jamsch/hacker-news-client

Usage

import Client from '@jamsch/hacker-news-client';

// Fetch the top 500 story IDs
const topStories = await Client.getTopStories();

// Latest stories, by any type
const newStories = await Client.getNewStories();

// Latest "Ask HN" stories
const askStories = await Client.getAskStories();

// Latest "Show HN" stories
const showStories = await Client.getShowStories();

// Latest "Jobs" stories
const jobStories = await Client.getJobStories();

// Fetch a single story
const story = await Client.getItem(topStories[0]));

// Fetch the user's info
const user = await Client.getUser(story.by);

// Fetch the first 5 stories in parallel. 
// Note: this makes one request per item provided, and the order of the results is not guaranteed.
const topFiveStories = await Client.getItems(topStories.slice(0,5));

Typescript Usage

import Client, { Story, Ask, Comment, Job, Poll, PollOpt } from '@jamsch/hacker-news-client';

// Fetch an Item
const item = await Client.getItem(1); // "Item" type

console.log(item); // Item (Story | Comment | Ask | Job | Poll | PollOpt)

// Check what kind of item it is
if (Client.is(item, 'story')) {
  console.log(item); // Story
} else if (Client.is(item, 'ask')) {
  console.log(item); // Ask
}

// If you know the item ID belongs to a specific item type:
const storyId = 123;
const storyItem = await Client.getItem<Story>(storyId);
console.log(storyItem); // Story

const storyIds = [1,2,3,4];
const topFiveStories = await Client.getItems<Story>(storyIds);
console.log(topFiveStories); // Story[]

// Alternatively, use a switch statement to figure out the type
switch (item.type) {
  case 'story': {
    console.log('Ask | Story:', item); // Ask | Story
    if ('text' in item) {
      console.log('Ask:', item); // Ask
    }
    if ('url' in item) {
      console.log('Story:', item); // Story
    }
    break;
  }
  case 'comment':
    console.log('Comment:', item); // Comment
    break;
  case 'job':
    console.log('Job:', item); // Job
    break;
  case 'poll':
    console.log('Poll:', item); // Poll
    break;
  case 'pollopt':
    console.log('Polopt', item); // PolOpt
    break;
}

Usage in UMD builds

<script src="{path_to_package}/umd/hacker-news-client.production.min.js"></script>

<script>
  HackerNewsClient.getTopStories().then(topStories => {
    console.log(topStories);
  });
</script>

FAQs

Last updated on 28 Aug 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc