Skip to content

Commit 90eb75e

Browse files
committedDec 18, 2024·
Fix type check issues
1 parent 5dbdbf9 commit 90eb75e

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed
 

‎src/app/(infoscreen)/@transit/page.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import { BusFront } from 'lucide-react';
66
const Transit = async () => {
77
const transitData = await getTransitData();
88
let stoptimes = [];
9-
for (const stop of transitData.data.stops) {
9+
for (const stop of transitData.stops) {
1010
const now = Date.now();
1111
for (const stoptime of stop.stoptimesWithoutPatterns) {
1212
const arrival = new Date(
1313
(stoptime.serviceDay + stoptime.realtimeArrival) * 1000
1414
);
15-
if (arrival - now < 120 * 1000)
15+
if (arrival.getTime() - now < 120 * 1000)
1616
// Don't show if less than 2 minutes
1717
continue;
1818
let minutesUntil;
19-
if (arrival - now < 1000 * 60 * 15) {
19+
if (arrival.getTime() - now < 1000 * 60 * 15) {
2020
minutesUntil =
21-
Math.floor((0, arrival - now) / 1000 / 60) + ' min';
21+
Math.floor((arrival.getTime() - now) / 1000 / 60) + ' min';
2222
}
2323

2424
stoptimes.push({
@@ -70,10 +70,10 @@ const Transit = async () => {
7070
}
7171
stoptimes = stoptimes.sort((a, b) => {
7272
const arrival_diff =
73-
Math.floor(a.arrival / 1000 / 60) -
74-
Math.floor(b.arrival / 1000 / 60);
73+
Math.floor(a.arrival.getTime() / 1000 / 60) -
74+
Math.floor(b.arrival.getTime() / 1000 / 60);
7575
if (arrival_diff != 0) return arrival_diff;
76-
return a.num - b.num;
76+
return a.num.localeCompare(b.num);
7777
});
7878
const leftStoptimes = stoptimes.map((x) => x.el);
7979
return (

‎src/server/transit.ts

+40-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const fetchTag = 'hsl-transit';
88
const client = createClient({
99
url: ENDPOINT,
1010
headers: {
11-
'digitransit-subscription-key': process.env.DIGITRANSIT_TOKEN,
11+
'digitransit-subscription-key': process.env.DIGITRANSIT_TOKEN ?? "",
1212
},
1313
shouldRetry: async (err: NetworkError, retries: number) => {
1414
if (retries > 3) {
@@ -17,7 +17,7 @@ const client = createClient({
1717
}
1818

1919
// try again when service unavailable, could be temporary
20-
if ([502, 503, 504].some(err.response?.status)) {
20+
if (err.response?.status != undefined && [502, 503, 504].includes(err.response.status)) {
2121
// wait one second (you can alternatively time the promise resolution to your preference)
2222
await new Promise((resolve) => setTimeout(resolve, 1000));
2323
return true;
@@ -37,13 +37,38 @@ const client = createClient({
3737
},
3838
});
3939

40-
export const getTransitData = async () => {
40+
type TransitData = {
41+
stops: {
42+
gtfsId: string
43+
name: string
44+
code: string
45+
stoptimesWithoutPatterns: {
46+
arrivalDelay: number
47+
departureDelay: number
48+
headsign: string
49+
realtime: boolean
50+
realtimeArrival: number
51+
realtimeDeparture: number
52+
realtimeState: string
53+
serviceDay: number
54+
scheduledArrival: number
55+
scheduledDeparture: number
56+
trip: {
57+
tripHeadsign: string
58+
route: {
59+
type: number
60+
}
61+
routeShortName: string
62+
}
63+
}[]
64+
}[]
65+
}
66+
67+
export const getTransitData = async (): Promise<TransitData> => {
4168
'use server';
42-
let result;
43-
console.log('Fetching transit data');
4469
return await new Promise((resolve, reject) => {
45-
let result;
46-
client.subscribe(
70+
let result: TransitData | null | undefined;
71+
client.subscribe<TransitData>(
4772
{
4873
query: `
4974
{
@@ -75,9 +100,14 @@ export const getTransitData = async () => {
75100
`,
76101
},
77102
{
78-
next: (data) => (result = data),
79-
error: (err) => reject,
80-
complete: () => resolve(result),
103+
next: (data) => (result = data.data),
104+
error: () => reject,
105+
complete: () => {
106+
if(result)
107+
resolve(result)
108+
else
109+
reject(new Error("received undefined result from transit API"))
110+
},
81111
}
82112
);
83113
});

0 commit comments

Comments
 (0)
Please sign in to comment.