From a723e56b4ce392d2b11d28f2745279aa825a2ee1 Mon Sep 17 00:00:00 2001 From: memdmp Date: Mon, 21 Jul 2025 22:53:11 +0200 Subject: feat: initial commit --- src/lib/motis-api.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/lib/motis-api.ts (limited to 'src/lib/motis-api.ts') diff --git a/src/lib/motis-api.ts b/src/lib/motis-api.ts new file mode 100644 index 0000000..1d43eea --- /dev/null +++ b/src/lib/motis-api.ts @@ -0,0 +1,57 @@ +import type { StoptimesResponse } from './motis-types'; +export class MotisAPI { + backend = 'https://api.transitous.org'; + fetch: typeof fetch = (url, init) => + fetch( + `${this.backend}${this.backend.endsWith('/') ? '' : '/'}${ + typeof url === 'string' + ? url.startsWith('/') + ? url.substring(1) + : url + : url instanceof URL + ? url.pathname + url.search + : url + }`, + init + ); + async getStopTimes( + id: string, + abortSignal?: AbortSignal, + arrivals = false, + limit = 128, + time: Date | undefined = arrivals + ? new Date( + // '2025-07-19T02:03:11Z' + Date.now() - 1000 * 60 + ) + : undefined + ) { + const res = await this.fetch( + `/api/v1/stoptimes?stopId=${encodeURIComponent( + id + )}&n=${encodeURIComponent(limit.toString())}&arriveBy=${ + arrivals ? 'true' : 'false' + }&withScheduledSkippedStops=${ + localStorage.getItem('with-scheduled-skipped-stops') ?? true + }&radius=${localStorage.getItem('radius') ?? 350}${ + time ? `&time=${time.toISOString()}` : '' + }`, + { + signal: abortSignal, + } + ); + if (res.status !== 200) + throw new Error( + `Stop Times: Expected 200 OK, got ${res.status} ${ + res.statusText + } (${await res + .text() + .catch((e) => `Could not get response body: ${e}`)})` + ); + const json = (await res.json()) as StoptimesResponse; + // TODO: validate + return json; + } +} +export const motis = new MotisAPI(); +export default motis; -- cgit v1.2.3