createClient(): request parameter -> opt parameter 💥

This commit is contained in:
Jannis R 2019-09-28 22:15:22 +02:00
parent 655c425ecf
commit 4162328fd4
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 28 additions and 8 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -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})

View file

@ -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

View file

@ -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
}