Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose routingMode as query parameter #46

Open
wants to merge 9 commits into
base: 6
Choose a base branch
from
9 changes: 9 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {parseBoolean, parseInteger} from 'hafas-rest-api/lib/parse.js'
import {loyaltyCardParser} from './lib/loyalty-cards.js'
import {route as stations} from './routes/stations.js'
import {route as station} from './routes/station.js'
import {parseRoutingMode} from './lib/parse.js'
import {routingModes} from 'hafas-client/p/db/routing-modes.js'

const pkg = require('./package.json')

Expand Down Expand Up @@ -67,6 +69,13 @@ const mapRouteParsers = (route, parsers) => {
defaultStr: '*adult*',
parse: parseInteger
},
routingMode: {
description: 'HAFAS routing mode, see the "Routing Mode" section',
type: 'string',
enum: Object.keys(routingModes),
defaultStr: '`REALTIME`',
parse: parseRoutingMode
},
}
}

Expand Down
10 changes: 10 additions & 0 deletions build/api-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ Uses [\`hafasClient.journeys()\`](https://github.com/public-transport/hafas-clie

Given a response, you can also fetch more journeys matching the same criteria. Instead of \`from*\`, \`to*\` & \`departure\`/\`arrival\`, pass \`earlierRef\` from the first response as \`earlierThan\` to get journeys "before", or \`laterRef\` as \`laterThan\` to get journeys "after".

*Pagination isn't fully functioning with the default [\`routingMode\`](#routing-mode).*

Check the [\`hafasClient.journeys()\` docs](https://github.com/public-transport/hafas-client/blob/6/docs/journeys.md) for more details.

### Routing mode

The \`routingMode\` parameter influences which data the system uses to compute the query results. The default is \`REALTIME\` and does *not* include canceled journeys. If you want canceled journeys to be included in the response, use \`HYBRID\` instead.

Furthermore, \`REALTIME\` doesn't support [Pagination](#pagination) fully. If you need fully functioning pagination, use \`HYBRID\` instead.
JohannesGandhi marked this conversation as resolved.
Show resolved Hide resolved

The \`hafas-client\` repository has [more details on the different routing modes](https://github.com/public-transport/hafas-client/blob/45610fc951bb834e1b6f09e363ee820c0b92b673/p/db/readme.md#using-the-routingmode-option).
`,
'/journeys/:ref': `\
Uses [\`hafasClient.refreshJourney()\`](https://github.com/public-transport/hafas-client/blob/6/docs/refresh-journey.md) to **"refresh" a journey, using its \`refreshToken\`**.
Expand Down
12 changes: 12 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {routingModes} from 'hafas-client/p/db/routing-modes.js'

const parseRoutingMode = (key, val) => {
if ('string' !== typeof val) throw new Error(key + ' must be a string')
val = val.toUpperCase()
if(Object.prototype.hasOwnProperty.call(routingModes, val)) return routingModes[val]
throw new Error(key + ' must be one of ' + Object.keys(routingModes).join(', '))
}

export {
parseRoutingMode
}
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ tape.test('/journeys?loyaltyCard works', async (t) => {
}, {}, '/journeys?from=123&to=234&loyaltyCard=bahncard-2nd-50')
})

tape.test('/journeys?routingMode works', async (t) => {
await fetchWithTestApi({
journeys: async (from, to, opt = {}) => {
t.equal(opt.routingMode, 'HYBRID', 'value is not case-sensitive')
return NO_JOURNEYS
}
}, {}, '/journeys?from=123&to=234&routingMode=HYbriD')
})

tape.test('/stations works', async (t) => {
const {data: allStations} = await pAllStations
const someStationId = Object.keys(allStations)[0]
Expand Down