From 4162328fd43d651c49ed5ee2aa8f6079fd9550f8 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 28 Sep 2019 22:15:22 +0200 Subject: [PATCH] createClient(): request parameter -> opt parameter :boom: --- index.js | 10 +++++++++- retry.js | 9 +++++++-- test/retry.js | 4 +++- test/throttle.js | 3 +-- throttle.js | 10 ++++++++-- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 351eb012..3dfb1177 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,11 @@ const validateLocation = (loc, name = 'location') => { } } -const createClient = (profile, userAgent, request = _request) => { +const defaults = { + request: _request +} + +const createClient = (profile, userAgent, opt = {}) => { profile = Object.assign({}, defaultProfile, profile) if (!profile.parseProducts) { profile.parseProducts = createParseBitmask(profile) @@ -40,6 +44,10 @@ const createClient = (profile, userAgent, request = _request) => { throw new TypeError('userAgent must be a string'); } + const { + request + } = Object.assign({}, defaults, opt) + const _stationBoard = (station, type, parser, opt = {}) => { if (isObj(station)) station = profile.formatStation(station.id) else if ('string' === typeof station) station = profile.formatStation(station) diff --git a/retry.js b/retry.js index e05fac31..cafe160b 100644 --- a/retry.js +++ b/retry.js @@ -13,7 +13,9 @@ const retryDefaults = { const withRetrying = (createClient, retryOpts = {}) => { retryOpts = Object.assign({}, retryDefaults, retryOpts) - const createRetryingClient = (profile, userAgent, request = _request) => { + const createRetryingClient = (profile, userAgent, opt = {}) => { + const request = 'request' in opt ? opt.request : _request + const retryingRequest = (profile, userAgent, opt, data) => { const attempt = () => { return request(profile, userAgent, opt, data) @@ -30,7 +32,10 @@ const withRetrying = (createClient, retryOpts = {}) => { return retry(attempt, retryOpts) } - return createClient(profile, userAgent, retryingRequest) + return createClient(profile, userAgent, { + ...opt, + request: retryingRequest + }) } return createRetryingClient } diff --git a/test/retry.js b/test/retry.js index 5618c81c..86ce4b02 100644 --- a/test/retry.js +++ b/test/retry.js @@ -34,7 +34,9 @@ test('withRetrying works', (t) => { factor: 2, randomize: false }) - const client = createRetryingClient(vbbProfile, userAgent, failingRequest) + const client = createRetryingClient(vbbProfile, userAgent, { + request: failingRequest + }) t.plan(1 + 4) client.departures(spichernstr, {duration: 1}) diff --git a/test/throttle.js b/test/throttle.js index 9a9dbbc9..64a31737 100644 --- a/test/throttle.js +++ b/test/throttle.js @@ -9,6 +9,7 @@ const vbbProfile = require('../p/vbb') const userAgent = 'public-transport/hafas-client:test' const spichernstr = '900000042101' +// todo: mock request() test('withThrottling works', (t) => { let calls = 0 const transformReqBody = (body) => { @@ -26,5 +27,3 @@ test('withThrottling works', (t) => { setTimeout(() => t.equal(calls, 4), 1500) setTimeout(() => t.equal(calls, 6), 2500) }) - -// todo diff --git a/throttle.js b/throttle.js index c868986f..b003011a 100644 --- a/throttle.js +++ b/throttle.js @@ -5,9 +5,15 @@ const throttle = require('p-throttle') const _request = require('./lib/request') const withThrottling = (createClient, limit = 5, interval = 1000) => { - const createThrottledClient = (profile, userAgent, request = _request) => { + const createThrottledClient = (profile, userAgent, opt = {}) => { + const request = 'request' in opt ? opt.request : _request + const throttledRequest = throttle(request, limit, interval) - return createClient(profile, userAgent, throttledRequest) + + return createClient(profile, userAgent, { + ...opt, + request: throttledRequest + }) } return createThrottledClient }