net

Overview

The net object exposes methods for retrieving data from the Web and performing arbitrary HTTP requests.
Under the hood, requests are made using the node-fetch polyfill of the Fetch API.
DOM parsing relies on JSDOM, an implementation of DOM/HTML for Node.

Methods and properties

Performing requests

Promise<String> net.getText(String url, [Object options])

Queries a URL and returns the response body as a string, as a Promise.
Rejects if the query fails.
url
The URL to query.
options
Custom options passed to fetch().
sample Get a string from a random number API
const trulyRandomNumber = await net.getText('https://www.random.org/cgi-bin/randbyte?nbytes=1&format=d');
cli.tell(trulyRandomNumber);

Promise<Any> net.getJSON(String url, [Object options])

Queries a URL and returns the response body parsed as JSON, as a Promise.
Rejects if the query fails, or if the body cannot be parsed as JSON.
url
The URL to query.
options
Custom options passed to fetch().
sample Get the release date of an NPM package
const tasklemonNpmDetails = await net.getJSON('https://registry.npmjs.org/tasklemon');
const lastReleaseDate = tasklemonNpmDetails.time.modified;

cli.tell('Last Tasklemon release was ' + format.date.relative(lastReleaseDate) + '.');

Promise<JSDOM> net.getDOM(String url, [Object options])

Queries a URL and returns the response body as a JSDOM object, as a Promise.
Rejects if the query fails, or if the body cannot be parsed as HTML.
url
The URL to query.
options
Custom options passed to fetch().
sample Query isitweekendyet.com
const weekendDOM = await net.getDOM('http://isitweekendyet.com');
const weekendEstimation = weekendDOM.querySelector('div').textContent.trim();

cli.tell('Is it the weekend yet? ' + weekendEstimation);

Promise<Response> net.fetch(String|Request input, [Object init])

Performs an arbitrary request by making a Fetch call.
input
The URL to query or request to perform.
init
Fetch options.
sample Check the response status of a URL
const response = await net.fetch(await cli.ask('URL to check?'));
cli.tell('URL is ' + response.ok ? 'OK' : 'not OK');