diff --git a/app/api/mdx/pages/[slug]/route.ts b/app/api/mdx/pages/[slug]/route.ts index 3462596..f65ef68 100644 --- a/app/api/mdx/pages/[slug]/route.ts +++ b/app/api/mdx/pages/[slug]/route.ts @@ -1,63 +1,65 @@ import { NextRequest, NextResponse } from "next/server"; -import fs from "fs"; +import { promises as fs } from "fs"; import path from "path"; import matter from "gray-matter"; -// Define the path to the changelogs directory -const pagesDir = path.join(process.cwd(), "data", "pages"); +const changelogsDir = path.resolve(process.cwd(), "data/pages"); -export async function GET( - request: NextRequest, - { params }: { params: { slug: string } } -) { - const { slug } = params; - const filePath = path.join(pagesDir, `${slug}.mdx`); - - try { - const content = fs.readFileSync(filePath, "utf8"); - // Use gray-matter to parse the front matter and content - const { data, content: mdxContent } = matter(content); - return NextResponse.json({ title: data.title, content: mdxContent }); - } catch (error) { - console.error("Failed to load page:", error); - return NextResponse.json({ error: "Page not found" }, { status: 404 }); - } +async function getMDXFiles(dir: string): Promise { + const files = await fs.readdir(dir); + return files.filter((file) => file.endsWith(".mdx")); } -export async function PUT( - request: NextRequest, - { params }: { params: { slug: string } } -) { - const { slug } = params; - const filePath = path.join(pagesDir, `${slug}.mdx`); - const { title, content } = await request.json(); - +export async function GET() { try { - fs.writeFileSync(filePath, `---\ntitle: ${title}\n---\n${content}`); - return NextResponse.json({ title, slug, content }); + const mdxFiles = await getMDXFiles(changelogsDir); + const pages = await Promise.all( + mdxFiles.map(async (file) => { + const filePath = path.join(changelogsDir, file); + const content = await fs.readFile(filePath, "utf-8"); + const { data, content: mdxContent } = matter(content); + const slug = path.basename(file, path.extname(file)); + return { + metadata: data, + slug, + content: mdxContent, + }; + }) + ); + return NextResponse.json(pages, { status: 200 }); } catch (error) { - console.error("Failed to update page:", error); + console.error("Failed to fetch pages:", error); return NextResponse.json( - { error: "Failed to update page" }, + { error: "Failed to fetch pages" }, { status: 500 } ); } } -export async function DELETE( - request: NextRequest, - { params }: { params: { slug: string } } -) { - const { slug } = params; - const filePath = path.join(pagesDir, `${slug}.mdx`); +export async function POST(request: NextRequest) { + const { title } = await request.json(); + const slug = title.toLowerCase().replace(/\s+/g, "-"); + const filePath = path.join(changelogsDir, `${slug}.mdx`); try { - fs.unlinkSync(filePath); - return NextResponse.json({}, { status: 204 }); + if ( + await fs + .stat(filePath) + .then(() => true) + .catch(() => false) + ) { + return NextResponse.json( + { error: "Page already exists" }, + { status: 400 } + ); + } + + await fs.writeFile(filePath, `---\ntitle: ${title}\n---\n`); + return NextResponse.json({ title, slug }, { status: 201 }); } catch (error) { - console.error("Failed to delete page:", error); + console.error("Failed to create page:", error); return NextResponse.json( - { error: "Failed to delete page" }, + { error: "Failed to create page" }, { status: 500 } ); } diff --git a/app/api/mdx/pages/route.ts b/app/api/mdx/pages/route.ts index 76679fe..2def051 100644 --- a/app/api/mdx/pages/route.ts +++ b/app/api/mdx/pages/route.ts @@ -1,52 +1,63 @@ -import fs from "fs"; -import path from "path"; import { NextRequest, NextResponse } from "next/server"; +import { promises as fs } from "fs"; +import path from "path"; import matter from "gray-matter"; -const changelogsDir = path.resolve(process.cwd(), "data/pages"); +// Define the path to the changelogs directory +const pagesDir = path.join(process.cwd(), "data", "pages"); -function getMDXFiles(dir: string): string[] { - return fs.readdirSync(dir).filter((file) => file.endsWith(".mdx")); +export async function GET( + request: NextRequest, + { params }: { params: { slug: string } } +) { + const { slug } = params; + const filePath = path.join(pagesDir, `${slug}.mdx`); + + try { + const content = await fs.readFile(filePath, "utf8"); + // Use gray-matter to parse the front matter and content + const { data, content: mdxContent } = matter(content); + return NextResponse.json({ title: data.title, content: mdxContent }); + } catch (error) { + console.error("Failed to load page:", error); + return NextResponse.json({ error: "Page not found" }, { status: 404 }); + } } -export async function GET() { +export async function PUT( + request: NextRequest, + { params }: { params: { slug: string } } +) { + const { slug } = params; + const filePath = path.join(pagesDir, `${slug}.mdx`); + const { title, content } = await request.json(); + try { - const mdxFiles = getMDXFiles(changelogsDir); - const pages = mdxFiles.map((file) => { - const filePath = path.join(changelogsDir, file); - const content = fs.readFileSync(filePath, "utf-8"); - const { data, content: mdxContent } = matter(content); - const slug = path.basename(file, path.extname(file)); - return { - metadata: data, - slug, - content: mdxContent, - }; - }); - return NextResponse.json(pages, { status: 200 }); + await fs.writeFile(filePath, `---\ntitle: ${title}\n---\n${content}`); + return NextResponse.json({ title, slug, content }); } catch (error) { + console.error("Failed to update page:", error); return NextResponse.json( - { error: "Failed to fetch pages" }, + { error: "Failed to update page" }, { status: 500 } ); } } -export async function POST(request: NextRequest) { - const { title } = await request.json(); - const slug = title.toLowerCase().replace(/\s+/g, "-"); - const filePath = path.join(changelogsDir, `${slug}.mdx`); - - if (fs.existsSync(filePath)) { - return NextResponse.json({ error: "Page already exists" }, { status: 400 }); - } +export async function DELETE( + request: NextRequest, + { params }: { params: { slug: string } } +) { + const { slug } = params; + const filePath = path.join(pagesDir, `${slug}.mdx`); try { - fs.writeFileSync(filePath, `---\ntitle: ${title}\n---\n`); - return NextResponse.json({ title, slug }, { status: 201 }); + await fs.unlink(filePath); + return NextResponse.json({}, { status: 204 }); } catch (error) { + console.error("Failed to delete page:", error); return NextResponse.json( - { error: "Failed to create page" }, + { error: "Failed to delete page" }, { status: 500 } ); }