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

@@ -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",
})
}
}
}