db-vendo-client/api.js

59 lines
1.5 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 {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';
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',
version: '6.0.0',
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-08 21:42:57 +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
2024-12-08 21:42:57 +00:00
const start = async () => {
2024-12-18 00:08:52 +00:00
const vendo = createClient(dbProfile, 'my-hafas-rest-api', 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();