mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-04-20 23:23:56 +03:00
createClient(): request parameter -> opt parameter 💥
This commit is contained in:
parent
655c425ecf
commit
4162328fd4
5 changed files with 28 additions and 8 deletions
10
index.js
10
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)
|
profile = Object.assign({}, defaultProfile, profile)
|
||||||
if (!profile.parseProducts) {
|
if (!profile.parseProducts) {
|
||||||
profile.parseProducts = createParseBitmask(profile)
|
profile.parseProducts = createParseBitmask(profile)
|
||||||
|
@ -40,6 +44,10 @@ const createClient = (profile, userAgent, request = _request) => {
|
||||||
throw new TypeError('userAgent must be a string');
|
throw new TypeError('userAgent must be a string');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
request
|
||||||
|
} = Object.assign({}, defaults, opt)
|
||||||
|
|
||||||
const _stationBoard = (station, type, parser, opt = {}) => {
|
const _stationBoard = (station, type, parser, opt = {}) => {
|
||||||
if (isObj(station)) station = profile.formatStation(station.id)
|
if (isObj(station)) station = profile.formatStation(station.id)
|
||||||
else if ('string' === typeof station) station = profile.formatStation(station)
|
else if ('string' === typeof station) station = profile.formatStation(station)
|
||||||
|
|
9
retry.js
9
retry.js
|
@ -13,7 +13,9 @@ const retryDefaults = {
|
||||||
const withRetrying = (createClient, retryOpts = {}) => {
|
const withRetrying = (createClient, retryOpts = {}) => {
|
||||||
retryOpts = Object.assign({}, retryDefaults, 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 retryingRequest = (profile, userAgent, opt, data) => {
|
||||||
const attempt = () => {
|
const attempt = () => {
|
||||||
return request(profile, userAgent, opt, data)
|
return request(profile, userAgent, opt, data)
|
||||||
|
@ -30,7 +32,10 @@ const withRetrying = (createClient, retryOpts = {}) => {
|
||||||
return retry(attempt, retryOpts)
|
return retry(attempt, retryOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
return createClient(profile, userAgent, retryingRequest)
|
return createClient(profile, userAgent, {
|
||||||
|
...opt,
|
||||||
|
request: retryingRequest
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return createRetryingClient
|
return createRetryingClient
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,9 @@ test('withRetrying works', (t) => {
|
||||||
factor: 2,
|
factor: 2,
|
||||||
randomize: false
|
randomize: false
|
||||||
})
|
})
|
||||||
const client = createRetryingClient(vbbProfile, userAgent, failingRequest)
|
const client = createRetryingClient(vbbProfile, userAgent, {
|
||||||
|
request: failingRequest
|
||||||
|
})
|
||||||
|
|
||||||
t.plan(1 + 4)
|
t.plan(1 + 4)
|
||||||
client.departures(spichernstr, {duration: 1})
|
client.departures(spichernstr, {duration: 1})
|
||||||
|
|
|
@ -9,6 +9,7 @@ const vbbProfile = require('../p/vbb')
|
||||||
const userAgent = 'public-transport/hafas-client:test'
|
const userAgent = 'public-transport/hafas-client:test'
|
||||||
const spichernstr = '900000042101'
|
const spichernstr = '900000042101'
|
||||||
|
|
||||||
|
// todo: mock request()
|
||||||
test('withThrottling works', (t) => {
|
test('withThrottling works', (t) => {
|
||||||
let calls = 0
|
let calls = 0
|
||||||
const transformReqBody = (body) => {
|
const transformReqBody = (body) => {
|
||||||
|
@ -26,5 +27,3 @@ test('withThrottling works', (t) => {
|
||||||
setTimeout(() => t.equal(calls, 4), 1500)
|
setTimeout(() => t.equal(calls, 4), 1500)
|
||||||
setTimeout(() => t.equal(calls, 6), 2500)
|
setTimeout(() => t.equal(calls, 6), 2500)
|
||||||
})
|
})
|
||||||
|
|
||||||
// todo
|
|
||||||
|
|
10
throttle.js
10
throttle.js
|
@ -5,9 +5,15 @@ const throttle = require('p-throttle')
|
||||||
const _request = require('./lib/request')
|
const _request = require('./lib/request')
|
||||||
|
|
||||||
const withThrottling = (createClient, limit = 5, interval = 1000) => {
|
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)
|
const throttledRequest = throttle(request, limit, interval)
|
||||||
return createClient(profile, userAgent, throttledRequest)
|
|
||||||
|
return createClient(profile, userAgent, {
|
||||||
|
...opt,
|
||||||
|
request: throttledRequest
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return createThrottledClient
|
return createThrottledClient
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue