UNPKG

hk-bus-eta

Version:

Query the ETA (Estimated Time of Arrival) of HK Bus/Minibus/MTR/Lightrail

50 lines 1.81 kB
import { isSafari } from "./utils"; export default function fetchEtas({ stopId, route, bound, stopList, }) { return fetch(`https://rt.data.gov.hk/v1/transport/mtr/getSchedule.php?line=${route}&sta=${stopId}`, { cache: isSafari ? "default" : "no-store", }) .catch(() => (fetch(`https://mtr.hkbus.app/?line=${route}&sta=${stopId}`, { cache: isSafari ? "default" : "no-store", }))) .then((response) => response.json()) .then(({ data, message, status, url }) => { if (status === 0) { if (message && typeof message === "string") { const message2 = message .replace("Please click here for more information.", "") .trim(); return [ { eta: null, remark: { zh: message2, en: message2, }, dest: { zh: "", en: "", }, co: "mtr", }, ]; } return []; } return data[`${route}-${stopId}`][bound.endsWith("UT") ? "UP" : "DOWN"].reduce((acc, { time, plat, dest }) => [ ...acc, { eta: time.replace(" ", "T") + "+08:00", remark: { zh: `${plat}號月台`, en: `Platform ${plat}`, }, dest: { zh: stopList[dest].name.zh, en: stopList[dest].name.en, }, co: "mtr", }, ], []); }); } //# sourceMappingURL=mtr.js.map