Add train headcode search

This commit is contained in:
2026-04-27 23:57:04 +01:00
parent 3225b60140
commit abb8663766
7 changed files with 124 additions and 10 deletions

View File

@@ -1,11 +1,13 @@
<script lang="ts">
import LocationBoardCard from '$lib/components/ui/cards/LocationBoardCard.svelte';
import HeadcodeSearchCard from '$lib/components/ui/cards/HeadcodeSearchCard.svelte';
import NearbyStationsCard from '$lib/components/ui/cards/NearbyStationsCard.svelte';
import QuickLinksCard from '$lib/components/ui/cards/QuickLinksCard.svelte';
</script>
<div class="card-container">
<LocationBoardCard />
<HeadcodeSearchCard />
<NearbyStationsCard />
<QuickLinksCard />
</div>

View File

@@ -0,0 +1,12 @@
<script lang="ts">
let { data } = $props();
</script>
<pre>{JSON.stringify(data.results, null, 1)}</pre>
<style>
pre {
background: #1e1e1e;
color: #00ff00;
padding: 1rem;
overflow: auto;
}
</style>

View File

@@ -0,0 +1,55 @@
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts';
import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';
export const load: PageLoad = async ({ url }) => {
const headcode = url.searchParams.get('h');
let dateParam = url.searchParams.get('d');
const toc = url.searchParams.get('t') || "";
const date: string | Date = (dateParam === "" || dateParam === null)
? new Date()
: dateParam;
if (!headcode) {
throw error(400, {
message: 'Headcode not provided',
owlCode: 'INVALID_DATA'
});
}
try {
const response = await OwlClient.trains.getByHeadcode(headcode, date, toc);
// Shouldn't be needed to cast the type...
const results = (response.data || []);
return {
title: headcode.toUpperCase(),
results: results,
}
} catch (e: unknown) {
if (e instanceof ValidationError) {
throw error(400, {
message: e.message,
owlCode: 'VALIDATION_ERROR',
});
} else if (e instanceof ApiError) {
console.log(e);
throw error(20, {
message: e.message,
owlCode: 'API_ERROR',
});
} else if (e instanceof Error) {
throw error(20, {
message: e.message,
owlCode: 'GEN_ERROR',
})
} else {
throw error(500, {
message: "Unexpected error",
owlCode: "UNKNOWN_ERR",
})
}
}
}