db-hafas@6, hafas-rest-api@5, cached-hafas-client@5 💥📝

closes #22
This commit is contained in:
Jannis R 2022-12-21 14:30:00 +01:00
parent 82a8a97e0f
commit 04543f0146
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
6 changed files with 26 additions and 27 deletions

View file

@ -1,4 +1,4 @@
import generateApiDocs from 'hafas-rest-api/tools/generate-docs.js' import {generateApiDocs} from 'hafas-rest-api/tools/generate-docs.js'
import {api} from './api.js' import {api} from './api.js'
const HEAD = `\ const HEAD = `\
@ -30,22 +30,22 @@ const order = [
const descriptions = { const descriptions = {
'/locations': `\ '/locations': `\
Uses [\`hafasClient.locations()\`](https://github.com/public-transport/hafas-client/blob/5/docs/locations.md) to **find stops/stations, POIs and addresses matching \`query\`**. Uses [\`hafasClient.locations()\`](https://github.com/public-transport/hafas-client/blob/6/docs/locations.md) to **find stops/stations, POIs and addresses matching \`query\`**.
`, `,
'/stops/nearby': `\ '/stops/nearby': `\
Uses [\`hafasClient.nearby()\`](https://github.com/public-transport/hafas-client/blob/5/docs/nearby.md) to **find stops/stations close to the given geolocation**. Uses [\`hafasClient.nearby()\`](https://github.com/public-transport/hafas-client/blob/6/docs/nearby.md) to **find stops/stations close to the given geolocation**.
`, `,
'/stops/reachable-from': `\ '/stops/reachable-from': `\
Uses [\`hafasClient.reachableFrom()\`](https://github.com/public-transport/hafas-client/blob/5/docs/reachable-from.md) to **find stops/stations reachable within a certain time from an address**. Uses [\`hafasClient.reachableFrom()\`](https://github.com/public-transport/hafas-client/blob/6/docs/reachable-from.md) to **find stops/stations reachable within a certain time from an address**.
`, `,
'/stops/:id': `\ '/stops/:id': `\
Uses [\`hafasClient.stop()\`](https://github.com/public-transport/hafas-client/blob/5/docs/stop.md) to **find a stop/station by ID**. Uses [\`hafasClient.stop()\`](https://github.com/public-transport/hafas-client/blob/6/docs/stop.md) to **find a stop/station by ID**.
`, `,
'/stops/:id/departures': `\ '/stops/:id/departures': `\
Uses [\`hafasClient.departures()\`](https://github.com/public-transport/hafas-client/blob/5/docs/departures.md) to **get departures at a stop/station**. Uses [\`hafasClient.departures()\`](https://github.com/public-transport/hafas-client/blob/6/docs/departures.md) to **get departures at a stop/station**.
`, `,
'/stops/:id/arrivals': `\ '/stops/:id/arrivals': `\
Works like [\`/stops/:id/departures\`](#get-stopsiddepartures), except that it uses [\`hafasClient.arrivals()\`](https://github.com/public-transport/hafas-client/blob/5/docs/arrivals.md) to **arrivals at a stop/station**. Works like [\`/stops/:id/departures\`](#get-stopsiddepartures), except that it uses [\`hafasClient.arrivals()\`](https://github.com/public-transport/hafas-client/blob/6/docs/arrivals.md) to **arrivals at a stop/station**.
`, `,
'/stations': `\ '/stations': `\
If the \`query\` parameter is used, it will use [\`db-stations-autocomplete@2\`](https://github.com/derhuerst/db-stations-autocomplete/tree/2.2.0) to autocomplete *Deutsche Bahn*-operated stops/stations. Otherwise, it will filter the stops/stations in [\`db-stations@3\`](https://github.com/derhuerst/db-stations/tree/3.0.1). If the \`query\` parameter is used, it will use [\`db-stations-autocomplete@2\`](https://github.com/derhuerst/db-stations-autocomplete/tree/2.2.0) to autocomplete *Deutsche Bahn*-operated stops/stations. Otherwise, it will filter the stops/stations in [\`db-stations@3\`](https://github.com/derhuerst/db-stations/tree/3.0.1).
@ -56,7 +56,7 @@ Instead of receiving a JSON response, you can request [newline-delimited JSON](h
Returns a stop/station from [\`db-stations\`](https://npmjs.com/package/db-stations). Returns a stop/station from [\`db-stations\`](https://npmjs.com/package/db-stations).
`, `,
'/journeys': `\ '/journeys': `\
Uses [\`hafasClient.journeys()\`](https://github.com/public-transport/hafas-client/blob/5/docs/journeys.md) to **find journeys from A (\`from\`) to B (\`to\`)**. Uses [\`hafasClient.journeys()\`](https://github.com/public-transport/hafas-client/blob/6/docs/journeys.md) to **find journeys from A (\`from\`) to B (\`to\`)**.
\`from\` (A), \`to\` (B), and the optional \`via\` must each have one of these formats: \`from\` (A), \`to\` (B), and the optional \`via\` must each have one of these formats:
@ -68,20 +68,20 @@ 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". 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".
Check the [\`hafasClient.journeys()\` docs](https://github.com/public-transport/hafas-client/blob/5/docs/journeys.md) for more details. Check the [\`hafasClient.journeys()\` docs](https://github.com/public-transport/hafas-client/blob/6/docs/journeys.md) for more details.
`, `,
'/journeys/:ref': `\ '/journeys/:ref': `\
Uses [\`hafasClient.refreshJourney()\`](https://github.com/public-transport/hafas-client/blob/5/docs/refresh-journey.md) to **"refresh" a journey, using its \`refreshToken\`**. Uses [\`hafasClient.refreshJourney()\`](https://github.com/public-transport/hafas-client/blob/6/docs/refresh-journey.md) to **"refresh" a journey, using its \`refreshToken\`**.
The journey will be the same (equal \`from\`, \`to\`, \`via\`, date/time & vehicles used), but you can get up-to-date realtime data, like delays & cancellations. The journey will be the same (equal \`from\`, \`to\`, \`via\`, date/time & vehicles used), but you can get up-to-date realtime data, like delays & cancellations.
`, `,
'/trips/:id': `\ '/trips/:id': `\
Uses [\`hafasClient.trip()\`](https://github.com/public-transport/hafas-client/blob/5/docs/trip.md) to **fetch a trip by ID**. Uses [\`hafasClient.trip()\`](https://github.com/public-transport/hafas-client/blob/6/docs/trip.md) to **fetch a trip by ID**.
A trip is a specific vehicle, stopping at a series of stops at specific points in time. Departures, arrivals & journey legs reference trips by their ID. A trip is a specific vehicle, stopping at a series of stops at specific points in time. Departures, arrivals & journey legs reference trips by their ID.
`, `,
'/radar': `\ '/radar': `\
Uses [\`hafasClient.radar()\`](https://github.com/public-transport/hafas-client/blob/5/docs/radar.md) to **find all vehicles currently in an area**, as well as their movements. Uses [\`hafasClient.radar()\`](https://github.com/public-transport/hafas-client/blob/6/docs/radar.md) to **find all vehicles currently in an area**, as well as their movements.
`, `,
} }

12
api.js
View file

@ -5,12 +5,12 @@ const require = createRequire(import.meta.url)
import {dirname, join as pathJoin} from 'node:path' import {dirname, join as pathJoin} from 'node:path'
import {fileURLToPath} from 'node:url' import {fileURLToPath} from 'node:url'
import createHafas from 'db-hafas' import {createDbHafas as createHafas} from 'db-hafas'
import createApi from 'hafas-rest-api' import {createHafasRestApi} from 'hafas-rest-api'
import createHealthCheck from 'hafas-client-health-check' import createHealthCheck from 'hafas-client-health-check'
import Redis from 'ioredis' import Redis from 'ioredis'
import withCache from 'cached-hafas-client' import {createCachedHafasClient} from 'cached-hafas-client'
import redisStore from 'cached-hafas-client/stores/redis.js' import {createRedisStore} from 'cached-hafas-client/stores/redis.js'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import {parseBoolean} from 'hafas-rest-api/lib/parse.js' import {parseBoolean} from 'hafas-rest-api/lib/parse.js'
import {loyaltyCardParser} from './lib/loyalty-cards.js' import {loyaltyCardParser} from './lib/loyalty-cards.js'
@ -29,7 +29,7 @@ let healthCheck = createHealthCheck(hafas, berlinHbf)
if (process.env.REDIS_URL) { if (process.env.REDIS_URL) {
const redis = new Redis(process.env.REDIS_URL || null) const redis = new Redis(process.env.REDIS_URL || null)
hafas = withCache(hafas, redisStore(redis), { hafas = createCachedHafasClient(hafas, createRedisStore(redis), {
cachePeriods: { cachePeriods: {
locations: 6 * 60 * 60 * 1000, // 6h locations: 6 * 60 * 60 * 1000, // 6h
}, },
@ -87,7 +87,7 @@ const config = {
modifyRoutes, modifyRoutes,
} }
const api = createApi(hafas, config, (api) => { const api = await createHafasRestApi(hafas, config, (api) => {
api.use('/', serveStatic(docsRoot, { api.use('/', serveStatic(docsRoot, {
extensions: ['html', 'htm'], extensions: ['html', 'htm'],
})) }))

View file

@ -4,7 +4,7 @@
[![API status](https://badgen.net/uptime-robot/status/m784879516-8a977fa91b975fc3884a9857)](https://stats.uptimerobot.com/57wNLs39M/784879516) [![API status](https://badgen.net/uptime-robot/status/m784879516-8a977fa91b975fc3884a9857)](https://stats.uptimerobot.com/57wNLs39M/784879516)
Because it wraps [an API](https://github.com/public-transport/hafas-client/blob/master/readme.md#background) of [Deutsche Bahn](https://de.wikipedia.org/wiki/Deutsche_Bahn), it **includes most of the long-distance and regional traffic, as well as some international trains and local buses**. Essentially, it returns whatever data the [*DB Navigator* app](https://www.bahn.de/p/view/service/mobile/db-navigator.shtml) shows, **including realtime delays and disruptions**. Because it wraps [an API](https://github.com/public-transport/hafas-client/blob/6/readme.md#background) of [Deutsche Bahn](https://de.wikipedia.org/wiki/Deutsche_Bahn), it **includes most of the long-distance and regional traffic, as well as some international trains and local buses**. Essentially, it returns whatever data the [*DB Navigator* app](https://www.bahn.de/p/view/service/mobile/db-navigator.shtml) shows, **including realtime delays and disruptions**.
- [Getting Started](getting-started.md) - [Getting Started](getting-started.md)
- [API documentation](api.md) - [API documentation](api.md)
@ -14,7 +14,7 @@ Because it wraps [an API](https://github.com/public-transport/hafas-client/blob/
### Realtime Data ### Realtime Data
This API returns realtime data whenever its upstream, the [API for DB's mobile app](https://github.com/public-transport/hafas-client/blob/33d7d30acf235c54887c6459a15fe581982c6a19/p/db/readme.md), provides it. This API returns realtime data whenever its upstream, the [API for DB's mobile app](https://github.com/public-transport/hafas-client/blob/6/p/db/readme.md), provides it.
### No API Key ### No API Key

View file

@ -1,5 +1,4 @@
import _loyaltyCards from 'hafas-client/p/db/loyalty-cards.js' import {data as cards} from 'hafas-client/p/db/loyalty-cards.js'
const {data: cards} = _loyaltyCards
const typesByName = new Map([ const typesByName = new Map([
// https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11 // https://github.com/public-transport/hafas-client/blob/68ecd7c5e976dd2f51c5c64a81600e7e181a8996/p/db/loyalty-cards.js#L6-L11

View file

@ -23,14 +23,14 @@
"node": ">=18" "node": ">=18"
}, },
"dependencies": { "dependencies": {
"cached-hafas-client": "^4.0.4", "cached-hafas-client": "^5.0.1",
"cli-native": "^1.0.0", "cli-native": "^1.0.0",
"db-hafas": "^5.0.2", "db-hafas": "^6.0.0",
"db-stations": "^3.0.0", "db-stations": "^3.0.0",
"db-stations-autocomplete": "^2.2.0", "db-stations-autocomplete": "^2.2.0",
"etag": "^1.8.1", "etag": "^1.8.1",
"hafas-client-health-check": "^2.1.1", "hafas-client-health-check": "^2.1.1",
"hafas-rest-api": "^3.8.0", "hafas-rest-api": "^5.1.0",
"ioredis": "^5.0.6", "ioredis": "^5.0.6",
"serve-buffer": "^2.0.0", "serve-buffer": "^2.0.0",
"serve-static": "^1.14.1" "serve-static": "^1.14.1"

View file

@ -1,4 +1,4 @@
import createApi from 'hafas-rest-api' import {createHafasRestApi} from 'hafas-rest-api'
import getPort from 'get-port' import getPort from 'get-port'
import {createServer} from 'node:http' import {createServer} from 'node:http'
import {promisify} from 'node:util' import {promisify} from 'node:util'
@ -21,7 +21,7 @@ const createTestApi = async (mocks, cfg) => {
...cfg, ...cfg,
} }
const api = createApi(mockedHafas, cfg, () => {}) const api = await createHafasRestApi(mockedHafas, cfg, () => {})
const server = createServer(api) const server = createServer(api)
const port = await getPort() const port = await getPort()