db-vendo-client/api.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-12-08 21:42:57 +00:00
import {createClient} from './index.js';
import {profile as dbProfile} from './p/db/index.js';
import {profile as dbnavProfile} from './p/dbnav/index.js';
2025-02-13 19:41:46 +00:00
import {profile as dbwebProfile} from './p/dbweb/index.js';
2024-12-08 21:42:57 +00:00
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';
2024-12-21 23:04:05 +00:00
// 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,
},
};
};
2024-12-07 16:16:31 +00:00
const config = {
hostname: process.env.HOSTNAME || 'localhost',
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
2024-12-08 21:42:57 +00:00
name: 'db-vendo-client',
description: 'db-vendo-client',
homepage: 'https://github.com/public-transport/db-vendo-client',
2025-01-10 18:59:32 +00:00
version: '6',
2024-12-07 16:16:31 +00:00
docsLink: 'https://github.com/public-transport/db-vendo-client',
openapiSpec: true,
logging: true,
aboutPage: true,
2024-12-18 00:08:52 +00:00
enrichStations: true,
2024-12-07 16:16:31 +00:00
etags: 'strong',
2024-12-18 00:10:59 +00:00
csp: 'default-src \'none\'; style-src \'self\' \'unsafe-inline\'; img-src https:',
mapRouteParsers,
2024-12-08 21:42:57 +00:00
};
2024-12-07 16:16:31 +00:00
2025-02-13 19:41:46 +00:00
const profiles = {
'db': dbProfile,
'dbnav': dbnavProfile,
'dbweb': dbwebProfile
}
2024-12-08 21:42:57 +00:00
const start = async () => {
const vendo = createClient(
2025-02-13 19:41:46 +00:00
profiles[process.env.DB_PROFILE] || dbnavProfile,
process.env.USER_AGENT || 'link-to-your-project-or-email',
config,
);
2024-12-08 21:42:57 +00:00
const api = await createApi(vendo, config);
2024-12-07 16:16:31 +00:00
2024-12-08 21:42:57 +00:00
api.listen(config.port, (err) => {
if (err) {
console.error(err);
}
});
};
2024-12-07 16:16:31 +00:00
2024-12-08 21:42:57 +00:00
start();