mirror of
https://github.com/SrIzan10/spongebin.git
synced 2026-05-01 11:05:09 +00:00
feat: add api; paste-action -> paste
This commit is contained in:
@@ -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";
|
||||
|
||||
54
src/app/api/paste/route.ts
Normal file
54
src/app/api/paste/route.ts
Normal 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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user