From ce5c3e33aba356b0cdcffec1c1231078b231adff Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 11 Jul 2023 12:56:49 +0200 Subject: [PATCH] add generic page getter --- src/lib/mabel-types.ts | 25 +++++++++++++++++++++++++ src/lib/url.ts | 5 +++++ src/routes/[slug]/+page.svelte | 14 ++++++++++++++ src/routes/[slug]/+page.ts | 20 ++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 src/lib/mabel-types.ts create mode 100644 src/lib/url.ts create mode 100644 src/routes/[slug]/+page.svelte create mode 100644 src/routes/[slug]/+page.ts diff --git a/src/lib/mabel-types.ts b/src/lib/mabel-types.ts new file mode 100644 index 0000000..35d0250 --- /dev/null +++ b/src/lib/mabel-types.ts @@ -0,0 +1,25 @@ +export interface Site { + name: string; + owner: string; + owner_display_name: string; + title: string; + description: string | null; + created_at: string; + modified_at: string | null; + deleted_at: string | null; +} + +export interface Post { + id: string; + site: string; + author: string; + author_display_name: string; + slug: string; + title: string; + description: string | null; + tags: string[]; + blocks: []; + created_at: string; + modified_at: string | null; + deleted_at: string | null; +} diff --git a/src/lib/url.ts b/src/lib/url.ts new file mode 100644 index 0000000..544926e --- /dev/null +++ b/src/lib/url.ts @@ -0,0 +1,5 @@ +import { PUBLIC_DOMAIN } from '$env/static/public'; + +export function getSiteName(origin: string) { + return origin.substring(0, origin.indexOf(PUBLIC_DOMAIN)).replaceAll('.', ''); +} diff --git a/src/routes/[slug]/+page.svelte b/src/routes/[slug]/+page.svelte new file mode 100644 index 0000000..8b0fcaa --- /dev/null +++ b/src/routes/[slug]/+page.svelte @@ -0,0 +1,14 @@ + + + + {data.site.title} - {data.page.title} + + +

{data.site.title}

+ +

{data.page.title}

+by {data.page.author_display_name} diff --git a/src/routes/[slug]/+page.ts b/src/routes/[slug]/+page.ts new file mode 100644 index 0000000..d7c76cc --- /dev/null +++ b/src/routes/[slug]/+page.ts @@ -0,0 +1,20 @@ +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;