justreddit
Version:
A simple package for getting random posts, random images, random SubReddits, and SubReddit info.
14 lines (13 loc) • 615 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fetch;
async function fetch(path, prefix = "r") {
path = `${prefix}/${path}.json`;
const response = await fetch(`https://www.reddit.com/${path}`);
if (!response.ok || response.status !== 200)
throw new Error(`Could not fetch data from path (${path}): ${response.statusText}`);
const body = await response.json();
if ("error" in body && "message" in body)
throw new Error(`Could not fetch data from path (${path}) with '${body.message}' and error '${body.error}'`);
return body;
}