mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
tripsByName: more options, add to debug CLI, skipped E2E test ✅📝
This commit is contained in:
parent
959e894d42
commit
fd6a349b64
4 changed files with 81 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
# `tripsByName(lineNameOrFahrtNr, [opt])`
|
# `tripsByName([lineNameOrFahrtNr], [opt])`
|
||||||
|
|
||||||
Get all trips matching a name.
|
Get all trips matching one or more criteria, e.g. a specific name.
|
||||||
|
|
||||||
## Response
|
## Response
|
||||||
|
|
||||||
|
@ -15,6 +15,19 @@ const client = createClient(vbbProfile, 'my-awesome-program')
|
||||||
console.log(await client.tripsByName('S1'))
|
console.log(await client.tripsByName('S1'))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
With `opt`, you can override the default options, which look like this:
|
||||||
|
|
||||||
|
```js
|
||||||
|
{
|
||||||
|
// use either this
|
||||||
|
when: null,
|
||||||
|
// or these
|
||||||
|
fromWhen: null, untilWhen: null,
|
||||||
|
|
||||||
|
onlyCurrentlyRunning: true,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
The response may look like this:
|
The response may look like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
38
index.js
38
index.js
|
@ -495,23 +495,45 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const tripsByName = (lineNameOrFahrtNr, opt = {}) => {
|
// todo [breaking]: rename to trips()?
|
||||||
|
const tripsByName = (lineNameOrFahrtNr = '*', opt = {}) => {
|
||||||
if (!isNonEmptyString(lineNameOrFahrtNr)) {
|
if (!isNonEmptyString(lineNameOrFahrtNr)) {
|
||||||
throw new TypeError('lineNameOrFahrtNr must be a non-empty string.')
|
throw new TypeError('lineNameOrFahrtNr must be a non-empty string.')
|
||||||
}
|
}
|
||||||
opt = Object.assign({
|
opt = Object.assign({
|
||||||
|
when: null,
|
||||||
|
fromWhen: null, untilWhen: null,
|
||||||
|
onlyCurrentlyRunning: true,
|
||||||
}, opt)
|
}, opt)
|
||||||
opt.when = new Date(opt.when || Date.now())
|
|
||||||
|
const req = {
|
||||||
|
// fields: https://github.com/marudor/BahnhofsAbfahrten/blob/f619e754f212980261eb7e2b151cd73ba0213da8/packages/types/HAFAS/JourneyMatch.ts#L4-L23
|
||||||
|
input: lineNameOrFahrtNr,
|
||||||
|
onlyCR: opt.onlyCurrentlyRunning,
|
||||||
|
// todo: passing `tripId` yields a `CGI_READ_FAILED` error
|
||||||
|
// todo: passing a stop ID as `extId` yields a `PARAMETER` error
|
||||||
|
// todo: `onlyRT: true` reduces the number of results, but filters recent trips 🤔
|
||||||
|
// todo: `onlyTN: true` yields a `NO_MATCH` error
|
||||||
|
// todo: jnyFltrL, useAeqi
|
||||||
|
}
|
||||||
|
if (opt.when !== null) {
|
||||||
|
req.date = profile.formatDate(profile, new Date(opt.when))
|
||||||
|
req.time = profile.formatTime(profile, new Date(opt.when))
|
||||||
|
}
|
||||||
|
// todo: fromWhen doesn't work yet, but untilWhen does
|
||||||
|
if (opt.fromWhen !== null) {
|
||||||
|
req.dateB = profile.formatDate(profile, new Date(opt.fromWhen))
|
||||||
|
req.timeB = profile.formatTime(profile, new Date(opt.fromWhen))
|
||||||
|
}
|
||||||
|
if (opt.untilWhen !== null) {
|
||||||
|
req.dateE = profile.formatDate(profile, new Date(opt.untilWhen))
|
||||||
|
req.timeE = profile.formatTime(profile, new Date(opt.untilWhen))
|
||||||
|
}
|
||||||
|
|
||||||
return profile.request({profile, opt}, userAgent, {
|
return profile.request({profile, opt}, userAgent, {
|
||||||
cfg: {polyEnc: 'GPA'},
|
cfg: {polyEnc: 'GPA'},
|
||||||
meth: 'JourneyMatch',
|
meth: 'JourneyMatch',
|
||||||
req: {
|
req,
|
||||||
input: lineNameOrFahrtNr,
|
|
||||||
// todo: date seems to be ignored
|
|
||||||
date: profile.formatDate(profile, opt.when),
|
|
||||||
// todo: there are probably more options
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.then(({res, common}) => {
|
.then(({res, common}) => {
|
||||||
const ctx = {profile, opt, common, res}
|
const ctx = {profile, opt, common, res}
|
||||||
|
|
|
@ -179,6 +179,40 @@ tap.test('trip details', async (t) => {
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// This currently fails because some trips' departure/arrival is out of the range
|
||||||
|
// around `when`, as expected by `assertValidWhen`, as called by `validate`.
|
||||||
|
// todo: allow turning this off?
|
||||||
|
tap.skip('trips', async (t) => {
|
||||||
|
const r1 = await client.tripsByName('S1')
|
||||||
|
t.ok(Array.isArray(r1))
|
||||||
|
t.ok(r1.length > 0)
|
||||||
|
t.ok(r1.every(t => t.line.name.trim() === 'S1'))
|
||||||
|
for (let i = 0; i < r1.length; i++) {
|
||||||
|
validate(t, r1[i], 'trip', `r1[${i}]`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const r2 = await client.tripsByName('S1', {
|
||||||
|
onlyCurrentlyRunning: false,
|
||||||
|
})
|
||||||
|
t.ok(Array.isArray(r2))
|
||||||
|
t.ok(r2.length > r1.length)
|
||||||
|
t.ok(r2.every(t => t.line.name.trim() === 'S1'))
|
||||||
|
for (let i = 0; i < r2.length; i++) {
|
||||||
|
validate(t, r2[i], 'trip', `r2[${i}]`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const r3 = await client.tripsByName('*', {
|
||||||
|
onlyCurrentlyRunning: false,
|
||||||
|
})
|
||||||
|
t.ok(Array.isArray(r3))
|
||||||
|
t.ok(r3.length > r2.length)
|
||||||
|
for (let i = 0; i < r3.length; i++) {
|
||||||
|
validate(t, r3[i], 'trip', `r3[${i}]`)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
||||||
tap.test('journeys – station to address', async (t) => {
|
tap.test('journeys – station to address', async (t) => {
|
||||||
const torfstr = {
|
const torfstr = {
|
||||||
type: 'location',
|
type: 'location',
|
||||||
|
|
|
@ -39,6 +39,8 @@ const parseArgs = [
|
||||||
['trip', 0, toString],
|
['trip', 0, toString],
|
||||||
['trip', 1, toString],
|
['trip', 1, toString],
|
||||||
['trip', 2, parseJsObject],
|
['trip', 2, parseJsObject],
|
||||||
|
['tripsByName', 0, toString],
|
||||||
|
['tripsByName', 1, parseJsObject],
|
||||||
['radar', 0, parseJsObject],
|
['radar', 0, parseJsObject],
|
||||||
['radar', 1, parseJsObject],
|
['radar', 1, parseJsObject],
|
||||||
['reachableFrom', 0, parseJsObject],
|
['reachableFrom', 0, parseJsObject],
|
||||||
|
|
Loading…
Add table
Reference in a new issue