Compare commits

..

No commits in common. "7a1e513fa2c6309987ba4049339bb1d49c28be36" and "9314e590531fe97631aefad4360b0a1ba7d36051" have entirely different histories.

15 changed files with 100 additions and 185 deletions

34
api.js
View file

@ -1,9 +1,32 @@
import {createClient} from './index.js'; import {createClient} from './index.js';
import {profile as dbProfile} from './p/db/index.js'; import {profile as dbProfile} from './p/db/index.js';
import {profile as dbnavProfile} from './p/dbnav/index.js'; import {profile as dbnavProfile} from './p/dbnav/index.js';
import {profile as dbwebProfile} from './p/dbweb/index.js';
import {mapRouteParsers} from './lib/api-parsers.js';
import {createHafasRestApi as createApi} from 'hafas-rest-api'; import {createHafasRestApi as createApi} from 'hafas-rest-api';
import {loyaltyCardParser} from 'db-rest/lib/loyalty-cards.js';
import {parseBoolean, parseInteger} from 'hafas-rest-api/lib/parse.js';
// TODO product support for nearby etc?
const mapRouteParsers = (route, parsers) => {
if (!route.includes('journey')) {
return parsers;
}
return {
...parsers,
loyaltyCard: loyaltyCardParser,
firstClass: {
description: 'Search for first-class options?',
type: 'boolean',
default: 'false',
parse: parseBoolean,
},
age: {
description: 'Age of traveller',
type: 'integer',
defaultStr: '*adult*',
parse: parseInteger,
},
};
};
const config = { const config = {
hostname: process.env.HOSTNAME || 'localhost', hostname: process.env.HOSTNAME || 'localhost',
@ -22,15 +45,10 @@ const config = {
mapRouteParsers, mapRouteParsers,
}; };
const profiles = {
db: dbProfile,
dbnav: dbnavProfile,
dbweb: dbwebProfile,
};
const start = async () => { const start = async () => {
const vendo = createClient( const vendo = createClient(
profiles[process.env.DB_PROFILE] || dbnavProfile, process.env.DB_PROFILE == 'db' ? dbProfile : dbnavProfile,
process.env.USER_AGENT || 'link-to-your-project-or-email', process.env.USER_AGENT || 'link-to-your-project-or-email',
config, config,
); );

View file

@ -24,7 +24,7 @@ With `opt`, you can override the default options, which look like this:
```js ```js
{ {
when: new Date(), when: new Date(),
direction: null, // only supported in `dbweb` and with `enrichStations=true` (experimental) direction: null, // not supported
line: null, // not supported line: null, // not supported
duration: 10, // show departures for the next n minutes duration: 10, // show departures for the next n minutes
results: null, // max. number of results; `null` means "whatever HAFAS wants" results: null, // max. number of results; `null` means "whatever HAFAS wants"
@ -35,7 +35,7 @@ With `opt`, you can override the default options, which look like this:
stopovers: false, // fetch & parse previous/next stopovers?, only supported with `dbweb` profile stopovers: false, // fetch & parse previous/next stopovers?, only supported with `dbweb` profile
// departures at related stations // departures at related stations
// e.g. those that belong together on the metro map. // e.g. those that belong together on the metro map.
includeRelatedStations: true, includeRelatedStations: true, // only true supported
language: 'en' // language to get results in language: 'en' // language to get results in
} }
``` ```

View file

@ -385,14 +385,13 @@ paths:
- bahncard-2nd-25 - bahncard-2nd-25
- bahncard-1st-50 - bahncard-1st-50
- bahncard-2nd-50 - bahncard-2nd-50
- bahncard-1st-100
- bahncard-2nd-100
- vorteilscard - vorteilscard
- halbtaxabo-railplus
- halbtaxabo - halbtaxabo
- generalabonnement-1st - voordeelurenabo-railplus
- generalabonnement-2nd - voordeelurenabo
- nl-40 - shcard
- at-klimaticket - generalabonnement
- name: firstClass - name: firstClass
in: query in: query
description: Search for first-class options? description: Search for first-class options?
@ -2088,7 +2087,7 @@ components:
type: string type: string
format: date-time format: date-time
direction: direction:
description: only show departures heading to this station, only supported for `dbweb` profile description: only show departures heading to this station
default: undefined default: undefined
type: string type: string
line: line:
@ -2125,7 +2124,7 @@ components:
type: boolean type: boolean
includeRelatedStations: includeRelatedStations:
description: departures at related stations description: departures at related stations
default: true default: false
type: boolean type: boolean
products: products:
$ref: '#/components/schemas/Products' $ref: '#/components/schemas/Products'

View file

@ -6,10 +6,9 @@ const c = {
VOORDEELURENABO: Symbol('Voordeelurenabo'), VOORDEELURENABO: Symbol('Voordeelurenabo'),
SHCARD: Symbol('SH-Card'), SHCARD: Symbol('SH-Card'),
GENERALABONNEMENT: Symbol('General-Abonnement'), GENERALABONNEMENT: Symbol('General-Abonnement'),
NL_40: Symbol('NL-40%'),
AT_KLIMATICKET: Symbol('AT-KlimaTicket'),
}; };
// see https://gist.github.com/juliuste/202bb04f450a79f8fa12a2ec3abcd72d
const formatLoyaltyCard = (data) => { const formatLoyaltyCard = (data) => {
if (!data) { if (!data) {
return { return {
@ -20,7 +19,7 @@ const formatLoyaltyCard = (data) => {
const cls = data.class === 1 ? 'KLASSE_1' : 'KLASSE_2'; const cls = data.class === 1 ? 'KLASSE_1' : 'KLASSE_2';
if (data.type.toString() === c.BAHNCARD.toString()) { if (data.type.toString() === c.BAHNCARD.toString()) {
return { return {
art: 'BAHNCARD' + (data.business ? 'BUSINESS' : '') + data.discount, art: 'BAHNCARD' + data.discount,
klasse: cls, klasse: cls,
}; };
} }
@ -36,24 +35,13 @@ const formatLoyaltyCard = (data) => {
klasse: 'KLASSENLOS', klasse: 'KLASSENLOS',
}; };
} }
// TODO Rest
if (data.type.toString() === c.GENERALABONNEMENT.toString()) { if (data.type.toString() === c.GENERALABONNEMENT.toString()) {
return { return {
art: 'CH-GENERAL-ABONNEMENT', art: 'CH-GENERAL-ABONNEMENT',
klasse: cls, klasse: cls,
}; };
} }
if (data.type.toString() === c.NL_40.toString()) {
return {
art: 'NL-40_OHNE_RAILPLUS',
klasse: 'KLASSENLOS',
};
}
if (data.type.toString() === c.AT_KLIMATICKET.toString()) {
return {
art: 'KLIMATICKET_OE',
klasse: 'KLASSENLOS',
};
}
return { return {
art: 'KEINE_ERMAESSIGUNG', art: 'KEINE_ERMAESSIGUNG',
klasse: 'KLASSENLOS', klasse: 'KLASSENLOS',

View file

@ -45,23 +45,15 @@ const loadEnrichedStationData = (profile) => new Promise((resolve, reject) => {
}); });
}); });
const applyEnrichedStationData = async (ctx, shouldLoadEnrichedStationData) => {
const {profile, common} = ctx;
if (shouldLoadEnrichedStationData && !common.locations) {
const locations = await loadEnrichedStationData(profile);
common.locations = locations;
}
};
const createClient = (profile, userAgent, opt = {}) => { const createClient = (profile, userAgent, opt = {}) => {
profile = Object.assign({}, defaultProfile, profile); profile = Object.assign({}, defaultProfile, profile);
validateProfile(profile); validateProfile(profile);
const common = {}; const common = {};
let shouldLoadEnrichedStationData = false; if (opt.enrichStations !== false) {
if (typeof opt.enrichStations === 'function') { loadEnrichedStationData(profile)
profile.enrichStation = opt.enrichStations; .then(locations => {
} else if (opt.enrichStations !== false) { common.locations = locations;
shouldLoadEnrichedStationData = true; });
} }
if ('string' !== typeof userAgent) { if ('string' !== typeof userAgent) {
@ -72,7 +64,6 @@ const createClient = (profile, userAgent, opt = {}) => {
} }
const _stationBoard = async (station, type, resultsField, parse, opt = {}) => { const _stationBoard = async (station, type, resultsField, parse, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if (isObj(station) && station.id) { if (isObj(station) && station.id) {
station = station.id; station = station.id;
} else if ('string' !== typeof station) { } else if ('string' !== typeof station) {
@ -115,15 +106,9 @@ const createClient = (profile, userAgent, opt = {}) => {
const {res} = await profile.request({profile, opt}, userAgent, req); const {res} = await profile.request({profile, opt}, userAgent, req);
const ctx = {profile, opt, common, res}; const ctx = {profile, opt, common, res};
let results = (res[resultsField] || res.items || res.bahnhofstafelAbfahrtPositionen || res.bahnhofstafelAnkunftPositionen || res.entries) const results = (res[resultsField] || res.items || res.bahnhofstafelAbfahrtPositionen || res.bahnhofstafelAnkunftPositionen || res.entries)
.map(res => parse(ctx, res)); // TODO sort?, slice .map(res => parse(ctx, res)); // TODO sort?, slice
if (!opt.includeRelatedStations) {
results = results.filter(r => !r.stop?.id || r.stop.id == station);
}
if (opt.direction) {
results = results.filter(r => !r.nextStopovers || r.nextStopovers.find(s => s.stop?.id == opt.direction || s.stop?.name == opt.direction));
}
return { return {
[resultsField]: results, [resultsField]: results,
realtimeDataUpdatedAt: null, // TODO realtimeDataUpdatedAt: null, // TODO
@ -138,7 +123,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const journeys = async (from, to, opt = {}) => { const journeys = async (from, to, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if ('earlierThan' in opt && 'laterThan' in opt) { if ('earlierThan' in opt && 'laterThan' in opt) {
throw new TypeError('opt.earlierThan and opt.laterThan are mutually exclusive.'); throw new TypeError('opt.earlierThan and opt.laterThan are mutually exclusive.');
} }
@ -221,8 +205,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const refreshJourney = async (refreshToken, opt = {}) => { const refreshJourney = async (refreshToken, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if ('string' !== typeof refreshToken || !refreshToken) { if ('string' !== typeof refreshToken || !refreshToken) {
throw new TypeError('refreshToken must be a non-empty string.'); throw new TypeError('refreshToken must be a non-empty string.');
} }
@ -249,8 +231,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const locations = async (query, opt = {}) => { const locations = async (query, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if (!isNonEmptyString(query)) { if (!isNonEmptyString(query)) {
throw new TypeError('query must be a non-empty string.'); throw new TypeError('query must be a non-empty string.');
} }
@ -277,8 +257,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const stop = async (stop, opt = {}) => { const stop = async (stop, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if (isObj(stop) && stop.id) { if (isObj(stop) && stop.id) {
stop = stop.id; stop = stop.id;
} else if ('string' !== typeof stop) { } else if ('string' !== typeof stop) {
@ -300,8 +278,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const nearby = async (location, opt = {}) => { const nearby = async (location, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
validateLocation(location, 'location'); validateLocation(location, 'location');
opt = Object.assign({ opt = Object.assign({
@ -332,8 +308,6 @@ const createClient = (profile, userAgent, opt = {}) => {
}; };
const trip = async (id, opt = {}) => { const trip = async (id, opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
if (!isNonEmptyString(id)) { if (!isNonEmptyString(id)) {
throw new TypeError('id must be a non-empty string.'); throw new TypeError('id must be a non-empty string.');
} }
@ -361,8 +335,6 @@ const createClient = (profile, userAgent, opt = {}) => {
// todo [breaking]: rename to trips()? // todo [breaking]: rename to trips()?
const tripsByName = async (_lineNameOrFahrtNr = '*', _opt = {}) => { const tripsByName = async (_lineNameOrFahrtNr = '*', _opt = {}) => {
await applyEnrichedStationData({profile, common}, shouldLoadEnrichedStationData);
throw new Error('not implemented'); throw new Error('not implemented');
}; };
@ -389,5 +361,4 @@ const createClient = (profile, userAgent, opt = {}) => {
export { export {
createClient, createClient,
loadEnrichedStationData,
}; };

View file

@ -1,74 +0,0 @@
import {data as cards} from '../format/loyalty-cards.js';
import {parseBoolean, parseInteger} from 'hafas-rest-api/lib/parse.js';
const typesByName = new Map([
['bahncard-1st-25', {type: cards.BAHNCARD, discount: 25, class: 1}],
['bahncard-2nd-25', {type: cards.BAHNCARD, discount: 25, class: 2}],
['bahncard-1st-50', {type: cards.BAHNCARD, discount: 50, class: 1}],
['bahncard-2nd-50', {type: cards.BAHNCARD, discount: 50, class: 2}],
['bahncard-1st-100', {type: cards.BAHNCARD, discount: 100, class: 1}],
['bahncard-2nd-100', {type: cards.BAHNCARD, discount: 100, class: 2}],
['vorteilscard', {type: cards.VORTEILSCARD}],
['halbtaxabo-railplus', {type: cards.HALBTAXABO}],
['halbtaxabo', {type: cards.HALBTAXABO}],
['voordeelurenabo-railplus', {type: cards.VOORDEELURENABO}],
['voordeelurenabo', {type: cards.VOORDEELURENABO}],
['shcard', {type: cards.SHCARD}],
['generalabonnement-1st', {type: cards.GENERALABONNEMENT, class: 1}],
['generalabonnement-2nd', {type: cards.GENERALABONNEMENT, class: 2}],
['generalabonnement', {type: cards.GENERALABONNEMENT}],
['nl-40', {type: cards.NL_40}],
['at-klimaticket', {type: cards.AT_KLIMATICKET}],
]);
const types = Array.from(typesByName.keys());
const parseLoyaltyCard = (key, val) => {
if (typesByName.has(val)) {
return typesByName.get(val);
}
if (!val) {
return null;
}
throw new Error(key + ' must be one of ' + types.join(', '));
};
const parseArrayOr = (parseEntry) => {
return (key, val) => {
if (Array.isArray(val)) {
return val.map(e => parseEntry(key, e));
}
return parseEntry(key, val);
};
};
const mapRouteParsers = (route, parsers) => {
if (route !== 'journeys') {
return parsers;
}
return {
...parsers,
firstClass: {
description: 'Search for first-class options?',
type: 'boolean',
default: 'false',
parse: parseBoolean,
},
loyaltyCard: {
description: 'Type of loyalty card in use.',
type: 'string',
enum: types,
defaultStr: '*none*',
parse: parseArrayOr(parseLoyaltyCard),
},
age: {
description: 'Age of traveller',
type: 'integer',
defaultStr: '*adult*',
parse: parseArrayOr(parseInteger),
},
};
};
export {
mapRouteParsers,
};

View file

@ -16,7 +16,7 @@ import {parseTrip} from '../parse/trip.js';
import {parseJourneyLeg} from '../parse/journey-leg.js'; import {parseJourneyLeg} from '../parse/journey-leg.js';
import {parseJourney} from '../parse/journey.js'; import {parseJourney} from '../parse/journey.js';
import {parseLine} from '../parse/line.js'; import {parseLine} from '../parse/line.js';
import {parseLocation, enrichStation} from '../parse/location.js'; import {parseLocation} from '../parse/location.js';
import {parsePolyline} from '../parse/polyline.js'; import {parsePolyline} from '../parse/polyline.js';
import {parseOperator} from '../parse/operator.js'; import {parseOperator} from '../parse/operator.js';
import {parseRemarks, parseCancelled} from '../parse/remarks.js'; import {parseRemarks, parseCancelled} from '../parse/remarks.js';
@ -82,7 +82,6 @@ const defaultProfile = {
parseLine, parseLine,
parseStationName: id, parseStationName: id,
parseLocation, parseLocation,
enrichStation,
parsePolyline, parsePolyline,
parseOperator, parseOperator,
parseRemarks, parseRemarks,

View file

@ -8,7 +8,7 @@ const formatStationBoardReq = (ctx, station, type) => {
ortExtId: station, ortExtId: station,
zeit: profile.formatTimeOfDay(profile, opt.when), zeit: profile.formatTimeOfDay(profile, opt.when),
datum: profile.formatDate(profile, opt.when), datum: profile.formatDate(profile, opt.when),
mitVias: opt.stopovers || Boolean(opt.direction) || undefined, mitVias: opt.stopovers || undefined,
verkehrsmittel: profile.formatProductsFilter(ctx, opt.products || {}), verkehrsmittel: profile.formatProductsFilter(ctx, opt.products || {}),
}, },
method: 'GET', method: 'GET',

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "db-vendo-client", "name": "db-vendo-client",
"version": "6.5.0", "version": "6.4.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "db-vendo-client", "name": "db-vendo-client",
"version": "6.5.0", "version": "6.4.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@derhuerst/round-robin-scheduler": "^1.0.4", "@derhuerst/round-robin-scheduler": "^1.0.4",

View file

@ -1,7 +1,7 @@
{ {
"name": "db-vendo-client", "name": "db-vendo-client",
"description": "Client for bahn.de public transport APIs.", "description": "Client for bahn.de public transport APIs.",
"version": "6.5.0", "version": "6.4.0",
"type": "module", "type": "module",
"main": "index.js", "main": "index.js",
"files": [ "files": [

View file

@ -40,7 +40,7 @@ const createParseArrOrDep = (prefix) => {
res.remarks = profile.parseRemarks(ctx, d); res.remarks = profile.parseRemarks(ctx, d);
} }
if ((opt.stopovers || opt.direction) && Array.isArray(d.ueber)) { if (opt.stopovers && Array.isArray(d.ueber)) {
const stopovers = d.ueber const stopovers = d.ueber
.map(viaName => profile.parseStopover(ctx, {name: viaName}, null)); .map(viaName => profile.parseStopover(ctx, {name: viaName}, null));

View file

@ -14,7 +14,7 @@ const parseLocation = (ctx, l) => {
} }
const lid = parse(l.id || l.locationId, {delimiter: '@'}); const lid = parse(l.id || l.locationId, {delimiter: '@'});
let res = { const res = {
type: 'location', type: 'location',
id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || l.bahnhofsId || '').replace(leadingZeros, '') || null, id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || l.bahnhofsId || '').replace(leadingZeros, '') || null,
}; };
@ -46,7 +46,13 @@ const parseLocation = (ctx, l) => {
stop.products = profile.parseProducts(ctx, l.products); stop.products = profile.parseProducts(ctx, l.products);
} }
stop = profile.enrichStation(ctx, stop); if (common && common.locations && common.locations[stop.id]) {
delete stop.type;
stop = {
...common.locations[stop.id],
...stop,
};
}
// TODO isMeta // TODO isMeta
// TODO entrances, lines // TODO entrances, lines
@ -64,8 +70,6 @@ const parseLocation = (ctx, l) => {
} }
res.name = name; res.name = name;
res = enrichStation(ctx, res);
if (l.type === ADDRESS || lid.A == '2') { if (l.type === ADDRESS || lid.A == '2') {
res.address = name; res.address = name;
} }
@ -76,22 +80,6 @@ const parseLocation = (ctx, l) => {
return res; return res;
}; };
const enrichStation = (ctx, stop, locations) => {
const {common} = ctx;
const locs = locations || common?.locations;
const rich = locs && (locs[stop.id] || locs[stop.name]);
if (rich) {
delete stop.type;
delete stop.id;
stop = {
...rich,
...stop,
};
}
return stop;
};
export { export {
parseLocation, parseLocation,
enrichStation,
}; };

View file

@ -10,7 +10,7 @@ import {profile as rawProfile} from '../p/dbweb/index.js';
const res = require('./fixtures/dbweb-departures.json'); const res = require('./fixtures/dbweb-departures.json');
import {dbwebDepartures as expected} from './fixtures/dbweb-departures.js'; import {dbwebDepartures as expected} from './fixtures/dbweb-departures.js';
const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: false}); const client = createClient(rawProfile, 'public-transport/hafas-client:test', {enrichStations: true});
const {profile} = client; const {profile} = client;
const opt = { const opt = {

View file

@ -395,8 +395,17 @@ tap.test('trip details', async (t) => {
}); });
tap.test('departures at Berlin Schwedter Str.', async (t) => { tap.test('departures at Berlin Schwedter Str.', async (t) => {
const res = await client.departures(blnSchwedterStr, { const res = await new Promise((resolve) => {
duration: 5, when, let interval = setInterval(async () => { // repeat evaluating `departures()` until stations are enriched
const res = await client.departures(blnSchwedterStr, {
duration: 5, when,
});
if (res.departures[0].stop.name !== undefined) { // ctx.common.locations have loaded
clearInterval(interval);
return resolve(res);
}
}, 4000);
}); });
await testDepartures({ await testDepartures({
@ -409,24 +418,42 @@ tap.test('departures at Berlin Schwedter Str.', async (t) => {
}); });
tap.test('departures with station object', async (t) => { tap.test('departures with station object', async (t) => {
const res = await client.departures({ const res = await new Promise((resolve) => {
type: 'station', let interval = setInterval(async () => { // repeat evaluating `departures()` until stations are enriched
id: jungfernheide, const res = await client.departures({
name: 'Berlin Jungfernheide', type: 'station',
location: { id: jungfernheide,
type: 'location', name: 'Berlin Jungfernheide',
latitude: 1.23, location: {
longitude: 2.34, type: 'location',
}, latitude: 1.23,
}, {when}); longitude: 2.34,
},
}, {when});
if (res.departures[0].stop.name !== undefined) { // ctx.common.locations have loaded
clearInterval(interval);
return resolve(res);
}
}, 4000);
});
validate(t, res, 'departuresResponse', 'res'); validate(t, res, 'departuresResponse', 'res');
t.end(); t.end();
}); });
tap.test('arrivals at Berlin Schwedter Str.', async (t) => { tap.test('arrivals at Berlin Schwedter Str.', async (t) => {
const res = await client.arrivals(blnSchwedterStr, { const res = await new Promise((resolve) => {
duration: 5, when, let interval = setInterval(async () => { // repeat evaluating `arrivals()` until stations are enriched
const res = await client.arrivals(blnSchwedterStr, {
duration: 5, when,
});
if (res.arrivals[0].stop.name !== undefined) { // ctx.common.locations have loaded
clearInterval(interval);
return resolve(res);
}
}, 4000);
}); });
await testArrivals({ await testArrivals({

View file

@ -4,7 +4,6 @@ import {parseProducts} from '../../parse/products.js';
const profile = { const profile = {
parseLocation: parse, parseLocation: parse,
enrichStation: (ctx, stop) => stop,
parseStationName: (_, name) => name.toLowerCase(), parseStationName: (_, name) => name.toLowerCase(),
parseProducts, parseProducts,
products: [{ products: [{