Skip to content

Commit

Permalink
add routingMode query parameter to /journeys route πŸ“βœ…
Browse files Browse the repository at this point in the history
closes #46

Co-Authored-By: Jannis R <[email protected]>
  • Loading branch information
JohannesGandhi and derhuerst committed Jan 21, 2025
1 parent beb8657 commit 1ffc757
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -103,6 +105,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.
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

0 comments on commit 1ffc757

Please sign in to comment.