hn-ts
Version:
TypeScript client for the Hacker News API
340 lines (295 loc) • 8.9 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var fetch = require('isomorphic-unfetch');
var htmlToText = require('html-to-text');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
/** Base URL for all calls to the Hacker News API */
const hnApiBaseUrl = "https://hacker-news.firebaseio.com/v0";
/**
* `getAskStoriesIds` returns the list of up to 200 IDs of the "Ask HN" stories.
*
* @see {@link https://github.com/HackerNews/API#ask-show-and-job-stories}
*/
async function getAskStoriesIds() {
const endpoint = `${hnApiBaseUrl}/askstories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `getBestStoriesIds` returns the list of up to 500 IDs of the best HN stories.
*
* @see {@link https://github.com/HackerNews/API#new-top-and-best-stories}
*/
async function getBestStoriesIds() {
const endpoint = `${hnApiBaseUrl}/beststories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `getRawItemById` returns the item with the given ID as is from the API.
* If the item is not available, `getRawItemById` returns `undefined`.
*
* @see {@link RawItem}
* @see {@link https://github.com/HackerNews/API#items}
*/
async function getRawItemById({
id
}) {
const endpoint = `${hnApiBaseUrl}/item/${id}.json`;
const res = await fetch__default["default"](endpoint);
const rawItemOrNull = await res.json();
return rawItemOrNull != null ? rawItemOrNull : undefined;
}
/** `unixToIsoString` converts a time in Unix seconds to a UTC ISO timestamp */
function unixToIsoString({
time
}) {
return new Date(time * 1000).toISOString();
}
/**
* `normalizeRawItem` transforms a `RawItem` into an `Item`.
*/
function normalizeRawItem({
rawItem
}) {
var _rawItem$type, _rawItem$deleted, _rawItem$dead;
// Make `type` not null
const type = (_rawItem$type = rawItem.type) != null ? _rawItem$type : "unknown"; // Convert Unix time in seconds to UTC timestamp
const timestamp = rawItem.time ? unixToIsoString({
time: rawItem.time
}) : undefined; // Make `deleted` and `dead` not null
const deleted = (_rawItem$deleted = rawItem.deleted) != null ? _rawItem$deleted : false;
const dead = (_rawItem$dead = rawItem.dead) != null ? _rawItem$dead : false; // Convert `title` and `text` from HTML to plain text;
// for example, see from API https://hacker-news.firebaseio.com/v0/item/24753101.json
// and rendered https://news.ycombinator.com/item?id=24753101
const title = rawItem.title ? htmlToText.htmlToText(rawItem.title, {
wordwrap: false
}) : undefined;
const text = rawItem.text ? htmlToText.htmlToText(rawItem.text, {
wordwrap: false
}) : undefined;
return {
id: rawItem.id,
type,
author: rawItem.by,
title,
url: rawItem.url,
text,
timestamp,
score: rawItem.score,
numChildren: rawItem.descendants,
deleted,
dead,
parentId: rawItem.parent,
childrenIds: rawItem.kids,
pollId: rawItem.poll,
pollOptionsIds: rawItem.parts
};
}
/**
* `getItemById` returns the `Item` with the given ID.
* If the item is not available, `getItemById` returns `undefined`.
*
* @example
* ```typescript
* 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 {@link Item}
* @see {@link https://github.com/HackerNews/API#items}
*/
async function getItemById({
id
}) {
const rawItem = await getRawItemById({
id
});
if (!rawItem) {
return undefined;
}
return normalizeRawItem({
rawItem
});
}
/**
* `getJobStoriesIds` returns the list of up to 200 IDs of the job HN stories.
*
* @see {@link https://github.com/HackerNews/API#ask-show-and-job-stories}
*/
async function getJobStoriesIds() {
const endpoint = `${hnApiBaseUrl}/jobstories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `getLatestItemAndUserUpdates` returns the latest updates for items and users.
*
* @see {@link https://github.com/HackerNews/API#changed-items-and-profiles}
*/
async function getLatestItemAndUserUpdates() {
const endpoint = `${hnApiBaseUrl}/updates.json`;
const res = await fetch__default["default"](endpoint);
const updates = await res.json();
return updates;
}
/**
* `getMaxItemId` returns the ID of the most recent item available from the API.
*
* @example
* ```typescript
* import { getMaxItemId } from 'hn-ts';
*
* (async () => {
* const id = await getMaxItemId();
*
* // Output: a number like `27107832`
* console.log(id);
* })();
* ```
*
* @see {@link https://github.com/HackerNews/API#max-item-id}
*/
async function getMaxItemId() {
const endpoint = `${hnApiBaseUrl}/maxitem.json`;
const res = await fetch__default["default"](endpoint);
const id = await res.json();
return id;
}
/**
* `getNewStoriesIds` returns the list of up to 500 IDs of the newer HN stories.
*
* @see {@link https://github.com/HackerNews/API#new-top-and-best-stories}
*/
async function getNewStoriesIds() {
const endpoint = `${hnApiBaseUrl}/newstories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `getRawUserById` returns the user with the given ID as is from the API.
* If the user is not available, `getRawUserById` returns `undefined`.
*
* @see {@link RawUser}
* @see {@link https://github.com/HackerNews/API#users}
*/
async function getRawUserById({
id
}) {
const endpoint = `${hnApiBaseUrl}/user/${id}.json`;
const res = await fetch__default["default"](endpoint);
const rawUserOrNull = await res.json();
return rawUserOrNull != null ? rawUserOrNull : undefined;
}
/**
* `getShowStoriesIds` returns the list of up to 200 IDs of the "Show HN" stories.
*
* @see {@link https://github.com/HackerNews/API#ask-show-and-job-stories}
*/
async function getShowStoriesIds() {
const endpoint = `${hnApiBaseUrl}/showstories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `getTopStoriesIds` returns the list of up to 500 IDs of the top HN stories.
*
* @see {@link https://github.com/HackerNews/API#new-top-and-best-stories}
*/
async function getTopStoriesIds() {
const endpoint = `${hnApiBaseUrl}/topstories.json`;
const res = await fetch__default["default"](endpoint);
const ids = await res.json();
return ids;
}
/**
* `normalizeRawUser` transforms a `RawUser` into a `User`.
*/
function normalizeRawUser({
rawUser
}) {
// Convert Unix time in second to UTC timestamp
const createdAt = unixToIsoString({
time: rawUser.created
}); // Convert `about` from HTML to plain text;
// for example, see from API https://hacker-news.firebaseio.com/v0/user/velut.json
// and rendered https://news.ycombinator.com/user?id=velut
const about = rawUser.about ? htmlToText.htmlToText(rawUser.about, {
wordwrap: false
}) : undefined;
return {
id: rawUser.id,
createdAt,
karma: rawUser.karma,
about,
submittedIds: rawUser.submitted
};
}
/**
* `getUserById` returns the `User` with the given ID.
* If the user is not available, `getUserById` returns `undefined`.
*
* @example
* ```typescript
* import { getUserById } from 'hn-ts';
*
* (async () => {
* const user = await getUserById({
* id: "velut",
* });
*
* // Output: `velut`
* console.log(user.id);
* })();
* ```
*
* @see {@link User}
* @see {@link https://github.com/HackerNews/API#users}
*/
async function getUserById({
id
}) {
const rawUser = await getRawUserById({
id
});
if (!rawUser) {
return undefined;
}
return normalizeRawUser({
rawUser
});
}
exports.getAskStoriesIds = getAskStoriesIds;
exports.getBestStoriesIds = getBestStoriesIds;
exports.getItemById = getItemById;
exports.getJobStoriesIds = getJobStoriesIds;
exports.getLatestItemAndUserUpdates = getLatestItemAndUserUpdates;
exports.getMaxItemId = getMaxItemId;
exports.getNewStoriesIds = getNewStoriesIds;
exports.getRawItemById = getRawItemById;
exports.getRawUserById = getRawUserById;
exports.getShowStoriesIds = getShowStoriesIds;
exports.getTopStoriesIds = getTopStoriesIds;
exports.getUserById = getUserById;
exports.hnApiBaseUrl = hnApiBaseUrl;
//# sourceMappingURL=hn-ts.cjs.development.js.map