diff --git a/src/actions/paste-action.ts b/src/actions/paste.ts similarity index 100% rename from src/actions/paste-action.ts rename to src/actions/paste.ts diff --git a/src/app/[id]/page.tsx b/src/app/[id]/page.tsx index c80fec0..5572da4 100644 --- a/src/app/[id]/page.tsx +++ b/src/app/[id]/page.tsx @@ -1,4 +1,4 @@ -import { getPasteById } from "~/actions/paste-action"; +import { getPasteById } from "~/actions/paste"; import { redirect } from "next/navigation"; import { MonacoEditor } from "~/components/monaco-editor"; import { EditorProvider } from "~/components/editor-provider"; diff --git a/src/app/api/paste/route.ts b/src/app/api/paste/route.ts new file mode 100644 index 0000000..313defe --- /dev/null +++ b/src/app/api/paste/route.ts @@ -0,0 +1,54 @@ +import { NextRequest, NextResponse } from "next/server"; +import { addPaste } from "~/actions/paste"; +import { LANGUAGES } from "~/utils/languages"; +import { THEME_MAP } from "~/utils/themes"; + +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + + if (!body.content) + return NextResponse.json( + { error: "Content is required" }, + { status: 400 }, + ); + + const language = body.language || "text"; + const theme = body.theme || "catppuccin-mocha"; + + if (!LANGUAGES.includes(language)) + return NextResponse.json( + { error: `Invalid language: ${language}` }, + { status: 400 }, + ); + + if (!Object.keys(THEME_MAP).includes(theme)) + return NextResponse.json( + { error: `Invalid theme: ${theme}` }, + { status: 400 }, + ); + + const paste = await addPaste(body.content, language, theme); + + if (!paste.id) + return NextResponse.json( + { error: "Failed to create paste" }, + { status: 500 }, + ); + + return NextResponse.json( + { + success: true, + id: paste.id, + url: `${request.nextUrl.origin}/${paste.id}`, + }, + { status: 201 }, + ); + } catch (error) { + console.error("API Error:", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/src/components/save-button.tsx b/src/components/save-button.tsx index 4d05320..4569982 100644 --- a/src/components/save-button.tsx +++ b/src/components/save-button.tsx @@ -3,7 +3,7 @@ import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { Button } from "~/components/ui/button"; -import { addPaste } from "~/actions/paste-action"; +import { addPaste } from "~/actions/paste"; import { useEffect } from "react"; interface SaveButtonProps {