ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
import { createClient as createVendoClient } from "db-vendo-client";
import { profile as dbnavProfile } from "db-vendo-client/p/dbnav/index.js";

const clients = {};
let   createHafasClient;

export let client;

export const initHafasClient = async profileName => {
	client = await getHafasClient(profileName);
};

export const getHafasClient = async profileName => {
	if (!clients[profileName]) {
		let profile;

		switch(profileName) {
			case "db":
				clients[profileName] = createVendoClient(dbnavProfile, "trainsearch", {enrichStations: false});
				console.info("initialized vendo client");
				return clients[profileName];

			case "bvg":
				const { profile: bvgProfile } = await import('hafas-client/p/bvg/index.js');
				profile = bvgProfile;
				break;

			case "nahsh":
				const { profile: nahshProfile } = await import('hafas-client/p/nahsh/index.js');
				profile = nahshProfile;
				break;

			case "rmv":
				const { profile: rmvProfile } = await import('hafas-client/p/rmv/index.js');
				profile = rmvProfile;
				break;

			case "vrn":
				const { profile: vrnProfile } = await import('hafas-client/p/vrn/index.js');
				profile = vrnProfile;
				break;

			case "oebb":
				const { profile: oebbProfile } = await import('hafas-client/p/oebb/index.js');
				profile = oebbProfile;
				break;

			default:
				throw "Unknown profile name: " + profileName;
		}

		if (!createHafasClient) createHafasClient = (await import("hafas-client")).createClient;

		clients[profileName] = createHafasClient(profile, "trainsearch");
	}

	console.info("initialized hafas client with profile " + profileName);
	return clients[profileName];
}