dipper/src/routes/[slug]/+page.ts

21 lines
711 B
TypeScript

import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import { PUBLIC_API_BASE } from '$env/static/public';
import { getSiteName } from '$lib/url';
import type { Post, Site } from '$lib/mabel-types';
export const load = (async ({ url, params }) => {
const site = getSiteName(url.hostname);
const siteData = await fetch(`${PUBLIC_API_BASE}/sites/${site}`);
if (siteData.status === 404) throw error(404, 'Not Found');
const pageData = await fetch(`${PUBLIC_API_BASE}/posts/${site}/${params.slug}`);
if (pageData.status === 404) throw error(404, 'Not Found');
return {
site: (await siteData.json()) as Site,
page: (await pageData.json()) as Post
};
}) satisfies PageLoad;