feat: add api; paste-action -> paste

This commit is contained in:
DuroCodes
2025-04-26 07:04:26 -04:00
parent 560832ff5e
commit 9f1b78dd2d
4 changed files with 56 additions and 2 deletions

View File

@@ -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";

View File

@@ -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 },
);
}
}

View File

@@ -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 {