Initial commit: RustyAstro + OpenAPI-Typen aus RustyCMS
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/// <reference types="astro/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly PUBLIC_CMS_URL: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
import '../styles/global.css';
|
||||
|
||||
interface Props {
|
||||
title?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
const { title = 'RustyAstro', description } = Astro.props;
|
||||
---
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<meta name="generator" content={Astro.generator} />
|
||||
<title>{title}</title>
|
||||
{description && <meta name="description" content={description} />}
|
||||
</head>
|
||||
<body class="min-h-screen bg-zinc-50 text-zinc-900">
|
||||
<slot />
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* RustyCMS-Client: OpenAPI-Spec laden, Page-Content abrufen.
|
||||
* Basis-URL über Umgebungsvariable PUBLIC_CMS_URL (z.B. http://localhost:3000).
|
||||
*
|
||||
* Typen für Page und API-Antworten kommen aus der generierten OpenAPI-Spec.
|
||||
* Nach Schema-Änderungen im CMS: `yarn generate:api-types` (RustyCMS muss laufen).
|
||||
*/
|
||||
|
||||
import type { components, operations } from "./cms-api.generated";
|
||||
|
||||
const getBaseUrl = (): string => {
|
||||
const url = import.meta.env.PUBLIC_CMS_URL;
|
||||
const base = (typeof url === "string" && url.trim() ? url : "http://localhost:3000").replace(/\/$/, "");
|
||||
return base;
|
||||
};
|
||||
|
||||
/** Page-Eintrag aus der API (aus OpenAPI-Spec generiert). */
|
||||
export type PageEntry = components["schemas"]["page"];
|
||||
|
||||
/** Antwort von GET /api/content/page (aus OpenAPI-Spec generiert). */
|
||||
export type PageListResponse = operations["listPage"]["responses"][200]["content"]["application/json"];
|
||||
|
||||
export type OpenApiSpec = {
|
||||
openapi: string;
|
||||
info: { title: string; version: string };
|
||||
paths: Record<string, unknown>;
|
||||
components?: { schemas?: Record<string, unknown> };
|
||||
};
|
||||
|
||||
let openApiCache: OpenApiSpec | null = null;
|
||||
|
||||
/**
|
||||
* Lädt die OpenAPI-Spezifikation von GET /api-docs/openapi.json.
|
||||
* Wird gecacht (einmal pro Request/Build).
|
||||
*/
|
||||
export async function fetchOpenApi(): Promise<OpenApiSpec> {
|
||||
if (openApiCache) return openApiCache;
|
||||
const base = getBaseUrl();
|
||||
const res = await fetch(`${base}/api-docs/openapi.json`);
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`RustyCMS OpenAPI fetch failed: ${res.status} ${res.statusText}. Is the CMS running at ${base}?`
|
||||
);
|
||||
}
|
||||
const spec = (await res.json()) as OpenApiSpec;
|
||||
openApiCache = spec;
|
||||
return spec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Page-Einträge (GET /api/content/page).
|
||||
*/
|
||||
export async function getPages(): Promise<PageEntry[]> {
|
||||
const base = getBaseUrl();
|
||||
const res = await fetch(`${base}/api/content/page`);
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`RustyCMS list page failed: ${res.status}. Is the CMS running at ${base}?`
|
||||
);
|
||||
}
|
||||
const data = (await res.json()) as PageListResponse;
|
||||
return data.items ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein Page-Eintrag nach Slug (GET /api/content/page/:slug).
|
||||
*/
|
||||
export async function getPageBySlug(slug: string): Promise<PageEntry | null> {
|
||||
const base = getBaseUrl();
|
||||
const res = await fetch(`${base}/api/content/page/${encodeURIComponent(slug)}`);
|
||||
if (res.status === 404) return null;
|
||||
if (!res.ok) {
|
||||
throw new Error(`RustyCMS get page failed: ${res.status} for slug "${slug}".`);
|
||||
}
|
||||
return (await res.json()) as PageEntry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Slugs für Page (für getStaticPaths / generate).
|
||||
*/
|
||||
export async function getPageSlugs(): Promise<string[]> {
|
||||
const items = await getPages();
|
||||
return items.map((p) => p._slug ?? p.slug ?? "").filter(Boolean);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
import Layout from '../layouts/Layout.astro';
|
||||
---
|
||||
|
||||
<Layout title="Seite nicht gefunden">
|
||||
<main class="mx-auto max-w-3xl px-4 py-12 text-center">
|
||||
<h1 class="text-3xl font-bold text-zinc-900">404</h1>
|
||||
<p class="mt-2 text-zinc-600">Seite nicht gefunden.</p>
|
||||
<a href="/" class="mt-6 inline-block rounded bg-zinc-900 px-4 py-2 text-white hover:bg-zinc-800">
|
||||
Zur Startseite
|
||||
</a>
|
||||
</main>
|
||||
</Layout>
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
import Layout from '../layouts/Layout.astro';
|
||||
import { getPageSlugs, getPageBySlug } from '../lib/cms';
|
||||
|
||||
export const prerender = true;
|
||||
|
||||
export async function getStaticPaths() {
|
||||
try {
|
||||
const slugs = await getPageSlugs();
|
||||
return slugs.map((slug) => ({ params: { slug } }));
|
||||
} catch (e) {
|
||||
console.error('getStaticPaths (pages):', e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
const { slug } = Astro.params as { slug: string };
|
||||
const page = await getPageBySlug(slug);
|
||||
|
||||
if (!page) {
|
||||
return Astro.redirect('/404');
|
||||
}
|
||||
---
|
||||
|
||||
<Layout
|
||||
title={page.seoTitle ?? page.headline ?? page.linkName ?? slug}
|
||||
description={page.seoDescription ?? page.subheadline}
|
||||
>
|
||||
<main class="mx-auto max-w-3xl px-4 py-12">
|
||||
<article>
|
||||
<header class="mb-8">
|
||||
<h1 class="text-3xl font-bold tracking-tight text-zinc-900">
|
||||
{page.headline ?? page.linkName ?? page._slug}
|
||||
</h1>
|
||||
{page.subheadline && (
|
||||
<p class="mt-2 text-lg text-zinc-600">
|
||||
{page.subheadline}
|
||||
</p>
|
||||
)}
|
||||
</header>
|
||||
<div class="prose prose-zinc max-w-none">
|
||||
<!-- Weitere Felder (z.B. row1Content, topFullwidthBanner) können hier gerendert werden -->
|
||||
</div>
|
||||
</article>
|
||||
</main>
|
||||
</Layout>
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
import Layout from '../layouts/Layout.astro';
|
||||
import { getPages, fetchOpenApi } from '../lib/cms';
|
||||
import type { PageEntry } from '../lib/cms';
|
||||
|
||||
let openApiTitle = 'RustyCMS API';
|
||||
let pages: PageEntry[] = [];
|
||||
let cmsError: string | null = null;
|
||||
|
||||
try {
|
||||
const spec = await fetchOpenApi();
|
||||
openApiTitle = (spec.info?.title as string) ?? openApiTitle;
|
||||
pages = await getPages();
|
||||
} catch (e) {
|
||||
cmsError = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
---
|
||||
|
||||
<Layout title="RustyAstro – Pages" description="Pages aus RustyCMS">
|
||||
<main class="mx-auto max-w-3xl px-4 py-12">
|
||||
<h1 class="text-3xl font-bold tracking-tight text-zinc-900">
|
||||
Pages
|
||||
</h1>
|
||||
<p class="mt-2 text-sm text-zinc-500">
|
||||
Schema von <strong>{openApiTitle}</strong> (OpenAPI).
|
||||
</p>
|
||||
<ul class="mt-8 space-y-3">
|
||||
{pages.map((page) => (
|
||||
<li>
|
||||
<a
|
||||
href={`/${encodeURIComponent(page._slug ?? page.slug)}`}
|
||||
class="block rounded-lg border border-zinc-200 bg-white px-4 py-3 shadow-sm transition hover:border-zinc-300 hover:shadow"
|
||||
>
|
||||
<span class="font-medium text-zinc-900">
|
||||
{page.linkName ?? page.name ?? page._slug}
|
||||
</span>
|
||||
{page.headline && (
|
||||
<span class="mt-1 block text-sm text-zinc-500">
|
||||
{page.headline}
|
||||
</span>
|
||||
)}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{cmsError && (
|
||||
<div class="mt-6 rounded-lg border border-amber-200 bg-amber-50 p-4 text-amber-800">
|
||||
<strong>CMS nicht erreichbar:</strong> {cmsError}
|
||||
</div>
|
||||
)}
|
||||
{!cmsError && pages.length === 0 && (
|
||||
<p class="mt-6 text-zinc-500">
|
||||
Keine Pages gefunden. RustyCMS unter <code class="rounded bg-zinc-200 px-1">PUBLIC_CMS_URL</code> starten und <code class="rounded bg-zinc-200 px-1">content/page/*.json5</code> anlegen.
|
||||
</p>
|
||||
)}
|
||||
</main>
|
||||
</Layout>
|
||||
@@ -0,0 +1 @@
|
||||
@import "tailwindcss";
|
||||
Reference in New Issue
Block a user