Minimal dependency http(s) library for node & modern browsers with:
- exponential backoff/retry (on status code: 429, 502, 503, 504)
- custom error parsing, define how to extract a message
- callback API
- support for native Promises
- support for bound Promise
- cancellation
- JSON by default
- less than
7kb
(minified) when bundled for the browser
Browser support: IE9+, with browserify/webpack Node support: v4.4+
const request = require('honeybee')
let cancel = request({url}, function (err, res) {
if (err) console.warn(err.statusCode, err.stack)
else console.log(res)
})
// Immediately abort the XHR or the node http request
// The callback will not be invoked
cancel()
import {withPromise as request} from 'honeybee'
request({url})
.then(res => console.log(res))
.catch(err => console.warn(err.statusCode, err.stack))
const Promise = require('bluebird')
const request = require('honeybee').withBindings(Promise)
request({url})
.then(res => console.log(res))
.catch(err => console.warn(err.statusCode, err.stack))
.cancel()
// or withBindings(Promise, defaults)
const request = require('honeybee').withBindings({
low: 500,
high: 2000,
total: 3,
parseError: parseMyErrorFormat
})
request({url})
{string} opt.url
{string} opt.method
GET, PUT, PATCH, POST, DELETE{object} opt.headers
e.g. Content-Type, Request-Id{object} opt.body
Will be processed byopt.serialize
{object} opt.query
Will be serialized as the url query string{number} opt.low
(default: 200) Initial retry delay in milliseconds{number} opt.high
(default: 1000) Maximum retry delay between attempts{number} opt.total
(default: 5) Number of connection attempts before giving up{function|string} opt.serialize(req)
Should convertreq.body
to a string or Buffer for transport and setreq.headers['content-type']
as appropriate- "json" => "application/json" (default)
- "form" => "application/x-www-form-urlencoded"
- "noop"
{function|string} opt.parseResponse(req, res) => Error|any
res.body is a string (browser) or a Buffer (node). Called with 2xx response codes- "json" => Object (default)
- "raw" => Buffer (node) or string (browser)
{function|string} opt.parseError(req, res) => Error
called with non-2xx response codes- "json"
- "text"
{bool} opt.withCredentials
Enable cross-origin cookies{function} opt.onProgress(percent)
0-50: upload, 50-100: download
{AuthorizationAgent} opt.auth
{number} opt.timeout
(default: 60e3) Total time before giving up{number} opt.maxRedirects
(default: 5){bool} opt.gzip
Compress the request body{http.Agent} opt.conn
Configure your own connection agent
class AuthorizationAgent {
/**
* Called (once) in the event of a 401 statusCode
* Use this to fetch a new access token for .toHeader()
* Supports both the node-callback style as well as a Promise
* @function refresh(opt, done)
* @param {object} req Current request options, including headers
* @param {function} done(err)
* @return {function|Promise} abort()
*/
/**
* @function toHeader()
* @return {string|null} The "Authorization" header
* @example => "Bearer 718b3f..."
*/
}
import request, {Error as RequestError, parseJSON} from 'honeybee'
// Get a Google OAuth2 access token
request({
method: 'POST',
uri: 'https://www.googleapis.com/oauth2/v3/token',
// Encode the request as
serialize: 'form',
body: {
refresh_token: refreshToken,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'refresh_token'
},
parseError: function parseGoogError(req, res) {
// Every API is different so we need to to some extra work
// to get a useful Error out of the response
var payload = parseJSON(res.body)
var message = payload && payload.error ? payload.error.message
? payload.error.message
: payload.error
: 'invalid JSON response'
return new RequestError(res.statusCode, message, res.constructor)
}
}, function (err, creds) {
// The response is JSON encoded and may be handled by the default parser
if (err) console.warn(err.stack)
else console.log('Authorization: Bearer ' + creds.access_token)
})
// Custom serializer & response parser
request({
method: 'POST',
url: 'http://localhost',
body: {foo: 1, bar: 2},
headers: {
'content-type': 'text/plain;charset=utf-8',
'accept': 'text/plain;charset=utf-8'
},
serialize: (req) => { req.body }
parseResponse: (res) => res.body,
parseError: (res) => new RequestError(res.statusCode, res.body)
})
Create a standalone UMD bundle (which exports to window.honeybee
as a last resort):
$ browserify -s honeybee . | uglifyjs -cm >honeybee-$(git describe --abbrev=0 --match 'v[0-9]*' --tags).min.js