This is a Node-specific transport for heya-io based on built-in http and https modules. The main purpose of the module is to provide an ergonomic simple light-weight HTTP I/O on Node leveraging existing customization facilities of heya-io where appropriate.
Following heya-io services are supported as is out of the box:
io.track — tracks I/O requests to eliminate duplicates, register an interest without initiating I/O requests, and much more.
io.mock — mocks responses to I/O requests without writing a special server courtesy of Mike Wilcox. Very useful for rapid prototyping and writing tests.
io.bust — a simple pseudo plugin to generate a randomized query value to bust cache.
io.retry — a flexible way to retry requests, e.g., to deal with unreliable servers or to watch for changing values.
Additionally it supports:
Completely transparent compression/decompression.
gzip and deflate are supported out of the box with no extra dependencies using built-in modules.
More compressions can be easily plugged in.
brotli is automatically supported if an underlying Node has it.
The compression is supported both ways.
Streaming.
Both streaming a server request and a server response are supported.
Streaming (since 1.1.0) + encoding a payload (since 1.2.0):
const ios = require('heya-io-node/stream');fs.createReadStream('sample.json') .pipe(ios.post({ url: 'https://example.com/analyze', headers: { 'Content-Type': 'application/json', '$-Content-Encoding': 'gzip', 'Accept: plain/text' } })) .pipe(process.stdout);// or it can be done more granularly:io.post({ url: 'https://example.com/analyze', headers: { 'Content-Type': 'application/json', '$-Content-Encoding': 'gzip', 'Accept: plain/text'
Mock in action:
// set up a mock handlerio.mock('/a*', (options, prep) => { console.log('Got call: ' + options.method + ' ' + prep.url); return 42;});// let's make a callio.get('/a/x').then(value => { console.log(value); // 42});// set up a redirect /b => /a/bio.mock('/b', options => io.get('/a/b', options.query || options.data || null));// let's make another call
Using url template to sanitize URLs (ES6):
const url = require('heya-io/url');const client = 'Bob & Jordan & Co';io.get(url`/api/${client}/details`).then(value => { // GET /api/Bob%20%26%20Jordan%20%26%20Co/details console.log(value);});