diff --git a/docs/readme.md b/docs/readme.md index bf5bcf70..c9578ab6 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -16,10 +16,12 @@ There's opt-in support for throttling requests to the endpoint. ```js -const createThrottledClient = require('hafas-client/throttle') +const withThrottling = require('hafas-client/throttle') +const createClient = require('hafas-client') const dbProfile = require('hafas-client/p/db') // create a throttled HAFAS client with Deutsche Bahn profile +const createThrottledClient = withThrottling(createClient) const client = createThrottledClient(dbProfile, 'my-awesome-program') // Berlin Jungfernheide to München Hbf @@ -28,11 +30,12 @@ client.journeys('8011167', '8000261', {results: 1}) .catch(console.error) ``` -You can pass custom values for the nr of requests (`limit`) per interval into `createThrottledClient`: +You can pass custom values for the nr of requests (`limit`) per interval into `withThrottling`: ```js // 2 requests per second -const client = createThrottledClient(dbProfile, 'my-awesome-program', 2, 1000) +const createThrottledClient = withThrottling(createClient, 2, 1000) +const client = createThrottledClient(dbProfile, 'my-awesome-program') ``` ## Retrying failed requests diff --git a/test/throttle.js b/test/throttle.js index bbbdea87..9a9dbbc9 100644 --- a/test/throttle.js +++ b/test/throttle.js @@ -2,13 +2,14 @@ const test = require('tape') -const createThrottledClient = require('../throttle') +const withThrottling = require('../throttle') +const createClient = require('..') const vbbProfile = require('../p/vbb') const userAgent = 'public-transport/hafas-client:test' const spichernstr = '900000042101' -test('throttle works', (t) => { +test('withThrottling works', (t) => { let calls = 0 const transformReqBody = (body) => { calls++ @@ -16,7 +17,8 @@ test('throttle works', (t) => { } const mockProfile = Object.assign({}, vbbProfile, {transformReqBody}) - const client = createThrottledClient(mockProfile, userAgent, 2, 1000) + const createThrottlingClient = withThrottling(createClient, 2, 1000) + const client = createThrottlingClient(mockProfile, userAgent) for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1}) t.plan(3) diff --git a/throttle.js b/throttle.js index 995e855e..c868986f 100644 --- a/throttle.js +++ b/throttle.js @@ -2,12 +2,14 @@ const throttle = require('p-throttle') -const request = require('./lib/request') -const createClient = require('.') +const _request = require('./lib/request') -const createThrottledClient = (profile, userAgent, limit = 5, interval = 1000) => { - const throttledRequest = throttle(request, limit, interval) - return createClient(profile, userAgent, throttledRequest) +const withThrottling = (createClient, limit = 5, interval = 1000) => { + const createThrottledClient = (profile, userAgent, request = _request) => { + const throttledRequest = throttle(request, limit, interval) + return createClient(profile, userAgent, throttledRequest) + } + return createThrottledClient } -module.exports = createThrottledClient +module.exports = withThrottling