31 lines
941 B
TypeScript
31 lines
941 B
TypeScript
import { error } from '@sveltejs/kit';
|
|
import type { PageServerLoad } from './$types';
|
|
import type { TournamentInfo } from '$lib/types';
|
|
|
|
export const load: PageServerLoad = async ({ fetch, params }) => {
|
|
try {
|
|
const response = await fetch(`/api/tournament/slug/${params.slug}`);
|
|
const text = await response.text();
|
|
if (!response.ok) {
|
|
let message = 'Kunde inte hämta turneringen.';
|
|
try {
|
|
const body = JSON.parse(text);
|
|
message = body.message ?? message;
|
|
} catch {
|
|
if (text) message = text;
|
|
}
|
|
throw error(response.status, message);
|
|
}
|
|
|
|
const body = JSON.parse(text) as { tournament: TournamentInfo };
|
|
if (!body?.tournament) {
|
|
throw error(404, 'Turneringen hittades inte.');
|
|
}
|
|
|
|
return { tournament: body.tournament };
|
|
} catch (err) {
|
|
if (err instanceof Response) throw err;
|
|
console.error('Failed to load tournament detail', err);
|
|
throw error(500, 'Kunde inte hämta turneringen.');
|
|
}
|
|
};
|