|
| 1 | +import { source } from "@/lib/source"; |
| 2 | +import { |
| 3 | + DocsBody, |
| 4 | + DocsDescription, |
| 5 | + DocsPage, |
| 6 | + DocsTitle, |
| 7 | +} from "fumadocs-ui/page"; |
| 8 | +import { createRelativeLink } from "fumadocs-ui/mdx"; |
| 9 | +import { notFound, redirect } from "next/navigation"; |
| 10 | +import { APIPage } from "@/components/api-page"; |
| 11 | +import { getMDXComponents } from "@/mdx-components"; |
| 12 | + |
| 13 | +const API_REFERENCE_INDEX = "/api-reference/createUser"; |
| 14 | + |
| 15 | +export default async function Page(props: { |
| 16 | + params: Promise<{ slug?: string[] }>; |
| 17 | +}) { |
| 18 | + const params = await props.params; |
| 19 | + const slugPath = params.slug?.join("/"); |
| 20 | + |
| 21 | + if (slugPath === "api-reference" || slugPath === "rest-api") { |
| 22 | + redirect(API_REFERENCE_INDEX); |
| 23 | + } |
| 24 | + |
| 25 | + const page = source.getPage(params.slug); |
| 26 | + if (!page) notFound(); |
| 27 | + |
| 28 | + if (page.data.type === "openapi") { |
| 29 | + return ( |
| 30 | + <DocsPage full> |
| 31 | + <DocsTitle>{page.data.title}</DocsTitle> |
| 32 | + <DocsDescription>{page.data.description}</DocsDescription> |
| 33 | + <DocsBody> |
| 34 | + <APIPage {...page.data.getAPIPageProps()} /> |
| 35 | + </DocsBody> |
| 36 | + </DocsPage> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + const MDXContent = page.data.body; |
| 41 | + |
| 42 | + return ( |
| 43 | + <DocsPage toc={page.data.toc} full={page.data.full}> |
| 44 | + <DocsTitle>{page.data.title}</DocsTitle> |
| 45 | + <DocsDescription>{page.data.description}</DocsDescription> |
| 46 | + <DocsBody> |
| 47 | + <MDXContent |
| 48 | + components={getMDXComponents({ |
| 49 | + a: createRelativeLink(source, page), |
| 50 | + })} |
| 51 | + /> |
| 52 | + </DocsBody> |
| 53 | + </DocsPage> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export async function generateStaticParams() { |
| 58 | + const params = source.generateParams(); |
| 59 | + const bySlug = new Map<string, { slug?: string[] }>(); |
| 60 | + |
| 61 | + for (const param of [ |
| 62 | + { slug: [] as string[] }, |
| 63 | + ...params, |
| 64 | + { slug: ["api-reference"] }, |
| 65 | + { slug: ["rest-api"] }, |
| 66 | + ]) { |
| 67 | + bySlug.set((param.slug ?? []).join("/"), param); |
| 68 | + } |
| 69 | + |
| 70 | + return [...bySlug.values()]; |
| 71 | +} |
| 72 | + |
| 73 | +export async function generateMetadata(props: { |
| 74 | + params: Promise<{ slug?: string[] }>; |
| 75 | +}) { |
| 76 | + const params = await props.params; |
| 77 | + |
| 78 | + if (params.slug?.join("/") === "api-reference") { |
| 79 | + return { |
| 80 | + title: "API Reference", |
| 81 | + description: "Interactive REST API documentation for CourseLit.", |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + const page = source.getPage(params.slug); |
| 86 | + if (!page) notFound(); |
| 87 | + |
| 88 | + return { |
| 89 | + title: page.data.title, |
| 90 | + description: page.data.description, |
| 91 | + }; |
| 92 | +} |
0 commit comments