import { error } from '@sveltejs/kit';
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
import { getErrorMessage } from '$lib/shared/errors';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ params, fetch }) => {
	const api = createApiClient(fetch);
	const result = await api.GET('/api/geschichten/{id}', {
		params: { path: { id: params.id } }
	});
	if (!result.response.ok) {
		throw error(result.response.status, getErrorMessage(extractErrorCode(result.error)));
	}
	return { geschichte: result.data! };
};
